No-op; fix GPL address and use the explicit-program-name version.
[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 "lib/util.h"
24 #include "lib/encoded_log_entry.h"
25 #include "lib/encode_server.h"
26 #include "lib/config.h"
27 #include "lib/log.h"
28 #include "lib/raw_convert.h"
29 #include "lib/signaller.h"
30 #include "lib/cross.h"
31 #include <wx/taskbar.h>
32 #include <wx/icon.h>
33 #include <boost/thread.hpp>
34 #include <boost/foreach.hpp>
35 #include <boost/optional.hpp>
36 #include <iostream>
37
38 using std::cout;
39 using std::string;
40 using std::exception;
41 using std::list;
42 using std::fixed;
43 using std::setprecision;
44 using boost::shared_ptr;
45 using boost::thread;
46 using boost::bind;
47 using boost::optional;
48 using boost::dynamic_pointer_cast;
49
50 enum {
51         ID_status = 1,
52         ID_quit,
53         ID_timer
54 };
55
56 static unsigned int const log_lines = 32;
57
58 class ServerLog : public Log, public Signaller
59 {
60 public:
61
62         string get () const {
63                 string a;
64                 BOOST_FOREACH (string const & i, _log) {
65                         a += i + "\n";
66                 }
67                 return a;
68         }
69
70         string head_and_tail (int) const {
71                 /* Not necessary */
72                 return "";
73         }
74
75         float fps () const {
76                 boost::mutex::scoped_lock lm (_state_mutex);
77                 return _fps;
78         }
79
80         boost::signals2::signal<void(string)> Appended;
81         boost::signals2::signal<void(int)> Removed;
82
83 private:
84         void do_log (shared_ptr<const LogEntry> entry)
85         {
86                 time_t const s = entry->seconds ();
87                 struct tm* local = localtime (&s);
88                 if (
89                         !_last_time ||
90                         local->tm_yday != _last_time->tm_yday ||
91                         local->tm_year != _last_time->tm_year ||
92                         local->tm_hour != _last_time->tm_hour ||
93                         local->tm_min != _last_time->tm_min
94                         ) {
95                         char buffer[64];
96                         strftime (buffer, 64, "%c", local);
97                         append (buffer);
98                 }
99
100                 append (entry->message ());
101                 if (_log.size() > log_lines) {
102                         emit (boost::bind (boost::ref (Removed), _log.front().length()));
103                         _log.pop_front ();
104                 }
105                 _last_time = *local;
106
107                 shared_ptr<const EncodedLogEntry> encoded = dynamic_pointer_cast<const EncodedLogEntry> (entry);
108                 if (encoded) {
109                         _history.push_back (encoded->seconds ());
110                         if (_history.size() > 48) {
111                                 _history.pop_front ();
112                         }
113                         if (_history.size() > 2) {
114                                 boost::mutex::scoped_lock lm (_state_mutex);
115                                 _fps = _history.size() / (_history.back() - _history.front());
116                         }
117                 }
118         }
119
120         void append (string s)
121         {
122                 _log.push_back (s);
123                 emit (boost::bind (boost::ref (Appended), s));
124         }
125
126         list<string> _log;
127         optional<struct tm> _last_time;
128         std::list<double> _history;
129
130         mutable boost::mutex _state_mutex;
131         float _fps;
132 };
133
134 static shared_ptr<ServerLog> server_log;
135
136 class StatusDialog : public wxDialog
137 {
138 public:
139         StatusDialog ()
140                 : wxDialog (
141                         0, wxID_ANY, _("DCP-o-matic Encode Server"),
142                         wxDefaultPosition, wxDefaultSize,
143                         wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER
144                         )
145         {
146                 wxFlexGridSizer* state_sizer = new wxFlexGridSizer (2, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
147
148                 add_label_to_sizer (state_sizer, this, _("Frames per second"), true);
149                 _fps = new wxStaticText (this, wxID_ANY, wxT(""));
150                 state_sizer->Add (_fps);
151
152                 wxFlexGridSizer* log_sizer = new wxFlexGridSizer (1, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
153                 log_sizer->AddGrowableCol (0, 1);
154
155                 wxClientDC dc (this);
156                 wxSize size = dc.GetTextExtent (wxT ("This is the length of the file label it should be quite long"));
157                 int const height = (size.GetHeight() + 2) * log_lines;
158                 SetSize (700, height + DCPOMATIC_SIZER_GAP * 2);
159
160                 _text = new wxTextCtrl (
161                         this, wxID_ANY, std_to_wx (server_log->get()), wxDefaultPosition, wxSize (-1, height),
162                         wxTE_READONLY | wxTE_MULTILINE
163                         );
164
165                 log_sizer->Add (_text, 1, wxEXPAND);
166
167                 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
168                 overall_sizer->Add (state_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
169                 overall_sizer->Add (log_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
170                 SetSizer (overall_sizer);
171                 overall_sizer->Layout ();
172
173                 Bind (wxEVT_TIMER, boost::bind (&StatusDialog::update_state, this));
174                 _timer.reset (new wxTimer (this));
175                 _timer->Start (1000);
176
177                 server_log->Appended.connect (bind (&StatusDialog::appended, this, _1));
178                 server_log->Removed.connect (bind (&StatusDialog::removed, this, _1));
179         }
180
181 private:
182         void appended (string s)
183         {
184                 (*_text) << s << "\n";
185         }
186
187         void removed (int n)
188         {
189                 _text->Remove (0, n + 1);
190         }
191
192         void update_state ()
193         {
194                 SafeStringStream s;
195                 s << fixed << setprecision(1) << server_log->fps ();
196                 _fps->SetLabel (std_to_wx (s.str()));
197         }
198
199         wxTextCtrl* _text;
200         wxStaticText* _fps;
201         boost::shared_ptr<wxTimer> _timer;
202 };
203
204 class TaskBarIcon : public wxTaskBarIcon
205 {
206 public:
207         TaskBarIcon ()
208                 : _status (0)
209         {
210 #ifdef DCPOMATIC_WINDOWS
211                 wxIcon icon (std_to_wx ("taskbar_icon"));
212 #else
213                 wxInitAllImageHandlers();
214                 wxBitmap bitmap (wxString::Format (wxT ("%s/dcpomatic2_server_small.png"), std_to_wx (shared_path().string())), wxBITMAP_TYPE_PNG);
215                 wxIcon icon;
216                 icon.CopyFromBitmap (bitmap);
217 #endif
218
219                 SetIcon (icon, std_to_wx ("DCP-o-matic Encode Server"));
220
221                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::status, this), ID_status);
222                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::quit, this), ID_quit);
223         }
224
225         wxMenu* CreatePopupMenu ()
226         {
227                 wxMenu* menu = new wxMenu;
228                 menu->Append (ID_status, std_to_wx ("Status..."));
229                 menu->Append (ID_quit, std_to_wx ("Quit"));
230                 return menu;
231         }
232
233 private:
234         void status ()
235         {
236                 if (!_status) {
237                         _status = new StatusDialog ();
238                 }
239                 _status->Show ();
240         }
241
242         void quit ()
243         {
244                 wxTheApp->ExitMainLoop ();
245         }
246
247         StatusDialog* _status;
248 };
249
250 class App : public wxApp, public ExceptionStore
251 {
252 public:
253         App ()
254                 : wxApp ()
255                 , _thread (0)
256                 , _icon (0)
257         {}
258
259 private:
260
261         bool OnInit ()
262         {
263                 if (!wxApp::OnInit ()) {
264                         return false;
265                 }
266
267                 server_log.reset (new ServerLog);
268
269                 dcpomatic_setup_path_encoding ();
270                 dcpomatic_setup_i18n ();
271                 dcpomatic_setup ();
272                 Config::drop ();
273
274                 signal_manager = new wxSignalManager (this);
275                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
276
277                 _icon = new TaskBarIcon;
278                 _thread = new thread (bind (&App::main_thread, this));
279
280                 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
281                 _timer.reset (new wxTimer (this));
282                 _timer->Start (1000);
283
284                 return true;
285         }
286
287         int OnExit ()
288         {
289                 delete _icon;
290                 return wxApp::OnExit ();
291         }
292
293         void main_thread ()
294         try {
295                 EncodeServer server (server_log, false, Config::instance()->num_local_encoding_threads());
296                 server.run ();
297         } catch (...) {
298                 store_current ();
299         }
300
301         void check ()
302         {
303                 try {
304                         rethrow ();
305                 } catch (exception& e) {
306                         error_dialog (0, std_to_wx (e.what ()));
307                         wxTheApp->ExitMainLoop ();
308                 } catch (...) {
309                         error_dialog (0, _("An unknown error has occurred with the DCP-o-matic server."));
310                         wxTheApp->ExitMainLoop ();
311                 }
312         }
313
314         void idle ()
315         {
316                 signal_manager->ui_idle ();
317         }
318
319         boost::thread* _thread;
320         TaskBarIcon* _icon;
321         shared_ptr<wxTimer> _timer;
322 };
323
324 IMPLEMENT_APP (App)