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