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>
36 #include <linux/input.h>
57 // Constants for the returned events when reading them from the device
62 // Constants for the signals sent.
63 #define JOG_BACK1 10001
64 #define JOG_FWD1 10002
65 #define KEY_EVENT_OFFSET 20000
67 // middle value for shuttle, will be +/-MAX_SHUTTLE_RANGE
68 #define JOG_STOP 10020
69 #define MAX_SHUTTLE_RANGE 7
71 void ShuttleThread::init(QObject *parent, const QString &device)
77 shuttlevalue = 0xffff;
82 bool ShuttleThread::isWorking()
87 void ShuttleThread::run()
89 kDebug() << "------- STARTING SHUTTLE: " << m_device;
91 /* open file descriptor */
92 const int fd = KDE_open((char *) m_device.toUtf8().data(), O_RDONLY);
94 perror("Can't open Jog Shuttle FILE DESCRIPTOR");
100 if (ioctl(fd, EVIOCGRAB, 1) < 0) {
101 fprintf(stderr, "Can't get exclusive access on Jog Shuttle FILE DESCRIPTOR\n");
107 struct timeval timeout;
109 int num_warnings = 0, readResult = 0;
110 int result, iof = -1;
112 /* get fd settings */
113 if ((iof = fcntl(fd, F_GETFL, 0)) == -1) {
114 fprintf(stderr, "Can't get Jog Shuttle file status\n");
120 else if (fcntl(fd, F_SETFL, iof | O_NONBLOCK) == -1) {
121 fprintf(stderr, "Can't set Jog Shuttle FILE DESCRIPTOR to O_NONBLOCK and stop thread\n");
127 /* enter thread loop */
129 /* reset the read set */
131 FD_SET(fd, &readset);
133 /* reinit the timeout structure */
135 timeout.tv_usec = 400000; /* 400 ms */
137 /* do select in blocked mode and wake up after timeout for eval stop_me */
138 result = select(fd+1, &readset, NULL, NULL, &timeout);
140 /* see if there was an error or timeout else process event */
141 if (result < 0 && errno == EINTR) {
142 /* EINTR event catched. This is not a problem - continue processing */
143 kDebug() << strerror(errno) << "\n";
144 /* continue processing */
146 } else if (result < 0) {
149 kDebug() << strerror(errno) << "\n";
150 } else if (result == 0) {
151 /* do nothing. reserved for future purposes */
154 if (FD_ISSET(fd, &readset)) {
156 readResult = read(fd, ev, sizeof(EV) * 64);
157 if (readResult < 0) {
158 if (num_warnings % 10000 == 0) {
159 /* if device is not available anymore - dead or disconnected */
160 fprintf(stderr, "Failed to read event from Jog Shuttle FILE DESCRIPTOR (repeated %d times)\n", num_warnings + 1);
162 /* exit if device is not available or the error occurs to long */
163 if (errno == ENODEV) {
164 perror("Failed to read from Jog Shuttle FILE DESCRIPTOR. Stop thread");
167 } else if (num_warnings > 1000000) {
168 perror("Failed to read from Jog Shuttle FILE DESCRIPTOR. Limit reached. Stop thread");
173 } else if (readResult < (int)sizeof(EV)) {
174 fprintf(stderr, "readResult < (int)sizeof(EV)\n");
177 } else if (readResult % (int)sizeof(EV) != 0) {
178 fprintf(stderr, "readResult mod (int)sizeof(EV) != 0\n");
182 for (int i = 0; i < readResult / (int)sizeof(EV); i++) {
191 kDebug() << "------- STOPPING SHUTTLE: ";
192 /* close the handle and return thread */
196 void ShuttleThread::handle_event(EV ev)
200 key(ev.code, ev.value);
205 if (ev.code == SHUTTLE)
211 void ShuttleThread::key(unsigned short code, unsigned int value)
214 // Button release (ignored)
223 //kDebug() << "Button PRESSED: " << code;
224 QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(KEY_EVENT_OFFSET + code)));
228 void ShuttleThread::shuttle(int value)
230 //gettimeofday( &last_shuttle, 0 );
231 //need_synthetic_shuttle = value != 0;
233 if (value == shuttlevalue) {
238 if (value > MAX_SHUTTLE_RANGE || value < -MAX_SHUTTLE_RANGE) {
239 fprintf(stderr, "Jog Shuttle returned value of %d (should be between -%d ad +%d)", value, MAX_SHUTTLE_RANGE, MAX_SHUTTLE_RANGE);
242 shuttlevalue = value;
244 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) (JOG_STOP + value)));
247 void ShuttleThread::jog(unsigned int value)
249 // generate a synthetic event for the shuttle going
250 // to the home position if we have not seen one recently.
251 //if (shuttlevalue != 0) {
252 // QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
256 // This code takes care of wrapping around the limits of the jog dial number (it is represented as a single byte, hence
257 // wraps at the 0/255 boundary). I used 25 as the difference to make sure that even in heavy load, with the jog dial
258 // turning fast and we miss some events that we do not mistakenly reverse the direction.
259 // Note also that at least the Contour ShuttlePRO v2 does not send an event for the value 0, so at the wrap it will
260 // need 2 events to go forward. But that is nothing we can do about...
261 if (jogvalue != 0xffff) {
262 //fprintf(stderr, "value=%d jogvalue=%d\n", value, jogvalue);
263 bool wrap = abs(value - jogvalue) > 25;
264 bool rewind = value < jogvalue;
265 bool forward = value > jogvalue;
266 if ((rewind && !wrap) || (forward && wrap))
267 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK1));
268 else if ((forward && !wrap) || (rewind && wrap))
269 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD1));
270 else if (!forward && !rewind && shuttlecounter > 2) {
271 // An event without changing the jog value is sent after each shuttle change.
272 // As the shuttle rest position does not get a shuttle event, only a non-position-changing jog event.
273 // Hence we stop on this when we see 2 non-position-changing jog events in a row.
275 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
279 if (shuttlecounter > 0) shuttlecounter++;
283 JogShuttle::JogShuttle(const QString &device, QObject *parent) :
289 JogShuttle::~JogShuttle()
294 void JogShuttle::initDevice(const QString &device)
296 if (m_shuttleProcess.isRunning()) {
297 if (device == m_shuttleProcess.m_device) return;
300 m_shuttleProcess.init(this, device);
301 m_shuttleProcess.start(QThread::LowestPriority);
304 void JogShuttle::stopDevice()
306 if (m_shuttleProcess.isRunning()) {
307 /* tell thread to stop */
308 m_shuttleProcess.stop_me = true;
309 m_shuttleProcess.exit();
310 /* give the thread some time (ms) to shutdown */
311 m_shuttleProcess.wait(600);
313 /* if still running - do it in the hardcore way */
314 if (m_shuttleProcess.isRunning()) {
315 m_shuttleProcess.terminate();
316 kDebug() << "/// terminate jogshuttle process\n";
321 void JogShuttle::customEvent(QEvent* e)
323 int code = e->type();
325 // Handle simple job events
335 int shuttle_pos = code - JOG_STOP;
336 if (shuttle_pos >= -MAX_SHUTTLE_RANGE && shuttle_pos <= MAX_SHUTTLE_RANGE) {
337 emit shuttlePos(shuttle_pos);
341 // we've got a key event.
342 //fprintf(stderr, "Firing button event for #%d\n", e->type() - KEY_EVENT_OFFSET); // DBG
343 emit button(e->type() - KEY_EVENT_OFFSET);
348 // #include "jogshuttle.moc"
351 #include "jogshuttle.moc"