Fix thinkos in cubasish theme
[ardour.git] / gtk2_ardour / video_monitor.cc
1 /*
2  * Copyright (C) 2013-2016 Tim Mayberry <mojofunk@gmail.com>
3  * Copyright (C) 2013-2018 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
5  * Copyright (C) 2013 John Emmas <john@creativepost.co.uk>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 #include <cstdio>
22
23 #include "pbd/file_utils.h"
24 #include "pbd/convert.h"
25 #include "ardour/filesystem_paths.h"
26
27 #include "gui_thread.h"
28 #include "timers.h"
29 #include "utils.h"
30 #include "public_editor.h"
31 #include "editor.h"
32 #include "video_monitor.h"
33
34 #include "pbd/i18n.h"
35
36 using namespace std;
37 using namespace PBD;
38 using namespace ARDOUR_UI_UTILS;
39
40 VideoMonitor::VideoMonitor (PublicEditor *ed, std::string xjadeo_bin_path)
41         : editor (ed)
42 {
43         manually_seeked_frame = 0;
44         fps =0.0; // = _session->timecode_frames_per_second();
45         sync_by_manual_seek = true;
46         _restore_settings_mask = 0;
47         clock_connection = sigc::connection();
48         state_connection = sigc::connection();
49         debug_enable = false;
50         state_clk_divide = 0;
51         starting = 0;
52         osdmode = 10; // 1: frameno, 2: timecode, 8: box
53
54         process = new ARDOUR::SystemExec(xjadeo_bin_path, X_("-R -J"));
55         process->ReadStdout.connect_same_thread (*this, boost::bind (&VideoMonitor::parse_output, this, _1 ,_2));
56         process->Terminated.connect (*this, invalidator (*this), boost::bind (&VideoMonitor::terminated, this), gui_context());
57         XJKeyEvent.connect (*this, invalidator (*this), boost::bind (&VideoMonitor::forward_keyevent, this, _1), gui_context());
58 }
59
60 VideoMonitor::~VideoMonitor ()
61 {
62         if (clock_connection.connected()) {
63                 clock_connection.disconnect();
64         }
65         if (state_connection.connected()) {
66                 state_connection.disconnect();
67         }
68         delete process;
69 }
70
71 bool
72 VideoMonitor::start ()
73 {
74         if (is_started()) {
75                 return true;
76         }
77
78         manually_seeked_frame = 0;
79         sync_by_manual_seek = false;
80         if (clock_connection.connected()) { clock_connection.disconnect(); }
81
82         if (process->start (debug_enable ? SystemExec::MergeWithStdin : SystemExec::IgnoreAndClose)) {
83                 return false;
84         }
85         return true;
86 }
87
88 void
89 VideoMonitor::query_full_state (bool wait)
90 {
91         knownstate = 0;
92         process->write_to_stdin("get windowsize\n");
93         process->write_to_stdin("get windowpos\n");
94         process->write_to_stdin("get letterbox\n");
95         process->write_to_stdin("get fullscreen\n");
96         process->write_to_stdin("get ontop\n");
97         process->write_to_stdin("get offset\n");
98         process->write_to_stdin("get osdcfg\n");
99         int timeout = 40;
100         if (wait && knownstate !=127 && --timeout) {
101                 Glib::usleep(50000);
102                 sched_yield();
103         }
104 }
105
106 void
107 VideoMonitor::quit ()
108 {
109         if (!is_started()) return;
110         if (state_connection.connected()) { state_connection.disconnect(); }
111         if (clock_connection.connected()) { clock_connection.disconnect(); }
112         query_full_state(true);
113         process->write_to_stdin("quit\n");
114         /* the 'quit' command should result in process termination
115          * but in case it fails (communication failure, SIGSTOP, ??)
116          * here's a timeout..
117          */
118         int timeout = 40;
119         while (is_started() && --timeout) {
120                 Glib::usleep(50000);
121                 sched_yield();
122         }
123         if (timeout <= 0) {
124                 process->terminate();
125         }
126 }
127
128 void
129 VideoMonitor::open (std::string filename)
130 {
131         if (!is_started()) return;
132         manually_seeked_frame = 0;
133         osdmode = 10; // 1: frameno, 2: timecode, 8: box
134         starting = 15;
135
136         std::string ardour_mono_file;
137         if (find_file (ARDOUR::ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
138                 process->write_to_stdin("osd font "+ ardour_mono_file  +"\n");
139         }
140
141         process->write_to_stdin("load " + filename + "\n");
142
143         process->write_to_stdin("set fps -1\n");
144         process->write_to_stdin("window resize 100%\n");
145         process->write_to_stdin("window ontop on\n");
146         process->write_to_stdin("set seekmode 1\n");
147         /* override bitwise flags -- see xjadeo.h
148          * 0x0001 : ignore 'q', ESC  / quit
149          * 0x0002 : ignore "window closed by WM" / quit
150          * 0x0004 : (osx only) menu-exit / quit
151          * 0x0008 : ignore mouse-button 1 -- resize
152          * 0x0010 : no A/V offset control with keyboard
153          * 0x0020 : don't use jack-session
154          * 0x0040 : disable jack transport control
155          * 0x0080 : disallow sync source change (OSX menu)
156          * 0x0100 : disallow file open (OSX menu, X11 DnD)
157          */
158         process->write_to_stdin("set override 504\n");
159         process->write_to_stdin("notify keyboard\n");
160         process->write_to_stdin("notify settings\n");
161         process->write_to_stdin("window letterbox on\n");
162         process->write_to_stdin("osd mode 10\n");
163
164         for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
165                 if (skip_setting(it->first)) { continue; }
166                 process->write_to_stdin(it->first + " " + it->second + "\n");
167         }
168         if (!state_connection.connected()) {
169                 starting = 15;
170                 querystate();
171                 state_clk_divide = 0;
172                 /* TODO once every two second or so -- state_clk_divide hack below */
173                 state_connection = Timers::rapid_connect (sigc::mem_fun (*this, &VideoMonitor::querystate));
174         }
175         sync_by_manual_seek = true;
176         clock_connection = Timers::fps_connect (sigc::mem_fun (*this, &VideoMonitor::srsupdate));
177         xjadeo_sync_setup();
178 }
179
180 void
181 VideoMonitor::querystate ()
182 {
183         /* clock-divider hack -- RapidScreenUpdate == every_point_one_seconds */
184         state_clk_divide = (state_clk_divide + 1) % 300; // 30 secs
185         if (state_clk_divide == 0) {
186                 // every 30 seconds
187                 query_full_state(false);
188                 return;
189         }
190         if (state_clk_divide%25 != 0) {
191                 return;
192         }
193         // every 2.5 seconds:
194         process->write_to_stdin("get fullscreen\n");
195         process->write_to_stdin("get ontop\n");
196         process->write_to_stdin("get osdcfg\n");
197         process->write_to_stdin("get letterbox\n");
198 }
199
200 bool
201 VideoMonitor::skip_setting (std::string which)
202 {
203         if (_restore_settings_mask & XJ_OSD && which == "osd mode") { return true; }
204         if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
205         if (_restore_settings_mask & XJ_WINDOW_SIZE && which == "window size") { return true; }
206         if (_restore_settings_mask & XJ_WINDOW_POS && which == "window xy") { return true; }
207         if (_restore_settings_mask & XJ_WINDOW_ONTOP && which == "window ontop") { return true; }
208         if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
209         if (_restore_settings_mask & XJ_OFFSET && which == "set offset") { return true; }
210         if (_restore_settings_mask & XJ_FULLSCREEN && which == "window zoom") { return true; }
211         return false;
212 }
213
214 void
215 VideoMonitor::send_cmd (int what, int param)
216 {
217         bool osd_update = false;
218         int prev_osdmode = osdmode;
219         if (!is_started()) return;
220         switch (what) {
221                 case 1:
222                         if (param) process->write_to_stdin("window ontop on\n");
223                         else process->write_to_stdin("window ontop off\n");
224                         break;
225                 case 2:
226                         if (param) osdmode |= 2;
227                         else osdmode &= ~2;
228                         osd_update = (prev_osdmode != osdmode);
229                         break;
230                 case 3:
231                         if (param) osdmode |= 1;
232                         else osdmode &= ~1;
233                         osd_update = (prev_osdmode != osdmode);
234                         break;
235                 case 4:
236                         if (param) osdmode |= 8;
237                         else osdmode &= ~8;
238                         osd_update = (prev_osdmode != osdmode);
239                         break;
240                 case 5:
241                         if (param) process->write_to_stdin("window zoom on\n");
242                         else process->write_to_stdin("window zoom off\n");
243                         break;
244                 case 6:
245                         if (param) process->write_to_stdin("window letterbox on\n");
246                         else process->write_to_stdin("window letterbox off\n");
247                         break;
248                 case 7:
249                         process->write_to_stdin("window resize 100%");
250                         break;
251                 default:
252                         break;
253         }
254         if (osd_update) {
255                 std::ostringstream osstream; osstream << "osd mode " << osdmode << "\n";
256                 process->write_to_stdin(osstream.str());
257         }
258 }
259
260 bool
261 VideoMonitor::is_started ()
262 {
263         return process->is_running();
264 }
265
266 void
267 VideoMonitor::forward_keyevent (unsigned int keyval)
268 {
269         emulate_key_event (keyval);
270 }
271
272 void
273 VideoMonitor::parse_output (std::string d, size_t /*s*/)
274 {
275         std::string line = d;
276         std::string::size_type start = 0;
277         std::string::size_type end = 0;
278
279         while (1) {
280                 end = d.find('\n', start);
281                 if (end == std::string::npos) break;
282                 line = d.substr(start,end-start);
283                 start=end+1;
284                 if (line.length() <4 || line.at(0)!='@') continue;
285 #if 1 /* DEBUG */
286                 if (debug_enable) {
287                         printf("xjadeo: '%s'\n", line.c_str());
288                 }
289 #endif
290                 int status = atoi(line.substr(1,3));
291                 switch(status / 100) {
292                         case 4: /* errors */
293                                 if (status == 403) {
294                                         PBD::warning << _("Video Monitor: File Not Found.") << endmsg;
295                                         /* check: we should only write from the main thread.
296                                          * However it should not matter for 'quit'.
297                                          */
298                                         process->write_to_stdin("quit\n");
299                                 }
300 #ifdef DEBUG_XJCOM
301                         else
302                                 printf("xjadeo: error '%s'\n", line.c_str());
303 #endif
304                         break;
305                         case 3: /* async notifications */
306                         {
307                                 std::string::size_type equalsign = line.find('=');
308                                 std::string::size_type comment = line.find('#');
309                                 if (comment != std::string::npos) { line = line.substr(0,comment); }
310                                 if (equalsign != std::string::npos) {
311                                         std::string key = line.substr(5, equalsign - 5);
312                                         std::string value = line.substr(equalsign + 1);
313
314                                         if (status == 310 && key=="keypress") {
315                                                 /* keyboard event */
316                                                 XJKeyEvent((unsigned int)atoi(value));
317                                         }
318 #ifdef DEBUG_XJCOM
319                                         else {
320                                                 std::string msg = line.substr(5);
321                                                 printf("xjadeo: async '%s' -> '%s'\n", key, value);
322                                         }
323 #endif
324                                 }
325 #ifdef DEBUG_XJCOM
326                                 else {
327                                         std::string msg = line.substr(5);
328                                         printf("xjadeo: async '%s'\n", msg.c_str());
329                                 }
330 #endif
331                         } break;
332                         case 1: /* text messages - command reply */
333                                 break;
334                         case 8: /* comments / info for humans */
335                                 break;
336                         case 2:
337 /* replies:
338  * 201: var=<int>
339  * 202: var=<double>
340  * 210: var=<int>x<int>
341  * 220: var=<string>
342  * 228: var=<smpte-string>
343  */
344                         {
345                                 std::string::size_type equalsign = line.find('=');
346                                 std::string::size_type comment = line.find('#');
347                                 if (comment != std::string::npos) { line = line.substr(0,comment); }
348                                 if (equalsign != std::string::npos) {
349                                         std::string key = line.substr(5, equalsign - 5);
350                                         std::string value = line.substr(equalsign + 1);
351 #if 0 /* DEBUG */
352                                         std::cout << "parsed: " << key << " => " << value << std::endl;
353 #endif
354                                         if       (key ==  "windowpos") {
355                                                 knownstate |= 16;
356                                                 if (xjadeo_settings["window xy"] != value) {
357                                                         if (!starting && _session) _session->set_dirty ();
358                                                 }
359                                                 xjadeo_settings["window xy"] = value;
360                                         } else if(key ==  "windowsize") {
361                                                 knownstate |= 32;
362                                                 if (xjadeo_settings["window size"] != value) {
363                                                         if (!starting && _session) _session->set_dirty ();
364                                                 }
365                                                 xjadeo_settings["window size"] = value;
366                                         } else if(key ==  "windowontop") {
367                                                 knownstate |= 2;
368                                                 if (starting || xjadeo_settings["window ontop"] != value) {
369                                                         if (!starting && _session) _session->set_dirty ();
370                                                         if (atoi(value)) { UiState("xjadeo-window-ontop-on"); }
371                                                         else { UiState("xjadeo-window-ontop-off"); }
372                                                         starting &= ~2;
373                                                 }
374                                                 xjadeo_settings["window ontop"] = value;
375                                         } else if(key ==  "fullscreen") {
376                                                 knownstate |= 4;
377                                                 if (starting || xjadeo_settings["window zoom"] != value) {
378                                                         if (!starting && _session) _session->set_dirty ();
379                                                         if (atoi(value)) { UiState("xjadeo-window-fullscreen-on"); }
380                                                         else { UiState("xjadeo-window-fullscreen-off"); }
381                                                         starting &= ~4;
382                                                 }
383                                                 xjadeo_settings["window zoom"] = value;
384                                         } else if(key ==  "letterbox") {
385                                                 knownstate |= 8;
386                                                 if (starting || xjadeo_settings["window letterbox"] != value) {
387                                                         if (!starting && _session) _session->set_dirty ();
388                                                         if (atoi(value)) { UiState("xjadeo-window-letterbox-on"); }
389                                                         else { UiState("xjadeo-window-letterbox-off"); }
390                                                         starting &= ~8;
391                                                 }
392                                                 xjadeo_settings["window letterbox"] = value;
393                                         } else if(key ==  "osdmode") {
394                                                 knownstate |= 1;
395                                                 osdmode = atoi(value) & ~0x40;
396                                                 if (starting || atoi(xjadeo_settings["osd mode"]) != osdmode) {
397                                                         if (!starting && _session) _session->set_dirty ();
398                                                         if ((osdmode & 1) == 1) { UiState("xjadeo-window-osd-frame-on"); }
399                                                         if ((osdmode & 1) == 0) { UiState("xjadeo-window-osd-frame-off"); }
400                                                         if ((osdmode & 2) == 2) { UiState("xjadeo-window-osd-timecode-on"); }
401                                                         if ((osdmode & 2) == 0) { UiState("xjadeo-window-osd-timecode-off"); }
402                                                         if ((osdmode & 8) == 8) { UiState("xjadeo-window-osd-box-on"); }
403                                                         if ((osdmode & 8) == 0) { UiState("xjadeo-window-osd-box-off"); }
404                                                 }
405                                                 starting &= ~1;
406                                                 xjadeo_settings["osd mode"] = value;
407                                         } else if(key ==  "offset") {
408                                                 knownstate |= 64;
409                                                 if (xjadeo_settings["set offset"] != value) {
410                                                         if (!starting && _session) _session->set_dirty ();
411                                                 }
412                                                 xjadeo_settings["set offset"] = value;
413 #ifdef DEBUG_XJCOM
414                                         } else {
415                                                 printf("xjadeo: '%s' -> '%s'\n", key.c_str(), value.c_str());
416 #endif
417                                         }
418                                 }
419                         }
420                                 break;
421                         default:
422                                 break;
423                 }
424         }
425 }
426
427 void
428 VideoMonitor::terminated ()
429 {
430         process->terminate(); // from gui-context clean up
431         Terminated();
432 }
433
434 void
435 VideoMonitor::save_session ()
436 {
437         if (!_session) { return; }
438         XMLNode* node = _session->extra_xml (X_("XJSettings"), true);
439         if (!node) return;
440         node->remove_nodes_and_delete("XJSetting");
441
442         for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
443           XMLNode* child = node->add_child (X_("XJSetting"));
444                 child->set_property (X_("k"), it->first);
445                 child->set_property (X_("v"), it->second);
446         }
447 }
448
449
450 void
451 VideoMonitor::set_session (ARDOUR::Session *s)
452 {
453         SessionHandlePtr::set_session (s);
454         if (!_session) { return; }
455         ARDOUR::Config->ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoMonitor::parameter_changed, this, _1), gui_context());
456         _session->config.ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoMonitor::parameter_changed, this, _1), gui_context());
457         XMLNode* node = _session->extra_xml (X_("XJSettings"));
458         if (!node) { return;}
459         xjadeo_settings.clear();
460
461         XMLNodeList nlist = node->children();
462         XMLNodeConstIterator niter;
463         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
464                 xjadeo_settings[(*niter)->property(X_("k"))->value()] = (*niter)->property(X_("v"))->value();
465   }
466 }
467
468 bool
469 VideoMonitor::set_custom_setting (const std::string k, const std::string v)
470 {
471         xjadeo_settings[k] = v;
472         return true; /* TODO: check if key is valid */
473 }
474
475 const std::string
476 VideoMonitor::get_custom_setting (const std::string k)
477 {
478         return (xjadeo_settings[k]);
479 }
480
481 #define NO_OFFSET (Temporal::max_samplepos) //< skip setting or modifying offset
482 void
483 VideoMonitor::srsupdate ()
484 {
485         if (!_session) { return; }
486         if (editor->dragging_playhead() || editor->preview_video_drag_active()) {
487                 return;
488         }
489         manual_seek(_session->audible_sample(), false, NO_OFFSET);
490 }
491
492 void
493 VideoMonitor::set_offset (ARDOUR::sampleoffset_t offset)
494 {
495         if (!is_started()) { return; }
496         if (!_session) { return; }
497         if (offset == NO_OFFSET ) { return; }
498
499         samplecnt_t video_frame_offset;
500         samplecnt_t audio_sample_rate;
501         if (_session->config.get_videotimeline_pullup()) {
502                 audio_sample_rate = _session->sample_rate();
503         } else {
504                 audio_sample_rate = _session->nominal_sample_rate();
505         }
506
507         /* Note: pull-up/down are applied here: sample_rate() vs. nominal_sample_rate() */
508         if (_session->config.get_use_video_file_fps()) {
509                 video_frame_offset = floor(offset * fps / audio_sample_rate);
510         } else {
511                 video_frame_offset = floor(offset * _session->timecode_frames_per_second() / audio_sample_rate);
512         }
513
514         if (video_offset == video_frame_offset) { return; }
515         video_offset = video_frame_offset;
516
517         std::ostringstream osstream1; osstream1 << -1 * video_frame_offset;
518         process->write_to_stdin("set offset " + osstream1.str() + "\n");
519 }
520
521 void
522 VideoMonitor::manual_seek (samplepos_t when, bool /*force*/, ARDOUR::sampleoffset_t offset)
523 {
524         if (!is_started()) { return; }
525         if (!_session) { return; }
526         samplecnt_t video_frame;
527         samplecnt_t audio_sample_rate;
528         if (_session->config.get_videotimeline_pullup()) {
529                 audio_sample_rate = _session->sample_rate();
530         } else {
531                 audio_sample_rate = _session->nominal_sample_rate();
532         }
533
534         /* Note: pull-up/down are applied here: sample_rate() vs. nominal_sample_rate() */
535         if (_session->config.get_use_video_file_fps()) {
536                 video_frame = floor(when * fps / audio_sample_rate);
537         } else {
538                 video_frame = floor(when * _session->timecode_frames_per_second() / audio_sample_rate);
539         }
540         if (video_frame < 0 ) video_frame = 0;
541
542         if (video_frame == manually_seeked_frame) { return; }
543         manually_seeked_frame = video_frame;
544
545 #if 0 /* DEBUG */
546         std::cout <<"seek: " << video_frame << std::endl;
547 #endif
548         std::ostringstream osstream; osstream << video_frame;
549         process->write_to_stdin("seek " + osstream.str() + "\n");
550
551         set_offset(offset);
552 }
553
554 void
555 VideoMonitor::parameter_changed (std::string const & p)
556 {
557         if (!is_started()) { return; }
558         if (!_session) { return; }
559         if (p != "external-sync" && p != "sync-source") {
560                 return;
561         }
562         xjadeo_sync_setup();
563 }
564
565 void
566 VideoMonitor::xjadeo_sync_setup ()
567 {
568         if (!is_started()) { return; }
569         if (!_session) { return; }
570
571         bool my_manual_seek = true;
572         if (_session->synced_to_engine ()) {
573                 my_manual_seek = false;
574         }
575
576         if (my_manual_seek != sync_by_manual_seek) {
577                 if (sync_by_manual_seek) {
578                         if (clock_connection.connected()) {
579                                 clock_connection.disconnect();
580                         }
581                         process->write_to_stdin("jack connect\n");
582                 } else {
583                         process->write_to_stdin("jack disconnect\n");
584                         clock_connection = Timers::fps_connect (sigc::mem_fun (*this, &VideoMonitor::srsupdate));
585                 }
586                 sync_by_manual_seek = my_manual_seek;
587         }
588 }