Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / lib / signal_manager.h
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #ifndef DCPOMATIC_SIGNAL_MANAGER_H
22 #define DCPOMATIC_SIGNAL_MANAGER_H
23
24 #include "exception_store.h"
25 #include <boost/asio.hpp>
26 #include <boost/thread.hpp>
27 #include <boost/noncopyable.hpp>
28
29 class Signaller;
30
31 /** A class to allow signals to be emitted from non-UI threads and handled
32  *  by a UI thread.
33  */
34 class SignalManager : public boost::noncopyable, public ExceptionStore
35 {
36 public:
37         /** Create a SignalManager.  Must be called from the UI thread */
38         SignalManager ()
39                 : _work (_service)
40         {
41                 _ui_thread = boost::this_thread::get_id ();
42         }
43
44         virtual ~SignalManager () {}
45
46         /* Do something next time the UI is idle */
47         template <typename T>
48         void when_idle (T f) {
49                 _service.post (f);
50         }
51
52         /** Call this in the UI when it is idle.
53          *  @return Number of idle handlers that were executed.
54          */
55         size_t ui_idle () {
56                 /* This executes one of the functors that has been post()ed to _service */
57                 return _service.poll_one ();
58         }
59
60         /** This should wake the UI and make it call ui_idle() */
61         virtual void wake_ui () {
62                 /* This is only a sensible implementation when there is no GUI */
63                 ui_idle ();
64         }
65
66 private:
67         /** Emit a signal from any thread whose handlers will be called in the UI
68          *  thread.  Use something like:
69          *
70          *  ui_signaller->emit (boost::bind (boost::ref (SomeSignal), parameter));
71          */
72         template <typename T>
73         void emit (T f) {
74                 if (boost::this_thread::get_id() == _ui_thread) {
75                         /* already in the UI thread */
76                         try {
77                                 f ();
78                         } catch (...) {
79                                 store_current ();
80                         }
81                 } else {
82                         /* non-UI thread; post to the service and wake up the UI */
83                         _service.post (f);
84                         wake_ui ();
85                 }
86         }
87
88         friend class Signaller;
89
90         /** A io_service which is used as the conduit for messages */
91         boost::asio::io_service _service;
92         /** Object required to keep io_service from stopping when it has nothing to do */
93         boost::asio::io_service::work _work;
94         /** The UI thread's ID */
95         boost::thread::id _ui_thread;
96 };
97
98 extern SignalManager* signal_manager;
99
100 #endif