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