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