]> git.sesse.net Git - kdenlive/blobdiff - src/jogshuttle.cpp
Revert "try to find solution for ShuttlePro V2 problem"
[kdenlive] / src / jogshuttle.cpp
index fd32153026a4425da4ff97612060d16aad057886..362886362f2cd4a25e2fe9fe29aa5256cdaa70f6 100644 (file)
@@ -31,6 +31,8 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <errno.h>
+#include <string.h>
 #include <linux/input.h>
 
 #define DELAY 10
@@ -88,7 +90,7 @@ void ShuttleThread::run()
        /* open file descriptor */
        const int fd = KDE_open((char *) m_device.toUtf8().data(), O_RDONLY);
        if (fd < 0) {
-               fprintf(stderr, "Can't open Jog Shuttle FILE DESCRIPTOR\n");
+               perror("Can't open Jog Shuttle FILE DESCRIPTOR");
                return;
        }
 
@@ -114,16 +116,19 @@ void ShuttleThread::run()
 
                /* reinit the timeout structure */
                timeout.tv_sec  = 0;
-               timeout.tv_usec = 300000; /* 300 ms */
+               timeout.tv_usec = 400000; /* 400 ms */
 
-               /* do the select */
+               /* do select in blocked mode and wake up after timeout for eval stop_me */
                result = select(fd+1, &readset, NULL, NULL, &timeout);
 
-               /* see if there was an error or timeout */
+               /* see if there was an error or timeout else process event */
                if (result < 0) {
-                       // perror("select failed");
+                       /* usually this is a condition for exit but EINTR error is sometimes
+                        * thrown under special circumstances. I think its ok to ignore this
+                        * if its not too often */
+                       kDebug() << strerror(errno) << "\n";
                } else if (result == 0) {
-                       //puts("TIMEOUT");
+                       /* do nothing. reserved for future purposes */
                } else {
                        /* we have input */
                        if (FD_ISSET(fd, &readset)) {
@@ -133,12 +138,22 @@ void ShuttleThread::run()
                                        fcntl(fd, F_SETFL, iof | O_NONBLOCK);
                                        /* read input */
                                        if (read(fd, &ev, sizeof(ev)) < 0) {
-                                               if (num_warnings % 10000 == 0)
-                                                       /* should not happen cause select called before */
+                                               if (num_warnings % 10000 == 0) {
+                                                       /* if device is not available anymore - dead or disconnected */
                                                        fprintf(stderr, "Failed to read event from Jog Shuttle FILE DESCRIPTOR (repeated %d times)\n", num_warnings + 1);
+                                               }
+                                               /* exit if device is not available or the error occurs to long */
+                                               if (errno == ENODEV) {
+                                                       perror("Failed to read from Jog Shuttle FILE DESCRIPTOR. Stop thread");
+                                                       /* stop thread */
+                                                       stop_me = true;
+                                               } else if (num_warnings > 1000000) {
+                                                       perror("Failed to read from Jog Shuttle FILE DESCRIPTOR. Limit reached. Stop thread");
+                                                       /* stop thread */
+                                                       stop_me = true;
+                                               }
                                                num_warnings++;
                                        }
-
                                        /* restore settings */
                                        if (iof != -1) {
                                                fcntl(fd, F_SETFL, iof);
@@ -146,7 +161,7 @@ void ShuttleThread::run()
                                        /* process event */
                                        handle_event(ev);
                                } else {
-                                       fprintf(stderr, "Can't set Jog Shuttle FILE DESCRIPTOR to O_NONBLOCK\n");
+                                       fprintf(stderr, "Can't set Jog Shuttle FILE DESCRIPTOR to O_NONBLOCK and stop thread\n");
                                        stop_me = true;
                                }
                        }
@@ -184,7 +199,7 @@ void ShuttleThread::key(unsigned short code, unsigned int value)
     if (code > 16)
         return;
 
-    kDebug() << "Button PRESSED: " << code;
+    //kDebug() << "Button PRESSED: " << code;
     QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(KEY_EVENT_OFFSET + code)));
 
 }