]> git.sesse.net Git - kdenlive/blob - src/jogshuttle.cpp
6982222609671c881621a66b15b597ff9310d2d2
[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         struct media_ctrl mc;
92
93     // open device
94     media_ctrl_open2(&mc, m_device.toUtf8().data());
95
96     // on failure return
97     if (mc.fd < 0) {
98         perror("Can't open Jog Shuttle FILE DESCRIPTOR");
99         return;
100     }
101
102     // init
103     int result;
104         fd_set readset;
105         struct timeval timeout;
106
107         /* enter thread loop */
108         while (!stop_me) {
109                 /* reset the read set */
110                 FD_ZERO(&readset);
111                 FD_SET(mc.fd, &readset);
112
113                 // reinit the timeout structure
114                 timeout.tv_sec  = 0;
115                 timeout.tv_usec = 400000; /* 400 ms */
116
117                 // do select in blocked mode and wake up after timeout
118                 // for stop_me evaluation
119                 result = select(mc.fd + 1, &readset, NULL, NULL, &timeout);
120
121                 /* see if there was an error or timeout else process event */
122                 if (result < 0 && errno == EINTR) {
123                         // EINTR event catched. This is not a problem - continue processing
124                         kDebug() << strerror(errno) << "\n";
125                         // continue processing
126                         continue;
127                 } else if (result < 0) {
128                         /* stop thread */
129                         stop_me = true;
130                         kDebug() << strerror(errno) << "\n";
131                 } else if (result > 0) {
132                         // we have input
133                         if (FD_ISSET(mc.fd, &readset)) {
134                             struct media_ctrl_event mev;
135                             mev.type = MEDIA_CTRL_EVENT_NONE;
136                 // read input
137                                 media_ctrl_read_event(&mc, &mev);
138                 // process event
139                 handle_event(mev);
140                         }
141                 } else if (result == 0) {
142                     // on timeout. let it here for debug
143                 }
144         }
145
146     kDebug() << "-------  STOPPING SHUTTLE: ";
147         /* close the handle and return thread */
148     media_ctrl_close(&mc);
149 }
150
151 void ShuttleThread::handle_event(struct media_ctrl_event ev)
152 {
153     if (ev.type == MEDIA_CTRL_EVENT_KEY)
154         key(ev);
155     else if (ev.type == MEDIA_CTRL_EVENT_JOG)
156         jog(ev);
157     else if (ev.type == MEDIA_CTRL_EVENT_SHUTTLE)
158         shuttle(ev);
159 }
160
161 void ShuttleThread::key(struct media_ctrl_event ev)
162 {
163     if (ev.value == KEY_PRESS) {
164         int code = ev.index + 1;
165         QApplication::postEvent(m_parent,
166             new QEvent((QEvent::Type)(KEY_EVENT_OFFSET + code)));
167     }
168 }
169
170 void ShuttleThread::shuttle(struct media_ctrl_event ev)
171 {
172     int value = ev.value / 2;
173
174     if (value > MAX_SHUTTLE_RANGE || value < -MAX_SHUTTLE_RANGE) {
175         kDebug() << "Jog shuttle value is out of range: " << MAX_SHUTTLE_RANGE;
176         return;
177     }
178
179     QApplication::postEvent(m_parent,
180         new QEvent((QEvent::Type) (JOG_STOP + (value))));
181 }
182
183 void ShuttleThread::jog(struct media_ctrl_event ev)
184 {
185     if (ev.value < 0)
186         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK1));
187     else if (ev.value > 0)
188         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD1));
189 }
190
191 void ShuttleThread::handle_event(EV ev)
192 {
193     switch (ev.type) {
194     case KEY :
195         key(ev.code, ev.value);
196         break;
197     case JOGSHUTTLE :
198         if (ev.code == JOG)
199             jog(ev.value);
200         if (ev.code == SHUTTLE)
201             shuttle(ev.value);
202         break;
203     }
204 }
205
206 void ShuttleThread::key(unsigned short code, unsigned int value)
207 {
208     if (value == 0) {
209         // Button release (ignored)
210         return;
211     }
212
213     // Check key index
214     code -= KEY1 - 1;
215     if (code > 16)
216         return;
217
218     //kDebug() << "Button PRESSED: " << code;
219     QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(KEY_EVENT_OFFSET + code)));
220
221 }
222
223 void ShuttleThread::shuttle(int value)
224 {
225     //gettimeofday( &last_shuttle, 0 );
226     //need_synthetic_shuttle = value != 0;
227
228     if (value == shuttlevalue) {
229         shuttlecounter = 1;
230         return;
231     }
232
233     if (value > MAX_SHUTTLE_RANGE || value < -MAX_SHUTTLE_RANGE) {
234         fprintf(stderr, "Jog Shuttle returned value of %d (should be between -%d ad +%d)", value, MAX_SHUTTLE_RANGE, MAX_SHUTTLE_RANGE);
235         return;
236     }
237     shuttlevalue = value;
238     shuttlecounter = 1;
239     QApplication::postEvent(m_parent, new QEvent((QEvent::Type) (JOG_STOP + value)));
240 }
241
242 void ShuttleThread::jog(unsigned int value)
243 {
244     // generate a synthetic event for the shuttle going
245     // to the home position if we have not seen one recently.
246     //if (shuttlevalue != 0) {
247     //  QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
248     //  shuttlevalue = 0;
249     //}
250
251     // This code takes care of wrapping around the limits of the jog dial number (it is represented as a single byte, hence
252     // wraps at the 0/255 boundary). I used 25 as the difference to make sure that even in heavy load, with the jog dial
253     // turning fast and we miss some events that we do not mistakenly reverse the direction.
254     // Note also that at least the Contour ShuttlePRO v2 does not send an event for the value 0, so at the wrap it will
255     // need 2 events to go forward. But that is nothing we can do about...
256     if (jogvalue != 0xffff) {
257         //fprintf(stderr, "value=%d jogvalue=%d\n", value, jogvalue);
258         bool wrap = abs(value - jogvalue) > 25;
259         bool rewind = value < jogvalue;
260         bool forward = value > jogvalue;
261         if ((rewind && !wrap) || (forward && wrap))
262             QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK1));
263         else if ((forward && !wrap) || (rewind && wrap))
264             QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD1));
265         else if (!forward && !rewind && shuttlecounter > 2) {
266             // An event without changing the jog value is sent after each shuttle change.
267             // As the shuttle rest position does not get a shuttle event, only a non-position-changing jog event.
268             // Hence we stop on this when we see 2 non-position-changing jog events in a row.
269             shuttlecounter = 0;
270             QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
271         }
272     }
273     jogvalue = value;
274     if (shuttlecounter > 0) shuttlecounter++;
275 }
276
277
278 JogShuttle::JogShuttle(const QString &device, QObject *parent) :
279         QObject(parent)
280 {
281     initDevice(device);
282 }
283
284 JogShuttle::~JogShuttle()
285 {
286         stopDevice();
287 }
288
289 void JogShuttle::initDevice(const QString &device)
290 {
291     if (m_shuttleProcess.isRunning()) {
292         if (device == m_shuttleProcess.m_device)
293             return;
294
295         stopDevice();
296     }
297
298     m_shuttleProcess.init(this, device);
299     m_shuttleProcess.start(QThread::LowestPriority);
300 }
301
302 void JogShuttle::stopDevice()
303 {
304     if (m_shuttleProcess.isRunning()) {
305         /* tell thread to stop */
306         m_shuttleProcess.stop_me = true;
307         m_shuttleProcess.exit();
308         /* give the thread some time (ms) to shutdown */
309         m_shuttleProcess.wait(600);
310
311         /* if still running - do it in the hardcore way */
312         if (m_shuttleProcess.isRunning()) {
313                 m_shuttleProcess.terminate();
314             kDebug() << "/// terminate jogshuttle process\n";
315         }
316     }
317 }
318
319 void JogShuttle::customEvent(QEvent* e)
320 {
321     int code = e->type();
322
323     // handle simple job events
324     switch (code) {
325         case JOG_BACK1:
326             emit jogBack();
327             return;
328         case JOG_FWD1:
329             emit jogForward();
330             return;
331     }
332
333     // FIXME: this is a bad shuttle indication
334     int shuttle_pos = code - JOG_STOP;
335     if (shuttle_pos >= -MAX_SHUTTLE_RANGE && shuttle_pos <= MAX_SHUTTLE_RANGE) {
336         emit shuttlePos(shuttle_pos);
337         return;
338     }
339
340     // we've got a key event.
341     emit button(e->type() - KEY_EVENT_OFFSET);
342 }
343
344 QString JogShuttle::canonicalDevice(const QString& device)
345 {
346     return QDir(device).canonicalPath();
347 }
348
349 DeviceMap JogShuttle::enumerateDevices(const QString& devPath)
350 {
351     DeviceMap devs;
352     QDir devDir(devPath);
353
354     if (!devDir.exists()) {
355         return devs;
356     }
357
358     QStringList fileList = devDir.entryList(QDir::System | QDir::Files);
359     foreach (const QString &fileName, fileList) {
360         QString devFullPath = devDir.absoluteFilePath(fileName);
361         QString fileLink = JogShuttle::canonicalDevice(devFullPath);
362         kDebug() << QString(" [%1] ").arg(fileName);
363         kDebug() << QString(" [%1] ").arg(fileLink);
364
365         struct media_ctrl mc;
366         media_ctrl_open2(&mc, (char*)fileLink.toUtf8().data());
367         if (mc.fd > 0 && mc.device) {
368             devs.insert(QString(mc.device->name), devFullPath);
369             kDebug() <<  QString(" [keys-count=%1] ").arg(
370                     media_ctrl_get_keys_count(&mc));
371         }
372         media_ctrl_close(&mc);
373     }
374
375     return devs;
376 }
377
378 int JogShuttle::keysCount(const QString& devPath)
379 {
380     struct media_ctrl mc;
381     int keysCount = 0;
382
383     QString fileLink = canonicalDevice(devPath);
384     media_ctrl_open2(&mc, (char*)fileLink.toUtf8().data());
385     if (mc.fd > 0 && mc.device) {
386         keysCount = media_ctrl_get_keys_count(&mc);
387     }
388
389     return keysCount;
390 }
391
392 // #include "jogshuttle.moc"
393
394
395 #include "jogshuttle.moc"