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