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