Build fixes for Boost >= 1.73
[dcpomatic.git] / src / tools / dcpomatic_server.cc
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 #include "wx/wx_util.h"
22 #include "wx/wx_signal_manager.h"
23 #include "wx/static_text.h"
24 #include "lib/util.h"
25 #include "lib/encoded_log_entry.h"
26 #include "lib/encode_server.h"
27 #include "lib/config.h"
28 #include "lib/log.h"
29 #include "lib/signaller.h"
30 #include "lib/cross.h"
31 #include "lib/dcpomatic_log.h"
32 #include <wx/taskbar.h>
33 #include <wx/splash.h>
34 #include <wx/icon.h>
35 #include <boost/thread.hpp>
36 #include <boost/foreach.hpp>
37 #include <boost/optional.hpp>
38 #include <iostream>
39
40 using std::cout;
41 using std::string;
42 using std::exception;
43 using std::list;
44 using std::fixed;
45 using std::setprecision;
46 using boost::shared_ptr;
47 using boost::thread;
48 using boost::bind;
49 using boost::optional;
50 using boost::dynamic_pointer_cast;
51 #if BOOST_VERSION >= 106100
52 using namespace boost::placeholders;
53 #endif
54
55 enum {
56         ID_status = 1,
57         ID_quit,
58         ID_timer
59 };
60
61 static unsigned int const log_lines = 32;
62
63 class ServerLog : public Log, public Signaller
64 {
65 public:
66         ServerLog ()
67                 : _fps (0)
68         {}
69
70         string get () const {
71                 string a;
72                 BOOST_FOREACH (string const & i, _log) {
73                         a += i + "\n";
74                 }
75                 return a;
76         }
77
78         string head_and_tail (int) const {
79                 /* Not necessary */
80                 return "";
81         }
82
83         float fps () const {
84                 boost::mutex::scoped_lock lm (_state_mutex);
85                 return _fps;
86         }
87
88         boost::signals2::signal<void(string)> Appended;
89         boost::signals2::signal<void(int)> Removed;
90
91 private:
92         void do_log (shared_ptr<const LogEntry> entry)
93         {
94                 time_t const s = entry->seconds ();
95                 struct tm* local = localtime (&s);
96                 if (
97                         !_last_time ||
98                         local->tm_yday != _last_time->tm_yday ||
99                         local->tm_year != _last_time->tm_year ||
100                         local->tm_hour != _last_time->tm_hour ||
101                         local->tm_min != _last_time->tm_min
102                         ) {
103                         char buffer[64];
104                         strftime (buffer, 64, "%c", local);
105                         append (buffer);
106                 }
107
108                 append (entry->message ());
109                 if (_log.size() > log_lines) {
110                         emit (boost::bind (boost::ref (Removed), _log.front().length()));
111                         _log.pop_front ();
112                 }
113                 _last_time = *local;
114
115                 shared_ptr<const EncodedLogEntry> encoded = dynamic_pointer_cast<const EncodedLogEntry> (entry);
116                 if (encoded) {
117                         _history.push_back (encoded->seconds ());
118                         if (_history.size() > 48) {
119                                 _history.pop_front ();
120                         }
121                         if (_history.size() > 2) {
122                                 boost::mutex::scoped_lock lm (_state_mutex);
123                                 _fps = _history.size() / (_history.back() - _history.front());
124                         }
125                 }
126         }
127
128         void append (string s)
129         {
130                 _log.push_back (s);
131                 emit (boost::bind (boost::ref (Appended), s));
132         }
133
134         list<string> _log;
135         optional<struct tm> _last_time;
136         std::list<double> _history;
137
138         mutable boost::mutex _state_mutex;
139         float _fps;
140 };
141
142 static shared_ptr<ServerLog> server_log;
143
144 class StatusDialog : public wxDialog
145 {
146 public:
147         StatusDialog ()
148                 : wxDialog (
149                         0, wxID_ANY, _("DCP-o-matic Encode Server"),
150                         wxDefaultPosition, wxDefaultSize,
151 #ifdef DCPOMATIC_OSX
152                         wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxSTAY_ON_TOP
153 #else
154                         wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER
155 #endif
156                         )
157         {
158                 wxFlexGridSizer* state_sizer = new wxFlexGridSizer (2, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
159
160                 add_label_to_sizer (state_sizer, this, _("Frames per second"), true);
161                 _fps = new StaticText (this, wxT(""));
162                 state_sizer->Add (_fps);
163
164                 wxFlexGridSizer* log_sizer = new wxFlexGridSizer (1, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
165                 log_sizer->AddGrowableCol (0, 1);
166
167                 wxClientDC dc (this);
168                 wxSize size = dc.GetTextExtent (wxT ("This is the length of the file label it should be quite long"));
169                 int const height = (size.GetHeight() + 2) * log_lines;
170                 SetSize (700, height + DCPOMATIC_SIZER_GAP * 2);
171
172                 _text = new wxTextCtrl (
173                         this, wxID_ANY, std_to_wx (server_log->get()), wxDefaultPosition, wxSize (-1, height),
174                         wxTE_READONLY | wxTE_MULTILINE
175                         );
176
177                 log_sizer->Add (_text, 1, wxEXPAND);
178
179                 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
180                 overall_sizer->Add (state_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
181                 overall_sizer->Add (log_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
182                 SetSizer (overall_sizer);
183                 overall_sizer->Layout ();
184
185                 Bind (wxEVT_TIMER, boost::bind (&StatusDialog::update_state, this));
186                 _timer.reset (new wxTimer (this));
187                 _timer->Start (1000);
188
189                 server_log->Appended.connect (bind (&StatusDialog::appended, this, _1));
190                 server_log->Removed.connect (bind (&StatusDialog::removed, this, _1));
191         }
192
193 private:
194         void appended (string s)
195         {
196                 (*_text) << s << "\n";
197         }
198
199         void removed (int n)
200         {
201                 _text->Remove (0, n + 1);
202         }
203
204         void update_state ()
205         {
206                 _fps->SetLabel (wxString::Format ("%.1f", server_log->fps()));
207         }
208
209         wxTextCtrl* _text;
210         wxStaticText* _fps;
211         boost::shared_ptr<wxTimer> _timer;
212 };
213
214 class TaskBarIcon : public wxTaskBarIcon
215 {
216 public:
217         TaskBarIcon ()
218                 : _status (0)
219         {
220 #ifdef DCPOMATIC_WINDOWS
221                 wxIcon icon (std_to_wx ("id"));
222 #else
223                 wxBitmap bitmap (wxString::Format (wxT ("%s/dcpomatic_small.png"), std_to_wx (shared_path().string())), wxBITMAP_TYPE_PNG);
224                 wxIcon icon;
225                 icon.CopyFromBitmap (bitmap);
226 #endif
227
228                 SetIcon (icon, std_to_wx ("DCP-o-matic Encode Server"));
229
230                 Bind (wxEVT_MENU, boost::bind (&TaskBarIcon::status, this), ID_status);
231                 Bind (wxEVT_MENU, boost::bind (&TaskBarIcon::quit, this), ID_quit);
232         }
233
234         wxMenu* CreatePopupMenu ()
235         {
236                 wxMenu* menu = new wxMenu;
237                 menu->Append (ID_status, std_to_wx ("Status..."));
238                 menu->Append (ID_quit, std_to_wx ("Quit"));
239                 return menu;
240         }
241
242 private:
243         void status ()
244         {
245                 if (!_status) {
246                         _status = new StatusDialog ();
247                 }
248                 _status->Show ();
249         }
250
251         void quit ()
252         {
253                 wxTheApp->ExitMainLoop ();
254         }
255
256         StatusDialog* _status;
257 };
258
259 class App : public wxApp, public ExceptionStore
260 {
261 public:
262         App ()
263                 : wxApp ()
264                 , _thread (0)
265                 , _icon (0)
266         {}
267
268 private:
269
270         bool OnInit ()
271         {
272                 if (!wxApp::OnInit ()) {
273                         return false;
274                 }
275
276                 wxInitAllImageHandlers ();
277
278                 server_log.reset (new ServerLog);
279                 server_log->set_types (LogEntry::TYPE_GENERAL | LogEntry::TYPE_WARNING | LogEntry::TYPE_ERROR);
280                 dcpomatic_log = server_log;
281
282                 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
283                 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
284
285                 wxSplashScreen* splash = maybe_show_splash ();
286
287                 dcpomatic_setup_path_encoding ();
288                 dcpomatic_setup_i18n ();
289                 dcpomatic_setup ();
290                 Config::drop ();
291
292                 signal_manager = new wxSignalManager (this);
293                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
294
295                 /* Bad things happen (on Linux at least) if the config is reloaded by main_thread;
296                    it seems like there's a race which results in the locked_sstream mutex being
297                    locked before it is initialised.  Calling Config::instance() here loads the config
298                    again in this thread, which seems to work around the problem.
299                 */
300                 Config::instance();
301
302 #ifdef DCPOMATIC_LINUX
303                 StatusDialog* d = new StatusDialog ();
304                 d->Show ();
305 #else
306                 _icon = new TaskBarIcon;
307 #endif
308                 _thread = new thread (bind (&App::main_thread, this));
309
310                 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
311                 _timer.reset (new wxTimer (this));
312                 _timer->Start (1000);
313
314                 if (splash) {
315                         splash->Destroy ();
316                 }
317
318                 SetExitOnFrameDelete (false);
319
320                 return true;
321         }
322
323         int OnExit ()
324         {
325                 delete _icon;
326                 return wxApp::OnExit ();
327         }
328
329         void main_thread ()
330         try {
331                 EncodeServer server (false, Config::instance()->server_encoding_threads());
332                 server.run ();
333         } catch (...) {
334                 store_current ();
335         }
336
337         void check ()
338         {
339                 try {
340                         rethrow ();
341                 } catch (exception& e) {
342                         error_dialog (0, std_to_wx (e.what ()));
343                         wxTheApp->ExitMainLoop ();
344                 } catch (...) {
345                         error_dialog (0, _("An unknown error has occurred with the DCP-o-matic server."));
346                         wxTheApp->ExitMainLoop ();
347                 }
348         }
349
350         void idle ()
351         {
352                 signal_manager->ui_idle ();
353         }
354
355         void config_failed_to_load ()
356         {
357                 message_dialog (0, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
358         }
359
360         void config_warning (string m)
361         {
362                 message_dialog (0, std_to_wx (m));
363         }
364
365         boost::thread* _thread;
366         TaskBarIcon* _icon;
367         shared_ptr<wxTimer> _timer;
368 };
369
370 IMPLEMENT_APP (App)