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"
24 #include "lib/encoded_log_entry.h"
25 #include "lib/encode_server.h"
26 #include "lib/config.h"
28 #include "lib/signaller.h"
29 #include "lib/cross.h"
30 #include <wx/taskbar.h>
32 #include <boost/thread.hpp>
33 #include <boost/foreach.hpp>
34 #include <boost/optional.hpp>
42 using std::setprecision;
43 using boost::shared_ptr;
46 using boost::optional;
47 using boost::dynamic_pointer_cast;
55 static unsigned int const log_lines = 32;
57 class ServerLog : public Log, public Signaller
63 BOOST_FOREACH (string const & i, _log) {
69 string head_and_tail (int) const {
75 boost::mutex::scoped_lock lm (_state_mutex);
79 boost::signals2::signal<void(string)> Appended;
80 boost::signals2::signal<void(int)> Removed;
83 void do_log (shared_ptr<const LogEntry> entry)
85 time_t const s = entry->seconds ();
86 struct tm* local = localtime (&s);
89 local->tm_yday != _last_time->tm_yday ||
90 local->tm_year != _last_time->tm_year ||
91 local->tm_hour != _last_time->tm_hour ||
92 local->tm_min != _last_time->tm_min
95 strftime (buffer, 64, "%c", local);
99 append (entry->message ());
100 if (_log.size() > log_lines) {
101 emit (boost::bind (boost::ref (Removed), _log.front().length()));
106 shared_ptr<const EncodedLogEntry> encoded = dynamic_pointer_cast<const EncodedLogEntry> (entry);
108 _history.push_back (encoded->seconds ());
109 if (_history.size() > 48) {
110 _history.pop_front ();
112 if (_history.size() > 2) {
113 boost::mutex::scoped_lock lm (_state_mutex);
114 _fps = _history.size() / (_history.back() - _history.front());
119 void append (string s)
122 emit (boost::bind (boost::ref (Appended), s));
126 optional<struct tm> _last_time;
127 std::list<double> _history;
129 mutable boost::mutex _state_mutex;
133 static shared_ptr<ServerLog> server_log;
135 class StatusDialog : public wxDialog
140 0, wxID_ANY, _("DCP-o-matic Encode Server"),
141 wxDefaultPosition, wxDefaultSize,
142 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER
145 wxFlexGridSizer* state_sizer = new wxFlexGridSizer (2, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
147 add_label_to_sizer (state_sizer, this, _("Frames per second"), true);
148 _fps = new wxStaticText (this, wxID_ANY, wxT(""));
149 state_sizer->Add (_fps);
151 wxFlexGridSizer* log_sizer = new wxFlexGridSizer (1, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
152 log_sizer->AddGrowableCol (0, 1);
154 wxClientDC dc (this);
155 wxSize size = dc.GetTextExtent (wxT ("This is the length of the file label it should be quite long"));
156 int const height = (size.GetHeight() + 2) * log_lines;
157 SetSize (700, height + DCPOMATIC_SIZER_GAP * 2);
159 _text = new wxTextCtrl (
160 this, wxID_ANY, std_to_wx (server_log->get()), wxDefaultPosition, wxSize (-1, height),
161 wxTE_READONLY | wxTE_MULTILINE
164 log_sizer->Add (_text, 1, wxEXPAND);
166 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
167 overall_sizer->Add (state_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
168 overall_sizer->Add (log_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
169 SetSizer (overall_sizer);
170 overall_sizer->Layout ();
172 Bind (wxEVT_TIMER, boost::bind (&StatusDialog::update_state, this));
173 _timer.reset (new wxTimer (this));
174 _timer->Start (1000);
176 server_log->Appended.connect (bind (&StatusDialog::appended, this, _1));
177 server_log->Removed.connect (bind (&StatusDialog::removed, this, _1));
181 void appended (string s)
183 (*_text) << s << "\n";
188 _text->Remove (0, n + 1);
193 _fps->SetLabel (wxString::Format ("%.1f", server_log->fps()));
198 boost::shared_ptr<wxTimer> _timer;
201 class TaskBarIcon : public wxTaskBarIcon
207 #ifdef DCPOMATIC_WINDOWS
208 wxIcon icon (std_to_wx ("id"));
210 wxInitAllImageHandlers();
211 wxBitmap bitmap (wxString::Format (wxT ("%s/dcpomatic2.png"), std_to_wx (shared_path().string())), wxBITMAP_TYPE_PNG);
213 icon.CopyFromBitmap (bitmap);
216 SetIcon (icon, std_to_wx ("DCP-o-matic Encode Server"));
218 Bind (wxEVT_MENU, boost::bind (&TaskBarIcon::status, this), ID_status);
219 Bind (wxEVT_MENU, boost::bind (&TaskBarIcon::quit, this), ID_quit);
222 wxMenu* CreatePopupMenu ()
224 wxMenu* menu = new wxMenu;
225 menu->Append (ID_status, std_to_wx ("Status..."));
226 menu->Append (ID_quit, std_to_wx ("Quit"));
234 _status = new StatusDialog ();
241 wxTheApp->ExitMainLoop ();
244 StatusDialog* _status;
247 class App : public wxApp, public ExceptionStore
260 if (!wxApp::OnInit ()) {
264 server_log.reset (new ServerLog);
266 dcpomatic_setup_path_encoding ();
267 dcpomatic_setup_i18n ();
271 signal_manager = new wxSignalManager (this);
272 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
274 _icon = new TaskBarIcon;
275 _thread = new thread (bind (&App::main_thread, this));
277 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
278 _timer.reset (new wxTimer (this));
279 _timer->Start (1000);
287 return wxApp::OnExit ();
292 EncodeServer server (server_log, false, Config::instance()->num_local_encoding_threads());
302 } catch (exception& e) {
303 error_dialog (0, std_to_wx (e.what ()));
304 wxTheApp->ExitMainLoop ();
306 error_dialog (0, _("An unknown error has occurred with the DCP-o-matic server."));
307 wxTheApp->ExitMainLoop ();
313 signal_manager->ui_idle ();
316 boost::thread* _thread;
318 shared_ptr<wxTimer> _timer;