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