1 /***************************************************************************
2 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
3 * Based on code by Arendt David <admin@prnet.org> *
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. *
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. *
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 ***************************************************************************/
22 #include "jogshuttle.h"
27 #include <QApplication>
37 #include <linux/input.h>
58 // Constants for the returned events when reading them from the device
63 // Constants for the signals sent.
64 #define JOG_BACK1 10001
65 #define JOG_FWD1 10002
66 #define KEY_EVENT_OFFSET 20000
68 // middle value for shuttle, will be +/-MAX_SHUTTLE_RANGE
69 #define JOG_STOP 10020
70 #define MAX_SHUTTLE_RANGE 7
72 void ShuttleThread::init(QObject *parent, const QString &device)
78 shuttlevalue = 0xffff;
83 bool ShuttleThread::isWorking()
88 void ShuttleThread::run()
90 kDebug() << "------- STARTING SHUTTLE: " << m_device;
92 media_ctrl_open2(&mc, m_device.toUtf8().data());
94 /* open file descriptor */
95 //const int fd = KDE_open((char *) m_device.toUtf8().data(), O_RDONLY);
97 perror("Can't open Jog Shuttle FILE DESCRIPTOR");
103 if (ioctl(fd, EVIOCGRAB, 1) < 0) {
104 fprintf(stderr, "Can't get exclusive access on Jog Shuttle FILE DESCRIPTOR\n");
111 struct timeval timeout;
113 int num_warnings = 0, readResult = 0;
114 int result, iof = -1;
116 /* get fd settings */
117 if ((iof = fcntl(fd, F_GETFL, 0)) == -1) {
118 fprintf(stderr, "Can't get Jog Shuttle file status\n");
124 else if (fcntl(fd, F_SETFL, iof | O_NONBLOCK) == -1) {
125 fprintf(stderr, "Can't set Jog Shuttle FILE DESCRIPTOR to O_NONBLOCK and stop thread\n");
130 struct media_ctrl_event mev;
132 /* enter thread loop */
134 /* reset the read set */
136 FD_SET(mc.fd, &readset);
138 /* reinit the timeout structure */
140 timeout.tv_usec = 400000; /* 400 ms */
142 /* do select in blocked mode and wake up after timeout for eval stop_me */
143 result = select(mc.fd+1, &readset, NULL, NULL, &timeout);
145 /* see if there was an error or timeout else process event */
146 if (result < 0 && errno == EINTR) {
147 /* EINTR event catched. This is not a problem - continue processing */
148 kDebug() << strerror(errno) << "\n";
149 /* continue processing */
151 } else if (result < 0) {
154 kDebug() << strerror(errno) << "\n";
155 } else if (result == 0) {
156 /* do nothing. reserved for future purposes */
159 if (FD_ISSET(mc.fd, &readset)) {
161 mev.type = MEDIA_CTRL_EVENT_NONE;
162 //readResult = read(fd, ev, sizeof(EV) * 64);
163 media_ctrl_read_event(&mc, &mev);
167 if (readResult < 0) {
168 if (num_warnings % 10000 == 0) {
169 /* if device is not available anymore - dead or disconnected */
170 fprintf(stderr, "Failed to read event from Jog Shuttle FILE DESCRIPTOR (repeated %d times)\n", num_warnings + 1);
172 /* exit if device is not available or the error occurs to long */
173 if (errno == ENODEV) {
174 perror("Failed to read from Jog Shuttle FILE DESCRIPTOR. Stop thread");
177 } else if (num_warnings > 1000000) {
178 perror("Failed to read from Jog Shuttle FILE DESCRIPTOR. Limit reached. Stop thread");
183 } else if (readResult < (int)sizeof(EV)) {
184 fprintf(stderr, "readResult < (int)sizeof(EV)\n");
187 } else if (readResult % (int)sizeof(EV) != 0) {
188 fprintf(stderr, "readResult mod (int)sizeof(EV) != 0\n");
192 for (int i = 0; i < readResult / (int)sizeof(EV); i++) {
202 kDebug() << "------- STOPPING SHUTTLE: ";
203 /* close the handle and return thread */
205 media_ctrl_close(&mc);
208 void ShuttleThread::handle_event(struct media_ctrl_event ev)
210 if (ev.type == MEDIA_CTRL_EVENT_KEY)
212 else if (ev.type == MEDIA_CTRL_EVENT_JOG)
214 else if (ev.type == MEDIA_CTRL_EVENT_SHUTTLE)
218 void ShuttleThread::handle_event(EV ev)
222 key(ev.code, ev.value);
227 if (ev.code == SHUTTLE)
233 void ShuttleThread::key(struct media_ctrl_event ev)
235 if (ev.value == KEY_PRESS) {
236 int code = ev.index + 1;
237 QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(KEY_EVENT_OFFSET + code)));
241 void ShuttleThread::key(unsigned short code, unsigned int value)
244 // Button release (ignored)
253 //kDebug() << "Button PRESSED: " << code;
254 QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(KEY_EVENT_OFFSET + code)));
258 void ShuttleThread::shuttle(struct media_ctrl_event ev)
260 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) (JOG_STOP + (ev.value/2))));
263 void ShuttleThread::shuttle(int value)
265 //gettimeofday( &last_shuttle, 0 );
266 //need_synthetic_shuttle = value != 0;
268 if (value == shuttlevalue) {
273 if (value > MAX_SHUTTLE_RANGE || value < -MAX_SHUTTLE_RANGE) {
274 fprintf(stderr, "Jog Shuttle returned value of %d (should be between -%d ad +%d)", value, MAX_SHUTTLE_RANGE, MAX_SHUTTLE_RANGE);
277 shuttlevalue = value;
279 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) (JOG_STOP + value)));
282 void ShuttleThread::jog(struct media_ctrl_event ev)
285 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK1));
286 else if (ev.value > 0)
287 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD1));
290 void ShuttleThread::jog(unsigned int value)
292 // generate a synthetic event for the shuttle going
293 // to the home position if we have not seen one recently.
294 //if (shuttlevalue != 0) {
295 // QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
299 // This code takes care of wrapping around the limits of the jog dial number (it is represented as a single byte, hence
300 // wraps at the 0/255 boundary). I used 25 as the difference to make sure that even in heavy load, with the jog dial
301 // turning fast and we miss some events that we do not mistakenly reverse the direction.
302 // Note also that at least the Contour ShuttlePRO v2 does not send an event for the value 0, so at the wrap it will
303 // need 2 events to go forward. But that is nothing we can do about...
304 if (jogvalue != 0xffff) {
305 //fprintf(stderr, "value=%d jogvalue=%d\n", value, jogvalue);
306 bool wrap = abs(value - jogvalue) > 25;
307 bool rewind = value < jogvalue;
308 bool forward = value > jogvalue;
309 if ((rewind && !wrap) || (forward && wrap))
310 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK1));
311 else if ((forward && !wrap) || (rewind && wrap))
312 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD1));
313 else if (!forward && !rewind && shuttlecounter > 2) {
314 // An event without changing the jog value is sent after each shuttle change.
315 // As the shuttle rest position does not get a shuttle event, only a non-position-changing jog event.
316 // Hence we stop on this when we see 2 non-position-changing jog events in a row.
318 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
322 if (shuttlecounter > 0) shuttlecounter++;
326 JogShuttle::JogShuttle(const QString &device, QObject *parent) :
332 JogShuttle::~JogShuttle()
337 void JogShuttle::initDevice(const QString &device)
339 if (m_shuttleProcess.isRunning()) {
340 if (device == m_shuttleProcess.m_device) return;
343 m_shuttleProcess.init(this, device);
344 m_shuttleProcess.start(QThread::LowestPriority);
347 void JogShuttle::stopDevice()
349 if (m_shuttleProcess.isRunning()) {
350 /* tell thread to stop */
351 m_shuttleProcess.stop_me = true;
352 m_shuttleProcess.exit();
353 /* give the thread some time (ms) to shutdown */
354 m_shuttleProcess.wait(600);
356 /* if still running - do it in the hardcore way */
357 if (m_shuttleProcess.isRunning()) {
358 m_shuttleProcess.terminate();
359 kDebug() << "/// terminate jogshuttle process\n";
364 void JogShuttle::customEvent(QEvent* e)
366 int code = e->type();
368 // Handle simple job events
378 int shuttle_pos = code - JOG_STOP;
379 if (shuttle_pos >= -MAX_SHUTTLE_RANGE && shuttle_pos <= MAX_SHUTTLE_RANGE) {
380 emit shuttlePos(shuttle_pos);
384 // we've got a key event.
385 //fprintf(stderr, "Firing button event for #%d\n", e->type() - KEY_EVENT_OFFSET); // DBG
386 emit button(e->type() - KEY_EVENT_OFFSET);
389 QString JogShuttle::enumerateDevice(const QString& device)
391 return QDir(device).canonicalPath();
394 DeviceMap JogShuttle::enumerateDevices(const QString& devPath)
397 QDir devDir(devPath);
399 if (!devDir.exists()) {
403 QStringList fileList = devDir.entryList(QDir::System | QDir::Files);
404 foreach (const QString &fileName, fileList) {
405 QString devFullPath = devDir.absoluteFilePath(fileName);
406 QString fileLink = JogShuttle::enumerateDevice(devFullPath);
407 kDebug() << QString(" [%1] ").arg(fileName);
408 kDebug() << QString(" [%1] ").arg(fileLink);
410 struct media_ctrl mc;
411 media_ctrl_open2(&mc, (char*)fileLink.toUtf8().data());
412 if (mc.fd > 0 && mc.device) {
413 devs.insert(QString(mc.device->name), devFullPath);
415 media_ctrl_close(&mc);
422 // #include "jogshuttle.moc"
425 #include "jogshuttle.moc"