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