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