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