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