1d6c0bef70a6b7a95b96e9909afa9249aaf7dc45
[dcpomatic.git] / src / tools / dcpomatic_server.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "wx/wx_util.h"
21 #include "wx/wx_signal_manager.h"
22 #include "lib/util.h"
23 #include "lib/server.h"
24 #include "lib/config.h"
25 #include "lib/log.h"
26 #include "lib/signaller.h"
27 #include <wx/taskbar.h>
28 #include <wx/icon.h>
29 #include <boost/thread.hpp>
30 #include <iostream>
31
32 using std::cout;
33 using std::string;
34 using std::exception;
35 using std::list;
36 using boost::shared_ptr;
37 using boost::thread;
38 using boost::bind;
39
40 enum {
41         ID_status = 1,
42         ID_quit,
43         ID_timer
44 };
45
46 static int const log_lines = 32;
47
48 class MemoryLog : public Log, public Signaller
49 {
50 public:
51
52         string head_and_tail (int) const {
53                 /* Not necessary */
54                 return "";
55         }
56
57         boost::signals2::signal<void(string)> Appended;
58         boost::signals2::signal<void(int)> Removed;
59
60 private:
61         void do_log (string m)
62         {
63                 _lengths.push_back (m.length ());
64                 emit (boost::bind (boost::ref (Appended), m));
65                 if (_lengths.size() > log_lines) {
66                         emit (boost::bind (boost::ref (Removed), _lengths.front()));
67                         _lengths.pop_front ();
68                 }
69         }
70
71         list<size_t> _lengths;
72 };
73
74 static shared_ptr<MemoryLog> memory_log (new MemoryLog);
75
76 class StatusDialog : public wxDialog
77 {
78 public:
79         StatusDialog ()
80                 : wxDialog (
81                         0, wxID_ANY, _("DCP-o-matic encode server"),
82                         wxDefaultPosition, wxDefaultSize,
83                         wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER
84                         )
85         {
86                 _sizer = new wxFlexGridSizer (1, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
87                 _sizer->AddGrowableCol (0, 1);
88
89                 wxClientDC dc (this);
90                 wxSize size = dc.GetTextExtent (wxT ("This is the length of the file label it should be quite long"));
91                 int const height = (size.GetHeight() + 2) * log_lines;
92                 SetSize (700, height + DCPOMATIC_SIZER_GAP * 2);
93
94                 _text = new wxTextCtrl (
95                         this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize (-1, height),
96                         wxTE_READONLY | wxTE_MULTILINE
97                         );
98
99                 _sizer->Add (_text, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
100
101                 SetSizer (_sizer);
102                 _sizer->Layout ();
103
104                 memory_log->Appended.connect (bind (&StatusDialog::appended, this, _1));
105                 memory_log->Removed.connect (bind (&StatusDialog::removed, this, _1));
106         }
107
108 private:
109         void appended (string s)
110         {
111                 (*_text) << s << "\n";
112         }
113
114         void removed (int n)
115         {
116                 _text->Remove (0, n + 1);
117         }
118
119         wxFlexGridSizer* _sizer;
120         wxTextCtrl* _text;
121 };
122
123 class TaskBarIcon : public wxTaskBarIcon
124 {
125 public:
126         TaskBarIcon ()
127         {
128 #ifdef DCPOMATIC_WINDOWS
129                 wxIcon icon (std_to_wx ("taskbar_icon"));
130 #else
131                 wxInitAllImageHandlers();
132                 wxBitmap bitmap (wxString::Format (wxT ("%s/dcpomatic2_server_small.png"), LINUX_SHARE_PREFIX), wxBITMAP_TYPE_PNG);
133                 wxIcon icon;
134                 icon.CopyFromBitmap (bitmap);
135 #endif
136
137                 SetIcon (icon, std_to_wx ("DCP-o-matic encode server"));
138
139                 _status = new StatusDialog ();
140
141                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::status, this), ID_status);
142                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::quit, this), ID_quit);
143         }
144
145         wxMenu* CreatePopupMenu ()
146         {
147                 wxMenu* menu = new wxMenu;
148                 menu->Append (ID_status, std_to_wx ("Status..."));
149                 menu->Append (ID_quit, std_to_wx ("Quit"));
150                 return menu;
151         }
152
153 private:
154         void status ()
155         {
156                 _status->Show ();
157         }
158
159         void quit ()
160         {
161                 wxTheApp->ExitMainLoop ();
162         }
163
164         StatusDialog* _status;
165 };
166
167 class App : public wxApp, public ExceptionStore
168 {
169 public:
170         App ()
171                 : wxApp ()
172                 , _thread (0)
173                 , _icon (0)
174         {}
175
176 private:
177
178         bool OnInit ()
179         {
180                 if (!wxApp::OnInit ()) {
181                         return false;
182                 }
183
184                 dcpomatic_setup_path_encoding ();
185                 dcpomatic_setup_i18n ();
186                 dcpomatic_setup ();
187                 Config::drop ();
188
189                 signal_manager = new wxSignalManager (this);
190                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
191
192                 _icon = new TaskBarIcon;
193                 _thread = new thread (bind (&App::main_thread, this));
194
195                 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
196                 _timer.reset (new wxTimer (this));
197                 _timer->Start (1000);
198
199                 return true;
200         }
201
202         int OnExit ()
203         {
204                 delete _icon;
205                 return wxApp::OnExit ();
206         }
207
208         void main_thread ()
209         try {
210                 Server server (memory_log, false);
211                 server.run (Config::instance()->num_local_encoding_threads ());
212         } catch (...) {
213                 store_current ();
214         }
215
216         void check ()
217         {
218                 try {
219                         rethrow ();
220                 } catch (exception& e) {
221                         error_dialog (0, std_to_wx (e.what ()));
222                         wxTheApp->ExitMainLoop ();
223                 } catch (...) {
224                         error_dialog (0, _("An unknown error has occurred with the DCP-o-matic server."));
225                         wxTheApp->ExitMainLoop ();
226                 }
227         }
228
229         void idle ()
230         {
231                 signal_manager->ui_idle ();
232         }
233
234         boost::thread* _thread;
235         TaskBarIcon* _icon;
236         shared_ptr<wxTimer> _timer;
237 };
238
239 IMPLEMENT_APP (App)