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 "wx/wx_signal_manager.h"
35 #include "wx/wx_util.h"
36 #include "wx/about_dialog.h"
37 #include "wx/report_problem_dialog.h"
38 #include "wx/film_viewer.h"
39 #include "wx/player_information.h"
40 #include "wx/update_dialog.h"
41 #include "wx/player_config_dialog.h"
42 #include "wx/verify_dcp_dialog.h"
44 #include <wx/stdpaths.h>
45 #include <wx/splash.h>
46 #include <wx/cmdline.h>
47 #include <wx/preferences.h>
48 #include <wx/progdlg.h>
50 #include <ApplicationServices/ApplicationServices.h>
52 #include <boost/bind.hpp>
66 using boost::shared_ptr;
67 using boost::optional;
68 using boost::dynamic_pointer_cast;
75 /* Allow spare IDs after _history for the recent files list */
78 /* Allow spare IDs for CPLs */
79 ID_view_scale_appropriate = 200,
82 ID_view_scale_quarter,
83 ID_help_report_a_problem,
85 ID_tools_check_for_updates,
86 /* IDs for shortcuts (with no associated menu item) */
92 class DOMFrame : public wxFrame
96 : wxFrame (0, -1, _("DCP-o-matic Player"))
97 , _update_news_requested (false)
102 , _history_position (0)
103 , _history_separator (0)
107 #if defined(DCPOMATIC_WINDOWS)
108 maybe_open_console ();
109 cout << "DCP-o-matic Player is starting." << "\n";
112 wxMenuBar* bar = new wxMenuBar;
114 set_menu_sensitivity ();
117 #ifdef DCPOMATIC_WINDOWS
118 SetIcon (wxIcon (std_to_wx ("id")));
121 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
124 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
125 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_ov, this), ID_file_add_ov);
126 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_kdm, this), ID_file_add_kdm);
127 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_history, this, _1), ID_file_history, ID_file_history + HISTORY_SIZE);
128 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_close, this), ID_file_close);
129 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
130 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
131 Bind (wxEVT_MENU, boost::bind (&DOMFrame::view_cpl, this, _1), ID_view_cpl, ID_view_cpl + MAX_CPLS);
132 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
133 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
134 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
135 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
136 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
137 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_verify, this), ID_tools_verify);
138 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
140 /* Use a panel as the only child of the Frame so that we avoid
141 the dark-grey background on Windows.
143 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
145 _viewer = new FilmViewer (overall_panel, false, false);
146 _viewer->set_dcp_decode_reduction (Config::instance()->decode_reduction ());
147 _info = new PlayerInformation (overall_panel, _viewer);
148 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
149 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
150 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
151 overall_panel->SetSizer (main_sizer);
154 int accelerators = 4;
156 int accelerators = 3;
159 wxAcceleratorEntry* accel = new wxAcceleratorEntry[accelerators];
160 accel[0].Set(wxACCEL_NORMAL, WXK_SPACE, ID_start_stop);
161 accel[1].Set(wxACCEL_NORMAL, WXK_LEFT, ID_back_frame);
162 accel[2].Set(wxACCEL_NORMAL, WXK_RIGHT, ID_forward_frame);
164 accel[3].Set(wxACCEL_CTRL, static_cast<int>('W'), ID_file_close);
166 wxAcceleratorTable accel_table (accelerators, accel);
167 SetAcceleratorTable (accel_table);
170 Bind (wxEVT_MENU, boost::bind (&DOMFrame::start_stop_pressed, this), ID_start_stop);
171 Bind (wxEVT_MENU, boost::bind (&DOMFrame::back_frame, this), ID_back_frame);
172 Bind (wxEVT_MENU, boost::bind (&DOMFrame::forward_frame, this), ID_forward_frame);
174 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
177 void set_decode_reduction (optional<int> reduction)
179 _viewer->set_dcp_decode_reduction (reduction);
180 _info->triggered_update ();
181 Config::instance()->set_decode_reduction (reduction);
184 void load_dcp (boost::filesystem::path dir)
186 _film.reset (new Film (optional<boost::filesystem::path>()));
187 shared_ptr<DCPContent> dcp;
189 dcp.reset (new DCPContent (_film, dir));
190 } catch (boost::filesystem::filesystem_error& e) {
191 error_dialog (this, _("Could not load DCP"), std_to_wx (e.what()));
195 _film->examine_and_add_content (dcp, true);
197 JobManager* jm = JobManager::instance ();
199 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), _("Loading DCP"));
201 while (jm->work_to_do() || signal_manager->ui_idle()) {
206 progress->Destroy ();
208 DCPOMATIC_ASSERT (!jm->get().empty());
210 shared_ptr<Job> last = jm->get().back();
211 if (last->finished_in_error()) {
212 error_dialog(this, std_to_wx(last->error_summary()) + ".\n", std_to_wx(last->error_details()));
217 dcp->subtitle->set_use (true);
220 Ratio const * r = Ratio::nearest_from_ratio(dcp->video->size().ratio());
222 _film->set_container(r);
225 _viewer->set_film (_film);
226 _viewer->set_position (DCPTime ());
227 _info->triggered_update ();
229 Config::instance()->add_to_player_history (dir);
231 set_menu_sensitivity ();
233 wxMenuItemList old = _cpl_menu->GetMenuItems();
234 for (wxMenuItemList::iterator i = old.begin(); i != old.end(); ++i) {
235 _cpl_menu->Remove (*i);
238 DCPExaminer ex (dcp);
239 int id = ID_view_cpl;
240 BOOST_FOREACH (shared_ptr<dcp::CPL> i, ex.cpls()) {
241 wxMenuItem* j = _cpl_menu->AppendRadioItem(id, i->id());
242 if (!dcp->cpl() || i->id() == *dcp->cpl()) {
250 void setup_menu (wxMenuBar* m)
252 _file_menu = new wxMenu;
253 _file_menu->Append (ID_file_open, _("&Open...\tCtrl-O"));
254 _file_add_ov = _file_menu->Append (ID_file_add_ov, _("&Add OV..."));
255 _file_add_kdm = _file_menu->Append (ID_file_add_kdm, _("&Add KDM..."));
257 _history_position = _file_menu->GetMenuItems().GetCount();
259 _file_menu->AppendSeparator ();
260 _file_menu->Append (ID_file_close, _("&Close"));
261 _file_menu->AppendSeparator ();
264 _file_menu->Append (wxID_EXIT, _("&Exit"));
266 _file_menu->Append (wxID_EXIT, _("&Quit"));
270 _file_menu->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
272 wxMenu* edit = new wxMenu;
273 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
276 _cpl_menu = new wxMenu;
278 wxMenu* view = new wxMenu;
279 optional<int> c = Config::instance()->decode_reduction();
280 _view_cpl = view->Append(ID_view_cpl, _("CPL"), _cpl_menu);
281 view->AppendSeparator();
282 view->AppendRadioItem(ID_view_scale_appropriate, _("Set decode resolution to match display"))->Check(!static_cast<bool>(c));
283 view->AppendRadioItem(ID_view_scale_full, _("Decode at full resolution"))->Check(c && c.get() == 0);
284 view->AppendRadioItem(ID_view_scale_half, _("Decode at half resolution"))->Check(c && c.get() == 1);
285 view->AppendRadioItem(ID_view_scale_quarter, _("Decode at quarter resolution"))->Check(c && c.get() == 2);
287 wxMenu* tools = new wxMenu;
288 _tools_verify = tools->Append (ID_tools_verify, _("Verify DCP"));
289 tools->AppendSeparator ();
290 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
292 wxMenu* help = new wxMenu;
294 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
296 help->Append (wxID_ABOUT, _("About"));
298 help->Append (ID_help_report_a_problem, _("Report a problem..."));
300 m->Append (_file_menu, _("&File"));
302 m->Append (edit, _("&Edit"));
304 m->Append (view, _("&View"));
305 m->Append (tools, _("&Tools"));
306 m->Append (help, _("&Help"));
311 wxString d = wxStandardPaths::Get().GetDocumentsDir();
312 if (Config::instance()->last_player_load_directory()) {
313 d = std_to_wx (Config::instance()->last_player_load_directory()->string());
316 wxDirDialog* c = new wxDirDialog (this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
321 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
322 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
329 boost::filesystem::path const dcp (wx_to_std (c->GetPath ()));
331 Config::instance()->set_last_player_load_directory (dcp.parent_path());
339 wxDirDialog* c = new wxDirDialog (
341 _("Select DCP to open as OV"),
342 wxStandardPaths::Get().GetDocumentsDir(),
343 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
349 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
350 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
357 DCPOMATIC_ASSERT (_film);
358 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
359 DCPOMATIC_ASSERT (dcp);
360 dcp->add_ov (wx_to_std(c->GetPath()));
361 dcp->examine (shared_ptr<Job>());
362 /* Maybe we just gained some subtitles */
364 dcp->subtitle->set_use (true);
369 _info->triggered_update ();
374 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
376 if (d->ShowModal() == wxID_OK) {
377 DCPOMATIC_ASSERT (_film);
378 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
379 DCPOMATIC_ASSERT (dcp);
381 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
382 dcp->examine (shared_ptr<Job>());
383 } catch (exception& e) {
384 error_dialog (this, wxString::Format (_("Could not load KDM.")), std_to_wx(e.what()));
391 _info->triggered_update ();
394 void file_history (wxCommandEvent& event)
396 vector<boost::filesystem::path> history = Config::instance()->player_history ();
397 int n = event.GetId() - ID_file_history;
398 if (n >= 0 && n < static_cast<int> (history.size ())) {
399 load_dcp (history[n]);
405 _viewer->set_film (shared_ptr<Film>());
407 _info->triggered_update ();
408 set_menu_sensitivity ();
416 void edit_preferences ()
418 if (!_config_dialog) {
419 _config_dialog = create_player_config_dialog ();
421 _config_dialog->Show (this);
424 void view_cpl (wxCommandEvent& ev)
426 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
427 DCPOMATIC_ASSERT (dcp);
428 DCPExaminer ex (dcp);
429 int id = ev.GetId() - ID_view_cpl;
430 DCPOMATIC_ASSERT (id >= 0);
431 DCPOMATIC_ASSERT (id < int(ex.cpls().size()));
432 list<shared_ptr<dcp::CPL> > cpls = ex.cpls();
433 list<shared_ptr<dcp::CPL> >::iterator i = cpls.begin();
439 dcp->set_cpl ((*i)->id());
440 dcp->examine (shared_ptr<Job>());
445 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
446 DCPOMATIC_ASSERT (dcp);
448 JobManager* jm = JobManager::instance ();
449 jm->add (shared_ptr<Job> (new VerifyDCPJob (dcp->directories())));
451 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), _("Verifying DCP"));
453 while (jm->work_to_do() || signal_manager->ui_idle()) {
458 progress->Destroy ();
460 DCPOMATIC_ASSERT (!jm->get().empty());
461 shared_ptr<VerifyDCPJob> last = dynamic_pointer_cast<VerifyDCPJob> (jm->get().back());
462 DCPOMATIC_ASSERT (last);
464 VerifyDCPDialog* d = new VerifyDCPDialog (this, last->notes ());
469 void tools_check_for_updates ()
471 UpdateChecker::instance()->run ();
472 _update_news_requested = true;
477 AboutDialog* d = new AboutDialog (this);
482 void help_report_a_problem ()
484 ReportProblemDialog* d = new ReportProblemDialog (this);
485 if (d->ShowModal () == wxID_OK) {
491 void update_checker_state_changed ()
493 UpdateChecker* uc = UpdateChecker::instance ();
495 bool const announce =
496 _update_news_requested ||
497 (uc->stable() && Config::instance()->check_for_updates()) ||
498 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
500 _update_news_requested = false;
506 if (uc->state() == UpdateChecker::YES) {
507 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
508 dialog->ShowModal ();
510 } else if (uc->state() == UpdateChecker::FAILED) {
511 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
513 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
516 _update_news_requested = false;
519 void config_changed ()
521 /* Instantly save any config changes when using the player GUI */
523 Config::instance()->write_config();
524 } catch (exception& e) {
528 _("Could not write to config file at %s. Your changes have not been saved."),
529 std_to_wx (Config::instance()->cinemas_file().string()).data()
534 for (int i = 0; i < _history_items; ++i) {
535 delete _file_menu->Remove (ID_file_history + i);
538 if (_history_separator) {
539 _file_menu->Remove (_history_separator);
541 delete _history_separator;
542 _history_separator = 0;
544 int pos = _history_position;
546 vector<boost::filesystem::path> history = Config::instance()->player_history ();
548 if (!history.empty ()) {
549 _history_separator = _file_menu->InsertSeparator (pos++);
552 for (size_t i = 0; i < history.size(); ++i) {
555 s = String::compose ("&%1 %2", i + 1, history[i].string());
557 s = history[i].string();
559 _file_menu->Insert (pos++, ID_file_history + i, std_to_wx (s));
562 _history_items = history.size ();
565 void set_menu_sensitivity ()
567 _tools_verify->Enable (static_cast<bool>(_film));
568 _file_add_ov->Enable (static_cast<bool>(_film));
569 _file_add_kdm->Enable (static_cast<bool>(_film));
570 _view_cpl->Enable (static_cast<bool>(_film));
573 void start_stop_pressed ()
575 if (_viewer->playing()) {
584 _viewer->back_frame ();
587 void forward_frame ()
589 _viewer->forward_frame ();
592 bool _update_news_requested;
593 PlayerInformation* _info;
594 wxPreferencesEditor* _config_dialog;
596 wxMenuItem* _view_cpl;
599 int _history_position;
600 wxMenuItem* _history_separator;
602 boost::shared_ptr<Film> _film;
603 boost::signals2::scoped_connection _config_changed_connection;
604 wxMenuItem* _file_add_ov;
605 wxMenuItem* _file_add_kdm;
606 wxMenuItem* _tools_verify;
609 static const wxCmdLineEntryDesc command_line_description[] = {
610 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
611 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
615 * @brief The magic App class for wxWidgets.
617 class App : public wxApp
630 wxInitAllImageHandlers ();
632 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
633 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
635 wxSplashScreen* splash = maybe_show_splash ();
637 SetAppName (_("DCP-o-matic Player"));
639 if (!wxApp::OnInit()) {
643 #ifdef DCPOMATIC_LINUX
644 unsetenv ("UBUNTU_MENUPROXY");
648 ProcessSerialNumber serial;
649 GetCurrentProcess (&serial);
650 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
653 dcpomatic_setup_path_encoding ();
655 /* Enable i18n; this will create a Config object
656 to look for a force-configured language. This Config
657 object will be wrong, however, because dcpomatic_setup
658 hasn't yet been called and there aren't any filters etc.
661 dcpomatic_setup_i18n ();
663 /* Set things up, including filters etc.
664 which will now be internationalised correctly.
668 /* Force the configuration to be re-loaded correctly next
673 _frame = new DOMFrame ();
674 SetTopWindow (_frame);
681 signal_manager = new wxSignalManager (this);
683 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
685 _frame->load_dcp (_dcp_to_load);
686 } catch (exception& e) {
687 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1.")), _dcp_to_load)), std_to_wx(e.what()));
691 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
693 if (Config::instance()->check_for_updates ()) {
694 UpdateChecker::instance()->run ();
701 error_dialog (0, _("DCP-o-matic Player could not start."), std_to_wx(e.what()));
705 void OnInitCmdLine (wxCmdLineParser& parser)
707 parser.SetDesc (command_line_description);
708 parser.SetSwitchChars (wxT ("-"));
711 bool OnCmdLineParsed (wxCmdLineParser& parser)
713 if (parser.GetParamCount() > 0) {
714 _dcp_to_load = wx_to_std (parser.GetParam (0));
720 void report_exception ()
724 } catch (FileError& e) {
728 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
729 std_to_wx (e.what()),
730 std_to_wx (e.file().string().c_str ())
733 } catch (exception& e) {
737 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
738 std_to_wx (e.what ())
742 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
746 /* An unhandled exception has occurred inside the main event loop */
747 bool OnExceptionInMainLoop ()
750 /* This will terminate the program */
754 void OnUnhandledException ()
761 signal_manager->ui_idle ();
764 void config_failed_to_load ()
766 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
769 void config_warning (string m)
771 message_dialog (_frame, std_to_wx (m));