2 Copyright (C) 2000-2009 Paul Davis
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.
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.
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.
20 #ifndef __pbd_base_ui_h__
21 #define __pbd_base_ui_h__
26 #include <sigc++/slot.h>
27 #include <sigc++/trackable.h>
29 #include <glibmm/threads.h>
30 #include <glibmm/main.h>
32 #include "pbd/libpbd_visibility.h"
33 #include "pbd/crossthread.h"
34 #include "pbd/event_loop.h"
35 #include "pbd/pthread_utils.h"
37 /** A BaseUI is an abstraction designed to be used with any "user
38 * interface" (not necessarily graphical) that needs to wait on
39 * events/requests and dispatch/process them as they arrive.
41 * This implementation starts up a thread that runs a Glib main loop
42 * to wait on events/requests etc.
46 class LIBPBD_API BaseUI : public sigc::trackable, public PBD::EventLoop
49 BaseUI (const std::string& name);
52 BaseUI* base_instance() { return base_ui_instance; }
54 Glib::RefPtr<Glib::MainLoop> main_loop() const { return _main_loop; }
55 Glib::Threads::Thread* event_loop_thread() const { return run_loop_thread; }
56 bool caller_is_self () const { return Glib::Threads::Thread::self() == run_loop_thread; }
58 bool ok() const { return _ok; }
60 static RequestType new_request_type();
61 static RequestType CallSlot;
62 static RequestType Quit;
64 /** start up a thread to run the main loop
68 /** stop the thread running the main loop (and block
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;
82 /* this signals _running from within the event loop,
86 bool signal_running ();
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.
93 virtual void thread_init () {};
95 int set_thread_priority (const int policy = PBD_SCHED_FIFO, int priority = 0) const;
97 /** Called when there input ready on the request_channel
99 bool request_handler (Glib::IOCondition);
101 void signal_new_request ();
102 void attach_request_source ();
104 /** Derived UI objects must implement this method,
105 * which will be called whenever there are requests
108 virtual void handle_ui_requests () = 0;
111 BaseUI* base_ui_instance;
113 CrossThreadChannel request_channel;
115 static uint64_t rt_bit;
117 int setup_request_pipe ();
121 #endif /* __pbd_base_ui_h__ */