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