Merge branch 'master' of /home/carl/git/dvdomatic
[dcpomatic.git] / src / tools / servomatic_gui.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 namespace std;
29 using namespace boost;
30
31 enum {
32         ID_status = 1,
33         ID_quit,
34         ID_timer
35 };
36
37 class MemoryLog : public Log
38 {
39 public:
40
41         string get () const {
42                 boost::mutex::scoped_lock (_mutex);
43                 return _log;
44         }
45
46 private:
47         void do_log (string m)
48         {
49                 _log = m;
50         }
51
52         string _log;    
53 };
54
55 static MemoryLog memory_log;
56
57 class StatusDialog : public wxDialog
58 {
59 public:
60         StatusDialog ()
61                 : wxDialog (0, wxID_ANY, _("DVD-o-matic encode server"), wxDefaultPosition, wxSize (600, 80), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
62                 , _timer (this, ID_timer)
63         {
64                 _sizer = new wxFlexGridSizer (1, 6, 6);
65                 _sizer->AddGrowableCol (0, 1);
66
67                 _text = new wxTextCtrl (this, wxID_ANY, _(""), wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
68                 _sizer->Add (_text, 1, wxEXPAND);
69
70                 SetSizer (_sizer);
71                 _sizer->Layout ();
72
73                 Connect (ID_timer, wxEVT_TIMER, wxTimerEventHandler (StatusDialog::update));
74                 _timer.Start (1000);
75         }
76
77 private:
78         void update (wxTimerEvent &)
79         {
80                 _text->ChangeValue (std_to_wx (memory_log.get ()));
81                 _sizer->Layout ();
82         }
83
84         wxFlexGridSizer* _sizer;
85         wxTextCtrl* _text;
86         wxTimer _timer;
87 };
88
89 class TaskBarIcon : public wxTaskBarIcon
90 {
91 public:
92         TaskBarIcon ()
93         {
94                 wxIcon icon (std_to_wx ("taskbar_icon"));
95                 SetIcon (icon, std_to_wx ("DVD-o-matic encode server"));
96
97                 Connect (ID_status, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (TaskBarIcon::status));
98                 Connect (ID_quit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (TaskBarIcon::quit));
99         }
100         
101         wxMenu* CreatePopupMenu ()
102         {
103                 wxMenu* menu = new wxMenu;
104                 menu->Append (ID_status, std_to_wx ("Status..."));
105                 menu->Append (ID_quit, std_to_wx ("Quit"));
106                 return menu;
107         }
108
109 private:
110         void status (wxCommandEvent &)
111         {
112                 StatusDialog* d = new StatusDialog;
113                 d->Show ();
114         }
115
116         void quit (wxCommandEvent &)
117         {
118                 wxTheApp->ExitMainLoop ();
119         }
120 };
121
122 class App : public wxApp
123 {
124 public:
125         App ()
126                 : wxApp ()
127                 , _thread (0)
128         {}
129
130 private:        
131         
132         bool OnInit ()
133         {
134                 dvdomatic_setup ();
135
136                 new TaskBarIcon;
137
138                 _thread = new thread (bind (&App::main_thread, this));
139                 return true;
140         }
141
142         void main_thread ()
143         {
144                 Server server (&memory_log);
145                 server.run (Config::instance()->num_local_encoding_threads ());
146         }
147
148         boost::thread* _thread;
149 };
150
151 IMPLEMENT_APP (App)