]> git.sesse.net Git - kdenlive/blob - src/jogshuttle.cpp
Merge branch 'master' into bugfix/jogshuttle
[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
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <linux/input.h>
35
36 #define DELAY 10
37
38 #define KEY 1
39 #define KEY1 256
40 #define KEY2 257
41 #define KEY3 258
42 #define KEY4 259
43 #define KEY5 260
44 #define KEY6 261
45 #define KEY7 262
46 #define KEY8 263
47 #define KEY9 264
48 #define KEY10 265
49 #define KEY11 266
50 #define KEY12 267
51 #define KEY13 268
52 #define KEY14 269
53 #define KEY15 270
54
55 // Constants for the returned events when reading them from the device
56 #define JOGSHUTTLE 2
57 #define JOG 7
58 #define SHUTTLE 8
59
60 // Constants for the signals sent.
61 #define JOG_BACK1 10001
62 #define JOG_FWD1 10002
63 #define KEY_EVENT_OFFSET 20000
64
65 // middle value for shuttle, will be +/-MAX_SHUTTLE_RANGE
66 #define JOG_STOP 10020
67 #define MAX_SHUTTLE_RANGE 7
68
69 void ShuttleThread::init(QObject *parent, QString device)
70 {
71     m_parent = parent;
72     m_device = device;
73     stop_me = false;
74     m_isWorking = false;
75     shuttlevalue = 0xffff;
76     shuttlecounter = 0;
77     jogvalue = 0xffff;
78 }
79
80 bool ShuttleThread::isWorking()
81 {
82     return m_isWorking;
83 }
84
85 void ShuttleThread::run()
86 {
87         kDebug() << "-------  STARTING SHUTTLE: " << m_device;
88         /* open file descriptor */
89         const int fd = KDE_open((char *) m_device.toUtf8().data(), O_RDONLY);
90         if (fd < 0) {
91                 fprintf(stderr, "Can't open Jog Shuttle FILE DESCRIPTOR\n");
92                 return;
93         }
94
95         EV ev;
96
97         if (ioctl(fd, EVIOCGRAB, 1) < 0) {
98                 fprintf(stderr, "Can't get exclusive access on  Jog Shuttle FILE DESCRIPTOR\n");
99                 close(fd);
100                 return;
101         }
102
103         fd_set             readset;
104         struct timeval timeout;
105
106         int num_warnings = 0;
107         int result, iof = -1;
108
109         /* enter thread loop */
110         while (!stop_me) {
111                 /* reset the read set */
112                 FD_ZERO(&readset);
113                 FD_SET(fd, &readset);
114
115                 /* reinit the timeout structure */
116                 timeout.tv_sec  = 0;
117                 timeout.tv_usec = 300000; /* 300 ms */
118
119                 /* do the select */
120                 result = select(fd+1, &readset, NULL, NULL, &timeout);
121
122                 /* see if there was an error or timeout */
123                 if (result < 0) {
124                         // perror("select failed");
125                 } else if (result == 0) {
126                         //puts("TIMEOUT");
127                 } else {
128                         /* we have input */
129                         if (FD_ISSET(fd, &readset)) {
130                                 /* get fd settings */
131                                 if ((iof = fcntl(fd, F_GETFL, 0)) != -1) {
132                                         /* set fd non blocking */
133                                         fcntl(fd, F_SETFL, iof | O_NONBLOCK);
134                                         /* read input */
135                                         if (read(fd, &ev, sizeof(ev)) < 0) {
136                                                 if (num_warnings % 10000 == 0)
137                                                         /* should not happen cause select called before */
138                                                         fprintf(stderr, "Failed to read event from Jog Shuttle FILE DESCRIPTOR (repeated %d times)\n", num_warnings + 1);
139                                                 num_warnings++;
140                                         }
141
142                                         /* restore settings */
143                                         if (iof != -1) {
144                                                 fcntl(fd, F_SETFL, iof);
145                                         }
146                                         /* process event */
147                                         handle_event(ev);
148                                 } else {
149                                         fprintf(stderr, "Can't set Jog Shuttle FILE DESCRIPTOR to O_NONBLOCK\n");
150                                         stop_me = true;
151                                 }
152                         }
153                 }
154         }
155
156         /* close the handle and return thread */
157         close(fd);
158 }
159
160 void ShuttleThread::handle_event(EV ev)
161 {
162     switch (ev.type) {
163     case KEY :
164         key(ev.code, ev.value);
165         break;
166     case JOGSHUTTLE :
167         if (ev.code == JOG)
168             jog(ev.value);
169         if (ev.code == SHUTTLE)
170             shuttle(ev.value);
171         break;
172     }
173 }
174
175 void ShuttleThread::key(unsigned short code, unsigned int value)
176 {
177     if (value == 0) {
178         // Button release (ignored)
179         return;
180     }
181
182     // Check key index
183     code -= KEY1 - 1;
184     if (code > 16)
185         return;
186
187     kDebug() << "Button PRESSED: " << code;
188     QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(KEY_EVENT_OFFSET + code)));
189
190 }
191
192 void ShuttleThread::shuttle(int value)
193 {
194     //gettimeofday( &last_shuttle, 0 );
195     //need_synthetic_shuttle = value != 0;
196
197     if (value == shuttlevalue) {
198         shuttlecounter = 1;
199         return;
200     }
201
202     if (value > MAX_SHUTTLE_RANGE || value < -MAX_SHUTTLE_RANGE) {
203         fprintf(stderr, "Jog Shuttle returned value of %d (should be between -%d ad +%d)", value, MAX_SHUTTLE_RANGE, MAX_SHUTTLE_RANGE);
204         return;
205     }
206     shuttlevalue = value;
207     shuttlecounter = 1;
208     QApplication::postEvent(m_parent, new QEvent((QEvent::Type) (JOG_STOP + value)));
209 }
210
211 void ShuttleThread::jog(unsigned int value)
212 {
213     // generate a synthetic event for the shuttle going
214     // to the home position if we have not seen one recently.
215     //if (shuttlevalue != 0) {
216     //  QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
217     //  shuttlevalue = 0;
218     //}
219
220     // This code takes care of wrapping around the limits of the jog dial number (it is represented as a single byte, hence
221     // wraps at the 0/255 boundary). I used 25 as the difference to make sure that even in heavy load, with the jog dial
222     // turning fast and we miss some events that we do not mistakenly reverse the direction.
223     // Note also that at least the Contour ShuttlePRO v2 does not send an event for the value 0, so at the wrap it will
224     // need 2 events to go forward. But that is nothing we can do about...
225     if (jogvalue != 0xffff) {
226         //fprintf(stderr, "value=%d jogvalue=%d\n", value, jogvalue);
227         bool wrap = abs(value - jogvalue) > 25;
228         bool rewind = value < jogvalue;
229         bool forward = value > jogvalue;
230         if ((rewind && !wrap) || (forward && wrap))
231             QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK1));
232         else if ((forward && !wrap) || (rewind && wrap))
233             QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD1));
234         else if (!forward && !rewind && shuttlecounter > 2) {
235             // An event without changing the jog value is sent after each shuttle change.
236             // As the shuttle rest position does not get a shuttle event, only a non-position-changing jog event.
237             // Hence we stop on this when we see 2 non-position-changing jog events in a row.
238             shuttlecounter = 0;
239             QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
240         }
241     }
242     jogvalue = value;
243     if (shuttlecounter > 0) shuttlecounter++;
244 }
245
246
247 JogShuttle::JogShuttle(QString device, QObject *parent) :
248         QObject(parent)
249 {
250     initDevice(device);
251 }
252
253 JogShuttle::~JogShuttle()
254 {
255         stopDevice();
256 }
257
258 void JogShuttle::initDevice(QString device)
259 {
260     if (m_shuttleProcess.isRunning()) {
261         if (device == m_shuttleProcess.m_device) return;
262         stopDevice();
263     }
264     m_shuttleProcess.init(this, device);
265     m_shuttleProcess.start(QThread::LowestPriority);
266 }
267
268 void JogShuttle::stopDevice()
269 {
270     if (m_shuttleProcess.isRunning()) {
271         /* tell thread to stop */
272         m_shuttleProcess.stop_me = true;
273         m_shuttleProcess.exit();
274         /* give the thread some time (ms) to shutdown */
275         m_shuttleProcess.wait(600);
276
277         /* if still running - do it in the hardcore way */
278         if (m_shuttleProcess.isRunning()) {
279                 m_shuttleProcess.terminate();
280             kDebug() << "/// terminate jogshuttle process\n";
281         }
282     }
283 }
284
285 void JogShuttle::customEvent(QEvent* e)
286 {
287     int code = e->type();
288
289     // Handle simple job events
290     switch (code) {
291     case JOG_BACK1:
292         emit jogBack();
293         return;
294     case JOG_FWD1:
295         emit jogForward();
296         return;
297     }
298
299     int shuttle_pos = code - JOG_STOP;
300     if (shuttle_pos >= -MAX_SHUTTLE_RANGE && shuttle_pos <= MAX_SHUTTLE_RANGE) {
301         emit shuttlePos(shuttle_pos);
302         return;
303     }
304
305     // we've got a key event.
306     //fprintf(stderr, "Firing button event for #%d\n", e->type() - KEY_EVENT_OFFSET); // DBG
307     emit button(e->type() - KEY_EVENT_OFFSET);
308 }
309
310
311
312 // #include "jogshuttle.moc"
313