]> git.sesse.net Git - kdenlive/blob - src/jogshuttle.cpp
jogshuttle: add support for shuttles on newer systems - part1
[kdenlive] / src / jogshuttle.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *   Based on code by Arendt David <admin@prnet.org>                       *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
19  ***************************************************************************/
20
21
22 #include "jogshuttle.h"
23
24 #include <KDebug>
25 #include <kde_file.h>
26
27 #include <QApplication>
28 #include <QEvent>
29 #include <QDir>
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <linux/input.h>
38
39 #define DELAY 10
40
41 #define KEY 1
42 #define KEY1 256
43 #define KEY2 257
44 #define KEY3 258
45 #define KEY4 259
46 #define KEY5 260
47 #define KEY6 261
48 #define KEY7 262
49 #define KEY8 263
50 #define KEY9 264
51 #define KEY10 265
52 #define KEY11 266
53 #define KEY12 267
54 #define KEY13 268
55 #define KEY14 269
56 #define KEY15 270
57
58 // Constants for the returned events when reading them from the device
59 #define JOGSHUTTLE 2
60 #define JOG 7
61 #define SHUTTLE 8
62
63 // Constants for the signals sent.
64 #define JOG_BACK1 10001
65 #define JOG_FWD1 10002
66 #define KEY_EVENT_OFFSET 20000
67
68 // middle value for shuttle, will be +/-MAX_SHUTTLE_RANGE
69 #define JOG_STOP 10020
70 #define MAX_SHUTTLE_RANGE 7
71
72 void ShuttleThread::init(QObject *parent, const QString &device)
73 {
74     m_parent = parent;
75     m_device = device;
76     stop_me = false;
77     m_isWorking = false;
78     shuttlevalue = 0xffff;
79     shuttlecounter = 0;
80     jogvalue = 0xffff;
81 }
82
83 bool ShuttleThread::isWorking()
84 {
85     return m_isWorking;
86 }
87
88 void ShuttleThread::run()
89 {
90         kDebug() << "-------  STARTING SHUTTLE: " << m_device;
91
92         /* open file descriptor */
93         const int fd = KDE_open((char *) m_device.toUtf8().data(), O_RDONLY);
94         if (fd < 0) {
95                 perror("Can't open Jog Shuttle FILE DESCRIPTOR");
96                 return;
97         }
98
99         EV ev[64];
100
101         if (ioctl(fd, EVIOCGRAB, 1) < 0) {
102                 fprintf(stderr, "Can't get exclusive access on  Jog Shuttle FILE DESCRIPTOR\n");
103                 close(fd);
104                 return;
105         }
106
107         fd_set             readset;
108         struct timeval timeout;
109
110         int num_warnings = 0, readResult = 0;
111         int result, iof = -1;
112
113         /* get fd settings */
114     if ((iof = fcntl(fd, F_GETFL, 0)) == -1) {
115         fprintf(stderr, "Can't get Jog Shuttle file status\n");
116         close(fd);
117         return;
118     }
119
120 #if 0
121     else if (fcntl(fd, F_SETFL, iof | O_NONBLOCK) == -1) {
122         fprintf(stderr, "Can't set Jog Shuttle FILE DESCRIPTOR to O_NONBLOCK and stop thread\n");
123         close(fd);
124         return;
125     }
126 #endif
127
128         /* enter thread loop */
129         while (!stop_me) {
130                 /* reset the read set */
131                 FD_ZERO(&readset);
132                 FD_SET(fd, &readset);
133
134                 /* reinit the timeout structure */
135                 timeout.tv_sec  = 0;
136                 timeout.tv_usec = 400000; /* 400 ms */
137
138                 /* do select in blocked mode and wake up after timeout for eval stop_me */
139                 result = select(fd+1, &readset, NULL, NULL, &timeout);
140
141                 /* see if there was an error or timeout else process event */
142                 if (result < 0 && errno == EINTR) {
143                         /* EINTR event catched. This is not a problem - continue processing */
144                         kDebug() << strerror(errno) << "\n";
145                         /* continue processing */
146                         continue;
147                 } else if (result < 0) {
148                         /* stop thread */
149                         stop_me = true;
150                         kDebug() << strerror(errno) << "\n";
151                 } else if (result == 0) {
152                         /* do nothing. reserved for future purposes */
153                 } else {
154                         /* we have input */
155                         if (FD_ISSET(fd, &readset)) {
156                                 /* read input */
157                                 readResult = read(fd, ev, sizeof(EV) * 64);
158                                 if (readResult < 0) {
159                                         if (num_warnings % 10000 == 0) {
160                                                 /* if device is not available anymore - dead or disconnected */
161                                                 fprintf(stderr, "Failed to read event from Jog Shuttle FILE DESCRIPTOR (repeated %d times)\n", num_warnings + 1);
162                                         }
163                                         /* exit if device is not available or the error occurs to long */
164                                         if (errno == ENODEV) {
165                                                 perror("Failed to read from Jog Shuttle FILE DESCRIPTOR. Stop thread");
166                                                 /* stop thread */
167                                                 stop_me = true;
168                                         } else if (num_warnings > 1000000) {
169                                                 perror("Failed to read from Jog Shuttle FILE DESCRIPTOR. Limit reached. Stop thread");
170                                                 /* stop thread */
171                                                 stop_me = true;
172                                         }
173                                         num_warnings++;
174                                 } else if (readResult < (int)sizeof(EV)) {
175                                         fprintf(stderr, "readResult < (int)sizeof(EV)\n");
176                                         /* stop thread */
177                                         stop_me = true;
178                                 } else if (readResult % (int)sizeof(EV) != 0) {
179                                         fprintf(stderr, "readResult mod (int)sizeof(EV) != 0\n");
180                                         /* stop thread */
181                                         stop_me = true;
182                                 } else {
183                                         for (int i = 0; i < readResult / (int)sizeof(EV); i++) {
184                                                 /* process event */
185                                                 handle_event(ev[i]);
186                                         }
187                                 }
188                         }
189                 }
190         }
191
192     kDebug() << "-------  STOPPING SHUTTLE: ";
193         /* close the handle and return thread */
194         close(fd);
195 }
196
197 void ShuttleThread::handle_event(EV ev)
198 {
199     switch (ev.type) {
200     case KEY :
201         key(ev.code, ev.value);
202         break;
203     case JOGSHUTTLE :
204         if (ev.code == JOG)
205             jog(ev.value);
206         if (ev.code == SHUTTLE)
207             shuttle(ev.value);
208         break;
209     }
210 }
211
212 void ShuttleThread::key(unsigned short code, unsigned int value)
213 {
214     if (value == 0) {
215         // Button release (ignored)
216         return;
217     }
218
219     // Check key index
220     code -= KEY1 - 1;
221     if (code > 16)
222         return;
223
224     //kDebug() << "Button PRESSED: " << code;
225     QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(KEY_EVENT_OFFSET + code)));
226
227 }
228
229 void ShuttleThread::shuttle(int value)
230 {
231     //gettimeofday( &last_shuttle, 0 );
232     //need_synthetic_shuttle = value != 0;
233
234     if (value == shuttlevalue) {
235         shuttlecounter = 1;
236         return;
237     }
238
239     if (value > MAX_SHUTTLE_RANGE || value < -MAX_SHUTTLE_RANGE) {
240         fprintf(stderr, "Jog Shuttle returned value of %d (should be between -%d ad +%d)", value, MAX_SHUTTLE_RANGE, MAX_SHUTTLE_RANGE);
241         return;
242     }
243     shuttlevalue = value;
244     shuttlecounter = 1;
245     QApplication::postEvent(m_parent, new QEvent((QEvent::Type) (JOG_STOP + value)));
246 }
247
248 void ShuttleThread::jog(unsigned int value)
249 {
250     // generate a synthetic event for the shuttle going
251     // to the home position if we have not seen one recently.
252     //if (shuttlevalue != 0) {
253     //  QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
254     //  shuttlevalue = 0;
255     //}
256
257     // This code takes care of wrapping around the limits of the jog dial number (it is represented as a single byte, hence
258     // wraps at the 0/255 boundary). I used 25 as the difference to make sure that even in heavy load, with the jog dial
259     // turning fast and we miss some events that we do not mistakenly reverse the direction.
260     // Note also that at least the Contour ShuttlePRO v2 does not send an event for the value 0, so at the wrap it will
261     // need 2 events to go forward. But that is nothing we can do about...
262     if (jogvalue != 0xffff) {
263         //fprintf(stderr, "value=%d jogvalue=%d\n", value, jogvalue);
264         bool wrap = abs(value - jogvalue) > 25;
265         bool rewind = value < jogvalue;
266         bool forward = value > jogvalue;
267         if ((rewind && !wrap) || (forward && wrap))
268             QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK1));
269         else if ((forward && !wrap) || (rewind && wrap))
270             QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD1));
271         else if (!forward && !rewind && shuttlecounter > 2) {
272             // An event without changing the jog value is sent after each shuttle change.
273             // As the shuttle rest position does not get a shuttle event, only a non-position-changing jog event.
274             // Hence we stop on this when we see 2 non-position-changing jog events in a row.
275             shuttlecounter = 0;
276             QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
277         }
278     }
279     jogvalue = value;
280     if (shuttlecounter > 0) shuttlecounter++;
281 }
282
283
284 JogShuttle::JogShuttle(const QString &device, QObject *parent) :
285         QObject(parent)
286 {
287     initDevice(device);
288 }
289
290 JogShuttle::~JogShuttle()
291 {
292         stopDevice();
293 }
294
295 void JogShuttle::initDevice(const QString &device)
296 {
297     if (m_shuttleProcess.isRunning()) {
298         if (device == m_shuttleProcess.m_device) return;
299         stopDevice();
300     }
301     m_shuttleProcess.init(this, device);
302     m_shuttleProcess.start(QThread::LowestPriority);
303 }
304
305 void JogShuttle::stopDevice()
306 {
307     if (m_shuttleProcess.isRunning()) {
308         /* tell thread to stop */
309         m_shuttleProcess.stop_me = true;
310         m_shuttleProcess.exit();
311         /* give the thread some time (ms) to shutdown */
312         m_shuttleProcess.wait(600);
313
314         /* if still running - do it in the hardcore way */
315         if (m_shuttleProcess.isRunning()) {
316                 m_shuttleProcess.terminate();
317             kDebug() << "/// terminate jogshuttle process\n";
318         }
319     }
320 }
321
322 void JogShuttle::customEvent(QEvent* e)
323 {
324     int code = e->type();
325
326     // Handle simple job events
327     switch (code) {
328     case JOG_BACK1:
329         emit jogBack();
330         return;
331     case JOG_FWD1:
332         emit jogForward();
333         return;
334     }
335
336     int shuttle_pos = code - JOG_STOP;
337     if (shuttle_pos >= -MAX_SHUTTLE_RANGE && shuttle_pos <= MAX_SHUTTLE_RANGE) {
338         emit shuttlePos(shuttle_pos);
339         return;
340     }
341
342     // we've got a key event.
343     //fprintf(stderr, "Firing button event for #%d\n", e->type() - KEY_EVENT_OFFSET); // DBG
344     emit button(e->type() - KEY_EVENT_OFFSET);
345 }
346
347 QString JogShuttle::enumerateDevice(const QString &device)
348 {
349     QDir canonDir(device);
350     //return QDir::canonicalPath(device);
351     return canonDir.canonicalPath();
352 }
353
354
355 // #include "jogshuttle.moc"
356
357
358 #include "jogshuttle.moc"