2 Copyright (C) 2017-2018 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 "lib/cross.h"
22 #include "lib/config.h"
24 #include "lib/update_checker.h"
25 #include "lib/compose.hpp"
26 #include "lib/dcp_content.h"
27 #include "lib/job_manager.h"
29 #include "lib/video_content.h"
30 #include "lib/subtitle_content.h"
31 #include "lib/ratio.h"
32 #include "lib/verify_dcp_job.h"
33 #include "lib/dcp_examiner.h"
34 #include "lib/examine_content_job.h"
35 #include "wx/wx_signal_manager.h"
36 #include "wx/wx_util.h"
37 #include "wx/about_dialog.h"
38 #include "wx/report_problem_dialog.h"
39 #include "wx/film_viewer.h"
40 #include "wx/player_information.h"
41 #include "wx/update_dialog.h"
42 #include "wx/player_config_dialog.h"
43 #include "wx/verify_dcp_dialog.h"
45 #include <wx/stdpaths.h>
46 #include <wx/splash.h>
47 #include <wx/cmdline.h>
48 #include <wx/preferences.h>
49 #include <wx/progdlg.h>
51 #include <ApplicationServices/ApplicationServices.h>
53 #include <boost/bind.hpp>
67 using boost::shared_ptr;
68 using boost::optional;
69 using boost::dynamic_pointer_cast;
76 /* Allow spare IDs after _history for the recent files list */
79 /* Allow spare IDs for CPLs */
80 ID_view_scale_appropriate = 200,
83 ID_view_scale_quarter,
84 ID_help_report_a_problem,
86 ID_tools_check_for_updates,
87 /* IDs for shortcuts (with no associated menu item) */
93 class DOMFrame : public wxFrame
97 : wxFrame (0, -1, _("DCP-o-matic Player"))
98 , _update_news_requested (false)
103 , _history_position (0)
104 , _history_separator (0)
108 #if defined(DCPOMATIC_WINDOWS)
109 maybe_open_console ();
110 cout << "DCP-o-matic Player is starting." << "\n";
113 wxMenuBar* bar = new wxMenuBar;
115 set_menu_sensitivity ();
118 #ifdef DCPOMATIC_WINDOWS
119 SetIcon (wxIcon (std_to_wx ("id")));
122 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
125 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
126 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_ov, this), ID_file_add_ov);
127 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_kdm, this), ID_file_add_kdm);
128 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_history, this, _1), ID_file_history, ID_file_history + HISTORY_SIZE);
129 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_close, this), ID_file_close);
130 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
131 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
132 Bind (wxEVT_MENU, boost::bind (&DOMFrame::view_cpl, this, _1), ID_view_cpl, ID_view_cpl + MAX_CPLS);
133 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
134 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
135 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
136 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
137 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
138 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_verify, this), ID_tools_verify);
139 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
141 /* Use a panel as the only child of the Frame so that we avoid
142 the dark-grey background on Windows.
144 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
146 _viewer = new FilmViewer (overall_panel, false, false);
147 _viewer->set_dcp_decode_reduction (Config::instance()->decode_reduction ());
148 _info = new PlayerInformation (overall_panel, _viewer);
149 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
150 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
151 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
152 overall_panel->SetSizer (main_sizer);
155 int accelerators = 4;
157 int accelerators = 3;
160 wxAcceleratorEntry* accel = new wxAcceleratorEntry[accelerators];
161 accel[0].Set(wxACCEL_NORMAL, WXK_SPACE, ID_start_stop);
162 accel[1].Set(wxACCEL_NORMAL, WXK_LEFT, ID_back_frame);
163 accel[2].Set(wxACCEL_NORMAL, WXK_RIGHT, ID_forward_frame);
165 accel[3].Set(wxACCEL_CTRL, static_cast<int>('W'), ID_file_close);
167 wxAcceleratorTable accel_table (accelerators, accel);
168 SetAcceleratorTable (accel_table);
171 Bind (wxEVT_MENU, boost::bind (&DOMFrame::start_stop_pressed, this), ID_start_stop);
172 Bind (wxEVT_MENU, boost::bind (&DOMFrame::back_frame, this), ID_back_frame);
173 Bind (wxEVT_MENU, boost::bind (&DOMFrame::forward_frame, this), ID_forward_frame);
175 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
178 void set_decode_reduction (optional<int> reduction)
180 _viewer->set_dcp_decode_reduction (reduction);
181 _info->triggered_update ();
182 Config::instance()->set_decode_reduction (reduction);
185 void load_dcp (boost::filesystem::path dir)
187 _film.reset (new Film (optional<boost::filesystem::path>()));
188 shared_ptr<DCPContent> dcp;
190 dcp.reset (new DCPContent (_film, dir));
191 } catch (boost::filesystem::filesystem_error& e) {
192 error_dialog (this, _("Could not load DCP"), std_to_wx (e.what()));
196 _film->examine_and_add_content (dcp, true);
197 bool const ok = progress (_("Loading DCP"));
198 if (!ok || !report_errors_from_last_job()) {
202 setup_from_dcp (dcp);
204 if (dcp->three_d()) {
205 _film->set_three_d (true);
208 _viewer->set_film (_film);
209 _viewer->set_position (DCPTime ());
210 _info->triggered_update ();
212 Config::instance()->add_to_player_history (dir);
214 set_menu_sensitivity ();
216 wxMenuItemList old = _cpl_menu->GetMenuItems();
217 for (wxMenuItemList::iterator i = old.begin(); i != old.end(); ++i) {
218 _cpl_menu->Remove (*i);
221 DCPExaminer ex (dcp);
222 int id = ID_view_cpl;
223 BOOST_FOREACH (shared_ptr<dcp::CPL> i, ex.cpls()) {
224 wxMenuItem* j = _cpl_menu->AppendRadioItem(id, i->id());
225 if (!dcp->cpl() || i->id() == *dcp->cpl()) {
234 void setup_menu (wxMenuBar* m)
236 _file_menu = new wxMenu;
237 _file_menu->Append (ID_file_open, _("&Open...\tCtrl-O"));
238 _file_add_ov = _file_menu->Append (ID_file_add_ov, _("&Add OV..."));
239 _file_add_kdm = _file_menu->Append (ID_file_add_kdm, _("&Add KDM..."));
241 _history_position = _file_menu->GetMenuItems().GetCount();
243 _file_menu->AppendSeparator ();
244 _file_menu->Append (ID_file_close, _("&Close"));
245 _file_menu->AppendSeparator ();
248 _file_menu->Append (wxID_EXIT, _("&Exit"));
250 _file_menu->Append (wxID_EXIT, _("&Quit"));
254 _file_menu->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
256 wxMenu* edit = new wxMenu;
257 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
260 _cpl_menu = new wxMenu;
262 wxMenu* view = new wxMenu;
263 optional<int> c = Config::instance()->decode_reduction();
264 _view_cpl = view->Append(ID_view_cpl, _("CPL"), _cpl_menu);
265 view->AppendSeparator();
266 view->AppendRadioItem(ID_view_scale_appropriate, _("Set decode resolution to match display"))->Check(!static_cast<bool>(c));
267 view->AppendRadioItem(ID_view_scale_full, _("Decode at full resolution"))->Check(c && c.get() == 0);
268 view->AppendRadioItem(ID_view_scale_half, _("Decode at half resolution"))->Check(c && c.get() == 1);
269 view->AppendRadioItem(ID_view_scale_quarter, _("Decode at quarter resolution"))->Check(c && c.get() == 2);
271 wxMenu* tools = new wxMenu;
272 _tools_verify = tools->Append (ID_tools_verify, _("Verify DCP"));
273 tools->AppendSeparator ();
274 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
276 wxMenu* help = new wxMenu;
278 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
280 help->Append (wxID_ABOUT, _("About"));
282 help->Append (ID_help_report_a_problem, _("Report a problem..."));
284 m->Append (_file_menu, _("&File"));
286 m->Append (edit, _("&Edit"));
288 m->Append (view, _("&View"));
289 m->Append (tools, _("&Tools"));
290 m->Append (help, _("&Help"));
295 wxString d = wxStandardPaths::Get().GetDocumentsDir();
296 if (Config::instance()->last_player_load_directory()) {
297 d = std_to_wx (Config::instance()->last_player_load_directory()->string());
300 wxDirDialog* c = new wxDirDialog (this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
305 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
306 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
313 boost::filesystem::path const dcp (wx_to_std (c->GetPath ()));
315 Config::instance()->set_last_player_load_directory (dcp.parent_path());
323 wxDirDialog* c = new wxDirDialog (
325 _("Select DCP to open as OV"),
326 wxStandardPaths::Get().GetDocumentsDir(),
327 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
333 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
334 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
341 DCPOMATIC_ASSERT (_film);
342 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
343 DCPOMATIC_ASSERT (dcp);
344 dcp->add_ov (wx_to_std(c->GetPath()));
345 JobManager::instance()->add(shared_ptr<Job>(new ExamineContentJob (_film, dcp)));
346 bool const ok = progress (_("Loading DCP"));
347 if (!ok || !report_errors_from_last_job()) {
350 setup_from_dcp (dcp);
354 _info->triggered_update ();
359 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
361 if (d->ShowModal() == wxID_OK) {
362 DCPOMATIC_ASSERT (_film);
363 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
364 DCPOMATIC_ASSERT (dcp);
366 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
367 dcp->examine (shared_ptr<Job>());
368 } catch (exception& e) {
369 error_dialog (this, wxString::Format (_("Could not load KDM.")), std_to_wx(e.what()));
376 _info->triggered_update ();
379 void file_history (wxCommandEvent& event)
381 vector<boost::filesystem::path> history = Config::instance()->player_history ();
382 int n = event.GetId() - ID_file_history;
383 if (n >= 0 && n < static_cast<int> (history.size ())) {
384 load_dcp (history[n]);
390 _viewer->set_film (shared_ptr<Film>());
392 _info->triggered_update ();
393 set_menu_sensitivity ();
401 void edit_preferences ()
403 if (!_config_dialog) {
404 _config_dialog = create_player_config_dialog ();
406 _config_dialog->Show (this);
409 void view_cpl (wxCommandEvent& ev)
411 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
412 DCPOMATIC_ASSERT (dcp);
413 DCPExaminer ex (dcp);
414 int id = ev.GetId() - ID_view_cpl;
415 DCPOMATIC_ASSERT (id >= 0);
416 DCPOMATIC_ASSERT (id < int(ex.cpls().size()));
417 list<shared_ptr<dcp::CPL> > cpls = ex.cpls();
418 list<shared_ptr<dcp::CPL> >::iterator i = cpls.begin();
424 dcp->set_cpl ((*i)->id());
425 dcp->examine (shared_ptr<Job>());
430 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
431 DCPOMATIC_ASSERT (dcp);
433 JobManager* jm = JobManager::instance ();
434 jm->add (shared_ptr<Job> (new VerifyDCPJob (dcp->directories())));
435 bool const ok = progress (_("Verifying DCP"));
440 DCPOMATIC_ASSERT (!jm->get().empty());
441 shared_ptr<VerifyDCPJob> last = dynamic_pointer_cast<VerifyDCPJob> (jm->get().back());
442 DCPOMATIC_ASSERT (last);
444 VerifyDCPDialog* d = new VerifyDCPDialog (this, last);
449 void tools_check_for_updates ()
451 UpdateChecker::instance()->run ();
452 _update_news_requested = true;
457 AboutDialog* d = new AboutDialog (this);
462 void help_report_a_problem ()
464 ReportProblemDialog* d = new ReportProblemDialog (this);
465 if (d->ShowModal () == wxID_OK) {
471 void update_checker_state_changed ()
473 UpdateChecker* uc = UpdateChecker::instance ();
475 bool const announce =
476 _update_news_requested ||
477 (uc->stable() && Config::instance()->check_for_updates()) ||
478 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
480 _update_news_requested = false;
486 if (uc->state() == UpdateChecker::YES) {
487 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
488 dialog->ShowModal ();
490 } else if (uc->state() == UpdateChecker::FAILED) {
491 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
493 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
496 _update_news_requested = false;
499 void config_changed ()
501 /* Instantly save any config changes when using the player GUI */
503 Config::instance()->write_config();
504 } catch (exception& e) {
508 _("Could not write to config file at %s. Your changes have not been saved."),
509 std_to_wx (Config::instance()->cinemas_file().string()).data()
514 for (int i = 0; i < _history_items; ++i) {
515 delete _file_menu->Remove (ID_file_history + i);
518 if (_history_separator) {
519 _file_menu->Remove (_history_separator);
521 delete _history_separator;
522 _history_separator = 0;
524 int pos = _history_position;
526 vector<boost::filesystem::path> history = Config::instance()->player_history ();
528 if (!history.empty ()) {
529 _history_separator = _file_menu->InsertSeparator (pos++);
532 for (size_t i = 0; i < history.size(); ++i) {
535 s = String::compose ("&%1 %2", i + 1, history[i].string());
537 s = history[i].string();
539 _file_menu->Insert (pos++, ID_file_history + i, std_to_wx (s));
542 _history_items = history.size ();
545 void set_menu_sensitivity ()
547 _tools_verify->Enable (static_cast<bool>(_film));
548 _file_add_ov->Enable (static_cast<bool>(_film));
549 _file_add_kdm->Enable (static_cast<bool>(_film));
550 _view_cpl->Enable (static_cast<bool>(_film));
553 void start_stop_pressed ()
555 if (_viewer->playing()) {
564 _viewer->back_frame ();
567 void forward_frame ()
569 _viewer->forward_frame ();
574 /** @return false if the task was cancelled */
575 bool progress (wxString task)
577 JobManager* jm = JobManager::instance ();
579 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), task, 100, 0, wxPD_CAN_ABORT);
583 while (jm->work_to_do() || signal_manager->ui_idle()) {
585 if (!progress->Pulse()) {
586 /* user pressed cancel */
587 BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
595 progress->Destroy ();
599 bool report_errors_from_last_job ()
601 JobManager* jm = JobManager::instance ();
603 DCPOMATIC_ASSERT (!jm->get().empty());
605 shared_ptr<Job> last = jm->get().back();
606 if (last->finished_in_error()) {
607 error_dialog(this, std_to_wx(last->error_summary()) + ".\n", std_to_wx(last->error_details()));
614 void setup_from_dcp (shared_ptr<DCPContent> dcp)
617 dcp->subtitle->set_use (true);
621 Ratio const * r = Ratio::nearest_from_ratio(dcp->video->size().ratio());
623 _film->set_container(r);
628 bool _update_news_requested;
629 PlayerInformation* _info;
630 wxPreferencesEditor* _config_dialog;
632 wxMenuItem* _view_cpl;
635 int _history_position;
636 wxMenuItem* _history_separator;
638 boost::shared_ptr<Film> _film;
639 boost::signals2::scoped_connection _config_changed_connection;
640 wxMenuItem* _file_add_ov;
641 wxMenuItem* _file_add_kdm;
642 wxMenuItem* _tools_verify;
645 static const wxCmdLineEntryDesc command_line_description[] = {
646 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
647 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
651 * @brief The magic App class for wxWidgets.
653 class App : public wxApp
666 wxInitAllImageHandlers ();
668 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
669 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
671 wxSplashScreen* splash = maybe_show_splash ();
673 SetAppName (_("DCP-o-matic Player"));
675 if (!wxApp::OnInit()) {
679 #ifdef DCPOMATIC_LINUX
680 unsetenv ("UBUNTU_MENUPROXY");
684 ProcessSerialNumber serial;
685 GetCurrentProcess (&serial);
686 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
689 dcpomatic_setup_path_encoding ();
691 /* Enable i18n; this will create a Config object
692 to look for a force-configured language. This Config
693 object will be wrong, however, because dcpomatic_setup
694 hasn't yet been called and there aren't any filters etc.
697 dcpomatic_setup_i18n ();
699 /* Set things up, including filters etc.
700 which will now be internationalised correctly.
704 /* Force the configuration to be re-loaded correctly next
709 _frame = new DOMFrame ();
710 SetTopWindow (_frame);
717 signal_manager = new wxSignalManager (this);
719 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
721 _frame->load_dcp (_dcp_to_load);
722 } catch (exception& e) {
723 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1.")), _dcp_to_load)), std_to_wx(e.what()));
727 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
729 if (Config::instance()->check_for_updates ()) {
730 UpdateChecker::instance()->run ();
737 error_dialog (0, _("DCP-o-matic Player could not start."), std_to_wx(e.what()));
741 void OnInitCmdLine (wxCmdLineParser& parser)
743 parser.SetDesc (command_line_description);
744 parser.SetSwitchChars (wxT ("-"));
747 bool OnCmdLineParsed (wxCmdLineParser& parser)
749 if (parser.GetParamCount() > 0) {
750 _dcp_to_load = wx_to_std (parser.GetParam (0));
756 void report_exception ()
760 } catch (FileError& e) {
764 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
765 std_to_wx (e.what()),
766 std_to_wx (e.file().string().c_str ())
769 } catch (exception& e) {
773 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
774 std_to_wx (e.what ())
778 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
782 /* An unhandled exception has occurred inside the main event loop */
783 bool OnExceptionInMainLoop ()
786 /* This will terminate the program */
790 void OnUnhandledException ()
797 signal_manager->ui_idle ();
800 void config_failed_to_load ()
802 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
805 void config_warning (string m)
807 message_dialog (_frame, std_to_wx (m));