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