Add swaroop variant and use it to remove the screen options from the View menu.
[dcpomatic.git] / src / tools / dcpomatic_player.cc
1 /*
2     Copyright (C) 2017-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "wx/wx_signal_manager.h"
22 #include "wx/wx_util.h"
23 #include "wx/about_dialog.h"
24 #include "wx/report_problem_dialog.h"
25 #include "wx/film_viewer.h"
26 #include "wx/player_information.h"
27 #include "wx/update_dialog.h"
28 #include "wx/player_config_dialog.h"
29 #include "wx/verify_dcp_dialog.h"
30 #include "wx/controls.h"
31 #include "lib/cross.h"
32 #include "lib/config.h"
33 #include "lib/util.h"
34 #include "lib/update_checker.h"
35 #include "lib/compose.hpp"
36 #include "lib/dcp_content.h"
37 #include "lib/job_manager.h"
38 #include "lib/job.h"
39 #include "lib/film.h"
40 #include "lib/video_content.h"
41 #include "lib/text_content.h"
42 #include "lib/ratio.h"
43 #include "lib/verify_dcp_job.h"
44 #include "lib/dcp_examiner.h"
45 #include "lib/examine_content_job.h"
46 #include "lib/server.h"
47 #include "lib/dcpomatic_socket.h"
48 #include <wx/wx.h>
49 #include <wx/stdpaths.h>
50 #include <wx/splash.h>
51 #include <wx/cmdline.h>
52 #include <wx/preferences.h>
53 #include <wx/progdlg.h>
54 #include <wx/display.h>
55 #ifdef __WXOSX__
56 #include <ApplicationServices/ApplicationServices.h>
57 #endif
58 #include <boost/bind.hpp>
59 #include <iostream>
60
61 #ifdef check
62 #undef check
63 #endif
64
65 #define MAX_CPLS 32
66
67 using std::string;
68 using std::cout;
69 using std::list;
70 using std::exception;
71 using std::vector;
72 using boost::shared_ptr;
73 using boost::scoped_array;
74 using boost::optional;
75 using boost::dynamic_pointer_cast;
76 using boost::thread;
77 using boost::bind;
78
79 enum {
80         ID_file_open = 1,
81         ID_file_add_ov,
82         ID_file_add_kdm,
83         ID_file_history,
84         /* Allow spare IDs after _history for the recent files list */
85         ID_file_close = 100,
86         ID_view_cpl,
87         /* Allow spare IDs for CPLs */
88         ID_view_full_screen = 200,
89         ID_view_dual_screen,
90         ID_view_closed_captions,
91         ID_view_scale_appropriate,
92         ID_view_scale_full,
93         ID_view_scale_half,
94         ID_view_scale_quarter,
95         ID_help_report_a_problem,
96         ID_tools_verify,
97         ID_tools_check_for_updates,
98         /* IDs for shortcuts (with no associated menu item) */
99         ID_start_stop,
100         ID_back_frame,
101         ID_forward_frame
102 };
103
104 class DOMFrame : public wxFrame
105 {
106 public:
107         DOMFrame ()
108                 : wxFrame (0, -1, _("DCP-o-matic Player"))
109                 , _dual_screen (0)
110                 , _update_news_requested (false)
111                 , _info (0)
112                 , _mode (Config::instance()->player_mode())
113                 , _config_dialog (0)
114                 , _file_menu (0)
115                 , _history_items (0)
116                 , _history_position (0)
117                 , _history_separator (0)
118                 , _view_full_screen (0)
119                 , _view_dual_screen (0)
120         {
121
122 #if defined(DCPOMATIC_WINDOWS)
123                 maybe_open_console ();
124                 cout << "DCP-o-matic Player is starting." << "\n";
125 #endif
126
127                 wxMenuBar* bar = new wxMenuBar;
128                 setup_menu (bar);
129                 set_menu_sensitivity ();
130                 SetMenuBar (bar);
131
132 #ifdef DCPOMATIC_WINDOWS
133                 SetIcon (wxIcon (std_to_wx ("id")));
134 #endif
135
136                 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
137                 config_changed ();
138
139                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
140                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_ov, this), ID_file_add_ov);
141                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_kdm, this), ID_file_add_kdm);
142                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_history, this, _1), ID_file_history, ID_file_history + HISTORY_SIZE);
143                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_close, this), ID_file_close);
144                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
145                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
146                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::view_full_screen, this), ID_view_full_screen);
147                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::view_dual_screen, this), ID_view_dual_screen);
148                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::view_closed_captions, this), ID_view_closed_captions);
149                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::view_cpl, this, _1), ID_view_cpl, ID_view_cpl + MAX_CPLS);
150                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
151                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
152                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
153                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
154                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
155                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_verify, this), ID_tools_verify);
156                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
157
158                 /* Use a panel as the only child of the Frame so that we avoid
159                    the dark-grey background on Windows.
160                 */
161                 _overall_panel = new wxPanel (this, wxID_ANY);
162
163                 _viewer.reset (new FilmViewer (_overall_panel));
164                 _controls = new Controls (_overall_panel, _viewer, false, false, false);
165                 _viewer->set_dcp_decode_reduction (Config::instance()->decode_reduction ());
166                 _viewer->PlaybackPermitted.connect (bind(&DOMFrame::playback_permitted, this));
167                 _viewer->Started.connect (bind(&DOMFrame::playback_started, this));
168                 _viewer->Stopped.connect (bind(&DOMFrame::playback_stopped, this));
169                 _info = new PlayerInformation (_overall_panel, _viewer);
170                 setup_main_sizer (true);
171 #ifdef __WXOSX__
172                 int accelerators = 4;
173 #else
174                 int accelerators = 3;
175 #endif
176
177                 wxAcceleratorEntry* accel = new wxAcceleratorEntry[accelerators];
178                 accel[0].Set(wxACCEL_NORMAL, WXK_SPACE, ID_start_stop);
179                 accel[1].Set(wxACCEL_NORMAL, WXK_LEFT, ID_back_frame);
180                 accel[2].Set(wxACCEL_NORMAL, WXK_RIGHT, ID_forward_frame);
181 #ifdef __WXOSX__
182                 accel[3].Set(wxACCEL_CTRL, static_cast<int>('W'), ID_file_close);
183 #endif
184                 wxAcceleratorTable accel_table (accelerators, accel);
185                 SetAcceleratorTable (accel_table);
186                 delete[] accel;
187
188                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::start_stop_pressed, this), ID_start_stop);
189                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::back_frame, this), ID_back_frame);
190                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::forward_frame, this), ID_forward_frame);
191
192                 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
193
194                 setup_screen ();
195         }
196
197         void setup_main_sizer (bool with_viewer)
198         {
199                 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
200                 if (with_viewer) {
201                         main_sizer->Add (_viewer->panel(), 1, wxEXPAND | wxALIGN_CENTER_VERTICAL);
202                 }
203                 main_sizer->Add (_controls, 0, wxEXPAND | wxALL, 6);
204                 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
205                 _overall_panel->SetSizer (main_sizer);
206                 _overall_panel->Layout ();
207         }
208
209         bool playback_permitted ()
210         {
211                 if (!_film || !Config::instance()->respect_kdm_validity_periods()) {
212                         return true;
213                 }
214
215                 bool ok = true;
216                 BOOST_FOREACH (shared_ptr<Content> i, _film->content()) {
217                         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent>(i);
218                         if (d && !d->kdm_timing_window_valid()) {
219                                 ok = false;
220                         }
221                 }
222
223                 if (!ok) {
224                         error_dialog (this, _("The KDM does not allow playback of this content at this time."));
225                 }
226
227                 return ok;
228         }
229
230         void playback_started ()
231         {
232                 optional<boost::filesystem::path> log = Config::instance()->player_log_file();
233                 if (!log) {
234                         return;
235                 }
236
237                 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
238                 DCPOMATIC_ASSERT (dcp);
239                 DCPExaminer ex (dcp);
240                 shared_ptr<dcp::CPL> playing_cpl;
241                 BOOST_FOREACH (shared_ptr<dcp::CPL> i, ex.cpls()) {
242                         if (!dcp->cpl() || i->id() == *dcp->cpl()) {
243                                 playing_cpl = i;
244                         }
245                 }
246                 DCPOMATIC_ASSERT (playing_cpl)
247
248                 FILE* f = fopen_boost(*log, "a");
249                 fprintf (
250                         f,
251                         "%s playback-started %s %s\n",
252                         dcp::LocalTime().as_string().c_str(),
253                         dcp->directories().front().string().c_str(),
254                         playing_cpl->annotation_text().c_str()
255                         );
256                 fclose (f);
257         }
258
259         void playback_stopped ()
260         {
261                 optional<boost::filesystem::path> log = Config::instance()->player_log_file();
262                 if (!log) {
263                         return;
264                 }
265
266                 FILE* f = fopen_boost(*log, "a");
267                 fprintf (f, "%s playback-stopped\n", dcp::LocalTime().as_string().c_str());
268                 fclose (f);
269         }
270
271         void set_decode_reduction (optional<int> reduction)
272         {
273                 _viewer->set_dcp_decode_reduction (reduction);
274                 _info->triggered_update ();
275                 Config::instance()->set_decode_reduction (reduction);
276         }
277
278         void load_dcp (boost::filesystem::path dir)
279         {
280                 _film.reset (new Film (optional<boost::filesystem::path>()));
281                 shared_ptr<DCPContent> dcp;
282                 try {
283                         dcp.reset (new DCPContent (_film, dir));
284                 } catch (boost::filesystem::filesystem_error& e) {
285                         error_dialog (this, _("Could not load DCP"), std_to_wx (e.what()));
286                         return;
287                 }
288
289                 _film->examine_and_add_content (dcp, true);
290                 bool const ok = progress (_("Loading DCP"));
291                 if (!ok || !report_errors_from_last_job()) {
292                         return;
293                 }
294
295                 setup_from_dcp (dcp);
296
297                 if (dcp->three_d()) {
298                         _film->set_three_d (true);
299                 }
300
301                 _viewer->set_film (_film);
302                 _viewer->seek (DCPTime(), true);
303                 _info->triggered_update ();
304
305                 Config::instance()->add_to_player_history (dir);
306
307                 set_menu_sensitivity ();
308
309                 wxMenuItemList old = _cpl_menu->GetMenuItems();
310                 for (wxMenuItemList::iterator i = old.begin(); i != old.end(); ++i) {
311                         _cpl_menu->Remove (*i);
312                 }
313
314                 DCPExaminer ex (dcp);
315                 int id = ID_view_cpl;
316                 BOOST_FOREACH (shared_ptr<dcp::CPL> i, ex.cpls()) {
317                         wxMenuItem* j = _cpl_menu->AppendRadioItem(
318                                 id,
319                                 wxString::Format("%s (%s)", std_to_wx(i->annotation_text()).data(), std_to_wx(i->id()).data())
320                                 );
321                         j->Check(!dcp->cpl() || i->id() == *dcp->cpl());
322                         ++id;
323                 }
324         }
325
326 private:
327
328         void setup_menu (wxMenuBar* m)
329         {
330                 _file_menu = new wxMenu;
331                 _file_menu->Append (ID_file_open, _("&Open...\tCtrl-O"));
332                 _file_add_ov = _file_menu->Append (ID_file_add_ov, _("&Add OV..."));
333                 _file_add_kdm = _file_menu->Append (ID_file_add_kdm, _("&Add KDM..."));
334
335                 _history_position = _file_menu->GetMenuItems().GetCount();
336
337                 _file_menu->AppendSeparator ();
338                 _file_menu->Append (ID_file_close, _("&Close"));
339                 _file_menu->AppendSeparator ();
340
341 #ifdef __WXOSX__
342                 _file_menu->Append (wxID_EXIT, _("&Exit"));
343 #else
344                 _file_menu->Append (wxID_EXIT, _("&Quit"));
345 #endif
346
347 #ifdef __WXOSX__
348                 _file_menu->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
349 #else
350                 wxMenu* edit = new wxMenu;
351                 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
352 #endif
353
354                 _cpl_menu = new wxMenu;
355
356                 wxMenu* view = new wxMenu;
357                 optional<int> c = Config::instance()->decode_reduction();
358                 _view_cpl = view->Append(ID_view_cpl, _("CPL"), _cpl_menu);
359                 view->AppendSeparator();
360 #ifndef DCPOMATIC_VARIANT_SWAROOP
361                 _view_full_screen = view->AppendCheckItem(ID_view_full_screen, _("Full screen\tF11"));
362                 _view_dual_screen = view->AppendCheckItem(ID_view_dual_screen, _("Dual screen\tShift+F11"));
363 #endif
364                 setup_menu ();
365                 view->AppendSeparator();
366                 view->Append(ID_view_closed_captions, _("Closed captions..."));
367                 view->AppendSeparator();
368                 view->AppendRadioItem(ID_view_scale_appropriate, _("Set decode resolution to match display"))->Check(!static_cast<bool>(c));
369                 view->AppendRadioItem(ID_view_scale_full, _("Decode at full resolution"))->Check(c && c.get() == 0);
370                 view->AppendRadioItem(ID_view_scale_half, _("Decode at half resolution"))->Check(c && c.get() == 1);
371                 view->AppendRadioItem(ID_view_scale_quarter, _("Decode at quarter resolution"))->Check(c && c.get() == 2);
372
373                 wxMenu* tools = new wxMenu;
374                 _tools_verify = tools->Append (ID_tools_verify, _("Verify DCP"));
375                 tools->AppendSeparator ();
376                 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
377
378                 wxMenu* help = new wxMenu;
379 #ifdef __WXOSX__
380                 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
381 #else
382                 help->Append (wxID_ABOUT, _("About"));
383 #endif
384                 help->Append (ID_help_report_a_problem, _("Report a problem..."));
385
386                 m->Append (_file_menu, _("&File"));
387 #ifndef __WXOSX__
388                 m->Append (edit, _("&Edit"));
389 #endif
390                 m->Append (view, _("&View"));
391                 m->Append (tools, _("&Tools"));
392                 m->Append (help, _("&Help"));
393         }
394
395         void file_open ()
396         {
397                 wxString d = wxStandardPaths::Get().GetDocumentsDir();
398                 if (Config::instance()->last_player_load_directory()) {
399                         d = std_to_wx (Config::instance()->last_player_load_directory()->string());
400                 }
401
402                 wxDirDialog* c = new wxDirDialog (this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
403
404                 int r;
405                 while (true) {
406                         r = c->ShowModal ();
407                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
408                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
409                         } else {
410                                 break;
411                         }
412                 }
413
414                 if (r == wxID_OK) {
415                         boost::filesystem::path const dcp (wx_to_std (c->GetPath ()));
416                         load_dcp (dcp);
417                         Config::instance()->set_last_player_load_directory (dcp.parent_path());
418                 }
419
420                 c->Destroy ();
421         }
422
423         void file_add_ov ()
424         {
425                 wxDirDialog* c = new wxDirDialog (
426                         this,
427                         _("Select DCP to open as OV"),
428                         wxStandardPaths::Get().GetDocumentsDir(),
429                         wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
430                         );
431
432                 int r;
433                 while (true) {
434                         r = c->ShowModal ();
435                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
436                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
437                         } else {
438                                 break;
439                         }
440                 }
441
442                 if (r == wxID_OK) {
443                         DCPOMATIC_ASSERT (_film);
444                         shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
445                         DCPOMATIC_ASSERT (dcp);
446                         dcp->add_ov (wx_to_std(c->GetPath()));
447                         JobManager::instance()->add(shared_ptr<Job>(new ExamineContentJob (_film, dcp)));
448                         bool const ok = progress (_("Loading DCP"));
449                         if (!ok || !report_errors_from_last_job()) {
450                                 return;
451                         }
452                         setup_from_dcp (dcp);
453                 }
454
455                 c->Destroy ();
456                 _info->triggered_update ();
457         }
458
459         void file_add_kdm ()
460         {
461                 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
462
463                 if (d->ShowModal() == wxID_OK) {
464                         DCPOMATIC_ASSERT (_film);
465                         shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
466                         DCPOMATIC_ASSERT (dcp);
467                         try {
468                                 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
469                                 dcp->examine (shared_ptr<Job>());
470                         } catch (exception& e) {
471                                 error_dialog (this, wxString::Format (_("Could not load KDM.")), std_to_wx(e.what()));
472                                 d->Destroy ();
473                                 return;
474                         }
475                 }
476
477                 d->Destroy ();
478                 _info->triggered_update ();
479         }
480
481         void file_history (wxCommandEvent& event)
482         {
483                 vector<boost::filesystem::path> history = Config::instance()->player_history ();
484                 int n = event.GetId() - ID_file_history;
485                 if (n >= 0 && n < static_cast<int> (history.size ())) {
486                         load_dcp (history[n]);
487                 }
488         }
489
490         void file_close ()
491         {
492                 _viewer->set_film (shared_ptr<Film>());
493                 _film.reset ();
494                 _info->triggered_update ();
495                 set_menu_sensitivity ();
496         }
497
498         void file_exit ()
499         {
500                 Close ();
501         }
502
503         void edit_preferences ()
504         {
505                 if (!_config_dialog) {
506                         _config_dialog = create_player_config_dialog ();
507                 }
508                 _config_dialog->Show (this);
509         }
510
511         void view_cpl (wxCommandEvent& ev)
512         {
513                 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
514                 DCPOMATIC_ASSERT (dcp);
515                 DCPExaminer ex (dcp);
516                 int id = ev.GetId() - ID_view_cpl;
517                 DCPOMATIC_ASSERT (id >= 0);
518                 DCPOMATIC_ASSERT (id < int(ex.cpls().size()));
519                 list<shared_ptr<dcp::CPL> > cpls = ex.cpls();
520                 list<shared_ptr<dcp::CPL> >::iterator i = cpls.begin();
521                 while (id > 0) {
522                         ++i;
523                         --id;
524                 }
525
526                 dcp->set_cpl ((*i)->id());
527                 dcp->examine (shared_ptr<Job>());
528         }
529
530         void view_full_screen ()
531         {
532                 if (_mode == Config::PLAYER_MODE_FULL) {
533                         _mode = Config::PLAYER_MODE_WINDOW;
534                 } else {
535                         _mode = Config::PLAYER_MODE_FULL;
536                 }
537                 setup_screen ();
538                 setup_menu ();
539         }
540
541         void view_dual_screen ()
542         {
543                 if (_mode == Config::PLAYER_MODE_DUAL) {
544                         _mode = Config::PLAYER_MODE_WINDOW;
545                 } else {
546                         _mode = Config::PLAYER_MODE_DUAL;
547                 }
548                 setup_screen ();
549                 setup_menu ();
550         }
551
552         void setup_menu ()
553         {
554                 if (_view_full_screen) {
555                         _view_full_screen->Check (_mode == Config::PLAYER_MODE_FULL);
556                 }
557                 if (_view_dual_screen) {
558                         _view_dual_screen->Check (_mode == Config::PLAYER_MODE_DUAL);
559                 }
560         }
561
562         void setup_screen ()
563         {
564                 _controls->Show (_mode != Config::PLAYER_MODE_FULL);
565                 _info->Show (_mode != Config::PLAYER_MODE_FULL);
566                 _overall_panel->SetBackgroundColour (_mode == Config::PLAYER_MODE_FULL ? wxColour(0, 0, 0) : wxNullColour);
567                 ShowFullScreen (_mode == Config::PLAYER_MODE_FULL);
568
569                 if (_mode == Config::PLAYER_MODE_DUAL) {
570                         _dual_screen = new wxFrame (this, wxID_ANY, wxT(""));
571                         _dual_screen->SetBackgroundColour (wxColour(0, 0, 0));
572                         _dual_screen->ShowFullScreen (true);
573                         _viewer->panel()->Reparent (_dual_screen);
574                         _dual_screen->Show ();
575                         if (wxDisplay::GetCount() > 1) {
576                                 switch (Config::instance()->image_display()) {
577                                 case 0:
578                                         _dual_screen->Move (0, 0);
579                                         Move (wxDisplay(0).GetClientArea().GetWidth(), 0);
580                                         break;
581                                 case 1:
582                                         _dual_screen->Move (wxDisplay(0).GetClientArea().GetWidth(), 0);
583                                         // (0, 0) doesn't seem to work for some strange reason
584                                         Move (8, 8);
585                                         break;
586                                 }
587                         }
588                 } else {
589                         if (_dual_screen) {
590                                 _viewer->panel()->Reparent (_overall_panel);
591                                 _dual_screen->Destroy ();
592                                 _dual_screen = 0;
593                         }
594                 }
595
596                 setup_main_sizer (_mode != Config::PLAYER_MODE_DUAL);
597         }
598
599         void view_closed_captions ()
600         {
601                 _viewer->show_closed_captions ();
602         }
603
604         void tools_verify ()
605         {
606                 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
607                 DCPOMATIC_ASSERT (dcp);
608
609                 JobManager* jm = JobManager::instance ();
610                 jm->add (shared_ptr<Job> (new VerifyDCPJob (dcp->directories())));
611                 bool const ok = progress (_("Verifying DCP"));
612                 if (!ok) {
613                         return;
614                 }
615
616                 DCPOMATIC_ASSERT (!jm->get().empty());
617                 shared_ptr<VerifyDCPJob> last = dynamic_pointer_cast<VerifyDCPJob> (jm->get().back());
618                 DCPOMATIC_ASSERT (last);
619
620                 VerifyDCPDialog* d = new VerifyDCPDialog (this, last);
621                 d->ShowModal ();
622                 d->Destroy ();
623         }
624
625         void tools_check_for_updates ()
626         {
627                 UpdateChecker::instance()->run ();
628                 _update_news_requested = true;
629         }
630
631         void help_about ()
632         {
633                 AboutDialog* d = new AboutDialog (this);
634                 d->ShowModal ();
635                 d->Destroy ();
636         }
637
638         void help_report_a_problem ()
639         {
640                 ReportProblemDialog* d = new ReportProblemDialog (this);
641                 if (d->ShowModal () == wxID_OK) {
642                         d->report ();
643                 }
644                 d->Destroy ();
645         }
646
647         void update_checker_state_changed ()
648         {
649                 UpdateChecker* uc = UpdateChecker::instance ();
650
651                 bool const announce =
652                         _update_news_requested ||
653                         (uc->stable() && Config::instance()->check_for_updates()) ||
654                         (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
655
656                 _update_news_requested = false;
657
658                 if (!announce) {
659                         return;
660                 }
661
662                 if (uc->state() == UpdateChecker::YES) {
663                         UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
664                         dialog->ShowModal ();
665                         dialog->Destroy ();
666                 } else if (uc->state() == UpdateChecker::FAILED) {
667                         error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
668                 } else {
669                         error_dialog (this, _("There are no new versions of DCP-o-matic available."));
670                 }
671
672                 _update_news_requested = false;
673         }
674
675         void config_changed ()
676         {
677                 /* Instantly save any config changes when using the player GUI */
678                 try {
679                         Config::instance()->write_config();
680                 } catch (exception& e) {
681                         error_dialog (
682                                 this,
683                                 wxString::Format (
684                                         _("Could not write to config file at %s.  Your changes have not been saved."),
685                                         std_to_wx (Config::instance()->cinemas_file().string()).data()
686                                         )
687                                 );
688                 }
689
690                 for (int i = 0; i < _history_items; ++i) {
691                         delete _file_menu->Remove (ID_file_history + i);
692                 }
693
694                 if (_history_separator) {
695                         _file_menu->Remove (_history_separator);
696                 }
697                 delete _history_separator;
698                 _history_separator = 0;
699
700                 int pos = _history_position;
701
702                 vector<boost::filesystem::path> history = Config::instance()->player_history ();
703
704                 if (!history.empty ()) {
705                         _history_separator = _file_menu->InsertSeparator (pos++);
706                 }
707
708                 for (size_t i = 0; i < history.size(); ++i) {
709                         string s;
710                         if (i < 9) {
711                                 s = String::compose ("&%1 %2", i + 1, history[i].string());
712                         } else {
713                                 s = history[i].string();
714                         }
715                         _file_menu->Insert (pos++, ID_file_history + i, std_to_wx (s));
716                 }
717
718                 _history_items = history.size ();
719         }
720
721         void set_menu_sensitivity ()
722         {
723                 _tools_verify->Enable (static_cast<bool>(_film));
724                 _file_add_ov->Enable (static_cast<bool>(_film));
725                 _file_add_kdm->Enable (static_cast<bool>(_film));
726                 _view_cpl->Enable (static_cast<bool>(_film));
727         }
728
729         void start_stop_pressed ()
730         {
731                 if (_viewer->playing()) {
732                         _viewer->stop();
733                 } else {
734                         _viewer->start();
735                 }
736         }
737
738         void back_frame ()
739         {
740                 _viewer->seek_by (-_viewer->one_video_frame(), true);
741         }
742
743         void forward_frame ()
744         {
745                 _viewer->seek_by (_viewer->one_video_frame(), true);
746         }
747
748 private:
749
750         /** @return false if the task was cancelled */
751         bool progress (wxString task)
752         {
753                 JobManager* jm = JobManager::instance ();
754
755                 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), task, 100, 0, wxPD_CAN_ABORT);
756
757                 bool ok = true;
758
759                 while (jm->work_to_do() || signal_manager->ui_idle()) {
760                         dcpomatic_sleep (1);
761                         if (!progress->Pulse()) {
762                                 /* user pressed cancel */
763                                 BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
764                                         i->cancel();
765                                 }
766                                 ok = false;
767                                 break;
768                         }
769                 }
770
771                 progress->Destroy ();
772                 return ok;
773         }
774
775         bool report_errors_from_last_job ()
776         {
777                 JobManager* jm = JobManager::instance ();
778
779                 DCPOMATIC_ASSERT (!jm->get().empty());
780
781                 shared_ptr<Job> last = jm->get().back();
782                 if (last->finished_in_error()) {
783                         error_dialog(this, std_to_wx(last->error_summary()) + ".\n", std_to_wx(last->error_details()));
784                         return false;
785                 }
786
787                 return true;
788         }
789
790         void setup_from_dcp (shared_ptr<DCPContent> dcp)
791         {
792                 BOOST_FOREACH (shared_ptr<TextContent> i, dcp->text) {
793                         i->set_use (true);
794                 }
795
796                 if (dcp->video) {
797                         Ratio const * r = Ratio::nearest_from_ratio(dcp->video->size().ratio());
798                         if (r) {
799                                 _film->set_container(r);
800                         }
801                 }
802         }
803
804         wxFrame* _dual_screen;
805         bool _update_news_requested;
806         PlayerInformation* _info;
807         Config::PlayerMode _mode;
808         wxPreferencesEditor* _config_dialog;
809         wxPanel* _overall_panel;
810         wxMenu* _file_menu;
811         wxMenuItem* _view_cpl;
812         wxMenu* _cpl_menu;
813         int _history_items;
814         int _history_position;
815         wxMenuItem* _history_separator;
816         shared_ptr<FilmViewer> _viewer;
817         Controls* _controls;
818         boost::shared_ptr<Film> _film;
819         boost::signals2::scoped_connection _config_changed_connection;
820         wxMenuItem* _file_add_ov;
821         wxMenuItem* _file_add_kdm;
822         wxMenuItem* _tools_verify;
823         wxMenuItem* _view_full_screen;
824         wxMenuItem* _view_dual_screen;
825 };
826
827 static const wxCmdLineEntryDesc command_line_description[] = {
828         { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
829         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
830 };
831
832 class PlayServer : public Server
833 {
834 public:
835         explicit PlayServer (DOMFrame* frame)
836                 : Server (PLAYER_PLAY_PORT)
837                 , _frame (frame)
838         {}
839
840         void handle (shared_ptr<Socket> socket)
841         {
842                 try {
843                         int const length = socket->read_uint32 ();
844                         scoped_array<char> buffer (new char[length]);
845                         socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
846                         string s (buffer.get());
847                         signal_manager->when_idle (bind (&DOMFrame::load_dcp, _frame, s));
848                         socket->write (reinterpret_cast<uint8_t const *> ("OK"), 3);
849                 } catch (...) {
850
851                 }
852         }
853
854 private:
855         DOMFrame* _frame;
856 };
857
858 /** @class App
859  *  @brief The magic App class for wxWidgets.
860  */
861 class App : public wxApp
862 {
863 public:
864         App ()
865                 : wxApp ()
866                 , _frame (0)
867         {}
868
869 private:
870
871         bool OnInit ()
872         try
873         {
874                 wxInitAllImageHandlers ();
875
876                 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
877                 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
878
879                 wxSplashScreen* splash = maybe_show_splash ();
880
881                 SetAppName (_("DCP-o-matic Player"));
882
883                 if (!wxApp::OnInit()) {
884                         return false;
885                 }
886
887 #ifdef DCPOMATIC_LINUX
888                 unsetenv ("UBUNTU_MENUPROXY");
889 #endif
890
891 #ifdef __WXOSX__
892                 ProcessSerialNumber serial;
893                 GetCurrentProcess (&serial);
894                 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
895 #endif
896
897                 dcpomatic_setup_path_encoding ();
898
899                 /* Enable i18n; this will create a Config object
900                    to look for a force-configured language.  This Config
901                    object will be wrong, however, because dcpomatic_setup
902                    hasn't yet been called and there aren't any filters etc.
903                    set up yet.
904                 */
905                 dcpomatic_setup_i18n ();
906
907                 /* Set things up, including filters etc.
908                    which will now be internationalised correctly.
909                 */
910                 dcpomatic_setup ();
911
912                 /* Force the configuration to be re-loaded correctly next
913                    time it is needed.
914                 */
915                 Config::drop ();
916
917                 _frame = new DOMFrame ();
918                 SetTopWindow (_frame);
919                 _frame->Maximize ();
920                 if (splash) {
921                         splash->Destroy ();
922                 }
923                 _frame->Show ();
924
925                 signal_manager = new wxSignalManager (this);
926
927                 PlayServer* server = new PlayServer (_frame);
928                 new thread (boost::bind (&PlayServer::run, server));
929
930                 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
931                         try {
932                                 _frame->load_dcp (_dcp_to_load);
933                         } catch (exception& e) {
934                                 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1.")), _dcp_to_load)), std_to_wx(e.what()));
935                         }
936                 }
937
938                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
939
940                 if (Config::instance()->check_for_updates ()) {
941                         UpdateChecker::instance()->run ();
942                 }
943
944                 return true;
945         }
946         catch (exception& e)
947         {
948                 error_dialog (0, _("DCP-o-matic Player could not start."), std_to_wx(e.what()));
949                 return true;
950         }
951
952         void OnInitCmdLine (wxCmdLineParser& parser)
953         {
954                 parser.SetDesc (command_line_description);
955                 parser.SetSwitchChars (wxT ("-"));
956         }
957
958         bool OnCmdLineParsed (wxCmdLineParser& parser)
959         {
960                 if (parser.GetParamCount() > 0) {
961                         _dcp_to_load = wx_to_std (parser.GetParam (0));
962                 }
963
964                 return true;
965         }
966
967         void report_exception ()
968         {
969                 try {
970                         throw;
971                 } catch (FileError& e) {
972                         error_dialog (
973                                 0,
974                                 wxString::Format (
975                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
976                                         std_to_wx (e.what()),
977                                         std_to_wx (e.file().string().c_str ())
978                                         )
979                                 );
980                 } catch (exception& e) {
981                         error_dialog (
982                                 0,
983                                 wxString::Format (
984                                         _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
985                                         std_to_wx (e.what ())
986                                         )
987                                 );
988                 } catch (...) {
989                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
990                 }
991         }
992
993         /* An unhandled exception has occurred inside the main event loop */
994         bool OnExceptionInMainLoop ()
995         {
996                 report_exception ();
997                 /* This will terminate the program */
998                 return false;
999         }
1000
1001         void OnUnhandledException ()
1002         {
1003                 report_exception ();
1004         }
1005
1006         void idle ()
1007         {
1008                 signal_manager->ui_idle ();
1009         }
1010
1011         void config_failed_to_load ()
1012         {
1013                 message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
1014         }
1015
1016         void config_warning (string m)
1017         {
1018                 message_dialog (_frame, std_to_wx (m));
1019         }
1020
1021         DOMFrame* _frame;
1022         string _dcp_to_load;
1023 };
1024
1025 IMPLEMENT_APP (App)