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