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