Update task bar icon when GUI theme changes (#1986).
[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 "lib/warnings.h"
33 DCPOMATIC_DISABLE_WARNINGS
34 #include <wx/taskbar.h>
35 #include <wx/splash.h>
36 #include <wx/icon.h>
37 DCPOMATIC_ENABLE_WARNINGS
38 #include <boost/thread.hpp>
39 #include <boost/optional.hpp>
40 #include <iostream>
41
42 using std::cout;
43 using std::string;
44 using std::exception;
45 using std::list;
46 using std::fixed;
47 using std::setprecision;
48 using std::shared_ptr;
49 using boost::thread;
50 using boost::bind;
51 using boost::optional;
52 using std::dynamic_pointer_cast;
53 #if BOOST_VERSION >= 106100
54 using namespace boost::placeholders;
55 #endif
56
57 enum {
58         ID_status = 1,
59         ID_quit,
60         ID_timer
61 };
62
63 static unsigned int const log_lines = 32;
64
65
66 class ServerLog : public Log, public Signaller
67 {
68 public:
69         ServerLog ()
70                 : _fps (0)
71         {}
72
73         string get () const {
74                 string a;
75                 for (auto const& i: _log) {
76                         a += i + "\n";
77                 }
78                 return a;
79         }
80
81         float fps () const {
82                 boost::mutex::scoped_lock lm (_state_mutex);
83                 return _fps;
84         }
85
86         boost::signals2::signal<void(string)> Appended;
87         boost::signals2::signal<void(int)> Removed;
88
89 private:
90         void do_log (shared_ptr<const LogEntry> entry)
91         {
92                 time_t const s = entry->seconds ();
93                 struct tm* local = localtime (&s);
94                 if (
95                         !_last_time ||
96                         local->tm_yday != _last_time->tm_yday ||
97                         local->tm_year != _last_time->tm_year ||
98                         local->tm_hour != _last_time->tm_hour ||
99                         local->tm_min != _last_time->tm_min
100                         ) {
101                         char buffer[64];
102                         strftime (buffer, 64, "%c", local);
103                         append (buffer);
104                 }
105
106                 append (entry->message ());
107                 if (_log.size() > log_lines) {
108                         emit (boost::bind (boost::ref (Removed), _log.front().length()));
109                         _log.pop_front ();
110                 }
111                 _last_time = *local;
112
113                 auto encoded = dynamic_pointer_cast<const EncodedLogEntry> (entry);
114                 if (encoded) {
115                         _history.push_back (encoded->seconds ());
116                         if (_history.size() > 48) {
117                                 _history.pop_front ();
118                         }
119                         if (_history.size() > 2) {
120                                 boost::mutex::scoped_lock lm (_state_mutex);
121                                 _fps = _history.size() / (_history.back() - _history.front());
122                         }
123                 }
124         }
125
126         void append (string s)
127         {
128                 _log.push_back (s);
129                 emit (boost::bind(boost::ref(Appended), s));
130         }
131
132         list<string> _log;
133         optional<struct tm> _last_time;
134         std::list<double> _history;
135
136         mutable boost::mutex _state_mutex;
137         float _fps;
138 };
139
140
141 static shared_ptr<ServerLog> server_log;
142
143
144 class StatusDialog : public wxDialog
145 {
146 public:
147         StatusDialog ()
148                 : wxDialog (
149                         nullptr, 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                 auto 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                 auto 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         std::shared_ptr<wxTimer> _timer;
212 };
213
214
215 static StatusDialog* status_dialog = nullptr;
216
217
218 class TaskBarIcon : public wxTaskBarIcon
219 {
220 public:
221         TaskBarIcon ()
222         {
223                 set_icon ();
224
225                 Bind (wxEVT_MENU, boost::bind (&TaskBarIcon::status, this), ID_status);
226                 Bind (wxEVT_MENU, boost::bind (&TaskBarIcon::quit, this), ID_quit);
227         }
228
229         wxMenu* CreatePopupMenu ()
230         {
231                 auto menu = new wxMenu;
232                 menu->Append (ID_status, std_to_wx ("Status..."));
233                 menu->Append (ID_quit, std_to_wx ("Quit"));
234                 return menu;
235         }
236
237         void set_icon ()
238         {
239 #ifdef DCPOMATIC_WINDOWS
240                 wxIcon icon (std_to_wx("id"));
241 #else
242                 string const colour = gui_is_dark() ? "white" : "black";
243                 wxBitmap bitmap (
244                         bitmap_path(String::compose("dcpomatic_small_%1", colour)),
245                         wxBITMAP_TYPE_PNG
246                         );
247                 wxIcon icon;
248                 icon.CopyFromBitmap (bitmap);
249 #endif
250
251                 SetIcon (icon, std_to_wx ("DCP-o-matic Encode Server"));
252         }
253
254 private:
255         void status ()
256         {
257                 status_dialog->Show ();
258         }
259
260         void quit ()
261         {
262                 wxTheApp->ExitMainLoop ();
263         }
264 };
265
266
267 class App : public wxApp, public ExceptionStore
268 {
269 public:
270         App ()
271                 : wxApp ()
272         {}
273
274 private:
275
276         bool OnInit ()
277         {
278                 if (!wxApp::OnInit()) {
279                         return false;
280                 }
281
282                 wxInitAllImageHandlers ();
283
284                 server_log.reset (new ServerLog);
285                 server_log->set_types (LogEntry::TYPE_GENERAL | LogEntry::TYPE_WARNING | LogEntry::TYPE_ERROR);
286                 dcpomatic_log = server_log;
287
288                 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
289                 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
290
291                 auto splash = maybe_show_splash ();
292
293                 dcpomatic_setup_path_encoding ();
294                 dcpomatic_setup_i18n ();
295                 dcpomatic_setup ();
296                 Config::drop ();
297
298                 signal_manager = new wxSignalManager (this);
299                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
300
301                 /* Bad things happen (on Linux at least) if the config is reloaded by main_thread;
302                    it seems like there's a race which results in the locked_sstream mutex being
303                    locked before it is initialised.  Calling Config::instance() here loads the config
304                    again in this thread, which seems to work around the problem.
305                 */
306                 Config::instance();
307
308                 status_dialog = new StatusDialog ();
309 #ifdef DCPOMATIC_LINUX
310                 status_dialog->Show ();
311 #else
312                 _icon = new TaskBarIcon;
313                 status_dialog->Bind (wxEVT_SYS_COLOUR_CHANGED, boost::bind(&TaskBarIcon::set_icon, _icon));
314 #endif
315                 _thread = thread (bind (&App::main_thread, this));
316
317                 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
318                 _timer.reset (new wxTimer (this));
319                 _timer->Start (1000);
320
321                 if (splash) {
322                         splash->Destroy ();
323                 }
324
325                 SetExitOnFrameDelete (false);
326
327                 return true;
328         }
329
330         int OnExit ()
331         {
332                 delete _icon;
333                 return wxApp::OnExit ();
334         }
335
336         void main_thread ()
337         try {
338                 EncodeServer server (false, Config::instance()->server_encoding_threads());
339                 server.run ();
340         } catch (...) {
341                 store_current ();
342         }
343
344         void check ()
345         {
346                 try {
347                         rethrow ();
348                 } catch (exception& e) {
349                         error_dialog (nullptr, std_to_wx(e.what()));
350                         wxTheApp->ExitMainLoop ();
351                 } catch (...) {
352                         error_dialog (nullptr, _("An unknown error has occurred with the DCP-o-matic server."));
353                         wxTheApp->ExitMainLoop ();
354                 }
355         }
356
357         void idle ()
358         {
359                 signal_manager->ui_idle ();
360         }
361
362         void config_failed_to_load ()
363         {
364                 message_dialog (nullptr, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
365         }
366
367         void config_warning (string m)
368         {
369                 message_dialog (nullptr, std_to_wx(m));
370         }
371
372         boost::thread _thread;
373         TaskBarIcon* _icon = nullptr;
374         shared_ptr<wxTimer> _timer;
375 };
376
377 IMPLEMENT_APP (App)