Merge branch 'master' into windows
[ardour.git] / libs / pbd / pbd / base_ui.h
1 /*
2     Copyright (C) 2000-2009 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __pbd_base_ui_h__
21 #define __pbd_base_ui_h__
22
23 #include <string>
24 #include <stdint.h>
25
26 #include <sigc++/slot.h>
27 #include <sigc++/trackable.h>
28
29 #include <glibmm/threads.h>
30 #include <glibmm/main.h>
31
32 #include "pbd/crossthread.h"
33 #include "pbd/event_loop.h"
34
35 /** A BaseUI is an abstraction designed to be used with any "user
36  * interface" (not necessarily graphical) that needs to wait on
37  * events/requests and dispatch/process them as they arrive.
38  *
39  * This implementation starts up a thread that runs a Glib main loop
40  * to wait on events/requests etc. 
41  */
42
43
44 class BaseUI : public sigc::trackable, public PBD::EventLoop
45 {
46   public:
47         BaseUI (const std::string& name);
48         virtual ~BaseUI();
49
50         BaseUI* base_instance() { return base_ui_instance; }
51
52         Glib::RefPtr<Glib::MainLoop> main_loop() const { return _main_loop; }
53         Glib::Threads::Thread* event_loop_thread() const { return run_loop_thread; }
54         bool caller_is_self () const { return Glib::Threads::Thread::self() == run_loop_thread; }
55
56         std::string name() const { return _name; }
57
58         bool ok() const { return _ok; }
59         
60         static RequestType new_request_type();
61         static RequestType CallSlot;
62         static RequestType Quit;
63
64         /** start up a thread to run the main loop 
65          */
66         void run ();
67
68         /** stop the thread running the main loop (and block
69          *   until it exits)
70          */
71         void quit ();
72
73   protected:
74         bool _ok; 
75
76         Glib::RefPtr<Glib::MainLoop> _main_loop;
77         Glib::RefPtr<Glib::MainContext> m_context;
78         Glib::Threads::Thread*       run_loop_thread;
79         Glib::Threads::Mutex        _run_lock;
80         Glib::Threads::Cond         _running;
81
82         /* this signals _running from within the event loop,
83            from an idle callback 
84         */
85
86         bool signal_running ();
87
88         /** Derived UI objects can implement thread_init()
89          * which will be called by the event loop thread
90          * immediately before it enters the event loop.
91          */
92
93         virtual void thread_init () {};
94
95 #ifdef PLATFORM_WINDOWS
96         static gboolean _request_handler (gpointer);
97         bool request_handler ();
98 #else
99         /** Called when there input ready on the request_channel
100          */
101         bool request_handler (Glib::IOCondition);
102 #endif
103
104         void signal_new_request ();
105         void attach_request_source ();
106
107         /** Derived UI objects must implement this method,
108          * which will be called whenever there are requests
109          * to be dealt with.
110          */
111         virtual void handle_ui_requests () = 0;
112
113   private:
114         std::string _name; 
115         BaseUI* base_ui_instance;
116
117 #ifndef PLATFORM_WINDOWS
118         CrossThreadChannel request_channel;
119 #endif
120         
121         static uint64_t rt_bit;
122
123         int setup_request_pipe ();
124         void main_thread ();
125 };
126
127 #endif /* __pbd_base_ui_h__ */