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