Add missing files.
[dcpomatic.git] / src / tools / dcpomatic_server.cc
1 /*
2     Copyright (C) 2012 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 <boost/thread.hpp>
21 #include <wx/taskbar.h>
22 #include <wx/icon.h>
23 #include "wx_util.h"
24 #include "lib/util.h"
25 #include "lib/server.h"
26 #include "lib/config.h"
27
28 using std::cout;
29 using std::string;
30 using boost::shared_ptr;
31 using boost::thread;
32 using boost::bind;
33
34 enum {
35         ID_status = 1,
36         ID_quit,
37         ID_timer
38 };
39
40 class MemoryLog : public Log
41 {
42 public:
43
44         string get () const {
45                 boost::mutex::scoped_lock (_mutex);
46                 return _log;
47         }
48
49 private:
50         void do_log (string m)
51         {
52                 _log = m;
53         }
54
55         string _log;    
56 };
57
58 static shared_ptr<MemoryLog> memory_log (new MemoryLog);
59
60 class StatusDialog : public wxDialog
61 {
62 public:
63         StatusDialog ()
64                 : wxDialog (0, wxID_ANY, _("DCP-o-matic encode server"), wxDefaultPosition, wxSize (600, 80), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
65                 , _timer (this, ID_timer)
66         {
67                 _sizer = new wxFlexGridSizer (1, 6, 6);
68                 _sizer->AddGrowableCol (0, 1);
69
70                 _text = new wxTextCtrl (this, wxID_ANY, _(""), wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
71                 _sizer->Add (_text, 1, wxEXPAND);
72
73                 SetSizer (_sizer);
74                 _sizer->Layout ();
75
76                 Connect (ID_timer, wxEVT_TIMER, wxTimerEventHandler (StatusDialog::update));
77                 _timer.Start (1000);
78         }
79
80 private:
81         void update (wxTimerEvent &)
82         {
83                 _text->ChangeValue (std_to_wx (memory_log->get ()));
84                 _sizer->Layout ();
85         }
86
87         wxFlexGridSizer* _sizer;
88         wxTextCtrl* _text;
89         wxTimer _timer;
90 };
91
92 class TaskBarIcon : public wxTaskBarIcon
93 {
94 public:
95         TaskBarIcon ()
96         {
97 #ifdef __WXMSW__                
98                 wxIcon icon (std_to_wx ("taskbar_icon"));
99 #endif
100 #ifdef __WXGTK__
101                 wxInitAllImageHandlers();
102                 wxBitmap bitmap (wxString::Format (wxT ("%s/taskbar_icon.png"), POSIX_ICON_PREFIX), wxBITMAP_TYPE_PNG);
103                 wxIcon icon;
104                 icon.CopyFromBitmap (bitmap);
105 #endif          
106                 SetIcon (icon, std_to_wx ("DCP-o-matic encode server"));
107
108                 Connect (ID_status, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (TaskBarIcon::status));
109                 Connect (ID_quit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (TaskBarIcon::quit));
110         }
111         
112         wxMenu* CreatePopupMenu ()
113         {
114                 wxMenu* menu = new wxMenu;
115                 menu->Append (ID_status, std_to_wx ("Status..."));
116                 menu->Append (ID_quit, std_to_wx ("Quit"));
117                 return menu;
118         }
119
120 private:
121         void status (wxCommandEvent &)
122         {
123                 StatusDialog* d = new StatusDialog;
124                 d->Show ();
125         }
126
127         void quit (wxCommandEvent &)
128         {
129                 wxTheApp->ExitMainLoop ();
130         }
131 };
132
133 class App : public wxApp
134 {
135 public:
136         App ()
137                 : wxApp ()
138                 , _thread (0)
139                 , _icon (0)
140         {}
141
142 private:        
143         
144         bool OnInit ()
145         {
146                 if (!wxApp::OnInit ()) {
147                         return false;
148                 }
149                 
150                 dcpomatic_setup ();
151
152                 _icon = new TaskBarIcon;
153                 _thread = new thread (bind (&App::main_thread, this));
154                 
155                 return true;
156         }
157
158         int OnExit ()
159         {
160                 delete _icon;
161                 return wxApp::OnExit ();
162         }
163
164         void main_thread ()
165         {
166                 Server server (memory_log);
167                 server.run (Config::instance()->num_local_encoding_threads ());
168         }
169
170         boost::thread* _thread;
171         TaskBarIcon* _icon;
172 };
173
174 IMPLEMENT_APP (App)