0f77e3bc3f63848412a2b6ca1094099ab9f73df3
[dcpomatic.git] / src / tools / dcpomatic_player.cc
1 /*
2     Copyright (C) 2017-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "lib/cross.h"
22 #include "lib/config.h"
23 #include "lib/util.h"
24 #include "lib/update_checker.h"
25 #include "lib/compose.hpp"
26 #include "lib/dcp_content.h"
27 #include "lib/job_manager.h"
28 #include "lib/job.h"
29 #include "lib/video_content.h"
30 #include "lib/subtitle_content.h"
31 #include "lib/ratio.h"
32 #include "wx/wx_signal_manager.h"
33 #include "wx/wx_util.h"
34 #include "wx/about_dialog.h"
35 #include "wx/report_problem_dialog.h"
36 #include "wx/film_viewer.h"
37 #include "wx/player_information.h"
38 #include "wx/update_dialog.h"
39 #include "wx/player_config_dialog.h"
40 #include <wx/wx.h>
41 #include <wx/stdpaths.h>
42 #include <wx/splash.h>
43 #include <wx/cmdline.h>
44 #include <wx/preferences.h>
45 #include <wx/progdlg.h>
46 #ifdef __WXOSX__
47 #include <ApplicationServices/ApplicationServices.h>
48 #endif
49 #include <boost/bind.hpp>
50 #include <iostream>
51
52 #ifdef check
53 #undef check
54 #endif
55
56 using std::string;
57 using std::cout;
58 using std::exception;
59 using std::vector;
60 using boost::shared_ptr;
61 using boost::optional;
62
63 enum {
64         ID_file_open = 1,
65         ID_file_add_ov,
66         ID_file_add_kdm,
67         ID_file_history,
68         /* Allow spare IDs after _history for the recent files list */
69         ID_file_close = 100,
70         ID_view_scale_appropriate,
71         ID_view_scale_full,
72         ID_view_scale_half,
73         ID_view_scale_quarter,
74         ID_help_report_a_problem,
75         ID_tools_check_for_updates,
76 };
77
78 class DOMFrame : public wxFrame
79 {
80 public:
81         DOMFrame ()
82                 : wxFrame (0, -1, _("DCP-o-matic Player"))
83                 , _update_news_requested (false)
84                 , _info (0)
85                 , _config_dialog (0)
86                 , _file_menu (0)
87                 , _history_items (0)
88                 , _history_position (0)
89                 , _history_separator (0)
90                 , _viewer (0)
91         {
92
93 #if defined(DCPOMATIC_WINDOWS)
94                 maybe_open_console ();
95                 cout << "DCP-o-matic Player is starting." << "\n";
96 #endif
97
98                 wxMenuBar* bar = new wxMenuBar;
99                 setup_menu (bar);
100                 set_menu_sensitivity ();
101                 SetMenuBar (bar);
102
103 #ifdef DCPOMATIC_WINDOWS
104                 SetIcon (wxIcon (std_to_wx ("id")));
105 #endif
106
107                 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
108                 config_changed ();
109
110                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
111                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_ov, this), ID_file_add_ov);
112                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_kdm, this), ID_file_add_kdm);
113                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_history, this, _1), ID_file_history, ID_file_history + HISTORY_SIZE);
114                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_close, this), ID_file_close);
115                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
116                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
117                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>()), ID_view_scale_appropriate);
118                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
119                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
120                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
121                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
122                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
123                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
124
125                 /* Use a panel as the only child of the Frame so that we avoid
126                    the dark-grey background on Windows.
127                 */
128                 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
129
130                 _viewer = new FilmViewer (overall_panel, false, false);
131                 _info = new PlayerInformation (overall_panel, _viewer);
132                 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
133                 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
134                 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
135                 overall_panel->SetSizer (main_sizer);
136
137 #ifdef __WXOSX__
138                 wxAcceleratorEntry* accel = new wxAcceleratorEntry[1];
139                 accel[0].Set(wxACCEL_CTRL, static_cast<int>('W'), ID_file_close);
140                 wxAcceleratorTable accel_table (1, accel);
141                 SetAcceleratorTable (accel_table);
142                 delete[] accel;
143 #endif
144
145                 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
146         }
147
148         void set_decode_reduction (optional<int> reduction)
149         {
150                 _viewer->set_dcp_decode_reduction (reduction);
151                 _info->triggered_update ();
152         }
153
154         void load_dcp (boost::filesystem::path dir)
155         {
156                 _film.reset (new Film (optional<boost::filesystem::path>()));
157                 shared_ptr<DCPContent> dcp;
158                 try {
159                         dcp.reset (new DCPContent (_film, dir));
160                 } catch (boost::filesystem::filesystem_error& e) {
161                         error_dialog (this, _("Could not load DCP"), std_to_wx (e.what()));
162                         return;
163                 }
164
165                 _film->examine_and_add_content (dcp, true);
166
167                 JobManager* jm = JobManager::instance ();
168
169                 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), _("Loading DCP"));
170
171                 while (jm->work_to_do() || signal_manager->ui_idle()) {
172                         dcpomatic_sleep (1);
173                         progress->Pulse ();
174                 }
175
176                 progress->Destroy ();
177
178                 DCPOMATIC_ASSERT (!jm->get().empty());
179
180                 shared_ptr<Job> last = jm->get().back();
181                 if (last->finished_in_error()) {
182                         error_dialog (this, std_to_wx (last->error_summary()) + ".\n");
183                         return;
184                 }
185
186                 if (dcp->subtitle) {
187                         dcp->subtitle->set_use (true);
188                 }
189
190                 Ratio const * r = Ratio::nearest_from_ratio(dcp->video->size().ratio());
191                 if (r) {
192                         _film->set_container(r);
193                 }
194
195                 _viewer->set_film (_film);
196                 _viewer->set_position (DCPTime ());
197                 _info->triggered_update ();
198
199                 Config::instance()->add_to_player_history (dir);
200
201                 set_menu_sensitivity ();
202         }
203
204 private:
205
206         void setup_menu (wxMenuBar* m)
207         {
208                 _file_menu = new wxMenu;
209                 _file_menu->Append (ID_file_open, _("&Open...\tCtrl-O"));
210                 _file_add_ov = _file_menu->Append (ID_file_add_ov, _("&Add OV..."));
211                 _file_add_kdm = _file_menu->Append (ID_file_add_kdm, _("&Add KDM..."));
212
213                 _history_position = _file_menu->GetMenuItems().GetCount();
214
215                 _file_menu->AppendSeparator ();
216                 _file_menu->Append (ID_file_close, _("&Close"));
217                 _file_menu->AppendSeparator ();
218
219 #ifdef __WXOSX__
220                 _file_menu->Append (wxID_EXIT, _("&Exit"));
221 #else
222                 _file_menu->Append (wxID_EXIT, _("&Quit"));
223 #endif
224
225 #ifdef __WXOSX__
226                 _file_menu->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
227 #else
228                 wxMenu* edit = new wxMenu;
229                 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
230 #endif
231
232                 wxMenu* view = new wxMenu;
233                 view->AppendRadioItem (ID_view_scale_appropriate, _("Set decode resolution to match display"));
234                 view->AppendRadioItem (ID_view_scale_full, _("Decode at full resolution"));
235                 view->AppendRadioItem (ID_view_scale_half, _("Decode at half resolution"));
236                 view->AppendRadioItem (ID_view_scale_quarter, _("Decode at quarter resolution"));
237
238                 wxMenu* tools = new wxMenu;
239                 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
240
241                 wxMenu* help = new wxMenu;
242 #ifdef __WXOSX__
243                 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
244 #else
245                 help->Append (wxID_ABOUT, _("About"));
246 #endif
247                 help->Append (ID_help_report_a_problem, _("Report a problem..."));
248
249                 m->Append (_file_menu, _("&File"));
250 #ifndef __WXOSX__
251                 m->Append (edit, _("&Edit"));
252 #endif
253                 m->Append (view, _("&View"));
254                 m->Append (tools, _("&Tools"));
255                 m->Append (help, _("&Help"));
256         }
257
258         void file_open ()
259         {
260                 wxString d = wxStandardPaths::Get().GetDocumentsDir();
261                 if (Config::instance()->last_player_load_directory()) {
262                         d = std_to_wx (Config::instance()->last_player_load_directory()->string());
263                 }
264
265                 wxDirDialog* c = new wxDirDialog (this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
266
267                 int r;
268                 while (true) {
269                         r = c->ShowModal ();
270                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
271                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
272                         } else {
273                                 break;
274                         }
275                 }
276
277                 if (r == wxID_OK) {
278                         boost::filesystem::path const dcp (wx_to_std (c->GetPath ()));
279                         load_dcp (dcp);
280                         Config::instance()->set_last_player_load_directory (dcp.parent_path());
281                 }
282
283                 c->Destroy ();
284         }
285
286         void file_add_ov ()
287         {
288                 wxDirDialog* c = new wxDirDialog (
289                         this,
290                         _("Select DCP to open as OV"),
291                         wxStandardPaths::Get().GetDocumentsDir(),
292                         wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
293                         );
294
295                 int r;
296                 while (true) {
297                         r = c->ShowModal ();
298                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
299                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
300                         } else {
301                                 break;
302                         }
303                 }
304
305                 if (r == wxID_OK) {
306                         DCPOMATIC_ASSERT (_film);
307                         shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
308                         DCPOMATIC_ASSERT (dcp);
309                         dcp->add_ov (wx_to_std(c->GetPath()));
310                         dcp->examine (shared_ptr<Job>());
311                 }
312
313                 c->Destroy ();
314                 _info->triggered_update ();
315         }
316
317         void file_add_kdm ()
318         {
319                 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
320
321                 if (d->ShowModal() == wxID_OK) {
322                         DCPOMATIC_ASSERT (_film);
323                         shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
324                         DCPOMATIC_ASSERT (dcp);
325                         try {
326                                 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
327                                 dcp->examine (shared_ptr<Job>());
328                         } catch (exception& e) {
329                                 error_dialog (this, wxString::Format (_("Could not load KDM.")), std_to_wx(e.what()));
330                                 d->Destroy ();
331                                 return;
332                         }
333                 }
334
335                 d->Destroy ();
336                 _info->triggered_update ();
337         }
338
339         void file_history (wxCommandEvent& event)
340         {
341                 vector<boost::filesystem::path> history = Config::instance()->player_history ();
342                 int n = event.GetId() - ID_file_history;
343                 if (n >= 0 && n < static_cast<int> (history.size ())) {
344                         load_dcp (history[n]);
345                 }
346         }
347
348         void file_close ()
349         {
350                 _viewer->set_film (shared_ptr<Film>());
351                 _film.reset ();
352                 _info->triggered_update ();
353                 set_menu_sensitivity ();
354         }
355
356         void file_exit ()
357         {
358                 Close ();
359         }
360
361         void edit_preferences ()
362         {
363                 if (!_config_dialog) {
364                         _config_dialog = create_player_config_dialog ();
365                 }
366                 _config_dialog->Show (this);
367         }
368
369         void tools_check_for_updates ()
370         {
371                 UpdateChecker::instance()->run ();
372                 _update_news_requested = true;
373         }
374
375         void help_about ()
376         {
377                 AboutDialog* d = new AboutDialog (this);
378                 d->ShowModal ();
379                 d->Destroy ();
380         }
381
382         void help_report_a_problem ()
383         {
384                 ReportProblemDialog* d = new ReportProblemDialog (this);
385                 if (d->ShowModal () == wxID_OK) {
386                         d->report ();
387                 }
388                 d->Destroy ();
389         }
390
391         void update_checker_state_changed ()
392         {
393                 UpdateChecker* uc = UpdateChecker::instance ();
394
395                 bool const announce =
396                         _update_news_requested ||
397                         (uc->stable() && Config::instance()->check_for_updates()) ||
398                         (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
399
400                 _update_news_requested = false;
401
402                 if (!announce) {
403                         return;
404                 }
405
406                 if (uc->state() == UpdateChecker::YES) {
407                         UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
408                         dialog->ShowModal ();
409                         dialog->Destroy ();
410                 } else if (uc->state() == UpdateChecker::FAILED) {
411                         error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
412                 } else {
413                         error_dialog (this, _("There are no new versions of DCP-o-matic available."));
414                 }
415
416                 _update_news_requested = false;
417         }
418
419         void config_changed ()
420         {
421                 /* Instantly save any config changes when using the player GUI */
422                 try {
423                         Config::instance()->write_config();
424                 } catch (exception& e) {
425                         error_dialog (
426                                 this,
427                                 wxString::Format (
428                                         _("Could not write to config file at %s.  Your changes have not been saved."),
429                                         std_to_wx (Config::instance()->cinemas_file().string()).data()
430                                         )
431                                 );
432                 }
433
434                 for (int i = 0; i < _history_items; ++i) {
435                         delete _file_menu->Remove (ID_file_history + i);
436                 }
437
438                 if (_history_separator) {
439                         _file_menu->Remove (_history_separator);
440                 }
441                 delete _history_separator;
442                 _history_separator = 0;
443
444                 int pos = _history_position;
445
446                 vector<boost::filesystem::path> history = Config::instance()->player_history ();
447
448                 if (!history.empty ()) {
449                         _history_separator = _file_menu->InsertSeparator (pos++);
450                 }
451
452                 for (size_t i = 0; i < history.size(); ++i) {
453                         string s;
454                         if (i < 9) {
455                                 s = String::compose ("&%1 %2", i + 1, history[i].string());
456                         } else {
457                                 s = history[i].string();
458                         }
459                         _file_menu->Insert (pos++, ID_file_history + i, std_to_wx (s));
460                 }
461
462                 _history_items = history.size ();
463         }
464
465         void set_menu_sensitivity ()
466         {
467                 _file_add_ov->Enable (static_cast<bool>(_film));
468                 _file_add_kdm->Enable (static_cast<bool>(_film));
469         }
470
471         bool _update_news_requested;
472         PlayerInformation* _info;
473         wxPreferencesEditor* _config_dialog;
474         wxMenu* _file_menu;
475         int _history_items;
476         int _history_position;
477         wxMenuItem* _history_separator;
478         FilmViewer* _viewer;
479         boost::shared_ptr<Film> _film;
480         boost::signals2::scoped_connection _config_changed_connection;
481         wxMenuItem* _file_add_ov;
482         wxMenuItem* _file_add_kdm;
483 };
484
485 static const wxCmdLineEntryDesc command_line_description[] = {
486         { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
487         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
488 };
489
490 /** @class App
491  *  @brief The magic App class for wxWidgets.
492  */
493 class App : public wxApp
494 {
495 public:
496         App ()
497                 : wxApp ()
498                 , _frame (0)
499         {}
500
501 private:
502
503         bool OnInit ()
504         try
505         {
506                 wxInitAllImageHandlers ();
507
508                 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
509                 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
510
511                 wxSplashScreen* splash = maybe_show_splash ();
512
513                 SetAppName (_("DCP-o-matic Player"));
514
515                 if (!wxApp::OnInit()) {
516                         return false;
517                 }
518
519 #ifdef DCPOMATIC_LINUX
520                 unsetenv ("UBUNTU_MENUPROXY");
521 #endif
522
523 #ifdef __WXOSX__
524                 ProcessSerialNumber serial;
525                 GetCurrentProcess (&serial);
526                 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
527 #endif
528
529                 dcpomatic_setup_path_encoding ();
530
531                 /* Enable i18n; this will create a Config object
532                    to look for a force-configured language.  This Config
533                    object will be wrong, however, because dcpomatic_setup
534                    hasn't yet been called and there aren't any filters etc.
535                    set up yet.
536                 */
537                 dcpomatic_setup_i18n ();
538
539                 /* Set things up, including filters etc.
540                    which will now be internationalised correctly.
541                 */
542                 dcpomatic_setup ();
543
544                 /* Force the configuration to be re-loaded correctly next
545                    time it is needed.
546                 */
547                 Config::drop ();
548
549                 _frame = new DOMFrame ();
550                 SetTopWindow (_frame);
551                 _frame->Maximize ();
552                 if (splash) {
553                         splash->Destroy ();
554                 }
555                 _frame->Show ();
556
557                 signal_manager = new wxSignalManager (this);
558
559                 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
560                         try {
561                                 _frame->load_dcp (_dcp_to_load);
562                         } catch (exception& e) {
563                                 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1.")), _dcp_to_load)), std_to_wx(e.what()));
564                         }
565                 }
566
567                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
568
569                 if (Config::instance()->check_for_updates ()) {
570                         UpdateChecker::instance()->run ();
571                 }
572
573                 return true;
574         }
575         catch (exception& e)
576         {
577                 error_dialog (0, _("DCP-o-matic Player could not start."), std_to_wx(e.what()));
578                 return true;
579         }
580
581         void OnInitCmdLine (wxCmdLineParser& parser)
582         {
583                 parser.SetDesc (command_line_description);
584                 parser.SetSwitchChars (wxT ("-"));
585         }
586
587         bool OnCmdLineParsed (wxCmdLineParser& parser)
588         {
589                 if (parser.GetParamCount() > 0) {
590                         _dcp_to_load = wx_to_std (parser.GetParam (0));
591                 }
592
593                 return true;
594         }
595
596         void report_exception ()
597         {
598                 try {
599                         throw;
600                 } catch (FileError& e) {
601                         error_dialog (
602                                 0,
603                                 wxString::Format (
604                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
605                                         std_to_wx (e.what()),
606                                         std_to_wx (e.file().string().c_str ())
607                                         )
608                                 );
609                 } catch (exception& e) {
610                         error_dialog (
611                                 0,
612                                 wxString::Format (
613                                         _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
614                                         std_to_wx (e.what ())
615                                         )
616                                 );
617                 } catch (...) {
618                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
619                 }
620         }
621
622         /* An unhandled exception has occurred inside the main event loop */
623         bool OnExceptionInMainLoop ()
624         {
625                 report_exception ();
626                 /* This will terminate the program */
627                 return false;
628         }
629
630         void OnUnhandledException ()
631         {
632                 report_exception ();
633         }
634
635         void idle ()
636         {
637                 signal_manager->ui_idle ();
638         }
639
640         void config_failed_to_load ()
641         {
642                 message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
643         }
644
645         void config_warning (string m)
646         {
647                 message_dialog (_frame, std_to_wx (m));
648         }
649
650         DOMFrame* _frame;
651         string _dcp_to_load;
652 };
653
654 IMPLEMENT_APP (App)