major design changes: use glib event loop for MIDI thread/UI; rework design of BaseUI...
[ardour.git] / libs / pbd / pbd / base_ui.h
1 /*
2     Copyright (C) 2000-2007 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/thread.h>
30 #include <glibmm/main.h>
31
32 #include "pbd/crossthread.h"
33
34 class BaseUI : virtual public sigc::trackable {
35   public:
36         BaseUI (const std::string& name);
37         virtual ~BaseUI();
38
39         BaseUI* base_instance() { return base_ui_instance; }
40
41         Glib::RefPtr<Glib::MainLoop> main_loop() const { return _main_loop; }
42         Glib::Thread* event_loop_thread() const { return run_loop_thread; }
43         bool caller_is_self () const { return Glib::Thread::self() == run_loop_thread; }
44
45         std::string name() const { return _name; }
46
47         bool ok() const { return _ok; }
48
49         enum RequestType {
50                 range_guarantee = ~0
51         };
52
53         struct BaseRequestObject {
54             RequestType type;
55             sigc::slot<void> the_slot;
56         };
57
58         static RequestType new_request_type();
59         static RequestType CallSlot;
60
61         void run ();
62         void quit ();
63
64         virtual void call_slot (sigc::slot<void> theSlot) = 0;
65
66   protected:
67         CrossThreadChannel request_channel;
68         bool _ok; 
69
70         Glib::RefPtr<Glib::MainLoop> _main_loop;
71         Glib::Thread*                 run_loop_thread;
72
73         virtual void thread_init () {};
74         bool request_handler (Glib::IOCondition);
75
76         virtual void handle_ui_requests () = 0;
77
78   private:
79         std::string _name; 
80         BaseUI* base_ui_instance;
81         
82         static uint64_t rt_bit;
83
84         int setup_request_pipe ();
85         void main_thread ();
86 };
87
88 #endif /* __pbd_base_ui_h__ */