Various playlist editor developments and fixes.
[dcpomatic.git] / src / tools / dcpomatic_player.cc
1 /*
2     Copyright (C) 2017-2019 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 "wx/wx_signal_manager.h"
22 #include "wx/wx_util.h"
23 #include "wx/about_dialog.h"
24 #include "wx/report_problem_dialog.h"
25 #include "wx/film_viewer.h"
26 #include "wx/player_information.h"
27 #include "wx/update_dialog.h"
28 #include "wx/player_config_dialog.h"
29 #include "wx/verify_dcp_dialog.h"
30 #include "wx/standard_controls.h"
31 #ifdef DCPOMATIC_VARIANT_SWAROOP
32 #include "wx/swaroop_controls.h"
33 #endif
34 #include "wx/timer_display.h"
35 #include "wx/system_information_dialog.h"
36 #include "lib/cross.h"
37 #include "lib/config.h"
38 #include "lib/util.h"
39 #include "lib/internet.h"
40 #include "lib/update_checker.h"
41 #include "lib/compose.hpp"
42 #include "lib/dcp_content.h"
43 #include "lib/job_manager.h"
44 #include "lib/job.h"
45 #include "lib/film.h"
46 #include "lib/null_log.h"
47 #include "lib/video_content.h"
48 #include "lib/text_content.h"
49 #include "lib/ratio.h"
50 #include "lib/verify_dcp_job.h"
51 #include "lib/dcp_examiner.h"
52 #include "lib/examine_content_job.h"
53 #include "lib/server.h"
54 #include "lib/dcpomatic_socket.h"
55 #include "lib/scoped_temporary.h"
56 #include "lib/monitor_checker.h"
57 #include "lib/lock_file_checker.h"
58 #include "lib/ffmpeg_content.h"
59 #include "lib/dcpomatic_log.h"
60 #include "lib/file_log.h"
61 #include <dcp/dcp.h>
62 #include <dcp/raw_convert.h>
63 #include <dcp/exceptions.h>
64 #include <wx/wx.h>
65 #include <wx/stdpaths.h>
66 #include <wx/splash.h>
67 #include <wx/cmdline.h>
68 #include <wx/preferences.h>
69 #include <wx/progdlg.h>
70 #include <wx/display.h>
71 #ifdef __WXGTK__
72 #include <X11/Xlib.h>
73 #endif
74 #ifdef __WXOSX__
75 #include <ApplicationServices/ApplicationServices.h>
76 #endif
77 #include <boost/bind.hpp>
78 #include <boost/algorithm/string.hpp>
79 #include <iostream>
80
81 #ifdef check
82 #undef check
83 #endif
84
85 #define MAX_CPLS 32
86
87 using std::string;
88 using std::cout;
89 using std::list;
90 using std::exception;
91 using std::vector;
92 using boost::shared_ptr;
93 using boost::weak_ptr;
94 using boost::scoped_array;
95 using boost::optional;
96 using boost::dynamic_pointer_cast;
97 using boost::thread;
98 using boost::bind;
99 using dcp::raw_convert;
100 using namespace dcpomatic;
101
102 #ifdef DCPOMATIC_PLAYER_STRESS_TEST
103 #define STRESS_TEST_CHECK_INTERVAL 20
104
105 class Command
106 {
107 public:
108         enum Type {
109                 NONE,
110                 OPEN,
111                 PLAY,
112                 WAIT,
113                 STOP,
114                 SEEK,
115         };
116
117         Command(string line)
118                 : type (NONE)
119                 , int_param (0)
120         {
121                 vector<string> bits;
122                 boost::split (bits, line, boost::is_any_of(" "));
123                 if (bits[0] == "O") {
124                         if (bits.size() != 2) {
125                                 return;
126                         }
127                         type = OPEN;
128                         string_param = bits[1];
129                 } else if (bits[0] == "P") {
130                         type = PLAY;
131                 } else if (bits[0] == "W") {
132                         if (bits.size() != 2) {
133                                 return;
134                         }
135                         type = WAIT;
136                         int_param = raw_convert<int>(bits[1]);
137                 } else if (bits[0] == "S") {
138                         type = STOP;
139                 } else if (bits[0] == "K") {
140                         if (bits.size() != 2) {
141                                 return;
142                         }
143                         type = SEEK;
144                         int_param = raw_convert<int>(bits[1]);
145                 }
146         }
147
148         Type type;
149         string string_param;
150         int int_param;
151 };
152 #endif
153
154 enum {
155         ID_file_open = 1,
156         ID_file_add_ov,
157         ID_file_add_kdm,
158         ID_file_history,
159         /* Allow spare IDs after _history for the recent files list */
160         ID_file_close = 100,
161         ID_view_cpl,
162         /* Allow spare IDs for CPLs */
163         ID_view_full_screen = 200,
164         ID_view_dual_screen,
165         ID_view_closed_captions,
166         ID_view_scale_appropriate,
167         ID_view_scale_full,
168         ID_view_scale_half,
169         ID_view_scale_quarter,
170         ID_help_report_a_problem,
171         ID_tools_verify,
172         ID_tools_check_for_updates,
173         ID_tools_timing,
174         ID_tools_system_information,
175         /* IDs for shortcuts (with no associated menu item) */
176         ID_start_stop,
177         ID_back_frame,
178         ID_forward_frame
179 };
180
181 class DOMFrame : public wxFrame
182 {
183 public:
184         DOMFrame ()
185                 : wxFrame (0, -1, _("DCP-o-matic Player"))
186                 , _dual_screen (0)
187                 , _update_news_requested (false)
188                 , _info (0)
189                 , _mode (Config::instance()->player_mode())
190                 , _config_dialog (0)
191                 , _file_menu (0)
192                 , _history_items (0)
193                 , _history_position (0)
194                 , _history_separator (0)
195                 , _system_information_dialog (0)
196                 , _view_full_screen (0)
197                 , _view_dual_screen (0)
198 #ifdef DCPOMATIC_PLAYER_STRESS_TEST
199                 , _timer (this)
200 #endif
201 {
202                 dcpomatic_log.reset (new NullLog());
203
204 #if defined(DCPOMATIC_WINDOWS)
205                 maybe_open_console ();
206                 cout << "DCP-o-matic Player is starting." << "\n";
207 #endif
208
209                 wxMenuBar* bar = new wxMenuBar;
210                 setup_menu (bar);
211                 set_menu_sensitivity ();
212                 SetMenuBar (bar);
213
214 #ifdef DCPOMATIC_WINDOWS
215                 SetIcon (wxIcon (std_to_wx ("id")));
216 #endif
217
218                 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this, _1));
219                 update_from_config (Config::PLAYER_DEBUG_LOG);
220
221                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
222                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_ov, this), ID_file_add_ov);
223                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_kdm, this), ID_file_add_kdm);
224                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_history, this, _1), ID_file_history, ID_file_history + HISTORY_SIZE);
225                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_close, this), ID_file_close);
226                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
227                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
228                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::view_full_screen, this), ID_view_full_screen);
229                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::view_dual_screen, this), ID_view_dual_screen);
230                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::view_closed_captions, this), ID_view_closed_captions);
231                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::view_cpl, this, _1), ID_view_cpl, ID_view_cpl + MAX_CPLS);
232                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
233                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
234                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
235                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
236                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
237                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_verify, this), ID_tools_verify);
238                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
239                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_timing, this), ID_tools_timing);
240                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_system_information, this), ID_tools_system_information);
241
242                 /* Use a panel as the only child of the Frame so that we avoid
243                    the dark-grey background on Windows.
244                 */
245                 _overall_panel = new wxPanel (this, wxID_ANY);
246
247                 _viewer.reset (new FilmViewer (_overall_panel));
248 #ifdef DCPOMATIC_VARIANT_SWAROOP
249                 SwaroopControls* sc = new SwaroopControls (_overall_panel, _viewer);
250                 _controls = sc;
251                 sc->ResetFilm.connect (bind(&DOMFrame::reset_film_weak, this, _1));
252 #else
253                 _controls = new StandardControls (_overall_panel, _viewer, false);
254 #endif
255                 _viewer->set_dcp_decode_reduction (Config::instance()->decode_reduction ());
256                 _viewer->PlaybackPermitted.connect (bind(&DOMFrame::playback_permitted, this));
257                 _viewer->Started.connect (bind(&DOMFrame::playback_started, this, _1));
258                 _viewer->Stopped.connect (bind(&DOMFrame::playback_stopped, this, _1));
259                 _info = new PlayerInformation (_overall_panel, _viewer);
260                 setup_main_sizer (Config::instance()->player_mode());
261 #ifdef __WXOSX__
262                 int accelerators = 4;
263 #else
264                 int accelerators = 3;
265 #endif
266
267                 wxAcceleratorEntry* accel = new wxAcceleratorEntry[accelerators];
268                 accel[0].Set(wxACCEL_NORMAL, WXK_SPACE, ID_start_stop);
269                 accel[1].Set(wxACCEL_NORMAL, WXK_LEFT, ID_back_frame);
270                 accel[2].Set(wxACCEL_NORMAL, WXK_RIGHT, ID_forward_frame);
271 #ifdef __WXOSX__
272                 accel[3].Set(wxACCEL_CTRL, static_cast<int>('W'), ID_file_close);
273 #endif
274                 wxAcceleratorTable accel_table (accelerators, accel);
275                 SetAcceleratorTable (accel_table);
276                 delete[] accel;
277
278                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::start_stop_pressed, this), ID_start_stop);
279                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::back_frame, this), ID_back_frame);
280                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::forward_frame, this), ID_forward_frame);
281
282                 reset_film ();
283
284                 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
285 #ifdef DCPOMATIC_VARIANT_SWAROOP
286                 MonitorChecker::instance()->StateChanged.connect(boost::bind(&DOMFrame::monitor_checker_state_changed, this));
287                 MonitorChecker::instance()->run ();
288                 LockFileChecker::instance()->StateChanged.connect(boost::bind(&DOMFrame::lock_checker_state_changed, this));
289                 LockFileChecker::instance()->run ();
290 #endif
291                 setup_screen ();
292
293 #ifdef DCPOMATIC_VARIANT_SWAROOP
294                 sc->check_restart ();
295 #endif
296         }
297
298 #ifdef DCPOMATIC_PLAYER_STRESS_TEST
299         void stress (boost::filesystem::path script_file)
300         {
301                 Bind (wxEVT_TIMER, boost::bind(&DOMFrame::check_commands, this));
302                 _timer.Start(STRESS_TEST_CHECK_INTERVAL);
303                 vector<string> lines;
304                 string const script = dcp::file_to_string(script_file);
305                 boost::split (lines, script, boost::is_any_of("\n"));
306                 BOOST_FOREACH (string i, lines) {
307                         _commands.push_back (Command(i));
308                 }
309                 _current_command = _commands.begin();
310         }
311
312         void check_commands ()
313         {
314                 if (_current_command == _commands.end()) {
315                         _timer.Stop ();
316                         cout << "ST: finished.\n";
317                         return;
318                 }
319
320                 switch (_current_command->type) {
321                         case Command::OPEN:
322                                 cout << "ST: load " << _current_command->string_param << "\n";
323                                 load_dcp (_current_command->string_param);
324                                 ++_current_command;
325                                 break;
326                         case Command::PLAY:
327                                 cout << "ST: play\n";
328                                 _controls->play ();
329                                 ++_current_command;
330                                 break;
331                         case Command::WAIT:
332                                 if (_wait_remaining) {
333                                         _wait_remaining = *_wait_remaining - STRESS_TEST_CHECK_INTERVAL;
334                                         if (_wait_remaining < 0) {
335                                                 cout << "ST: wait done.\n";
336                                                 _wait_remaining = optional<int>();
337                                                 ++_current_command;
338                                         }
339                                 } else {
340                                         _wait_remaining = _current_command->int_param;
341                                         cout << "ST: waiting for " << *_wait_remaining << ".\n";
342                                 }
343                                 break;
344                         case Command::STOP:
345                                 cout << "ST: stop\n";
346                                 _controls->stop ();
347                                 ++_current_command;
348                                 break;
349                         case Command::NONE:
350                                 ++_current_command;
351                                 break;
352                         case Command::SEEK:
353                                 /* int_param here is a number between 0 and 4095, corresponding to the possible slider positions */
354                                 cout << "ST: seek to " << _current_command->int_param << "\n";
355                                 _controls->seek (_current_command->int_param);
356                                 ++_current_command;
357                                 break;
358                 }
359         }
360 #endif
361
362 #ifdef DCPOMATIC_VARIANT_SWAROOP
363         void monitor_checker_state_changed ()
364         {
365                 if (!MonitorChecker::instance()->ok()) {
366                         _viewer->stop ();
367                         error_dialog (this, _("The required display devices are not connected correctly."));
368                 }
369         }
370
371         void lock_checker_state_changed ()
372         {
373                 if (!LockFileChecker::instance()->ok()) {
374                         _viewer->stop ();
375                         error_dialog (this, _("The lock file is not present."));
376                 }
377         }
378 #endif
379
380         void setup_main_sizer (Config::PlayerMode mode)
381         {
382                 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
383                 if (mode != Config::PLAYER_MODE_DUAL) {
384                         main_sizer->Add (_viewer->panel(), 1, wxEXPAND | wxALIGN_CENTER_VERTICAL);
385                 }
386                 main_sizer->Add (_controls, mode == Config::PLAYER_MODE_DUAL ? 1 : 0, wxEXPAND | wxALL, 6);
387                 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
388                 _overall_panel->SetSizer (main_sizer);
389                 _overall_panel->Layout ();
390         }
391
392         bool playback_permitted ()
393         {
394 #ifdef DCPOMATIC_VARIANT_SWAROOP
395                 if (!MonitorChecker::instance()->ok()) {
396                         error_dialog (this, _("The required display devices are not connected correctly."));
397                         return false;
398                 }
399                 if (!LockFileChecker::instance()->ok()) {
400                         error_dialog (this, _("The lock file is not present."));
401                         return false;
402                 }
403 #endif
404                 if (!_film || !Config::instance()->respect_kdm_validity_periods()) {
405                         return true;
406                 }
407
408                 bool ok = true;
409                 BOOST_FOREACH (shared_ptr<Content> i, _film->content()) {
410                         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent>(i);
411                         if (d && !d->kdm_timing_window_valid()) {
412                                 ok = false;
413                         }
414                 }
415
416 #ifdef DCPOMATIC_VARIANT_SWAROOP
417                 BOOST_FOREACH (shared_ptr<Content> i, _film->content()) {
418                         shared_ptr<FFmpegContent> c = dynamic_pointer_cast<FFmpegContent>(i);
419                         if (c && !c->kdm_timing_window_valid()) {
420                                 ok = false;
421                         }
422                 }
423 #endif
424
425                 if (!ok) {
426                         error_dialog (this, _("The KDM does not allow playback of this content at this time."));
427                 }
428
429                 return ok;
430         }
431
432         void playback_started (DCPTime time)
433         {
434                 /* XXX: this only logs the first piece of content; probably should be each piece? */
435                 if (_film->content().empty()) {
436                         return;
437                 }
438
439                 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent>(_film->content().front());
440                 if (dcp) {
441                         DCPExaminer ex (dcp, true);
442                         shared_ptr<dcp::CPL> playing_cpl;
443                         BOOST_FOREACH (shared_ptr<dcp::CPL> i, ex.cpls()) {
444                                 if (!dcp->cpl() || i->id() == *dcp->cpl()) {
445                                         playing_cpl = i;
446                                 }
447                         }
448                         DCPOMATIC_ASSERT (playing_cpl);
449
450                         _controls->log (
451                                 wxString::Format(
452                                         "playback-started %s %s %s",
453                                         time.timecode(_film->video_frame_rate()).c_str(),
454                                         dcp->directories().front().string().c_str(),
455                                         playing_cpl->annotation_text().c_str()
456                                         )
457                                 );
458                 }
459
460                 shared_ptr<FFmpegContent> ffmpeg = dynamic_pointer_cast<FFmpegContent>(_film->content().front());
461                 if (ffmpeg) {
462                         _controls->log (
463                                 wxString::Format(
464                                         "playback-started %s %s",
465                                         time.timecode(_film->video_frame_rate()).c_str(),
466                                         ffmpeg->path(0).string().c_str()
467                                         )
468                                 );
469                 }
470         }
471
472         void playback_stopped (DCPTime time)
473         {
474 #ifdef DCPOMATIC_VARIANT_SWAROOP
475                 try {
476                         boost::filesystem::remove (Config::path("position"));
477                 } catch (...) {
478                         /* Never mind */
479                 }
480 #endif
481
482                 _controls->log (wxString::Format("playback-stopped %s", time.timecode(_film->video_frame_rate()).c_str()));
483         }
484
485         void set_decode_reduction (optional<int> reduction)
486         {
487                 _viewer->set_dcp_decode_reduction (reduction);
488                 _info->triggered_update ();
489                 Config::instance()->set_decode_reduction (reduction);
490         }
491
492         void load_dcp (boost::filesystem::path dir)
493         {
494                 DCPOMATIC_ASSERT (_film);
495
496                 reset_film ();
497                 try {
498                         shared_ptr<DCPContent> dcp (new DCPContent(dir));
499                         _film->examine_and_add_content (dcp, true);
500                         bool const ok = display_progress (_("DCP-o-matic Player"), _("Loading content"));
501                         if (!ok || !report_errors_from_last_job(this)) {
502                                 return;
503                         }
504 #ifndef DCPOMATIC_VARIANT_SWAROOP
505                         Config::instance()->add_to_player_history (dir);
506 #endif
507                 } catch (dcp::DCPReadError& e) {
508                         error_dialog (this, wxString::Format(_("Could not load a DCP from %s"), std_to_wx(dir.string())), std_to_wx(e.what()));
509                 }
510         }
511
512         void reset_film_weak (weak_ptr<Film> weak_film)
513         {
514                 shared_ptr<Film> film = weak_film.lock ();
515                 if (film) {
516                         reset_film (film);
517                 }
518         }
519
520         void reset_film (shared_ptr<Film> film = shared_ptr<Film>(new Film(optional<boost::filesystem::path>())))
521         {
522                 _film = film;
523                 _film->set_tolerant (true);
524                 _film->set_audio_channels (MAX_DCP_AUDIO_CHANNELS);
525                 _viewer->set_film (_film);
526                 _controls->set_film (_film);
527                 _film->Change.connect (bind(&DOMFrame::film_changed, this, _1, _2));
528                 _info->triggered_update ();
529         }
530
531         void film_changed (ChangeType type, Film::Property property)
532         {
533                 if (type != CHANGE_TYPE_DONE || property != Film::CONTENT) {
534                         return;
535                 }
536
537                 if (_viewer->playing ()) {
538                         _viewer->stop ();
539                 }
540
541                 /* Start off as Flat */
542                 _film->set_container (Ratio::from_id("185"));
543
544                 BOOST_FOREACH (shared_ptr<Content> i, _film->content()) {
545                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent>(i);
546
547                         BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
548                                 j->set_use (true);
549                         }
550
551                         if (i->video) {
552                                 Ratio const * r = Ratio::nearest_from_ratio(i->video->size().ratio());
553                                 if (r->id() == "239") {
554                                         /* Any scope content means we use scope */
555                                         _film->set_container(r);
556                                 }
557                         }
558
559                         /* Any 3D content means we use 3D mode */
560                         if (i->video && i->video->frame_type() != VIDEO_FRAME_TYPE_2D) {
561                                 _film->set_three_d (true);
562                         }
563                 }
564
565                 _viewer->seek (DCPTime(), true);
566                 _info->triggered_update ();
567
568                 set_menu_sensitivity ();
569
570                 wxMenuItemList old = _cpl_menu->GetMenuItems();
571                 for (wxMenuItemList::iterator i = old.begin(); i != old.end(); ++i) {
572                         _cpl_menu->Remove (*i);
573                 }
574
575                 if (_film->content().size() == 1) {
576                         /* Offer a CPL menu */
577                         shared_ptr<DCPContent> first = dynamic_pointer_cast<DCPContent>(_film->content().front());
578                         if (first) {
579                                 DCPExaminer ex (first, true);
580                                 int id = ID_view_cpl;
581                                 BOOST_FOREACH (shared_ptr<dcp::CPL> i, ex.cpls()) {
582                                         wxMenuItem* j = _cpl_menu->AppendRadioItem(
583                                                 id,
584                                                 wxString::Format("%s (%s)", std_to_wx(i->annotation_text()).data(), std_to_wx(i->id()).data())
585                                                 );
586                                         j->Check(!first->cpl() || i->id() == *first->cpl());
587                                         ++id;
588                                 }
589                         }
590                 }
591         }
592
593 private:
594
595         bool report_errors_from_last_job (wxWindow* parent) const
596         {
597                 JobManager* jm = JobManager::instance ();
598
599                 DCPOMATIC_ASSERT (!jm->get().empty());
600
601                 shared_ptr<Job> last = jm->get().back();
602                 if (last->finished_in_error()) {
603                         error_dialog(parent, wxString::Format(_("Could not load DCP.\n\n%s."), std_to_wx(last->error_summary()).data()), std_to_wx(last->error_details()));
604                         return false;
605                 }
606
607                 return true;
608         }
609
610         void setup_menu (wxMenuBar* m)
611         {
612                 _file_menu = new wxMenu;
613                 _file_menu->Append (ID_file_open, _("&Open...\tCtrl-O"));
614                 _file_add_ov = _file_menu->Append (ID_file_add_ov, _("&Add OV..."));
615                 _file_add_kdm = _file_menu->Append (ID_file_add_kdm, _("Add &KDM..."));
616
617                 _history_position = _file_menu->GetMenuItems().GetCount();
618
619                 _file_menu->AppendSeparator ();
620                 _file_menu->Append (ID_file_close, _("&Close"));
621                 _file_menu->AppendSeparator ();
622
623 #ifdef __WXOSX__
624                 _file_menu->Append (wxID_EXIT, _("&Exit"));
625 #else
626                 _file_menu->Append (wxID_EXIT, _("&Quit"));
627 #endif
628
629 #ifdef __WXOSX__
630                 wxMenuItem* prefs = _file_menu->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
631 #else
632                 wxMenu* edit = new wxMenu;
633                 wxMenuItem* prefs = edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
634 #endif
635
636                 prefs->Enable (Config::instance()->have_write_permission());
637
638                 _cpl_menu = new wxMenu;
639
640                 wxMenu* view = new wxMenu;
641                 optional<int> c = Config::instance()->decode_reduction();
642                 _view_cpl = view->Append(ID_view_cpl, _("CPL"), _cpl_menu);
643                 view->AppendSeparator();
644 #ifndef DCPOMATIC_VARIANT_SWAROOP
645                 _view_full_screen = view->AppendCheckItem(ID_view_full_screen, _("Full screen\tF11"));
646                 _view_dual_screen = view->AppendCheckItem(ID_view_dual_screen, _("Dual screen\tShift+F11"));
647 #endif
648                 setup_menu ();
649                 view->AppendSeparator();
650                 view->Append(ID_view_closed_captions, _("Closed captions..."));
651                 view->AppendSeparator();
652                 view->AppendRadioItem(ID_view_scale_appropriate, _("Set decode resolution to match display"))->Check(!static_cast<bool>(c));
653                 view->AppendRadioItem(ID_view_scale_full, _("Decode at full resolution"))->Check(c && c.get() == 0);
654                 view->AppendRadioItem(ID_view_scale_half, _("Decode at half resolution"))->Check(c && c.get() == 1);
655                 view->AppendRadioItem(ID_view_scale_quarter, _("Decode at quarter resolution"))->Check(c && c.get() == 2);
656
657                 wxMenu* tools = new wxMenu;
658                 _tools_verify = tools->Append (ID_tools_verify, _("Verify DCP"));
659                 tools->AppendSeparator ();
660                 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
661                 tools->Append (ID_tools_timing, _("Timing..."));
662                 tools->Append (ID_tools_system_information, _("System information..."));
663
664                 wxMenu* help = new wxMenu;
665 #ifdef __WXOSX__
666                 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
667 #else
668                 help->Append (wxID_ABOUT, _("About"));
669 #endif
670                 help->Append (ID_help_report_a_problem, _("Report a problem..."));
671
672                 m->Append (_file_menu, _("&File"));
673 #ifndef __WXOSX__
674                 m->Append (edit, _("&Edit"));
675 #endif
676                 m->Append (view, _("&View"));
677                 m->Append (tools, _("&Tools"));
678                 m->Append (help, _("&Help"));
679         }
680
681         void file_open ()
682         {
683                 wxString d = wxStandardPaths::Get().GetDocumentsDir();
684                 if (Config::instance()->last_player_load_directory()) {
685                         d = std_to_wx (Config::instance()->last_player_load_directory()->string());
686                 }
687
688                 wxDirDialog* c = new wxDirDialog (this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
689
690                 int r;
691                 while (true) {
692                         r = c->ShowModal ();
693                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
694                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
695                         } else {
696                                 break;
697                         }
698                 }
699
700                 if (r == wxID_OK) {
701                         boost::filesystem::path const dcp (wx_to_std (c->GetPath ()));
702                         load_dcp (dcp);
703                         Config::instance()->set_last_player_load_directory (dcp.parent_path());
704                 }
705
706                 c->Destroy ();
707         }
708
709         void file_add_ov ()
710         {
711                 wxDirDialog* c = new wxDirDialog (
712                         this,
713                         _("Select DCP to open as OV"),
714                         wxStandardPaths::Get().GetDocumentsDir(),
715                         wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
716                         );
717
718                 int r;
719                 while (true) {
720                         r = c->ShowModal ();
721                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
722                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
723                         } else {
724                                 break;
725                         }
726                 }
727
728                 if (r == wxID_OK) {
729                         DCPOMATIC_ASSERT (_film);
730                         shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
731                         DCPOMATIC_ASSERT (dcp);
732                         dcp->add_ov (wx_to_std(c->GetPath()));
733                         JobManager::instance()->add(shared_ptr<Job>(new ExamineContentJob (_film, dcp)));
734                         bool const ok = display_progress (_("DCP-o-matic Player"), _("Loading content"));
735                         if (!ok || !report_errors_from_last_job(this)) {
736                                 return;
737                         }
738                         BOOST_FOREACH (shared_ptr<TextContent> i, dcp->text) {
739                                 i->set_use (true);
740                         }
741                         if (dcp->video) {
742                                 Ratio const * r = Ratio::nearest_from_ratio(dcp->video->size().ratio());
743                                 if (r) {
744                                         _film->set_container(r);
745                                 }
746                         }
747                 }
748
749                 c->Destroy ();
750                 _info->triggered_update ();
751         }
752
753         void file_add_kdm ()
754         {
755                 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
756
757                 if (d->ShowModal() == wxID_OK) {
758                         DCPOMATIC_ASSERT (_film);
759 #ifdef DCPOMATIC_VARIANT_SWAROOP
760                         shared_ptr<FFmpegContent> ffmpeg = boost::dynamic_pointer_cast<FFmpegContent>(_film->content().front());
761                         if (ffmpeg) {
762                                 try {
763                                         ffmpeg->add_kdm (EncryptedECinemaKDM(dcp::file_to_string(wx_to_std(d->GetPath()), MAX_KDM_SIZE)));
764                                 } catch (exception& e) {
765                                         error_dialog (this, wxString::Format(_("Could not load KDM.")), std_to_wx(e.what()));
766                                         d->Destroy();
767                                         return;
768                                 }
769                         }
770 #endif
771                         shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
772 #ifndef DCPOMATIC_VARIANT_SWAROOP
773                         DCPOMATIC_ASSERT (dcp);
774 #endif
775                         try {
776                                 if (dcp) {
777                                         dcp->add_kdm (dcp::EncryptedKDM(dcp::file_to_string(wx_to_std(d->GetPath()), MAX_KDM_SIZE)));
778                                         dcp->examine (_film, shared_ptr<Job>());
779                                 }
780                         } catch (exception& e) {
781                                 error_dialog (this, wxString::Format (_("Could not load KDM.")), std_to_wx(e.what()));
782                                 d->Destroy ();
783                                 return;
784                         }
785                 }
786
787                 d->Destroy ();
788                 _info->triggered_update ();
789         }
790
791         void file_history (wxCommandEvent& event)
792         {
793                 vector<boost::filesystem::path> history = Config::instance()->player_history ();
794                 int n = event.GetId() - ID_file_history;
795                 if (n >= 0 && n < static_cast<int> (history.size ())) {
796                         try {
797                                 load_dcp (history[n]);
798                         } catch (exception& e) {
799                                 error_dialog (0, std_to_wx(String::compose(wx_to_std(_("Could not load DCP %1.")), history[n])), std_to_wx(e.what()));
800                         }
801                 }
802         }
803
804         void file_close ()
805         {
806                 reset_film ();
807                 _info->triggered_update ();
808                 set_menu_sensitivity ();
809         }
810
811         void file_exit ()
812         {
813                 Close ();
814         }
815
816         void edit_preferences ()
817         {
818                 if (!Config::instance()->have_write_permission()) {
819                         return;
820                 }
821
822                 if (!_config_dialog) {
823                         _config_dialog = create_player_config_dialog ();
824                 }
825                 _config_dialog->Show (this);
826         }
827
828         void view_cpl (wxCommandEvent& ev)
829         {
830                 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
831                 DCPOMATIC_ASSERT (dcp);
832                 DCPExaminer ex (dcp, true);
833                 int id = ev.GetId() - ID_view_cpl;
834                 DCPOMATIC_ASSERT (id >= 0);
835                 DCPOMATIC_ASSERT (id < int(ex.cpls().size()));
836                 list<shared_ptr<dcp::CPL> > cpls = ex.cpls();
837                 list<shared_ptr<dcp::CPL> >::iterator i = cpls.begin();
838                 while (id > 0) {
839                         ++i;
840                         --id;
841                 }
842
843                 dcp->set_cpl ((*i)->id());
844                 dcp->examine (_film, shared_ptr<Job>());
845         }
846
847         void view_full_screen ()
848         {
849                 if (_mode == Config::PLAYER_MODE_FULL) {
850                         _mode = Config::PLAYER_MODE_WINDOW;
851                 } else {
852                         _mode = Config::PLAYER_MODE_FULL;
853                 }
854                 setup_screen ();
855                 setup_menu ();
856         }
857
858         void view_dual_screen ()
859         {
860                 if (_mode == Config::PLAYER_MODE_DUAL) {
861                         _mode = Config::PLAYER_MODE_WINDOW;
862                 } else {
863                         _mode = Config::PLAYER_MODE_DUAL;
864                 }
865                 setup_screen ();
866                 setup_menu ();
867         }
868
869         void setup_menu ()
870         {
871                 if (_view_full_screen) {
872                         _view_full_screen->Check (_mode == Config::PLAYER_MODE_FULL);
873                 }
874                 if (_view_dual_screen) {
875                         _view_dual_screen->Check (_mode == Config::PLAYER_MODE_DUAL);
876                 }
877         }
878
879         void setup_screen ()
880         {
881                 _controls->Show (_mode != Config::PLAYER_MODE_FULL);
882                 _info->Show (_mode != Config::PLAYER_MODE_FULL);
883                 _overall_panel->SetBackgroundColour (_mode == Config::PLAYER_MODE_FULL ? wxColour(0, 0, 0) : wxNullColour);
884                 ShowFullScreen (_mode == Config::PLAYER_MODE_FULL);
885                 _viewer->set_pad_black (_mode != Config::PLAYER_MODE_WINDOW);
886
887                 if (_mode == Config::PLAYER_MODE_DUAL) {
888                         _dual_screen = new wxFrame (this, wxID_ANY, wxT(""));
889                         _dual_screen->SetBackgroundColour (wxColour(0, 0, 0));
890                         _dual_screen->ShowFullScreen (true);
891                         _viewer->panel()->Reparent (_dual_screen);
892                         _dual_screen->Show ();
893                         if (wxDisplay::GetCount() > 1) {
894                                 switch (Config::instance()->image_display()) {
895                                 case 0:
896                                         _dual_screen->Move (0, 0);
897                                         Move (wxDisplay(0).GetClientArea().GetWidth(), 0);
898                                         break;
899                                 case 1:
900                                         _dual_screen->Move (wxDisplay(0).GetClientArea().GetWidth(), 0);
901                                         // (0, 0) doesn't seem to work for some strange reason
902                                         Move (8, 8);
903                                         break;
904                                 }
905                         }
906                 } else {
907                         if (_dual_screen) {
908                                 _viewer->panel()->Reparent (_overall_panel);
909                                 _dual_screen->Destroy ();
910                                 _dual_screen = 0;
911                         }
912                 }
913
914                 setup_main_sizer (_mode);
915         }
916
917         void view_closed_captions ()
918         {
919                 _viewer->show_closed_captions ();
920         }
921
922         void tools_verify ()
923         {
924                 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
925                 DCPOMATIC_ASSERT (dcp);
926
927                 JobManager* jm = JobManager::instance ();
928                 jm->add (shared_ptr<Job> (new VerifyDCPJob (dcp->directories())));
929                 bool const ok = display_progress (_("DCP-o-matic Player"), _("Verifying DCP"));
930                 if (!ok) {
931                         return;
932                 }
933
934                 DCPOMATIC_ASSERT (!jm->get().empty());
935                 shared_ptr<VerifyDCPJob> last = dynamic_pointer_cast<VerifyDCPJob> (jm->get().back());
936                 DCPOMATIC_ASSERT (last);
937
938                 VerifyDCPDialog* d = new VerifyDCPDialog (this, last);
939                 d->ShowModal ();
940                 d->Destroy ();
941         }
942
943         void tools_check_for_updates ()
944         {
945                 UpdateChecker::instance()->run ();
946                 _update_news_requested = true;
947         }
948
949         void tools_timing ()
950         {
951                 TimerDisplay* d = new TimerDisplay (this, _viewer->state_timer(), _viewer->gets());
952                 d->ShowModal ();
953                 d->Destroy ();
954         }
955
956         void tools_system_information ()
957         {
958                 if (!_system_information_dialog) {
959                         _system_information_dialog = new SystemInformationDialog (this, _viewer);
960                 }
961
962                 _system_information_dialog->Show ();
963         }
964
965         void help_about ()
966         {
967                 AboutDialog* d = new AboutDialog (this);
968                 d->ShowModal ();
969                 d->Destroy ();
970         }
971
972         void help_report_a_problem ()
973         {
974                 ReportProblemDialog* d = new ReportProblemDialog (this);
975                 if (d->ShowModal () == wxID_OK) {
976                         d->report ();
977                 }
978                 d->Destroy ();
979         }
980
981         void update_checker_state_changed ()
982         {
983                 UpdateChecker* uc = UpdateChecker::instance ();
984
985                 bool const announce =
986                         _update_news_requested ||
987                         (uc->stable() && Config::instance()->check_for_updates()) ||
988                         (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
989
990                 _update_news_requested = false;
991
992                 if (!announce) {
993                         return;
994                 }
995
996                 if (uc->state() == UpdateChecker::YES) {
997                         UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
998                         dialog->ShowModal ();
999                         dialog->Destroy ();
1000                 } else if (uc->state() == UpdateChecker::FAILED) {
1001                         error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
1002                 } else {
1003                         error_dialog (this, _("There are no new versions of DCP-o-matic available."));
1004                 }
1005
1006                 _update_news_requested = false;
1007         }
1008
1009         void config_changed (Config::Property prop)
1010         {
1011                 /* Instantly save any config changes when using the player GUI */
1012                 try {
1013                         Config::instance()->write_config();
1014                 } catch (FileError& e) {
1015                         if (prop != Config::HISTORY) {
1016                                 error_dialog (
1017                                         this,
1018                                         wxString::Format(
1019                                                 _("Could not write to config file at %s.  Your changes have not been saved."),
1020                                                 std_to_wx(e.file().string())
1021                                                 )
1022                                         );
1023                         }
1024                 } catch (exception& e) {
1025                         error_dialog (
1026                                 this,
1027                                 _("Could not write to config file.  Your changes have not been saved.")
1028                                 );
1029                 }
1030
1031                 update_from_config (prop);
1032         }
1033
1034         void update_from_config (Config::Property prop)
1035         {
1036                 for (int i = 0; i < _history_items; ++i) {
1037                         delete _file_menu->Remove (ID_file_history + i);
1038                 }
1039
1040                 if (_history_separator) {
1041                         _file_menu->Remove (_history_separator);
1042                 }
1043                 delete _history_separator;
1044                 _history_separator = 0;
1045
1046                 int pos = _history_position;
1047
1048                 /* Clear out non-existant history items before we re-build the menu */
1049                 Config::instance()->clean_player_history ();
1050                 vector<boost::filesystem::path> history = Config::instance()->player_history ();
1051
1052                 if (!history.empty ()) {
1053                         _history_separator = _file_menu->InsertSeparator (pos++);
1054                 }
1055
1056                 for (size_t i = 0; i < history.size(); ++i) {
1057                         string s;
1058                         if (i < 9) {
1059                                 s = String::compose ("&%1 %2", i + 1, history[i].string());
1060                         } else {
1061                                 s = history[i].string();
1062                         }
1063                         _file_menu->Insert (pos++, ID_file_history + i, std_to_wx (s));
1064                 }
1065
1066                 _history_items = history.size ();
1067
1068                 if (prop == Config::PLAYER_DEBUG_LOG) {
1069                         optional<boost::filesystem::path> p = Config::instance()->player_debug_log_file();
1070                         if (p) {
1071                                 dcpomatic_log.reset (new FileLog(*p));
1072                         } else {
1073                                 dcpomatic_log.reset (new NullLog());
1074                         }
1075                         dcpomatic_log->set_types (LogEntry::TYPE_GENERAL | LogEntry::TYPE_WARNING | LogEntry::TYPE_ERROR | LogEntry::TYPE_DEBUG_PLAYER);
1076                 }
1077         }
1078
1079         void set_menu_sensitivity ()
1080         {
1081                 _tools_verify->Enable (static_cast<bool>(_film));
1082                 _file_add_ov->Enable (static_cast<bool>(_film));
1083                 _file_add_kdm->Enable (static_cast<bool>(_film));
1084                 _view_cpl->Enable (static_cast<bool>(_film));
1085         }
1086
1087         void start_stop_pressed ()
1088         {
1089                 if (_viewer->playing()) {
1090                         _viewer->stop();
1091                 } else {
1092                         _viewer->start();
1093                 }
1094         }
1095
1096         void back_frame ()
1097         {
1098                 _viewer->seek_by (-_viewer->one_video_frame(), true);
1099         }
1100
1101         void forward_frame ()
1102         {
1103                 _viewer->seek_by (_viewer->one_video_frame(), true);
1104         }
1105
1106 private:
1107
1108         wxFrame* _dual_screen;
1109         bool _update_news_requested;
1110         PlayerInformation* _info;
1111         Config::PlayerMode _mode;
1112         wxPreferencesEditor* _config_dialog;
1113         wxPanel* _overall_panel;
1114         wxMenu* _file_menu;
1115         wxMenuItem* _view_cpl;
1116         wxMenu* _cpl_menu;
1117         int _history_items;
1118         int _history_position;
1119         wxMenuItem* _history_separator;
1120         shared_ptr<FilmViewer> _viewer;
1121         Controls* _controls;
1122         SystemInformationDialog* _system_information_dialog;
1123         boost::shared_ptr<Film> _film;
1124         boost::signals2::scoped_connection _config_changed_connection;
1125         wxMenuItem* _file_add_ov;
1126         wxMenuItem* _file_add_kdm;
1127         wxMenuItem* _tools_verify;
1128         wxMenuItem* _view_full_screen;
1129         wxMenuItem* _view_dual_screen;
1130 #ifdef DCPOMATIC_PLAYER_STRESS_TEST
1131         wxTimer _timer;
1132         list<Command> _commands;
1133         list<Command>::const_iterator _current_command;
1134         optional<int> _wait_remaining;
1135 #endif
1136 };
1137
1138 static const wxCmdLineEntryDesc command_line_description[] = {
1139         { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
1140         { wxCMD_LINE_OPTION, "c", "config", "Directory containing config.xml", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
1141 #ifdef DCPOMATIC_PLAYER_STRESS_TEST
1142         { wxCMD_LINE_OPTION, "s", "stress", "File containing description of stress test", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
1143 #endif
1144         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
1145 };
1146
1147 class PlayServer : public Server
1148 {
1149 public:
1150         explicit PlayServer (DOMFrame* frame)
1151                 : Server (PLAYER_PLAY_PORT)
1152                 , _frame (frame)
1153         {}
1154
1155         void handle (shared_ptr<Socket> socket)
1156         {
1157                 try {
1158                         int const length = socket->read_uint32 ();
1159                         scoped_array<char> buffer (new char[length]);
1160                         socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
1161                         string s (buffer.get());
1162                         signal_manager->when_idle (bind (&DOMFrame::load_dcp, _frame, s));
1163                         socket->write (reinterpret_cast<uint8_t const *> ("OK"), 3);
1164                 } catch (...) {
1165
1166                 }
1167         }
1168
1169 private:
1170         DOMFrame* _frame;
1171 };
1172
1173 /** @class App
1174  *  @brief The magic App class for wxWidgets.
1175  */
1176 class App : public wxApp
1177 {
1178 public:
1179         App ()
1180                 : wxApp ()
1181                 , _frame (0)
1182         {
1183 #ifdef DCPOMATIC_LINUX
1184                 XInitThreads ();
1185 #endif
1186         }
1187
1188 private:
1189
1190         bool OnInit ()
1191         {
1192                 wxSplashScreen* splash = 0;
1193                 try {
1194                         wxInitAllImageHandlers ();
1195
1196                         Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
1197                         Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
1198
1199                         splash = maybe_show_splash ();
1200
1201                         SetAppName (_("DCP-o-matic Player"));
1202
1203                         if (!wxApp::OnInit()) {
1204                                 return false;
1205                         }
1206
1207 #ifdef DCPOMATIC_LINUX
1208                         unsetenv ("UBUNTU_MENUPROXY");
1209 #endif
1210
1211 #ifdef __WXOSX__
1212                         ProcessSerialNumber serial;
1213                         GetCurrentProcess (&serial);
1214                         TransformProcessType (&serial, kProcessTransformToForegroundApplication);
1215 #endif
1216
1217                         dcpomatic_setup_path_encoding ();
1218
1219                         /* Enable i18n; this will create a Config object
1220                            to look for a force-configured language.  This Config
1221                            object will be wrong, however, because dcpomatic_setup
1222                            hasn't yet been called and there aren't any filters etc.
1223                            set up yet.
1224                         */
1225                         dcpomatic_setup_i18n ();
1226
1227                         /* Set things up, including filters etc.
1228                            which will now be internationalised correctly.
1229                         */
1230                         dcpomatic_setup ();
1231
1232                         /* Force the configuration to be re-loaded correctly next
1233                            time it is needed.
1234                         */
1235                         Config::drop ();
1236
1237                         signal_manager = new wxSignalManager (this);
1238
1239                         _frame = new DOMFrame ();
1240                         SetTopWindow (_frame);
1241                         _frame->Maximize ();
1242                         if (splash) {
1243                                 splash->Destroy ();
1244                                 splash = 0;
1245                         }
1246                         _frame->Show ();
1247
1248                         PlayServer* server = new PlayServer (_frame);
1249                         new thread (boost::bind (&PlayServer::run, server));
1250
1251                         if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
1252                                 try {
1253                                         _frame->load_dcp (_dcp_to_load);
1254                                 } catch (exception& e) {
1255                                         error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1.")), _dcp_to_load)), std_to_wx(e.what()));
1256                                 }
1257                         }
1258
1259 #ifdef DCPOMATIC_PLAYER_STRESS_TEST
1260                         if (_stress) {
1261                                 try {
1262                                         _frame->stress (_stress.get());
1263                                 } catch (exception& e) {
1264                                         error_dialog (0, wxString::Format("Could not load stress test file %s", std_to_wx(*_stress)));
1265                                 }
1266                         }
1267 #endif
1268
1269                         Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
1270
1271                         if (Config::instance()->check_for_updates ()) {
1272                                 UpdateChecker::instance()->run ();
1273                         }
1274                 }
1275                 catch (exception& e)
1276                 {
1277                         if (splash) {
1278                                 splash->Destroy ();
1279                         }
1280                         error_dialog (0, _("DCP-o-matic Player could not start."), std_to_wx(e.what()));
1281                 }
1282
1283                 return true;
1284         }
1285
1286         void OnInitCmdLine (wxCmdLineParser& parser)
1287         {
1288                 parser.SetDesc (command_line_description);
1289                 parser.SetSwitchChars (wxT ("-"));
1290         }
1291
1292         bool OnCmdLineParsed (wxCmdLineParser& parser)
1293         {
1294                 if (parser.GetParamCount() > 0) {
1295                         _dcp_to_load = wx_to_std (parser.GetParam (0));
1296                 }
1297
1298                 wxString config;
1299                 if (parser.Found("c", &config)) {
1300                         Config::override_path = wx_to_std (config);
1301                 }
1302 #ifdef DCPOMATIC_PLAYER_STRESS_TEST
1303                 wxString stress;
1304                 if (parser.Found("s", &stress)) {
1305                         _stress = wx_to_std (stress);
1306                 }
1307 #endif
1308
1309                 return true;
1310         }
1311
1312         void report_exception ()
1313         {
1314                 try {
1315                         throw;
1316                 } catch (FileError& e) {
1317                         error_dialog (
1318                                 0,
1319                                 wxString::Format (
1320                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
1321                                         std_to_wx (e.what()),
1322                                         std_to_wx (e.file().string().c_str ())
1323                                         )
1324                                 );
1325                 } catch (exception& e) {
1326                         error_dialog (
1327                                 0,
1328                                 wxString::Format (
1329                                         _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
1330                                         std_to_wx (e.what ())
1331                                         )
1332                                 );
1333                 } catch (...) {
1334                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
1335                 }
1336         }
1337
1338         /* An unhandled exception has occurred inside the main event loop */
1339         bool OnExceptionInMainLoop ()
1340         {
1341                 report_exception ();
1342                 /* This will terminate the program */
1343                 return false;
1344         }
1345
1346         void OnUnhandledException ()
1347         {
1348                 report_exception ();
1349         }
1350
1351         void idle ()
1352         {
1353                 signal_manager->ui_idle ();
1354         }
1355
1356         void config_failed_to_load ()
1357         {
1358                 message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
1359         }
1360
1361         void config_warning (string m)
1362         {
1363                 message_dialog (_frame, std_to_wx (m));
1364         }
1365
1366         DOMFrame* _frame;
1367         string _dcp_to_load;
1368 #ifdef DCPOMATIC_PLAYER_STRESS_TEST
1369         boost::optional<string> _stress;
1370 #endif
1371 };
1372
1373 IMPLEMENT_APP (App)