swaroop: Disable play/stop/pause and slider during ad content.
[dcpomatic.git] / src / wx / controls.cc
1 /*
2     Copyright (C) 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 "controls.h"
22 #include "film_viewer.h"
23 #include "wx_util.h"
24 #include "playhead_to_timecode_dialog.h"
25 #include "playhead_to_frame_dialog.h"
26 #include "lib/job_manager.h"
27 #include "lib/player_video.h"
28 #include "lib/dcp_content.h"
29 #include <dcp/dcp.h>
30 #include <dcp/cpl.h>
31 #include <dcp/reel.h>
32 #include <dcp/reel_picture_asset.h>
33 #include <wx/wx.h>
34 #include <wx/tglbtn.h>
35 #include <wx/listctrl.h>
36
37 using std::string;
38 using std::list;
39 using std::make_pair;
40 using boost::optional;
41 using boost::shared_ptr;
42 using boost::weak_ptr;
43 using boost::dynamic_pointer_cast;
44
45 Controls::Controls (wxWindow* parent, shared_ptr<FilmViewer> viewer, bool editor_controls)
46         : wxPanel (parent)
47         , _viewer (viewer)
48         , _slider_being_moved (false)
49         , _was_running_before_slider (false)
50         , _outline_content (0)
51         , _eye (0)
52         , _jump_to_selected (0)
53         , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
54         , _rewind_button (new wxButton (this, wxID_ANY, wxT("|<")))
55         , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
56         , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
57         , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
58         , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
59 #ifdef DCPOMATIC_VARIANT_SWAROOP
60         , _play_button (new wxButton(this, wxID_ANY, _("Play")))
61         , _pause_button (new wxButton(this, wxID_ANY, _("Pause")))
62         , _stop_button (new wxButton(this, wxID_ANY, _("Stop")))
63 #else
64         , _play_button (new wxToggleButton(this, wxID_ANY, _("Play")))
65 #endif
66 {
67         _v_sizer = new wxBoxSizer (wxVERTICAL);
68         SetSizer (_v_sizer);
69
70         wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
71         if (editor_controls) {
72                 _outline_content = new wxCheckBox (this, wxID_ANY, _("Outline content"));
73                 view_options->Add (_outline_content, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
74                 _eye = new wxChoice (this, wxID_ANY);
75                 _eye->Append (_("Left"));
76                 _eye->Append (_("Right"));
77                 _eye->SetSelection (0);
78                 view_options->Add (_eye, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
79                 _jump_to_selected = new wxCheckBox (this, wxID_ANY, _("Jump to selected content"));
80                 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
81         }
82
83         _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
84
85         wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
86
87         _cpl = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
88         /* time */
89         _cpl->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
90         /* type */
91         _cpl->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
92         /* annotation text */
93         _cpl->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 580);
94         e_sizer->Add (_cpl, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
95
96         _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
97         _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
98         _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
99         _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 580);
100         e_sizer->Add (_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
101
102         wxBoxSizer* buttons_sizer = new wxBoxSizer (wxVERTICAL);
103         _add_button = new wxButton(this, wxID_ANY, _("Add"));
104         buttons_sizer->Add (_add_button);
105         e_sizer->Add (buttons_sizer, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
106
107         _v_sizer->Add (e_sizer, 1, wxEXPAND);
108
109         _log = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(-1, 200), wxTE_READONLY | wxTE_MULTILINE);
110         _v_sizer->Add (_log, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
111
112         _cpl->Show (false);
113         _spl_view->Show (false);
114         _add_button->Show (false);
115         _log->Show (false);
116
117         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
118
119         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
120         time_sizer->Add (_frame_number, 0, wxEXPAND);
121         time_sizer->Add (_timecode, 0, wxEXPAND);
122
123         h_sizer->Add (_rewind_button, 0, wxALL, 2);
124         h_sizer->Add (_back_button, 0, wxALL, 2);
125         h_sizer->Add (time_sizer, 0, wxEXPAND);
126         h_sizer->Add (_forward_button, 0, wxALL, 2);
127         h_sizer->Add (_play_button, 0, wxEXPAND);
128 #ifdef DCPOMATIC_VARIANT_SWAROOP
129         h_sizer->Add (_pause_button, 0, wxEXPAND);
130         h_sizer->Add (_stop_button, 0, wxEXPAND);
131 #endif
132         h_sizer->Add (_slider, 1, wxEXPAND);
133
134         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
135
136         _frame_number->SetMinSize (wxSize (84, -1));
137         _rewind_button->SetMinSize (wxSize (32, -1));
138         _back_button->SetMinSize (wxSize (32, -1));
139         _forward_button->SetMinSize (wxSize (32, -1));
140
141         if (_eye) {
142                 _eye->Bind (wxEVT_CHOICE, boost::bind (&Controls::eye_changed, this));
143         }
144         if (_outline_content) {
145                 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::outline_content_changed, this));
146         }
147
148         _slider->Bind           (wxEVT_SCROLL_THUMBTRACK,    boost::bind(&Controls::slider_moved,    this, false));
149         _slider->Bind           (wxEVT_SCROLL_PAGEUP,        boost::bind(&Controls::slider_moved,    this, true));
150         _slider->Bind           (wxEVT_SCROLL_PAGEDOWN,      boost::bind(&Controls::slider_moved,    this, true));
151         _slider->Bind           (wxEVT_SCROLL_CHANGED,       boost::bind(&Controls::slider_released, this));
152 #ifdef DCPOMATIC_VARIANT_SWAROOP
153         _play_button->Bind      (wxEVT_BUTTON,               boost::bind(&Controls::play_clicked,    this));
154         _pause_button->Bind     (wxEVT_BUTTON,               boost::bind(&Controls::pause_clicked,   this));
155         _stop_button->Bind      (wxEVT_BUTTON,               boost::bind(&Controls::stop_clicked,    this));
156 #else
157         _play_button->Bind      (wxEVT_TOGGLEBUTTON,         boost::bind(&Controls::play_clicked,    this));
158 #endif
159         _rewind_button->Bind    (wxEVT_LEFT_DOWN,            boost::bind(&Controls::rewind_clicked,  this, _1));
160         _back_button->Bind      (wxEVT_LEFT_DOWN,            boost::bind(&Controls::back_clicked,    this, _1));
161         _forward_button->Bind   (wxEVT_LEFT_DOWN,            boost::bind(&Controls::forward_clicked, this, _1));
162         _frame_number->Bind     (wxEVT_LEFT_DOWN,            boost::bind(&Controls::frame_number_clicked, this));
163         _timecode->Bind         (wxEVT_LEFT_DOWN,            boost::bind(&Controls::timecode_clicked, this));
164         _cpl->Bind              (wxEVT_LIST_ITEM_SELECTED,   boost::bind(&Controls::setup_sensitivity, this));
165         _cpl->Bind              (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&Controls::setup_sensitivity, this));
166         if (_jump_to_selected) {
167                 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::jump_to_selected_clicked, this));
168                 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
169         }
170         _add_button->Bind       (wxEVT_BUTTON,              boost::bind(&Controls::add_clicked, this));
171
172         _viewer->PositionChanged.connect (boost::bind(&Controls::position_changed, this));
173         _viewer->Started.connect (boost::bind(&Controls::started, this));
174         _viewer->Stopped.connect (boost::bind(&Controls::stopped, this));
175         _viewer->FilmChanged.connect (boost::bind(&Controls::film_changed, this));
176         _viewer->ImageChanged.connect (boost::bind(&Controls::image_changed, this, _1));
177
178         film_changed ();
179
180         setup_sensitivity ();
181         update_dcp_directory ();
182
183         JobManager::instance()->ActiveJobsChanged.connect (
184                 bind (&Controls::active_jobs_changed, this, _2)
185                 );
186
187         _config_changed_connection = Config::instance()->Changed.connect (bind(&Controls::config_changed, this, _1));
188 }
189
190 void
191 Controls::add_clicked ()
192 {
193         optional<CPL> sel = selected_cpl ();
194         DCPOMATIC_ASSERT (sel);
195         _spl.push_back (SPLEntry(sel->first, sel->second));
196         add_cpl_to_list (sel->first, _spl_view);
197         SPLChanged (_spl);
198         setup_sensitivity ();
199 }
200
201 void
202 Controls::config_changed (int property)
203 {
204         if (property == Config::PLAYER_DCP_DIRECTORY) {
205                 update_dcp_directory ();
206         }
207 }
208
209 void
210 Controls::started ()
211 {
212 #ifdef DCPOMATIC_VARIANT_SWAROOP
213         _play_button->Enable (false);
214         _pause_button->Enable (true);
215 #else
216         _play_button->SetValue (true);
217 #endif
218         setup_sensitivity ();
219 }
220
221 void
222 Controls::stopped ()
223 {
224 #ifdef DCPOMATIC_VARIANT_SWAROOP
225         _play_button->Enable (true);
226         _pause_button->Enable (false);
227 #else
228         _play_button->SetValue (false);
229 #endif
230         setup_sensitivity ();
231 }
232
233 void
234 Controls::position_changed ()
235 {
236         if (!_slider_being_moved) {
237                 update_position_label ();
238                 update_position_slider ();
239         }
240 }
241
242 void
243 Controls::eye_changed ()
244 {
245         _viewer->set_eyes (_eye->GetSelection() == 0 ? EYES_LEFT : EYES_RIGHT);
246 }
247
248 void
249 Controls::outline_content_changed ()
250 {
251         _viewer->set_outline_content (_outline_content->GetValue());
252 }
253
254 void
255 Controls::film_change (ChangeType type, Film::Property p)
256 {
257         if (type != CHANGE_TYPE_DONE) {
258                 return;
259         }
260
261         if (p == Film::CONTENT || p == Film::THREE_D) {
262                 setup_sensitivity ();
263         }
264 }
265
266 /** @param page true if this was a PAGEUP/PAGEDOWN event for which we won't receive a THUMBRELEASE */
267 void
268 Controls::slider_moved (bool page)
269 {
270         if (!_film) {
271                 return;
272         }
273
274         if (!page && !_slider_being_moved) {
275                 /* This is the first event of a drag; stop playback for the duration of the drag */
276                 _was_running_before_slider = _viewer->stop ();
277                 _slider_being_moved = true;
278         }
279
280         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
281         t = t.round (_film->video_frame_rate());
282         /* Ensure that we hit the end of the film at the end of the slider */
283         if (t >= _film->length ()) {
284                 t = _film->length() - _viewer->one_video_frame();
285         }
286         _viewer->seek (t, false);
287         update_position_label ();
288 }
289
290 void
291 Controls::slider_released ()
292 {
293         if (_was_running_before_slider) {
294                 /* Restart after a drag */
295                 _viewer->start ();
296         }
297         _slider_being_moved = false;
298 }
299
300 void
301 Controls::play_clicked ()
302 {
303 #ifdef DCPOMATIC_VARIANT_SWAROOP
304         _viewer->start ();
305 #else
306         check_play_state ();
307 #endif
308 }
309
310
311 #ifndef DCPOMATIC_VARIANT_SWAROOP
312 void
313 Controls::check_play_state ()
314 {
315         if (!_film || _film->video_frame_rate() == 0) {
316                 return;
317         }
318
319         if (_play_button->GetValue()) {
320                 _viewer->start ();
321         } else {
322                 _viewer->stop ();
323         }
324 }
325 #endif
326
327 void
328 Controls::update_position_slider ()
329 {
330         if (!_film) {
331                 _slider->SetValue (0);
332                 return;
333         }
334
335         DCPTime const len = _film->length ();
336
337         if (len.get ()) {
338                 int const new_slider_position = 4096 * _viewer->position().get() / len.get();
339                 if (new_slider_position != _slider->GetValue()) {
340                         _slider->SetValue (new_slider_position);
341                 }
342         }
343 }
344
345 void
346 Controls::update_position_label ()
347 {
348         if (!_film) {
349                 _frame_number->SetLabel ("0");
350                 _timecode->SetLabel ("0:0:0.0");
351                 return;
352         }
353
354         double const fps = _film->video_frame_rate ();
355         /* Count frame number from 1 ... not sure if this is the best idea */
356         _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_viewer->position().seconds() * fps) + 1));
357         _timecode->SetLabel (time_to_timecode (_viewer->position(), fps));
358 }
359
360 void
361 Controls::active_jobs_changed (optional<string> j)
362 {
363         _active_job = j;
364         setup_sensitivity ();
365 }
366
367 DCPTime
368 Controls::nudge_amount (wxKeyboardState& ev)
369 {
370         DCPTime amount = _viewer->one_video_frame ();
371
372         if (ev.ShiftDown() && !ev.ControlDown()) {
373                 amount = DCPTime::from_seconds (1);
374         } else if (!ev.ShiftDown() && ev.ControlDown()) {
375                 amount = DCPTime::from_seconds (10);
376         } else if (ev.ShiftDown() && ev.ControlDown()) {
377                 amount = DCPTime::from_seconds (60);
378         }
379
380         return amount;
381 }
382
383 void
384 Controls::rewind_clicked (wxMouseEvent& ev)
385 {
386         _viewer->seek (DCPTime(), true);
387         ev.Skip();
388 }
389
390 void
391 Controls::back_frame ()
392 {
393         _viewer->seek_by (-_viewer->one_video_frame(), true);
394 }
395
396 void
397 Controls::forward_frame ()
398 {
399         _viewer->seek_by (_viewer->one_video_frame(), true);
400 }
401
402 void
403 Controls::back_clicked (wxKeyboardState& ev)
404 {
405         _viewer->seek_by (-nudge_amount(ev), true);
406 }
407
408 void
409 Controls::forward_clicked (wxKeyboardState& ev)
410 {
411         _viewer->seek_by (nudge_amount(ev), true);
412 }
413
414 void
415 Controls::setup_sensitivity ()
416 {
417         /* examine content is the only job which stops the viewer working */
418         bool const active_job = _active_job && *_active_job != "examine_content";
419         bool const c = ((_film && !_film->content().empty()) || !_spl.empty()) && !active_job;
420
421         _slider->Enable (c);
422         _rewind_button->Enable (c);
423         _back_button->Enable (c);
424         _forward_button->Enable (c);
425 #ifdef DCPOMATIC_VARIANT_SWAROOP
426         _play_button->Enable (c && !_viewer->playing());
427         _pause_button->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT) && _viewer->playing());
428         _stop_button->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT));
429         _slider->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT));
430 #else
431         _play_button->Enable (c);
432 #endif
433         if (_outline_content) {
434                 _outline_content->Enable (c);
435         }
436         _frame_number->Enable (c);
437         _timecode->Enable (c);
438         if (_jump_to_selected) {
439                 _jump_to_selected->Enable (c);
440         }
441
442         if (_eye) {
443                 _eye->Enable (c && _film->three_d ());
444         }
445
446         _add_button->Enable (static_cast<bool>(selected_cpl()));
447 }
448
449 optional<Controls::CPL>
450 Controls::selected_cpl () const
451 {
452         long int s = _cpl->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
453         if (s == -1) {
454                 return optional<CPL>();
455         }
456
457         DCPOMATIC_ASSERT (s < int(_cpls.size()));
458         return _cpls[s];
459 }
460
461 void
462 Controls::timecode_clicked ()
463 {
464         PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
465         if (dialog->ShowModal() == wxID_OK) {
466                 _viewer->seek (dialog->get(), true);
467         }
468         dialog->Destroy ();
469 }
470
471 void
472 Controls::frame_number_clicked ()
473 {
474         PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
475         if (dialog->ShowModal() == wxID_OK) {
476                 _viewer->seek (dialog->get(), true);
477         }
478         dialog->Destroy ();
479 }
480
481 void
482 Controls::jump_to_selected_clicked ()
483 {
484         Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
485 }
486
487 void
488 Controls::film_changed ()
489 {
490         shared_ptr<Film> film = _viewer->film ();
491
492         if (_film == film) {
493                 return;
494         }
495
496         _film = film;
497
498         setup_sensitivity ();
499
500         update_position_slider ();
501         update_position_label ();
502
503         if (_film) {
504                 _film->Change.connect (boost::bind (&Controls::film_change, this, _1, _2));
505         }
506 }
507
508 shared_ptr<Film>
509 Controls::film () const
510 {
511         return _film;
512 }
513
514 void
515 Controls::show_extended_player_controls (bool s)
516 {
517         _cpl->Show (s);
518         if (s) {
519                 update_dcp_directory ();
520         }
521         _spl_view->Show (s);
522         _log->Show (s);
523         _add_button->Show (s);
524         _v_sizer->Layout ();
525 }
526
527 void
528 Controls::add_cpl_to_list (shared_ptr<dcp::CPL> cpl, wxListCtrl* ctrl)
529 {
530         list<shared_ptr<dcp::Reel> > reels = cpl->reels ();
531
532         int const N = ctrl->GetItemCount();
533
534         wxListItem it;
535         if (!reels.empty() && reels.front()->main_picture()) {
536                 it.SetId(N);
537                 it.SetColumn(0);
538                 int seconds = rint(double(cpl->duration()) / reels.front()->main_picture()->frame_rate().as_float());
539                 int minutes = seconds / 60;
540                 seconds -= minutes * 60;
541                 int hours = minutes / 60;
542                 minutes -= hours * 60;
543                 it.SetText(wxString::Format("%02d:%02d:%02d", hours, minutes, seconds));
544                 ctrl->InsertItem(it);
545         }
546
547         it.SetId(N);
548         it.SetColumn(1);
549         it.SetText(std_to_wx(dcp::content_kind_to_string(cpl->content_kind())));
550         ctrl->SetItem(it);
551
552         it.SetId(N);
553         it.SetColumn(2);
554         it.SetText(std_to_wx(cpl->annotation_text()));
555         ctrl->SetItem(it);
556 }
557
558 void
559 Controls::update_dcp_directory ()
560 {
561         if (!_cpl->IsShown()) {
562                 return;
563         }
564
565         using namespace boost::filesystem;
566
567         _cpl->DeleteAllItems ();
568         _cpls.clear ();
569         optional<path> dir = Config::instance()->player_dcp_directory();
570         if (!dir) {
571                 return;
572         }
573
574         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
575                 try {
576                         if (is_directory(*i) && (is_regular_file(*i / "ASSETMAP") || is_regular_file(*i / "ASSETMAP.xml"))) {
577                                 string const x = i->path().string().substr(dir->string().length() + 1);
578                                 dcp::DCP dcp (*i);
579                                 dcp.read ();
580                                 BOOST_FOREACH (shared_ptr<dcp::CPL> j, dcp.cpls()) {
581                                         add_cpl_to_list (j, _cpl);
582                                         _cpls.push_back (make_pair(j, *i));
583                                 }
584                         }
585                 } catch (boost::filesystem::filesystem_error& e) {
586                         /* Never mind */
587                 }
588         }
589 }
590
591 #ifdef DCPOMATIC_VARIANT_SWAROOP
592 void
593 Controls::pause_clicked ()
594 {
595         _viewer->stop ();
596 }
597
598 void
599 Controls::stop_clicked ()
600 {
601         _viewer->stop ();
602         _viewer->seek (DCPTime(), true);
603 }
604 #endif
605
606 void
607 Controls::log (wxString s)
608 {
609         struct timeval time;
610         gettimeofday (&time, 0);
611         char buffer[64];
612         time_t const sec = time.tv_sec;
613         struct tm* t = localtime (&sec);
614         strftime (buffer, 64, "%c", t);
615         wxString ts = std_to_wx(string(buffer)) + N_(": ");
616         _log->SetValue(_log->GetValue() + ts + s + "\n");
617 }
618
619 void
620 Controls::image_changed (boost::weak_ptr<PlayerVideo> weak_pv)
621 {
622 #ifdef DCPOMATIC_VARIANT_SWAROOP
623         shared_ptr<PlayerVideo> pv = weak_pv.lock ();
624         if (!pv) {
625                 return;
626         }
627
628         shared_ptr<Content> c = pv->content().lock();
629         if (!c) {
630                 return;
631         }
632
633         shared_ptr<DCPContent> dc = dynamic_pointer_cast<DCPContent> (c);
634         if (!dc) {
635                 return;
636         }
637
638         if (!_current_kind || *_current_kind != dc->content_kind()) {
639                 _current_kind = dc->content_kind ();
640                 setup_sensitivity ();
641         }
642 #endif
643 }