Merge branch 'master' of git@git.ardour.org:ardour/ardour
[ardour.git] / gtk2_ardour / video_monitor.cc
index fabe4201dd61dd6d5f5227bb047227c9ebcbcc6d..75ae6b494663cdb2c429371aa9a7eccff45fd0ec 100644 (file)
@@ -17,9 +17,8 @@
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
 */
-#ifdef WITH_VIDEOTIMELINE
-
 #include "pbd/file_utils.h"
+#include "pbd/convert.h"
 #include "gui_thread.h"
 #include "ardour_ui.h"
 
@@ -30,6 +29,7 @@
 #include "i18n.h"
 
 using namespace std;
+using namespace PBD;
 
 VideoMonitor::VideoMonitor (PublicEditor *ed, std::string xjadeo_bin_path)
        : editor (ed)
@@ -79,11 +79,9 @@ VideoMonitor::start ()
 }
 
 void
-VideoMonitor::quit ()
+VideoMonitor::query_full_state (bool wait)
 {
-       if (!is_started()) return;
-       if (state_connection.connected()) { state_connection.disconnect(); }
-       if (clock_connection.connected()) { clock_connection.disconnect(); }
+       knownstate = 0;
        process->write_to_stdin("get windowsize\n");
        process->write_to_stdin("get windowpos\n");
        process->write_to_stdin("get letterbox\n");
@@ -91,10 +89,21 @@ VideoMonitor::quit ()
        process->write_to_stdin("get ontop\n");
        process->write_to_stdin("get offset\n");
        process->write_to_stdin("get osdcfg\n");
+       int timeout = 40;
+       if (wait && knownstate !=127 && --timeout) {
+               usleep(50000);
+               sched_yield();
+       }
+}
+
+void
+VideoMonitor::quit ()
+{
+       if (!is_started()) return;
+       if (state_connection.connected()) { state_connection.disconnect(); }
+       if (clock_connection.connected()) { clock_connection.disconnect(); }
        process->write_to_stdin("quit\n");
-#if 1
-       /* wait for replies to the config requests above.
-        * the 'quit' command should result in process termination
+       /* the 'quit' command should result in process termination
         * but in case it fails (communication failure, SIGSTOP, ??)
         * here's a timeout..
         */
@@ -104,11 +113,8 @@ VideoMonitor::quit ()
                sched_yield();
        }
        if (timeout <= 0) {
-               printf("xjadeo connection: time-out. session may not be saved.\n");
                process->terminate();
        }
-#endif
-       save_session();
 }
 
 void
@@ -118,7 +124,7 @@ VideoMonitor::open (std::string filename)
        manually_seeked_frame = 0;
        osdmode = 10; // 1: frameno, 2: timecode, 8: box
        sync_by_manual_seek = false;
-       starting = 7;
+       starting = 15;
        process->write_to_stdin("load " + filename + "\n");
        process->write_to_stdin("set fps -1\n");
        process->write_to_stdin("window resize 100%\n");
@@ -132,7 +138,7 @@ VideoMonitor::open (std::string filename)
                process->write_to_stdin(it->first + " " + it->second + "\n");
        }
        if (!state_connection.connected()) {
-               starting = 7;
+               starting = 15;
                querystate();
                state_clk_divide = 0;
                /* TODO once every two second or so -- state_clk_divide hack below */
@@ -145,9 +151,16 @@ void
 VideoMonitor::querystate ()
 {
        /* clock-divider hack -- RapidScreenUpdate == every_point_one_seconds */
-       state_clk_divide = (state_clk_divide + 1) % 15; // every 1.5 seconds
-       if (state_clk_divide != 0) return;
-
+       state_clk_divide = (state_clk_divide + 1) % 300; // 30 secs
+       if (state_clk_divide == 0) {
+               // every 30 seconds
+               query_full_state(false);
+               return;
+       }
+       if (state_clk_divide%25 != 0) {
+               return;
+       }
+       // every 2.5 seconds:
        process->write_to_stdin("get fullscreen\n");
        process->write_to_stdin("get ontop\n");
        process->write_to_stdin("get osdcfg\n");
@@ -197,6 +210,13 @@ VideoMonitor::send_cmd (int what, int param)
                        if (param) process->write_to_stdin("window zoom on\n");
                        else process->write_to_stdin("window zoom off\n");
                        break;
+               case 6:
+                       if (param) process->write_to_stdin("window letterbox on\n");
+                       else process->write_to_stdin("window letterbox off\n");
+                       break;
+               case 7:
+                       process->write_to_stdin("window resize 100%");
+                       break;
                default:
                        break;
        }
@@ -213,7 +233,7 @@ VideoMonitor::is_started ()
 }
 
 void
-VideoMonitor::parse_output (std::string d, size_t s)
+VideoMonitor::parse_output (std::string d, size_t /*s*/)
 {
        std::string line = d;
        std::string::size_type start = 0;
@@ -230,7 +250,7 @@ VideoMonitor::parse_output (std::string d, size_t s)
                        printf("xjadeo: '%s'\n", line.c_str());
                }
 #endif
-               int status = atoi(line.substr(1,3).c_str());
+               int status = atoi(line.substr(1,3));
                switch(status / 100) {
                        case 4: /* errors */
                                if (status == 403) {
@@ -261,30 +281,50 @@ VideoMonitor::parse_output (std::string d, size_t s)
 #if 0 /* DEBUG */
                                        std::cout << "parsed: " << key << " => " << value << std::endl;
 #endif
-                                              if(key ==  "windowpos") {
+                                       if       (key ==  "windowpos") {
+                                               knownstate |= 16;
+                                               if (xjadeo_settings["window xy"] != value) {
+                                                       if (!starting && _session) _session->set_dirty ();
+                                               }
                                                xjadeo_settings["window xy"] = value;
                                        } else if(key ==  "windowsize") {
+                                               knownstate |= 32;
+                                               if (xjadeo_settings["window size"] != value) {
+                                                       if (!starting && _session) _session->set_dirty ();
+                                               }
                                                xjadeo_settings["window size"] = value;
                                        } else if(key ==  "windowontop") {
+                                               knownstate |= 2;
                                                if (starting || xjadeo_settings["window ontop"] != value) {
-                                                       starting &= ~2;
-                                                       if (atoi(value.c_str())) { UiState("xjadeo-window-ontop-on"); }
+                                                       if (!starting && _session) _session->set_dirty ();
+                                                       if (atoi(value)) { UiState("xjadeo-window-ontop-on"); }
                                                        else { UiState("xjadeo-window-ontop-off"); }
+                                                       starting &= ~2;
                                                }
                                                xjadeo_settings["window ontop"] = value;
                                        } else if(key ==  "fullscreen") {
+                                               knownstate |= 4;
                                                if (starting || xjadeo_settings["window zoom"] != value) {
-                                                       starting &= ~4;
-                                                       if (atoi(value.c_str())) { UiState("xjadeo-window-fullscreen-on"); }
+                                                       if (!starting && _session) _session->set_dirty ();
+                                                       if (atoi(value)) { UiState("xjadeo-window-fullscreen-on"); }
                                                        else { UiState("xjadeo-window-fullscreen-off"); }
+                                                       starting &= ~4;
                                                }
                                                xjadeo_settings["window zoom"] = value;
                                        } else if(key ==  "letterbox") {
+                                               knownstate |= 8;
+                                               if (starting || xjadeo_settings["window letterbox"] != value) {
+                                                       if (!starting && _session) _session->set_dirty ();
+                                                       if (atoi(value)) { UiState("xjadeo-window-letterbox-on"); }
+                                                       else { UiState("xjadeo-window-letterbox-off"); }
+                                                       starting &= ~8;
+                                               }
                                                xjadeo_settings["window letterbox"] = value;
                                        } else if(key ==  "osdmode") {
-                                               if (starting || xjadeo_settings["osd mode"] != value) {
-                                                       starting &= ~1;
-                                                       osdmode = atoi(value.c_str());
+                                               knownstate |= 1;
+                                               osdmode = atoi(value);
+                                               if (starting || atoi(xjadeo_settings["osd mode"]) != osdmode) {
+                                                       if (!starting && _session) _session->set_dirty ();
                                                        if ((osdmode & 1) == 1) { UiState("xjadeo-window-osd-frame-on"); }
                                                        if ((osdmode & 1) == 0) { UiState("xjadeo-window-osd-frame-off"); }
                                                        if ((osdmode & 2) == 2) { UiState("xjadeo-window-osd-timecode-on"); }
@@ -292,8 +332,13 @@ VideoMonitor::parse_output (std::string d, size_t s)
                                                        if ((osdmode & 8) == 8) { UiState("xjadeo-window-osd-box-on"); }
                                                        if ((osdmode & 8) == 0) { UiState("xjadeo-window-osd-box-off"); }
                                                }
+                                               starting &= ~1;
                                                xjadeo_settings["osd mode"] = value;
                                        } else if(key ==  "offset") {
+                                               knownstate |= 64;
+                                               if (xjadeo_settings["set offset"] != value) {
+                                                       if (!starting && _session) _session->set_dirty ();
+                                               }
                                                xjadeo_settings["set offset"] = value;
                                        }
                                }
@@ -309,7 +354,6 @@ void
 VideoMonitor::terminated ()
 {
        process->terminate(); // from gui-context clean up
-       save_session();
        Terminated();
 }
 
@@ -317,35 +361,13 @@ void
 VideoMonitor::save_session ()
 {
        if (!_session) { return; }
-       bool is_dirty = false;
-
-       XMLNode* prev = _session->extra_xml (X_("XJSettings"));
-       XMLNode* node = new XMLNode(X_("XJSettings"));
-       XMLNodeList nlist;
-       if (!prev) { is_dirty = true; }
-       else { nlist = prev->children(); }
+       XMLNode* node = _session->extra_xml (X_("XJSettings"));
+       if (!node) return;
 
        for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
          XMLNode* child = node->add_child (X_("XJSetting"));
                child->add_property (X_("k"), it->first);
                child->add_property (X_("v"), it->second);
-               if (!is_dirty) {
-                       bool found = false;
-                       XMLNodeConstIterator i;
-                       for (i = nlist.begin(); i != nlist.end(); ++i) {
-                               if ((*i)->property(X_("k"))->value() == it->first &&
-                                   (*i)->property(X_("v"))->value() == it->second ) {
-                                       found=true;
-                                       break;
-                               }
-                       }
-                       if (!found) {is_dirty = true;}
-               }
-       }
-
-       if (is_dirty) {
-         _session->add_extra_xml (*node);
-         _session->set_dirty ();
        }
 }
 
@@ -427,7 +449,7 @@ VideoMonitor::set_offset (ARDOUR::frameoffset_t offset)
 }
 
 void
-VideoMonitor::manual_seek (framepos_t when, bool force, ARDOUR::frameoffset_t offset)
+VideoMonitor::manual_seek (framepos_t when, bool /*force*/, ARDOUR::frameoffset_t offset)
 {
        if (!is_started()) { return; }
        if (!_session) { return; }
@@ -495,4 +517,3 @@ VideoMonitor::xjadeo_sync_setup ()
                sync_by_manual_seek = my_manual_seek;
        }
 }
-#endif /* WITH_VIDEOTIMELINE */