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