Fix non-variant build.
[dcpomatic.git] / src / lib / monitor_checker.cc
1 /*
2     Copyright (C) 2018 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 #ifdef DCPOMATIC_VARIANT_SWAROOP
22
23 #include "monitor_checker.h"
24 #include "config.h"
25 #include "cross.h"
26
27 using boost::bind;
28 using boost::ref;
29
30 MonitorChecker* MonitorChecker::_instance = 0;
31
32 MonitorChecker::MonitorChecker ()
33         : _thread (0)
34         , _terminate (false)
35         , _ok (true)
36 {
37
38 }
39
40 void
41 MonitorChecker::run ()
42 {
43         _thread = new boost::thread (boost::bind (&MonitorChecker::thread, this));
44 }
45
46 MonitorChecker::~MonitorChecker ()
47 {
48         {
49                 boost::mutex::scoped_lock lm (_mutex);
50                 _terminate = true;
51         }
52
53         if (_thread) {
54                 /* Ideally this would be a DCPOMATIC_ASSERT(_thread->joinable()) but we
55                    can't throw exceptions from a destructor.
56                 */
57                 _thread->interrupt ();
58                 try {
59                         if (_thread->joinable ()) {
60                                 _thread->join ();
61                         }
62                 } catch (boost::thread_interrupted& e) {
63                         /* No problem */
64                 }
65         }
66         delete _thread;
67 }
68
69 void
70 MonitorChecker::thread ()
71 {
72         while (true) {
73                 boost::mutex::scoped_lock lm (_mutex);
74                 if (_terminate) {
75                         break;
76                 }
77
78                 bool const was_ok = _ok;
79                 _ok = Config::instance()->required_monitors().empty() || get_monitors() == Config::instance()->required_monitors();
80                 if (was_ok != _ok) {
81                         emit (bind(boost::ref(StateChanged)));
82                 }
83
84                 lm.unlock ();
85                 dcpomatic_sleep (60);
86         }
87 }
88
89 bool
90 MonitorChecker::ok () const
91 {
92         boost::mutex::scoped_lock lm (_mutex);
93         return _ok;
94 }
95
96 MonitorChecker *
97 MonitorChecker::instance ()
98 {
99         if (!_instance) {
100                 _instance = new MonitorChecker ();
101         }
102
103         return _instance;
104 }
105
106 #endif