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