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