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>
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, QString device)
78 shuttlevalue = 0xffff;
83 bool ShuttleThread::isWorking()
90 printf(" Signal catcher called for signal %d\n", sig);
93 void ShuttleThread::run()
95 kDebug() << "------- STARTING SHUTTLE: " << m_device;
96 /* open file descriptor */
97 const int fd = KDE_open((char *) m_device.toUtf8().data(), O_RDONLY);
99 perror("Can't open Jog Shuttle FILE DESCRIPTOR");
105 if (ioctl(fd, EVIOCGRAB, 1) < 0) {
106 fprintf(stderr, "Can't get exclusive access on Jog Shuttle FILE DESCRIPTOR\n");
112 struct timeval timeout;
114 int num_warnings, readResult = 0;
115 int result, iof = -1;
117 /* get fd settings */
118 if ((iof = fcntl(fd, F_GETFL, 0)) != -1) {
119 /* set fd non blocking */
120 fcntl(fd, F_SETFL, iof | O_NONBLOCK);
122 fprintf(stderr, "Can't set Jog Shuttle FILE DESCRIPTOR to O_NONBLOCK and stop thread\n");
126 /* enter thread loop */
128 /* reset the read set */
130 FD_SET(fd, &readset);
132 /* reinit the timeout structure */
134 timeout.tv_usec = 400000; /* 400 ms */
136 /* do select in blocked mode and wake up after timeout for eval stop_me */
137 result = select(fd+1, &readset, NULL, NULL, &timeout);
139 /* see if there was an error or timeout else process event */
140 if (result < 0 && errno == EINTR) {
141 /* EINTR event catched. This is not a problem - continue processing */
142 kDebug() << strerror(errno) << "\n";
143 /* continue processing */
145 } else if (result < 0) {
148 kDebug() << strerror(errno) << "\n";
149 } else if (result == 0) {
150 /* do nothing. reserved for future purposes */
153 if (FD_ISSET(fd, &readset)) {
155 readResult = read(fd, &ev, sizeof(ev));
156 if (readResult < 0) {
157 if (num_warnings % 10000 == 0) {
158 /* if device is not available anymore - dead or disconnected */
159 fprintf(stderr, "Failed to read event from Jog Shuttle FILE DESCRIPTOR (repeated %d times)\n", num_warnings + 1);
161 /* exit if device is not available or the error occurs to long */
162 if (errno == ENODEV) {
163 perror("Failed to read from Jog Shuttle FILE DESCRIPTOR. Stop thread");
166 } else if (num_warnings > 1000000) {
167 perror("Failed to read from Jog Shuttle FILE DESCRIPTOR. Limit reached. Stop thread");
172 } else if (readResult == sizeof(ev)) {
176 fprintf(stderr, "Read nr of bytes != sizeof(ev)\n");
182 // kDebug() << "Close thread" << "\n";
183 /* close the handle and return thread */
187 void ShuttleThread::handle_event(EV ev)
191 key(ev.code, ev.value);
196 if (ev.code == SHUTTLE)
202 void ShuttleThread::key(unsigned short code, unsigned int value)
205 // Button release (ignored)
214 //kDebug() << "Button PRESSED: " << code;
215 QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(KEY_EVENT_OFFSET + code)));
219 void ShuttleThread::shuttle(int value)
221 //gettimeofday( &last_shuttle, 0 );
222 //need_synthetic_shuttle = value != 0;
224 if (value == shuttlevalue) {
229 if (value > MAX_SHUTTLE_RANGE || value < -MAX_SHUTTLE_RANGE) {
230 fprintf(stderr, "Jog Shuttle returned value of %d (should be between -%d ad +%d)", value, MAX_SHUTTLE_RANGE, MAX_SHUTTLE_RANGE);
233 shuttlevalue = value;
235 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) (JOG_STOP + value)));
238 void ShuttleThread::jog(unsigned int value)
240 // generate a synthetic event for the shuttle going
241 // to the home position if we have not seen one recently.
242 //if (shuttlevalue != 0) {
243 // QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
247 // This code takes care of wrapping around the limits of the jog dial number (it is represented as a single byte, hence
248 // wraps at the 0/255 boundary). I used 25 as the difference to make sure that even in heavy load, with the jog dial
249 // turning fast and we miss some events that we do not mistakenly reverse the direction.
250 // Note also that at least the Contour ShuttlePRO v2 does not send an event for the value 0, so at the wrap it will
251 // need 2 events to go forward. But that is nothing we can do about...
252 if (jogvalue != 0xffff) {
253 //fprintf(stderr, "value=%d jogvalue=%d\n", value, jogvalue);
254 bool wrap = abs(value - jogvalue) > 25;
255 bool rewind = value < jogvalue;
256 bool forward = value > jogvalue;
257 if ((rewind && !wrap) || (forward && wrap))
258 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK1));
259 else if ((forward && !wrap) || (rewind && wrap))
260 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD1));
261 else if (!forward && !rewind && shuttlecounter > 2) {
262 // An event without changing the jog value is sent after each shuttle change.
263 // As the shuttle rest position does not get a shuttle event, only a non-position-changing jog event.
264 // Hence we stop on this when we see 2 non-position-changing jog events in a row.
266 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
270 if (shuttlecounter > 0) shuttlecounter++;
274 JogShuttle::JogShuttle(QString device, QObject *parent) :
280 JogShuttle::~JogShuttle()
285 void JogShuttle::initDevice(QString device)
287 if (m_shuttleProcess.isRunning()) {
288 if (device == m_shuttleProcess.m_device) return;
291 m_shuttleProcess.init(this, device);
292 m_shuttleProcess.start(QThread::LowestPriority);
295 void JogShuttle::stopDevice()
297 if (m_shuttleProcess.isRunning()) {
298 /* tell thread to stop */
299 m_shuttleProcess.stop_me = true;
300 m_shuttleProcess.exit();
301 /* give the thread some time (ms) to shutdown */
302 m_shuttleProcess.wait(600);
304 /* if still running - do it in the hardcore way */
305 if (m_shuttleProcess.isRunning()) {
306 m_shuttleProcess.terminate();
307 kDebug() << "/// terminate jogshuttle process\n";
312 void JogShuttle::customEvent(QEvent* e)
314 int code = e->type();
316 // Handle simple job events
326 int shuttle_pos = code - JOG_STOP;
327 if (shuttle_pos >= -MAX_SHUTTLE_RANGE && shuttle_pos <= MAX_SHUTTLE_RANGE) {
328 emit shuttlePos(shuttle_pos);
332 // we've got a key event.
333 //fprintf(stderr, "Firing button event for #%d\n", e->type() - KEY_EVENT_OFFSET); // DBG
334 emit button(e->type() - KEY_EVENT_OFFSET);
339 // #include "jogshuttle.moc"