Fix failure to update player information when changing CPLs.
[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                 _info->triggered_update ();
789         }
790
791         void view_full_screen ()
792         {
793                 if (_mode == Config::PLAYER_MODE_FULL) {
794                         _mode = Config::PLAYER_MODE_WINDOW;
795                 } else {
796                         _mode = Config::PLAYER_MODE_FULL;
797                 }
798                 setup_screen ();
799                 setup_menu ();
800         }
801
802         void view_dual_screen ()
803         {
804                 if (_mode == Config::PLAYER_MODE_DUAL) {
805                         _mode = Config::PLAYER_MODE_WINDOW;
806                 } else {
807                         _mode = Config::PLAYER_MODE_DUAL;
808                 }
809                 setup_screen ();
810                 setup_menu ();
811         }
812
813         void setup_menu ()
814         {
815                 if (_view_full_screen) {
816                         _view_full_screen->Check (_mode == Config::PLAYER_MODE_FULL);
817                 }
818                 if (_view_dual_screen) {
819                         _view_dual_screen->Check (_mode == Config::PLAYER_MODE_DUAL);
820                 }
821         }
822
823         void setup_screen ()
824         {
825                 _controls->Show (_mode != Config::PLAYER_MODE_FULL);
826                 _info->Show (_mode != Config::PLAYER_MODE_FULL);
827                 _overall_panel->SetBackgroundColour (_mode == Config::PLAYER_MODE_FULL ? wxColour(0, 0, 0) : wxNullColour);
828                 ShowFullScreen (_mode == Config::PLAYER_MODE_FULL);
829                 _viewer->set_pad_black (_mode != Config::PLAYER_MODE_WINDOW);
830
831                 if (_mode == Config::PLAYER_MODE_DUAL) {
832                         _dual_screen = new wxFrame (this, wxID_ANY, wxT(""));
833                         _dual_screen->SetBackgroundColour (wxColour(0, 0, 0));
834                         _dual_screen->ShowFullScreen (true);
835                         _viewer->panel()->Reparent (_dual_screen);
836                         _dual_screen->Show ();
837                         if (wxDisplay::GetCount() > 1) {
838                                 switch (Config::instance()->image_display()) {
839                                 case 0:
840                                         _dual_screen->Move (0, 0);
841                                         Move (wxDisplay(0U).GetClientArea().GetWidth(), 0);
842                                         break;
843                                 case 1:
844                                         _dual_screen->Move (wxDisplay(0U).GetClientArea().GetWidth(), 0);
845                                         // (0, 0) doesn't seem to work for some strange reason
846                                         Move (8, 8);
847                                         break;
848                                 }
849                         }
850                 } else {
851                         if (_dual_screen) {
852                                 _viewer->panel()->Reparent (_overall_panel);
853                                 _dual_screen->Destroy ();
854                                 _dual_screen = 0;
855                         }
856                 }
857
858                 setup_main_sizer (_mode);
859         }
860
861         void view_closed_captions ()
862         {
863                 _viewer->show_closed_captions ();
864         }
865
866         void tools_verify ()
867         {
868                 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
869                 DCPOMATIC_ASSERT (dcp);
870
871                 JobManager* jm = JobManager::instance ();
872                 jm->add (shared_ptr<Job> (new VerifyDCPJob (dcp->directories())));
873                 bool const ok = display_progress (_("DCP-o-matic Player"), _("Verifying DCP"));
874                 if (!ok) {
875                         return;
876                 }
877
878                 DCPOMATIC_ASSERT (!jm->get().empty());
879                 shared_ptr<VerifyDCPJob> last = dynamic_pointer_cast<VerifyDCPJob> (jm->get().back());
880                 DCPOMATIC_ASSERT (last);
881
882                 VerifyDCPDialog* d = new VerifyDCPDialog (this, last);
883                 d->ShowModal ();
884                 d->Destroy ();
885         }
886
887         void tools_check_for_updates ()
888         {
889                 UpdateChecker::instance()->run ();
890                 _update_news_requested = true;
891         }
892
893         void tools_timing ()
894         {
895                 TimerDisplay* d = new TimerDisplay (this, _viewer->state_timer(), _viewer->gets());
896                 d->ShowModal ();
897                 d->Destroy ();
898         }
899
900         void tools_system_information ()
901         {
902                 if (!_system_information_dialog) {
903                         _system_information_dialog = new SystemInformationDialog (this, _viewer);
904                 }
905
906                 _system_information_dialog->Show ();
907         }
908
909         void help_about ()
910         {
911                 AboutDialog* d = new AboutDialog (this);
912                 d->ShowModal ();
913                 d->Destroy ();
914         }
915
916         void help_report_a_problem ()
917         {
918                 ReportProblemDialog* d = new ReportProblemDialog (this);
919                 if (d->ShowModal () == wxID_OK) {
920                         d->report ();
921                 }
922                 d->Destroy ();
923         }
924
925         void update_checker_state_changed ()
926         {
927                 UpdateChecker* uc = UpdateChecker::instance ();
928
929                 bool const announce =
930                         _update_news_requested ||
931                         (uc->stable() && Config::instance()->check_for_updates()) ||
932                         (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
933
934                 _update_news_requested = false;
935
936                 if (!announce) {
937                         return;
938                 }
939
940                 if (uc->state() == UpdateChecker::YES) {
941                         UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
942                         dialog->ShowModal ();
943                         dialog->Destroy ();
944                 } else if (uc->state() == UpdateChecker::FAILED) {
945                         error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
946                 } else {
947                         error_dialog (this, _("There are no new versions of DCP-o-matic available."));
948                 }
949
950                 _update_news_requested = false;
951         }
952
953         void config_changed (Config::Property prop)
954         {
955                 /* Instantly save any config changes when using the player GUI */
956                 try {
957                         Config::instance()->write_config();
958                 } catch (FileError& e) {
959                         if (prop != Config::HISTORY) {
960                                 error_dialog (
961                                         this,
962                                         wxString::Format(
963                                                 _("Could not write to config file at %s.  Your changes have not been saved."),
964                                                 std_to_wx(e.file().string())
965                                                 )
966                                         );
967                         }
968                 } catch (exception& e) {
969                         error_dialog (
970                                 this,
971                                 _("Could not write to config file.  Your changes have not been saved.")
972                                 );
973                 }
974
975                 update_from_config (prop);
976         }
977
978         void update_from_config (Config::Property prop)
979         {
980                 for (int i = 0; i < _history_items; ++i) {
981                         delete _file_menu->Remove (ID_file_history + i);
982                 }
983
984                 if (_history_separator) {
985                         _file_menu->Remove (_history_separator);
986                 }
987                 delete _history_separator;
988                 _history_separator = 0;
989
990                 int pos = _history_position;
991
992                 /* Clear out non-existant history items before we re-build the menu */
993                 Config::instance()->clean_player_history ();
994                 vector<boost::filesystem::path> history = Config::instance()->player_history ();
995
996                 if (!history.empty ()) {
997                         _history_separator = _file_menu->InsertSeparator (pos++);
998                 }
999
1000                 for (size_t i = 0; i < history.size(); ++i) {
1001                         string s;
1002                         if (i < 9) {
1003                                 s = String::compose ("&%1 %2", i + 1, history[i].string());
1004                         } else {
1005                                 s = history[i].string();
1006                         }
1007                         _file_menu->Insert (pos++, ID_file_history + i, std_to_wx (s));
1008                 }
1009
1010                 _history_items = history.size ();
1011
1012                 if (prop == Config::PLAYER_DEBUG_LOG) {
1013                         optional<boost::filesystem::path> p = Config::instance()->player_debug_log_file();
1014                         if (p) {
1015                                 dcpomatic_log.reset (new FileLog(*p));
1016                         } else {
1017                                 dcpomatic_log.reset (new NullLog());
1018                         }
1019                         dcpomatic_log->set_types (LogEntry::TYPE_GENERAL | LogEntry::TYPE_WARNING | LogEntry::TYPE_ERROR | LogEntry::TYPE_DEBUG_VIDEO_VIEW);
1020                 }
1021         }
1022
1023         void set_menu_sensitivity ()
1024         {
1025                 _tools_verify->Enable (static_cast<bool>(_film));
1026                 _file_add_ov->Enable (static_cast<bool>(_film));
1027                 _file_add_kdm->Enable (static_cast<bool>(_film));
1028                 _view_cpl->Enable (static_cast<bool>(_film));
1029         }
1030
1031         void start_stop_pressed ()
1032         {
1033                 if (_viewer->playing()) {
1034                         _viewer->stop();
1035                 } else {
1036                         _viewer->start();
1037                 }
1038         }
1039
1040         void go_back_frame ()
1041         {
1042                 _viewer->seek_by (-_viewer->one_video_frame(), true);
1043         }
1044
1045         void go_forward_frame ()
1046         {
1047                 _viewer->seek_by (_viewer->one_video_frame(), true);
1048         }
1049
1050         void go_seconds (int s)
1051         {
1052                 _viewer->seek_by (DCPTime::from_seconds(s), true);
1053         }
1054
1055         void go_to_start ()
1056         {
1057                 _viewer->seek (DCPTime(), true);
1058         }
1059
1060         void go_to_end ()
1061         {
1062                 _viewer->seek (_film->length() - _viewer->one_video_frame(), true);
1063         }
1064
1065         wxFrame* _dual_screen;
1066         bool _update_news_requested;
1067         PlayerInformation* _info;
1068         Config::PlayerMode _mode;
1069         wxPreferencesEditor* _config_dialog;
1070         wxPanel* _overall_panel;
1071         wxMenu* _file_menu;
1072         wxMenuItem* _view_cpl;
1073         wxMenu* _cpl_menu;
1074         int _history_items;
1075         int _history_position;
1076         wxMenuItem* _history_separator;
1077         shared_ptr<FilmViewer> _viewer;
1078         Controls* _controls;
1079         SystemInformationDialog* _system_information_dialog;
1080         boost::shared_ptr<Film> _film;
1081         boost::signals2::scoped_connection _config_changed_connection;
1082         boost::signals2::scoped_connection _examine_job_connection;
1083         wxMenuItem* _file_add_ov;
1084         wxMenuItem* _file_add_kdm;
1085         wxMenuItem* _tools_verify;
1086         wxMenuItem* _view_full_screen;
1087         wxMenuItem* _view_dual_screen;
1088         wxSizer* _main_sizer;
1089         PlayerStressTester _stress;
1090 };
1091
1092 static const wxCmdLineEntryDesc command_line_description[] = {
1093         { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
1094         { wxCMD_LINE_OPTION, "c", "config", "Directory containing config.xml", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
1095         { wxCMD_LINE_OPTION, "s", "stress", "File containing description of stress test", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
1096         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
1097 };
1098
1099 class PlayServer : public Server
1100 {
1101 public:
1102         explicit PlayServer (DOMFrame* frame)
1103                 : Server (PLAYER_PLAY_PORT)
1104                 , _frame (frame)
1105         {}
1106
1107         void handle (shared_ptr<Socket> socket)
1108         {
1109                 try {
1110                         int const length = socket->read_uint32 ();
1111                         scoped_array<char> buffer (new char[length]);
1112                         socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
1113                         string s (buffer.get());
1114                         signal_manager->when_idle (bind (&DOMFrame::load_dcp, _frame, s));
1115                         socket->write (reinterpret_cast<uint8_t const *> ("OK"), 3);
1116                 } catch (...) {
1117
1118                 }
1119         }
1120
1121 private:
1122         DOMFrame* _frame;
1123 };
1124
1125 /** @class App
1126  *  @brief The magic App class for wxWidgets.
1127  */
1128 class App : public wxApp
1129 {
1130 public:
1131         App ()
1132                 : wxApp ()
1133                 , _frame (0)
1134         {
1135 #ifdef DCPOMATIC_LINUX
1136                 XInitThreads ();
1137 #endif
1138         }
1139
1140 private:
1141
1142         bool OnInit ()
1143         {
1144                 wxSplashScreen* splash = 0;
1145                 try {
1146                         wxInitAllImageHandlers ();
1147
1148                         Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
1149                         Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
1150
1151                         splash = maybe_show_splash ();
1152
1153                         SetAppName (_("DCP-o-matic Player"));
1154
1155                         if (!wxApp::OnInit()) {
1156                                 return false;
1157                         }
1158
1159 #ifdef DCPOMATIC_LINUX
1160                         unsetenv ("UBUNTU_MENUPROXY");
1161 #endif
1162
1163 #ifdef DCPOMATIC_OSX
1164                         make_foreground_application ();
1165 #endif
1166
1167                         dcpomatic_setup_path_encoding ();
1168
1169                         /* Enable i18n; this will create a Config object
1170                            to look for a force-configured language.  This Config
1171                            object will be wrong, however, because dcpomatic_setup
1172                            hasn't yet been called and there aren't any filters etc.
1173                            set up yet.
1174                         */
1175                         dcpomatic_setup_i18n ();
1176
1177                         /* Set things up, including filters etc.
1178                            which will now be internationalised correctly.
1179                         */
1180                         dcpomatic_setup ();
1181
1182                         /* Force the configuration to be re-loaded correctly next
1183                            time it is needed.
1184                         */
1185                         Config::drop ();
1186
1187                         signal_manager = new wxSignalManager (this);
1188
1189                         _frame = new DOMFrame ();
1190                         SetTopWindow (_frame);
1191                         _frame->Maximize ();
1192                         if (splash) {
1193                                 splash->Destroy ();
1194                                 splash = 0;
1195                         }
1196                         _frame->Show ();
1197
1198                         PlayServer* server = new PlayServer (_frame);
1199                         new thread (boost::bind (&PlayServer::run, server));
1200
1201                         if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
1202                                 try {
1203                                         _frame->load_dcp (_dcp_to_load);
1204                                 } catch (exception& e) {
1205                                         error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1.")), _dcp_to_load)), std_to_wx(e.what()));
1206                                 }
1207                         }
1208
1209                         if (_stress) {
1210                                 try {
1211                                         _frame->load_stress_script (*_stress);
1212                                 } catch (exception& e) {
1213                                         error_dialog (0, wxString::Format("Could not load stress test file %s", std_to_wx(*_stress)));
1214                                 }
1215                         }
1216
1217                         Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
1218
1219                         if (Config::instance()->check_for_updates ()) {
1220                                 UpdateChecker::instance()->run ();
1221                         }
1222                 }
1223                 catch (exception& e)
1224                 {
1225                         if (splash) {
1226                                 splash->Destroy ();
1227                         }
1228                         error_dialog (0, _("DCP-o-matic Player could not start."), std_to_wx(e.what()));
1229                 }
1230
1231                 return true;
1232         }
1233
1234         void OnInitCmdLine (wxCmdLineParser& parser)
1235         {
1236                 parser.SetDesc (command_line_description);
1237                 parser.SetSwitchChars (wxT ("-"));
1238         }
1239
1240         bool OnCmdLineParsed (wxCmdLineParser& parser)
1241         {
1242                 if (parser.GetParamCount() > 0) {
1243                         _dcp_to_load = wx_to_std (parser.GetParam (0));
1244                 }
1245
1246                 wxString config;
1247                 if (parser.Found("c", &config)) {
1248                         Config::override_path = wx_to_std (config);
1249                 }
1250                 wxString stress;
1251                 if (parser.Found("s", &stress)) {
1252                         _stress = wx_to_std (stress);
1253                 }
1254
1255                 return true;
1256         }
1257
1258         void report_exception ()
1259         {
1260                 try {
1261                         throw;
1262                 } catch (FileError& e) {
1263                         error_dialog (
1264                                 0,
1265                                 wxString::Format (
1266                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
1267                                         std_to_wx (e.what()),
1268                                         std_to_wx (e.file().string().c_str ())
1269                                         )
1270                                 );
1271                 } catch (exception& e) {
1272                         error_dialog (
1273                                 0,
1274                                 wxString::Format (
1275                                         _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
1276                                         std_to_wx (e.what ())
1277                                         )
1278                                 );
1279                 } catch (...) {
1280                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
1281                 }
1282         }
1283
1284         /* An unhandled exception has occurred inside the main event loop */
1285         bool OnExceptionInMainLoop ()
1286         {
1287                 report_exception ();
1288                 /* This will terminate the program */
1289                 return false;
1290         }
1291
1292         void OnUnhandledException ()
1293         {
1294                 report_exception ();
1295         }
1296
1297         void idle ()
1298         {
1299                 signal_manager->ui_idle ();
1300         }
1301
1302         void config_failed_to_load ()
1303         {
1304                 message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
1305         }
1306
1307         void config_warning (string m)
1308         {
1309                 message_dialog (_frame, std_to_wx (m));
1310         }
1311
1312         DOMFrame* _frame;
1313         string _dcp_to_load;
1314         boost::optional<string> _stress;
1315 };
1316
1317 IMPLEMENT_APP (App)