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