Add very basic logging in the dual-screen player.
[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 <wx/wx.h>
28 #include <wx/tglbtn.h>
29 #include <wx/listctrl.h>
30
31 using std::string;
32 using boost::optional;
33 using boost::shared_ptr;
34 using boost::weak_ptr;
35
36 Controls::Controls (wxWindow* parent, shared_ptr<FilmViewer> viewer, bool editor_controls)
37         : wxPanel (parent)
38         , _viewer (viewer)
39         , _slider_being_moved (false)
40         , _was_running_before_slider (false)
41         , _outline_content (0)
42         , _eye (0)
43         , _jump_to_selected (0)
44         , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
45         , _rewind_button (new wxButton (this, wxID_ANY, wxT("|<")))
46         , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
47         , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
48         , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
49         , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
50 #ifdef DCPOMATIC_VARIANT_SWAROOP
51         , _play_button (new wxButton(this, wxID_ANY, _("Play")))
52         , _pause_button (new wxButton(this, wxID_ANY, _("Pause")))
53         , _stop_button (new wxButton(this, wxID_ANY, _("Stop")))
54 #else
55         , _play_button (new wxToggleButton(this, wxID_ANY, _("Play")))
56 #endif
57 {
58         _v_sizer = new wxBoxSizer (wxVERTICAL);
59         SetSizer (_v_sizer);
60
61         wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
62         if (editor_controls) {
63                 _outline_content = new wxCheckBox (this, wxID_ANY, _("Outline content"));
64                 view_options->Add (_outline_content, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
65                 _eye = new wxChoice (this, wxID_ANY);
66                 _eye->Append (_("Left"));
67                 _eye->Append (_("Right"));
68                 _eye->SetSelection (0);
69                 view_options->Add (_eye, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
70                 _jump_to_selected = new wxCheckBox (this, wxID_ANY, _("Jump to selected content"));
71                 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
72         }
73
74         _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
75
76         wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
77
78         _dcp_directory = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
79         _dcp_directory->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 580);
80         e_sizer->Add (_dcp_directory, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
81
82         _log = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(-1, 400), wxTE_READONLY | wxTE_MULTILINE);
83         e_sizer->Add (_log, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
84
85         _dcp_directory->Show (false);
86
87         _v_sizer->Add (e_sizer, 0, wxEXPAND);
88
89         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
90
91         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
92         time_sizer->Add (_frame_number, 0, wxEXPAND);
93         time_sizer->Add (_timecode, 0, wxEXPAND);
94
95         h_sizer->Add (_rewind_button, 0, wxALL, 2);
96         h_sizer->Add (_back_button, 0, wxALL, 2);
97         h_sizer->Add (time_sizer, 0, wxEXPAND);
98         h_sizer->Add (_forward_button, 0, wxALL, 2);
99         h_sizer->Add (_play_button, 0, wxEXPAND);
100 #ifdef DCPOMATIC_VARIANT_SWAROOP
101         h_sizer->Add (_pause_button, 0, wxEXPAND);
102         h_sizer->Add (_stop_button, 0, wxEXPAND);
103 #endif
104         h_sizer->Add (_slider, 1, wxEXPAND);
105
106         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
107
108         _frame_number->SetMinSize (wxSize (84, -1));
109         _rewind_button->SetMinSize (wxSize (32, -1));
110         _back_button->SetMinSize (wxSize (32, -1));
111         _forward_button->SetMinSize (wxSize (32, -1));
112
113         if (_eye) {
114                 _eye->Bind (wxEVT_CHOICE, boost::bind (&Controls::eye_changed, this));
115         }
116         if (_outline_content) {
117                 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::outline_content_changed, this));
118         }
119
120         _slider->Bind           (wxEVT_SCROLL_THUMBTRACK,   boost::bind (&Controls::slider_moved,    this, false));
121         _slider->Bind           (wxEVT_SCROLL_PAGEUP,       boost::bind (&Controls::slider_moved,    this, true));
122         _slider->Bind           (wxEVT_SCROLL_PAGEDOWN,     boost::bind (&Controls::slider_moved,    this, true));
123         _slider->Bind           (wxEVT_SCROLL_CHANGED,      boost::bind (&Controls::slider_released, this));
124 #ifdef DCPOMATIC_VARIANT_SWAROOP
125         _play_button->Bind      (wxEVT_BUTTON,              boost::bind (&Controls::play_clicked,    this));
126         _pause_button->Bind     (wxEVT_BUTTON,              boost::bind (&Controls::pause_clicked,   this));
127         _stop_button->Bind      (wxEVT_BUTTON,              boost::bind (&Controls::stop_clicked,    this));
128 #else
129         _play_button->Bind      (wxEVT_TOGGLEBUTTON,        boost::bind (&Controls::play_clicked,    this));
130 #endif
131         _rewind_button->Bind    (wxEVT_LEFT_DOWN,           boost::bind (&Controls::rewind_clicked,  this, _1));
132         _back_button->Bind      (wxEVT_LEFT_DOWN,           boost::bind (&Controls::back_clicked,    this, _1));
133         _forward_button->Bind   (wxEVT_LEFT_DOWN,           boost::bind (&Controls::forward_clicked, this, _1));
134         _frame_number->Bind     (wxEVT_LEFT_DOWN,           boost::bind (&Controls::frame_number_clicked, this));
135         _timecode->Bind         (wxEVT_LEFT_DOWN,           boost::bind (&Controls::timecode_clicked, this));
136         _dcp_directory->Bind    (wxEVT_LIST_ITEM_SELECTED,  boost::bind (&Controls::dcp_directory_selected, this));
137         if (_jump_to_selected) {
138                 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::jump_to_selected_clicked, this));
139                 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
140         }
141
142         _viewer->PositionChanged.connect (boost::bind(&Controls::position_changed, this));
143         _viewer->Started.connect (boost::bind(&Controls::started, this));
144         _viewer->Stopped.connect (boost::bind(&Controls::stopped, this));
145         _viewer->FilmChanged.connect (boost::bind(&Controls::film_changed, this));
146
147         film_changed ();
148
149         setup_sensitivity ();
150         update_dcp_directory ();
151
152         JobManager::instance()->ActiveJobsChanged.connect (
153                 bind (&Controls::active_jobs_changed, this, _2)
154                 );
155
156         _config_changed_connection = Config::instance()->Changed.connect (bind(&Controls::config_changed, this, _1));
157 }
158
159 void
160 Controls::config_changed (int property)
161 {
162         if (property == Config::PLAYER_DCP_DIRECTORY) {
163                 update_dcp_directory ();
164         }
165 }
166
167 void
168 Controls::started ()
169 {
170 #ifdef DCPOMATIC_VARIANT_SWAROOP
171         _play_button->Enable (false);
172         _pause_button->Enable (true);
173 #else
174         _play_button->SetValue (true);
175 #endif
176         setup_sensitivity ();
177 }
178
179 void
180 Controls::stopped ()
181 {
182 #ifdef DCPOMATIC_VARIANT_SWAROOP
183         _play_button->Enable (true);
184         _pause_button->Enable (false);
185 #else
186         _play_button->SetValue (false);
187 #endif
188         setup_sensitivity ();
189 }
190
191 void
192 Controls::position_changed ()
193 {
194         if (!_slider_being_moved) {
195                 update_position_label ();
196                 update_position_slider ();
197         }
198 }
199
200 void
201 Controls::eye_changed ()
202 {
203         _viewer->set_eyes (_eye->GetSelection() == 0 ? EYES_LEFT : EYES_RIGHT);
204 }
205
206 void
207 Controls::outline_content_changed ()
208 {
209         _viewer->set_outline_content (_outline_content->GetValue());
210 }
211
212 void
213 Controls::film_change (ChangeType type, Film::Property p)
214 {
215         if (type != CHANGE_TYPE_DONE) {
216                 return;
217         }
218
219         if (p == Film::CONTENT || p == Film::THREE_D) {
220                 setup_sensitivity ();
221         }
222 }
223
224 /** @param page true if this was a PAGEUP/PAGEDOWN event for which we won't receive a THUMBRELEASE */
225 void
226 Controls::slider_moved (bool page)
227 {
228         if (!_film) {
229                 return;
230         }
231
232         if (!page && !_slider_being_moved) {
233                 /* This is the first event of a drag; stop playback for the duration of the drag */
234                 _was_running_before_slider = _viewer->stop ();
235                 _slider_being_moved = true;
236         }
237
238         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
239         t = t.round (_film->video_frame_rate());
240         /* Ensure that we hit the end of the film at the end of the slider */
241         if (t >= _film->length ()) {
242                 t = _film->length() - _viewer->one_video_frame();
243         }
244         _viewer->seek (t, false);
245         update_position_label ();
246 }
247
248 void
249 Controls::slider_released ()
250 {
251         if (_was_running_before_slider) {
252                 /* Restart after a drag */
253                 _viewer->start ();
254         }
255         _slider_being_moved = false;
256 }
257
258 void
259 Controls::play_clicked ()
260 {
261 #ifdef DCPOMATIC_VARIANT_SWAROOP
262         _viewer->start ();
263 #else
264         check_play_state ();
265 #endif
266 }
267
268
269 #ifndef DCPOMATIC_VARIANT_SWAROOP
270 void
271 Controls::check_play_state ()
272 {
273         if (!_film || _film->video_frame_rate() == 0) {
274                 return;
275         }
276
277         if (_play_button->GetValue()) {
278                 _viewer->start ();
279         } else {
280                 _viewer->stop ();
281         }
282 }
283 #endif
284
285 void
286 Controls::update_position_slider ()
287 {
288         if (!_film) {
289                 _slider->SetValue (0);
290                 return;
291         }
292
293         DCPTime const len = _film->length ();
294
295         if (len.get ()) {
296                 int const new_slider_position = 4096 * _viewer->position().get() / len.get();
297                 if (new_slider_position != _slider->GetValue()) {
298                         _slider->SetValue (new_slider_position);
299                 }
300         }
301 }
302
303 void
304 Controls::update_position_label ()
305 {
306         if (!_film) {
307                 _frame_number->SetLabel ("0");
308                 _timecode->SetLabel ("0:0:0.0");
309                 return;
310         }
311
312         double const fps = _film->video_frame_rate ();
313         /* Count frame number from 1 ... not sure if this is the best idea */
314         _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_viewer->position().seconds() * fps) + 1));
315         _timecode->SetLabel (time_to_timecode (_viewer->position(), fps));
316 }
317
318 void
319 Controls::active_jobs_changed (optional<string> j)
320 {
321         _active_job = j;
322         setup_sensitivity ();
323 }
324
325 DCPTime
326 Controls::nudge_amount (wxKeyboardState& ev)
327 {
328         DCPTime amount = _viewer->one_video_frame ();
329
330         if (ev.ShiftDown() && !ev.ControlDown()) {
331                 amount = DCPTime::from_seconds (1);
332         } else if (!ev.ShiftDown() && ev.ControlDown()) {
333                 amount = DCPTime::from_seconds (10);
334         } else if (ev.ShiftDown() && ev.ControlDown()) {
335                 amount = DCPTime::from_seconds (60);
336         }
337
338         return amount;
339 }
340
341 void
342 Controls::rewind_clicked (wxMouseEvent& ev)
343 {
344         _viewer->seek (DCPTime(), true);
345         ev.Skip();
346 }
347
348 void
349 Controls::back_frame ()
350 {
351         _viewer->seek_by (-_viewer->one_video_frame(), true);
352 }
353
354 void
355 Controls::forward_frame ()
356 {
357         _viewer->seek_by (_viewer->one_video_frame(), true);
358 }
359
360 void
361 Controls::back_clicked (wxKeyboardState& ev)
362 {
363         _viewer->seek_by (-nudge_amount(ev), true);
364 }
365
366 void
367 Controls::forward_clicked (wxKeyboardState& ev)
368 {
369         _viewer->seek_by (nudge_amount(ev), true);
370 }
371
372 void
373 Controls::setup_sensitivity ()
374 {
375         /* examine content is the only job which stops the viewer working */
376         bool const c = _film && !_film->content().empty() && (!_active_job || *_active_job != "examine_content");
377
378         _slider->Enable (c);
379         _rewind_button->Enable (c);
380         _back_button->Enable (c);
381         _forward_button->Enable (c);
382 #ifdef DCPOMATIC_VARIANT_SWAROOP
383         _play_button->Enable (c && !_viewer->playing());
384         _pause_button->Enable (c && _viewer->playing());
385         _stop_button->Enable (c);
386 #else
387         _play_button->Enable (c);
388 #endif
389         if (_outline_content) {
390                 _outline_content->Enable (c);
391         }
392         _frame_number->Enable (c);
393         _timecode->Enable (c);
394         if (_jump_to_selected) {
395                 _jump_to_selected->Enable (c);
396         }
397
398         if (_eye) {
399                 _eye->Enable (c && _film->three_d ());
400         }
401 }
402
403 void
404 Controls::timecode_clicked ()
405 {
406         PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
407         if (dialog->ShowModal() == wxID_OK) {
408                 _viewer->seek (dialog->get(), true);
409         }
410         dialog->Destroy ();
411 }
412
413 void
414 Controls::frame_number_clicked ()
415 {
416         PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
417         if (dialog->ShowModal() == wxID_OK) {
418                 _viewer->seek (dialog->get(), true);
419         }
420         dialog->Destroy ();
421 }
422
423 void
424 Controls::jump_to_selected_clicked ()
425 {
426         Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
427 }
428
429 void
430 Controls::film_changed ()
431 {
432         shared_ptr<Film> film = _viewer->film ();
433
434         if (_film == film) {
435                 return;
436         }
437
438         _film = film;
439
440         setup_sensitivity ();
441
442         update_position_slider ();
443         update_position_label ();
444
445         if (_film) {
446                 _film->Change.connect (boost::bind (&Controls::film_change, this, _1, _2));
447         }
448 }
449
450 shared_ptr<Film>
451 Controls::film () const
452 {
453         return _film;
454 }
455
456 void
457 Controls::show_extended_player_controls (bool s)
458 {
459         _dcp_directory->Show (s);
460 }
461
462 void
463 Controls::update_dcp_directory ()
464 {
465         using namespace boost::filesystem;
466
467         _dcp_directory->DeleteAllItems ();
468         _dcp_directories.clear ();
469         optional<path> dir = Config::instance()->player_dcp_directory();
470         if (!dir) {
471                 return;
472         }
473
474         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
475                 try {
476                         if (is_directory(*i) && (is_regular_file(*i / "ASSETMAP") || is_regular_file(*i / "ASSETMAP.xml"))) {
477                                 string const x = i->path().string().substr(dir->string().length() + 1);
478                                 _dcp_directory->InsertItem(_dcp_directory->GetItemCount(), std_to_wx(x));
479                                 _dcp_directories.push_back(x);
480                         }
481                 } catch (boost::filesystem::filesystem_error& e) {
482                         /* Never mind */
483                 }
484         }
485 }
486
487 void
488 Controls::dcp_directory_selected ()
489 {
490         long int s = _dcp_directory->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
491         if (s == -1) {
492                 return;
493         }
494
495         DCPOMATIC_ASSERT (s < int(_dcp_directories.size()));
496         DCPDirectorySelected (*Config::instance()->player_dcp_directory() / _dcp_directories[s]);
497 }
498
499 #ifdef DCPOMATIC_VARIANT_SWAROOP
500 void
501 Controls::pause_clicked ()
502 {
503         _viewer->stop ();
504 }
505
506 void
507 Controls::stop_clicked ()
508 {
509         _viewer->stop ();
510         DCPEjected ();
511 }
512 #endif
513
514 void
515 Controls::log (wxString s)
516 {
517         struct timeval time;
518         gettimeofday (&time, 0);
519         char buffer[64];
520         time_t const sec = time.tv_sec;
521         struct tm* t = localtime (&sec);
522         strftime (buffer, 64, "%c", t);
523         wxString ts = std_to_wx(string(buffer)) + N_(": ");
524         _log->SetValue(_log->GetValue() + ts + s + "\n");
525 }