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