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