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 /* open file descriptor */
93 const int fd = KDE_open((char *) m_device.toUtf8().data(), O_RDONLY);
95 perror("Can't open Jog Shuttle FILE DESCRIPTOR");
101 if (ioctl(fd, EVIOCGRAB, 1) < 0) {
102 fprintf(stderr, "Can't get exclusive access on Jog Shuttle FILE DESCRIPTOR\n");
108 struct timeval timeout;
110 int num_warnings = 0, readResult = 0;
111 int result, iof = -1;
113 /* get fd settings */
114 if ((iof = fcntl(fd, F_GETFL, 0)) == -1) {
115 fprintf(stderr, "Can't get Jog Shuttle file status\n");
121 else if (fcntl(fd, F_SETFL, iof | O_NONBLOCK) == -1) {
122 fprintf(stderr, "Can't set Jog Shuttle FILE DESCRIPTOR to O_NONBLOCK and stop thread\n");
128 /* enter thread loop */
130 /* reset the read set */
132 FD_SET(fd, &readset);
134 /* reinit the timeout structure */
136 timeout.tv_usec = 400000; /* 400 ms */
138 /* do select in blocked mode and wake up after timeout for eval stop_me */
139 result = select(fd+1, &readset, NULL, NULL, &timeout);
141 /* see if there was an error or timeout else process event */
142 if (result < 0 && errno == EINTR) {
143 /* EINTR event catched. This is not a problem - continue processing */
144 kDebug() << strerror(errno) << "\n";
145 /* continue processing */
147 } else if (result < 0) {
150 kDebug() << strerror(errno) << "\n";
151 } else if (result == 0) {
152 /* do nothing. reserved for future purposes */
155 if (FD_ISSET(fd, &readset)) {
157 readResult = read(fd, ev, sizeof(EV) * 64);
158 if (readResult < 0) {
159 if (num_warnings % 10000 == 0) {
160 /* if device is not available anymore - dead or disconnected */
161 fprintf(stderr, "Failed to read event from Jog Shuttle FILE DESCRIPTOR (repeated %d times)\n", num_warnings + 1);
163 /* exit if device is not available or the error occurs to long */
164 if (errno == ENODEV) {
165 perror("Failed to read from Jog Shuttle FILE DESCRIPTOR. Stop thread");
168 } else if (num_warnings > 1000000) {
169 perror("Failed to read from Jog Shuttle FILE DESCRIPTOR. Limit reached. Stop thread");
174 } else if (readResult < (int)sizeof(EV)) {
175 fprintf(stderr, "readResult < (int)sizeof(EV)\n");
178 } else if (readResult % (int)sizeof(EV) != 0) {
179 fprintf(stderr, "readResult mod (int)sizeof(EV) != 0\n");
183 for (int i = 0; i < readResult / (int)sizeof(EV); i++) {
192 kDebug() << "------- STOPPING SHUTTLE: ";
193 /* close the handle and return thread */
197 void ShuttleThread::handle_event(EV ev)
201 key(ev.code, ev.value);
206 if (ev.code == SHUTTLE)
212 void ShuttleThread::key(unsigned short code, unsigned int value)
215 // Button release (ignored)
224 //kDebug() << "Button PRESSED: " << code;
225 QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(KEY_EVENT_OFFSET + code)));
229 void ShuttleThread::shuttle(int value)
231 //gettimeofday( &last_shuttle, 0 );
232 //need_synthetic_shuttle = value != 0;
234 if (value == shuttlevalue) {
239 if (value > MAX_SHUTTLE_RANGE || value < -MAX_SHUTTLE_RANGE) {
240 fprintf(stderr, "Jog Shuttle returned value of %d (should be between -%d ad +%d)", value, MAX_SHUTTLE_RANGE, MAX_SHUTTLE_RANGE);
243 shuttlevalue = value;
245 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) (JOG_STOP + value)));
248 void ShuttleThread::jog(unsigned int value)
250 // generate a synthetic event for the shuttle going
251 // to the home position if we have not seen one recently.
252 //if (shuttlevalue != 0) {
253 // QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
257 // This code takes care of wrapping around the limits of the jog dial number (it is represented as a single byte, hence
258 // wraps at the 0/255 boundary). I used 25 as the difference to make sure that even in heavy load, with the jog dial
259 // turning fast and we miss some events that we do not mistakenly reverse the direction.
260 // Note also that at least the Contour ShuttlePRO v2 does not send an event for the value 0, so at the wrap it will
261 // need 2 events to go forward. But that is nothing we can do about...
262 if (jogvalue != 0xffff) {
263 //fprintf(stderr, "value=%d jogvalue=%d\n", value, jogvalue);
264 bool wrap = abs(value - jogvalue) > 25;
265 bool rewind = value < jogvalue;
266 bool forward = value > jogvalue;
267 if ((rewind && !wrap) || (forward && wrap))
268 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK1));
269 else if ((forward && !wrap) || (rewind && wrap))
270 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD1));
271 else if (!forward && !rewind && shuttlecounter > 2) {
272 // An event without changing the jog value is sent after each shuttle change.
273 // As the shuttle rest position does not get a shuttle event, only a non-position-changing jog event.
274 // Hence we stop on this when we see 2 non-position-changing jog events in a row.
276 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
280 if (shuttlecounter > 0) shuttlecounter++;
284 JogShuttle::JogShuttle(const QString &device, QObject *parent) :
290 JogShuttle::~JogShuttle()
295 void JogShuttle::initDevice(const QString &device)
297 if (m_shuttleProcess.isRunning()) {
298 if (device == m_shuttleProcess.m_device) return;
301 m_shuttleProcess.init(this, device);
302 m_shuttleProcess.start(QThread::LowestPriority);
305 void JogShuttle::stopDevice()
307 if (m_shuttleProcess.isRunning()) {
308 /* tell thread to stop */
309 m_shuttleProcess.stop_me = true;
310 m_shuttleProcess.exit();
311 /* give the thread some time (ms) to shutdown */
312 m_shuttleProcess.wait(600);
314 /* if still running - do it in the hardcore way */
315 if (m_shuttleProcess.isRunning()) {
316 m_shuttleProcess.terminate();
317 kDebug() << "/// terminate jogshuttle process\n";
322 void JogShuttle::customEvent(QEvent* e)
324 int code = e->type();
326 // Handle simple job events
336 int shuttle_pos = code - JOG_STOP;
337 if (shuttle_pos >= -MAX_SHUTTLE_RANGE && shuttle_pos <= MAX_SHUTTLE_RANGE) {
338 emit shuttlePos(shuttle_pos);
342 // we've got a key event.
343 //fprintf(stderr, "Firing button event for #%d\n", e->type() - KEY_EVENT_OFFSET); // DBG
344 emit button(e->type() - KEY_EVENT_OFFSET);
347 QString JogShuttle::enumerateDevice(const QString &device)
349 QDir canonDir(device);
350 //return QDir::canonicalPath(device);
351 return canonDir.canonicalPath();
355 // #include "jogshuttle.moc"
358 #include "jogshuttle.moc"