2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "wx/wx_util.h"
21 #include "wx/wx_signal_manager.h"
23 #include "lib/server.h"
24 #include "lib/config.h"
26 #include "lib/signaller.h"
27 #include <wx/taskbar.h>
29 #include <boost/thread.hpp>
36 using boost::shared_ptr;
46 static int const log_lines = 32;
48 class MemoryLog : public Log, public Signaller
52 string head_and_tail (int) const {
57 boost::signals2::signal<void(string)> Appended;
58 boost::signals2::signal<void(int)> Removed;
61 void do_log (string m)
63 _lengths.push_back (m.length ());
64 emit (boost::bind (boost::ref (Appended), m));
65 if (_lengths.size() > log_lines) {
66 emit (boost::bind (boost::ref (Removed), _lengths.front()));
67 _lengths.pop_front ();
71 list<size_t> _lengths;
74 static shared_ptr<MemoryLog> memory_log (new MemoryLog);
76 class StatusDialog : public wxDialog
81 0, wxID_ANY, _("DCP-o-matic encode server"),
82 wxDefaultPosition, wxDefaultSize,
83 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER
86 _sizer = new wxFlexGridSizer (1, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
87 _sizer->AddGrowableCol (0, 1);
90 wxSize size = dc.GetTextExtent (wxT ("This is the length of the file label it should be quite long"));
91 int const height = (size.GetHeight() + 2) * log_lines;
92 SetSize (700, height + DCPOMATIC_SIZER_GAP * 2);
94 _text = new wxTextCtrl (
95 this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize (-1, height),
96 wxTE_READONLY | wxTE_MULTILINE
99 _sizer->Add (_text, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
104 memory_log->Appended.connect (bind (&StatusDialog::appended, this, _1));
105 memory_log->Removed.connect (bind (&StatusDialog::removed, this, _1));
109 void appended (string s)
111 (*_text) << s << "\n";
116 _text->Remove (0, n + 1);
119 wxFlexGridSizer* _sizer;
123 class TaskBarIcon : public wxTaskBarIcon
128 #ifdef DCPOMATIC_WINDOWS
129 wxIcon icon (std_to_wx ("taskbar_icon"));
131 wxInitAllImageHandlers();
132 wxBitmap bitmap (wxString::Format (wxT ("%s/dcpomatic2_server_small.png"), LINUX_SHARE_PREFIX), wxBITMAP_TYPE_PNG);
134 icon.CopyFromBitmap (bitmap);
137 SetIcon (icon, std_to_wx ("DCP-o-matic encode server"));
139 _status = new StatusDialog ();
141 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::status, this), ID_status);
142 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::quit, this), ID_quit);
145 wxMenu* CreatePopupMenu ()
147 wxMenu* menu = new wxMenu;
148 menu->Append (ID_status, std_to_wx ("Status..."));
149 menu->Append (ID_quit, std_to_wx ("Quit"));
161 wxTheApp->ExitMainLoop ();
164 StatusDialog* _status;
167 class App : public wxApp, public ExceptionStore
180 if (!wxApp::OnInit ()) {
184 dcpomatic_setup_path_encoding ();
185 dcpomatic_setup_i18n ();
189 signal_manager = new wxSignalManager (this);
190 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
192 _icon = new TaskBarIcon;
193 _thread = new thread (bind (&App::main_thread, this));
195 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
196 _timer.reset (new wxTimer (this));
197 _timer->Start (1000);
205 return wxApp::OnExit ();
210 Server server (memory_log, false);
211 server.run (Config::instance()->num_local_encoding_threads ());
220 } catch (exception& e) {
221 error_dialog (0, std_to_wx (e.what ()));
222 wxTheApp->ExitMainLoop ();
224 error_dialog (0, _("An unknown error has occurred with the DCP-o-matic server."));
225 wxTheApp->ExitMainLoop ();
231 signal_manager->ui_idle ();
234 boost::thread* _thread;
236 shared_ptr<wxTimer> _timer;