Try to improve _server on OSX.
[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 "lib/util.h"
22 #include "lib/server.h"
23 #include "lib/config.h"
24 #include "lib/log.h"
25 #include <wx/taskbar.h>
26 #include <wx/icon.h>
27 #include <boost/thread.hpp>
28 #include <iostream>
29
30 using std::cout;
31 using std::string;
32 using std::exception;
33 using boost::shared_ptr;
34 using boost::thread;
35 using boost::bind;
36
37 enum {
38         ID_status = 1,
39         ID_quit,
40         ID_timer
41 };
42
43 class MemoryLog : public Log
44 {
45 public:
46
47         string get () const {
48                 boost::mutex::scoped_lock (_mutex);
49                 return _log;
50         }
51
52         string head_and_tail (int amount = 1024) const {
53                 if (int (_log.size ()) < (2 * amount)) {
54                         return _log;
55                 }
56
57                 return _log.substr (0, amount) + _log.substr (_log.size() - amount - 1, amount);
58         }
59
60 private:
61         void do_log (string m)
62         {
63                 _log = m;
64         }
65
66         string _log;
67 };
68
69 static shared_ptr<MemoryLog> memory_log (new MemoryLog);
70
71 #ifdef DCPOMATIC_OSX
72 class StatusDialog : public wxFrame
73 #else
74 class StatusDialog : public wxDialog
75 #endif
76 {
77 public:
78         StatusDialog ()
79 #ifdef DCPOMATIC_OSX
80                 : wxFrame (0, wxID_ANY, _("DCP-o-matic encode server"), wxDefaultPosition, wxSize (600, 80), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
81 #else
82                 : wxDialog (0, wxID_ANY, _("DCP-o-matic encode server"), wxDefaultPosition, wxSize (600, 80), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
83 #endif
84                 , _timer (this, ID_timer)
85         {
86                 _sizer = new wxFlexGridSizer (1, 6, 6);
87                 _sizer->AddGrowableCol (0, 1);
88
89                 _text = new wxTextCtrl (this, wxID_ANY, _(""), wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
90                 _sizer->Add (_text, 1, wxEXPAND);
91
92                 SetSizer (_sizer);
93                 _sizer->Layout ();
94
95                 Bind (wxEVT_TIMER, boost::bind (&StatusDialog::update, this), ID_timer);
96                 _timer.Start (1000);
97         }
98
99 private:
100         void update ()
101         {
102                 _text->ChangeValue (std_to_wx (memory_log->get ()));
103                 _sizer->Layout ();
104         }
105
106         wxFlexGridSizer* _sizer;
107         wxTextCtrl* _text;
108         wxTimer _timer;
109 };
110
111 class TaskBarIcon : public wxTaskBarIcon
112 {
113 public:
114         TaskBarIcon ()
115         {
116 #ifdef DCPOMATIC_WINDOWS
117                 wxIcon icon (std_to_wx ("taskbar_icon"));
118 #endif
119 #ifdef DCPOMATIC_LINUX
120                 wxInitAllImageHandlers();
121                 wxBitmap bitmap (wxString::Format (wxT ("%s/dcpomatic2_server_small.png"), LINUX_SHARE_PREFIX), wxBITMAP_TYPE_PNG);
122                 wxIcon icon;
123                 icon.CopyFromBitmap (bitmap);
124 #endif
125 #ifndef DCPOMATIC_OSX
126                 SetIcon (icon, std_to_wx ("DCP-o-matic encode server"));
127 #endif
128
129 #ifdef DCPOMATIC_OSX
130                 wxMenu* file = new wxMenu;
131                 file->Append (wxID_EXIT, _("&Exit"));
132                 wxMenuBar* bar = new wxMenuBar;
133                 bar->Append (file, _("&File"));
134                 SetMenuBar (bar);
135
136                 status ();
137 #endif
138
139                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::status, this), ID_status);
140                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::quit, this), ID_quit);
141         }
142
143         wxMenu* CreatePopupMenu ()
144         {
145                 wxMenu* menu = new wxMenu;
146                 menu->Append (ID_status, std_to_wx ("Status..."));
147                 menu->Append (ID_quit, std_to_wx ("Quit"));
148                 return menu;
149         }
150
151 private:
152         void status ()
153         {
154                 StatusDialog* d = new StatusDialog;
155                 d->Show ();
156         }
157
158         void quit ()
159         {
160                 wxTheApp->ExitMainLoop ();
161         }
162 };
163
164 class App : public wxApp, public ExceptionStore
165 {
166 public:
167         App ()
168                 : wxApp ()
169                 , _thread (0)
170                 , _icon (0)
171         {}
172
173 private:
174
175         bool OnInit ()
176         {
177                 if (!wxApp::OnInit ()) {
178                         return false;
179                 }
180
181                 dcpomatic_setup_path_encoding ();
182                 dcpomatic_setup_i18n ();
183                 dcpomatic_setup ();
184                 Config::drop ();
185
186                 _icon = new TaskBarIcon;
187                 _thread = new thread (bind (&App::main_thread, this));
188
189                 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
190                 _timer.reset (new wxTimer (this));
191                 _timer->Start (1000);
192
193                 return true;
194         }
195
196         int OnExit ()
197         {
198                 delete _icon;
199                 return wxApp::OnExit ();
200         }
201
202         void main_thread ()
203         try {
204                 Server server (memory_log, false);
205                 server.run (Config::instance()->num_local_encoding_threads ());
206         } catch (...) {
207                 store_current ();
208         }
209
210         void check ()
211         {
212                 try {
213                         rethrow ();
214                 } catch (exception& e) {
215                         error_dialog (0, std_to_wx (e.what ()));
216                         wxTheApp->ExitMainLoop ();
217                 } catch (...) {
218                         error_dialog (0, _("An unknown error has occurred with the DCP-o-matic server."));
219                         wxTheApp->ExitMainLoop ();
220                 }
221         }
222
223         boost::thread* _thread;
224         TaskBarIcon* _icon;
225         shared_ptr<wxTimer> _timer;
226 };
227
228 IMPLEMENT_APP (App)