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