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