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