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