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