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