2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "wx/wx_util.h"
22 #include "wx/wx_signal_manager.h"
23 #include "wx/static_text.h"
25 #include "lib/encoded_log_entry.h"
26 #include "lib/encode_server.h"
27 #include "lib/config.h"
29 #include "lib/signaller.h"
30 #include "lib/cross.h"
31 #include "lib/dcpomatic_log.h"
32 #include "lib/warnings.h"
33 DCPOMATIC_DISABLE_WARNINGS
34 #include <wx/taskbar.h>
35 #include <wx/splash.h>
37 DCPOMATIC_ENABLE_WARNINGS
38 #include <boost/thread.hpp>
39 #include <boost/foreach.hpp>
40 #include <boost/optional.hpp>
48 using std::setprecision;
49 using boost::shared_ptr;
52 using boost::optional;
53 using boost::dynamic_pointer_cast;
54 #if BOOST_VERSION >= 106100
55 using namespace boost::placeholders;
64 static unsigned int const log_lines = 32;
66 class ServerLog : public Log, public Signaller
75 BOOST_FOREACH (string const & i, _log) {
82 boost::mutex::scoped_lock lm (_state_mutex);
86 boost::signals2::signal<void(string)> Appended;
87 boost::signals2::signal<void(int)> Removed;
90 void do_log (shared_ptr<const LogEntry> entry)
92 time_t const s = entry->seconds ();
93 struct tm* local = localtime (&s);
96 local->tm_yday != _last_time->tm_yday ||
97 local->tm_year != _last_time->tm_year ||
98 local->tm_hour != _last_time->tm_hour ||
99 local->tm_min != _last_time->tm_min
102 strftime (buffer, 64, "%c", local);
106 append (entry->message ());
107 if (_log.size() > log_lines) {
108 emit (boost::bind (boost::ref (Removed), _log.front().length()));
113 shared_ptr<const EncodedLogEntry> encoded = dynamic_pointer_cast<const EncodedLogEntry> (entry);
115 _history.push_back (encoded->seconds ());
116 if (_history.size() > 48) {
117 _history.pop_front ();
119 if (_history.size() > 2) {
120 boost::mutex::scoped_lock lm (_state_mutex);
121 _fps = _history.size() / (_history.back() - _history.front());
126 void append (string s)
129 emit (boost::bind (boost::ref (Appended), s));
133 optional<struct tm> _last_time;
134 std::list<double> _history;
136 mutable boost::mutex _state_mutex;
140 static shared_ptr<ServerLog> server_log;
142 class StatusDialog : public wxDialog
147 0, wxID_ANY, _("DCP-o-matic Encode Server"),
148 wxDefaultPosition, wxDefaultSize,
150 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxSTAY_ON_TOP
152 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER
156 wxFlexGridSizer* state_sizer = new wxFlexGridSizer (2, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
158 add_label_to_sizer (state_sizer, this, _("Frames per second"), true);
159 _fps = new StaticText (this, wxT(""));
160 state_sizer->Add (_fps);
162 wxFlexGridSizer* log_sizer = new wxFlexGridSizer (1, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
163 log_sizer->AddGrowableCol (0, 1);
165 wxClientDC dc (this);
166 wxSize size = dc.GetTextExtent (wxT ("This is the length of the file label it should be quite long"));
167 int const height = (size.GetHeight() + 2) * log_lines;
168 SetSize (700, height + DCPOMATIC_SIZER_GAP * 2);
170 _text = new wxTextCtrl (
171 this, wxID_ANY, std_to_wx (server_log->get()), wxDefaultPosition, wxSize (-1, height),
172 wxTE_READONLY | wxTE_MULTILINE
175 log_sizer->Add (_text, 1, wxEXPAND);
177 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
178 overall_sizer->Add (state_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
179 overall_sizer->Add (log_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
180 SetSizer (overall_sizer);
181 overall_sizer->Layout ();
183 Bind (wxEVT_TIMER, boost::bind (&StatusDialog::update_state, this));
184 _timer.reset (new wxTimer (this));
185 _timer->Start (1000);
187 server_log->Appended.connect (bind (&StatusDialog::appended, this, _1));
188 server_log->Removed.connect (bind (&StatusDialog::removed, this, _1));
192 void appended (string s)
194 (*_text) << s << "\n";
199 _text->Remove (0, n + 1);
204 _fps->SetLabel (wxString::Format ("%.1f", server_log->fps()));
209 boost::shared_ptr<wxTimer> _timer;
212 class TaskBarIcon : public wxTaskBarIcon
218 #ifdef DCPOMATIC_WINDOWS
219 wxIcon icon (std_to_wx ("id"));
221 wxBitmap bitmap (wxString::Format (wxT ("%s/dcpomatic_small.png"), std_to_wx (shared_path().string())), wxBITMAP_TYPE_PNG);
223 icon.CopyFromBitmap (bitmap);
226 SetIcon (icon, std_to_wx ("DCP-o-matic Encode Server"));
228 Bind (wxEVT_MENU, boost::bind (&TaskBarIcon::status, this), ID_status);
229 Bind (wxEVT_MENU, boost::bind (&TaskBarIcon::quit, this), ID_quit);
232 wxMenu* CreatePopupMenu ()
234 wxMenu* menu = new wxMenu;
235 menu->Append (ID_status, std_to_wx ("Status..."));
236 menu->Append (ID_quit, std_to_wx ("Quit"));
244 _status = new StatusDialog ();
251 wxTheApp->ExitMainLoop ();
254 StatusDialog* _status;
257 class App : public wxApp, public ExceptionStore
269 if (!wxApp::OnInit ()) {
273 wxInitAllImageHandlers ();
275 server_log.reset (new ServerLog);
276 server_log->set_types (LogEntry::TYPE_GENERAL | LogEntry::TYPE_WARNING | LogEntry::TYPE_ERROR);
277 dcpomatic_log = server_log;
279 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
280 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
282 wxSplashScreen* splash = maybe_show_splash ();
284 dcpomatic_setup_path_encoding ();
285 dcpomatic_setup_i18n ();
289 signal_manager = new wxSignalManager (this);
290 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
292 /* Bad things happen (on Linux at least) if the config is reloaded by main_thread;
293 it seems like there's a race which results in the locked_sstream mutex being
294 locked before it is initialised. Calling Config::instance() here loads the config
295 again in this thread, which seems to work around the problem.
299 #ifdef DCPOMATIC_LINUX
300 StatusDialog* d = new StatusDialog ();
303 _icon = new TaskBarIcon;
305 _thread = thread (bind (&App::main_thread, this));
307 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
308 _timer.reset (new wxTimer (this));
309 _timer->Start (1000);
315 SetExitOnFrameDelete (false);
323 return wxApp::OnExit ();
328 EncodeServer server (false, Config::instance()->server_encoding_threads());
338 } catch (exception& e) {
339 error_dialog (0, std_to_wx (e.what ()));
340 wxTheApp->ExitMainLoop ();
342 error_dialog (0, _("An unknown error has occurred with the DCP-o-matic server."));
343 wxTheApp->ExitMainLoop ();
349 signal_manager->ui_idle ();
352 void config_failed_to_load ()
354 message_dialog (0, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
357 void config_warning (string m)
359 message_dialog (0, std_to_wx (m));
362 boost::thread _thread;
364 shared_ptr<wxTimer> _timer;