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