No-op; fix GPL address and use the explicit-program-name version.
[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 <boost/asio.hpp>
25 #include <boost/thread.hpp>
26
27 class Signaller;
28
29 /** A class to allow signals to be emitted from non-UI threads and handled
30  *  by a UI thread.
31  */
32 class SignalManager : public boost::noncopyable
33 {
34 public:
35         /** Create a SignalManager.  Must be called from the UI thread */
36         SignalManager ()
37                 : _work (_service)
38         {
39                 _ui_thread = boost::this_thread::get_id ();
40         }
41
42         virtual ~SignalManager () {}
43
44         /* Do something next time the UI is idle */
45         template <typename T>
46         void when_idle (T f) {
47                 _service.post (f);
48         }
49
50         /** Call this in the UI when it is idle */
51         size_t ui_idle () {
52                 /* This executes one of the functors that has been post()ed to _service */
53                 return _service.poll_one ();
54         }
55
56         /** This should wake the UI and make it call ui_idle() */
57         virtual void wake_ui () {
58                 /* This is only a sensible implementation when there is no GUI */
59                 ui_idle ();
60         }
61
62 private:
63         /** Emit a signal from any thread whose handlers will be called in the UI
64          *  thread.  Use something like:
65          *
66          *  ui_signaller->emit (boost::bind (boost::ref (SomeSignal), parameter));
67          */
68         template <typename T>
69         void emit (T f) {
70                 if (boost::this_thread::get_id() == _ui_thread) {
71                         /* already in the UI thread */
72                         f ();
73                 } else {
74                         /* non-UI thread; post to the service and wake up the UI */
75                         _service.post (f);
76                         wake_ui ();
77                 }
78         }
79
80         friend class Signaller;
81
82         /** A io_service which is used as the conduit for messages */
83         boost::asio::io_service _service;
84         /** Object required to keep io_service from stopping when it has nothing to do */
85         boost::asio::io_service::work _work;
86         /** The UI thread's ID */
87         boost::thread::id _ui_thread;
88 };
89
90 extern SignalManager* signal_manager;
91
92 #endif