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, QString device)
77 shuttlevalue = 0xffff;
82 bool ShuttleThread::isWorking()
87 void ShuttleThread::run()
89 kDebug() << "------- STARTING SHUTTLE: " << m_device;
90 /* open file descriptor */
91 const int fd = KDE_open((char *) m_device.toUtf8().data(), O_RDONLY);
93 perror("Can't open Jog Shuttle FILE DESCRIPTOR");
99 if (ioctl(fd, EVIOCGRAB, 1) < 0) {
100 fprintf(stderr, "Can't get exclusive access on Jog Shuttle FILE DESCRIPTOR\n");
106 struct timeval timeout;
108 int num_warnings = 0;
109 int result, iof = -1;
111 /* enter thread loop */
113 /* reset the read set */
115 FD_SET(fd, &readset);
117 /* reinit the timeout structure */
119 timeout.tv_usec = 400000; /* 400 ms */
121 /* do select in blocked mode and wake up after timeout for eval stop_me */
122 result = select(fd+1, &readset, NULL, NULL, &timeout);
124 /* see if there was an error or timeout else process event */
126 /* usually this is a condition for exit but EINTR error is sometimes
127 * thrown under special circumstances. I think its ok to ignore this
128 * if its not too often */
129 kDebug() << strerror(errno) << "\n";
130 } else if (result == 0) {
131 /* do nothing. reserved for future purposes */
134 if (FD_ISSET(fd, &readset)) {
135 /* get fd settings */
136 if ((iof = fcntl(fd, F_GETFL, 0)) != -1) {
137 /* set fd non blocking */
138 fcntl(fd, F_SETFL, iof | O_NONBLOCK);
140 if (read(fd, &ev, sizeof(ev)) < 0) {
141 if (num_warnings % 10000 == 0) {
142 /* if device is not available anymore - dead or disconnected */
143 fprintf(stderr, "Failed to read event from Jog Shuttle FILE DESCRIPTOR (repeated %d times)\n", num_warnings + 1);
145 /* exit if device is not available or the error occurs to long */
146 if (errno == ENODEV) {
147 perror("Failed to read from Jog Shuttle FILE DESCRIPTOR. Stop thread");
150 } else if (num_warnings > 1000000) {
151 perror("Failed to read from Jog Shuttle FILE DESCRIPTOR. Limit reached. Stop thread");
157 /* restore settings */
159 fcntl(fd, F_SETFL, iof);
164 fprintf(stderr, "Can't set Jog Shuttle FILE DESCRIPTOR to O_NONBLOCK and stop thread\n");
171 /* close the handle and return thread */
175 void ShuttleThread::handle_event(EV ev)
179 key(ev.code, ev.value);
184 if (ev.code == SHUTTLE)
190 void ShuttleThread::key(unsigned short code, unsigned int value)
193 // Button release (ignored)
202 kDebug() << "Button PRESSED: " << code;
203 QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(KEY_EVENT_OFFSET + code)));
207 void ShuttleThread::shuttle(int value)
209 //gettimeofday( &last_shuttle, 0 );
210 //need_synthetic_shuttle = value != 0;
212 if (value == shuttlevalue) {
217 if (value > MAX_SHUTTLE_RANGE || value < -MAX_SHUTTLE_RANGE) {
218 fprintf(stderr, "Jog Shuttle returned value of %d (should be between -%d ad +%d)", value, MAX_SHUTTLE_RANGE, MAX_SHUTTLE_RANGE);
221 shuttlevalue = value;
223 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) (JOG_STOP + value)));
226 void ShuttleThread::jog(unsigned int value)
228 // generate a synthetic event for the shuttle going
229 // to the home position if we have not seen one recently.
230 //if (shuttlevalue != 0) {
231 // QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
235 // This code takes care of wrapping around the limits of the jog dial number (it is represented as a single byte, hence
236 // wraps at the 0/255 boundary). I used 25 as the difference to make sure that even in heavy load, with the jog dial
237 // turning fast and we miss some events that we do not mistakenly reverse the direction.
238 // Note also that at least the Contour ShuttlePRO v2 does not send an event for the value 0, so at the wrap it will
239 // need 2 events to go forward. But that is nothing we can do about...
240 if (jogvalue != 0xffff) {
241 //fprintf(stderr, "value=%d jogvalue=%d\n", value, jogvalue);
242 bool wrap = abs(value - jogvalue) > 25;
243 bool rewind = value < jogvalue;
244 bool forward = value > jogvalue;
245 if ((rewind && !wrap) || (forward && wrap))
246 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK1));
247 else if ((forward && !wrap) || (rewind && wrap))
248 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD1));
249 else if (!forward && !rewind && shuttlecounter > 2) {
250 // An event without changing the jog value is sent after each shuttle change.
251 // As the shuttle rest position does not get a shuttle event, only a non-position-changing jog event.
252 // Hence we stop on this when we see 2 non-position-changing jog events in a row.
254 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
258 if (shuttlecounter > 0) shuttlecounter++;
262 JogShuttle::JogShuttle(QString device, QObject *parent) :
268 JogShuttle::~JogShuttle()
273 void JogShuttle::initDevice(QString device)
275 if (m_shuttleProcess.isRunning()) {
276 if (device == m_shuttleProcess.m_device) return;
279 m_shuttleProcess.init(this, device);
280 m_shuttleProcess.start(QThread::LowestPriority);
283 void JogShuttle::stopDevice()
285 if (m_shuttleProcess.isRunning()) {
286 /* tell thread to stop */
287 m_shuttleProcess.stop_me = true;
288 m_shuttleProcess.exit();
289 /* give the thread some time (ms) to shutdown */
290 m_shuttleProcess.wait(600);
292 /* if still running - do it in the hardcore way */
293 if (m_shuttleProcess.isRunning()) {
294 m_shuttleProcess.terminate();
295 kDebug() << "/// terminate jogshuttle process\n";
300 void JogShuttle::customEvent(QEvent* e)
302 int code = e->type();
304 // Handle simple job events
314 int shuttle_pos = code - JOG_STOP;
315 if (shuttle_pos >= -MAX_SHUTTLE_RANGE && shuttle_pos <= MAX_SHUTTLE_RANGE) {
316 emit shuttlePos(shuttle_pos);
320 // we've got a key event.
321 //fprintf(stderr, "Firing button event for #%d\n", e->type() - KEY_EVENT_OFFSET); // DBG
322 emit button(e->type() - KEY_EVENT_OFFSET);
327 // #include "jogshuttle.moc"