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