Use XMLNode::set_property in PBD::PropertyTemplate<T> class
[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/libpbd_visibility.h"
33 #include "pbd/crossthread.h"
34 #include "pbd/event_loop.h"
35
36 /** A BaseUI is an abstraction designed to be used with any "user
37  * interface" (not necessarily graphical) that needs to wait on
38  * events/requests and dispatch/process them as they arrive.
39  *
40  * This implementation starts up a thread that runs a Glib main loop
41  * to wait on events/requests etc.
42  */
43
44
45 class LIBPBD_API BaseUI : public sigc::trackable, public PBD::EventLoop
46 {
47   public:
48         BaseUI (const std::string& name);
49         virtual ~BaseUI();
50
51         BaseUI* base_instance() { return base_ui_instance; }
52
53         Glib::RefPtr<Glib::MainLoop> main_loop() const { return _main_loop; }
54         Glib::Threads::Thread* event_loop_thread() const { return run_loop_thread; }
55         bool caller_is_self () const { return Glib::Threads::Thread::self() == run_loop_thread; }
56
57         bool ok() const { return _ok; }
58
59         static RequestType new_request_type();
60         static RequestType CallSlot;
61         static RequestType Quit;
62
63         /** start up a thread to run the main loop
64          */
65         void run ();
66
67         /** stop the thread running the main loop (and block
68          *   until it exits)
69          */
70         void quit ();
71
72   protected:
73         bool _ok;
74
75         Glib::RefPtr<Glib::MainLoop> _main_loop;
76         Glib::RefPtr<Glib::MainContext> m_context;
77         Glib::Threads::Thread*       run_loop_thread;
78         Glib::Threads::Mutex        _run_lock;
79         Glib::Threads::Cond         _running;
80
81         /* this signals _running from within the event loop,
82            from an idle callback
83         */
84
85         bool signal_running ();
86
87         /** Derived UI objects can implement thread_init()
88          * which will be called by the event loop thread
89          * immediately before it enters the event loop.
90          */
91
92         virtual void thread_init () {};
93
94         /** Called when there input ready on the request_channel
95          */
96         bool request_handler (Glib::IOCondition);
97
98         void signal_new_request ();
99         void attach_request_source ();
100
101         /** Derived UI objects must implement this method,
102          * which will be called whenever there are requests
103          * to be dealt with.
104          */
105         virtual void handle_ui_requests () = 0;
106
107   private:
108         BaseUI* base_ui_instance;
109
110         CrossThreadChannel request_channel;
111
112         static uint64_t rt_bit;
113
114         int setup_request_pipe ();
115         void main_thread ();
116 };
117
118 #endif /* __pbd_base_ui_h__ */