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