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>
34 #include <linux/input.h>
55 // Constants for the returned events when reading them from the device
60 // Constants for the signals sent.
61 #define JOG_BACK1 10001
62 #define JOG_FWD1 10002
63 #define KEY_EVENT_OFFSET 20000
65 // middle value for shuttle, will be +/-MAX_SHUTTLE_RANGE
66 #define JOG_STOP 10020
67 #define MAX_SHUTTLE_RANGE 7
69 void ShuttleThread::init(QObject *parent, QString device)
75 shuttlevalue = 0xffff;
80 bool ShuttleThread::isWorking()
85 void ShuttleThread::run()
87 kDebug() << "------- STARTING SHUTTLE: " << m_device;
88 /* open file descriptor */
89 const int fd = KDE_open((char *) m_device.toUtf8().data(), O_RDONLY);
91 fprintf(stderr, "Can't open Jog Shuttle FILE DESCRIPTOR\n");
96 if (ioctl(fd, EVIOCGRAB, 1) < 0) {
97 fprintf(stderr, "Can't get exclusive access on Jog Shuttle FILE DESCRIPTOR\n");
103 struct timeval timeout;
105 int num_warnings = 0;
106 int result, iof = -1;
108 /* enter thread loop */
110 /* reset the read set */
112 FD_SET(fd, &readset);
114 /* reinit the timeout structure */
116 timeout.tv_usec = 300000; /* 300 ms */
119 result = select(fd+1, &readset, NULL, NULL, &timeout);
121 /* see if there was an error or timeout */
123 // perror("select failed");
124 } else if (result == 0) {
128 if (FD_ISSET(fd, &readset)) {
129 /* get fd settings */
130 if ((iof = fcntl(fd, F_GETFL, 0)) != -1) {
131 /* set fd non blocking */
132 fcntl(fd, F_SETFL, iof | O_NONBLOCK);
134 if (read(fd, &ev, sizeof(ev)) < 0) {
135 if (num_warnings % 10000 == 0)
136 /* should not happen cause select called before */
137 fprintf(stderr, "Failed to read event from Jog Shuttle FILE DESCRIPTOR (repeated %d times)\n", num_warnings + 1);
141 /* restore settings */
143 fcntl(fd, F_SETFL, iof);
148 fprintf(stderr, "Can't set Jog Shuttle FILE DESCRIPTOR to O_NONBLOCK\n");
155 /* close the handle and return thread */
159 void ShuttleThread::handle_event(EV ev)
163 key(ev.code, ev.value);
168 if (ev.code == SHUTTLE)
174 void ShuttleThread::key(unsigned short code, unsigned int value)
177 // Button release (ignored)
186 kDebug() << "Button PRESSED: " << code;
187 QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(KEY_EVENT_OFFSET + code)));
191 void ShuttleThread::shuttle(int value)
193 //gettimeofday( &last_shuttle, 0 );
194 //need_synthetic_shuttle = value != 0;
196 if (value == shuttlevalue) {
201 if (value > MAX_SHUTTLE_RANGE || value < -MAX_SHUTTLE_RANGE) {
202 fprintf(stderr, "Jog Shuttle returned value of %d (should be between -%d ad +%d)", value, MAX_SHUTTLE_RANGE, MAX_SHUTTLE_RANGE);
205 shuttlevalue = value;
207 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) (JOG_STOP + value)));
210 void ShuttleThread::jog(unsigned int value)
212 // generate a synthetic event for the shuttle going
213 // to the home position if we have not seen one recently.
214 //if (shuttlevalue != 0) {
215 // QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
219 // This code takes care of wrapping around the limits of the jog dial number (it is represented as a single byte, hence
220 // wraps at the 0/255 boundary). I used 25 as the difference to make sure that even in heavy load, with the jog dial
221 // turning fast and we miss some events that we do not mistakenly reverse the direction.
222 // Note also that at least the Contour ShuttlePRO v2 does not send an event for the value 0, so at the wrap it will
223 // need 2 events to go forward. But that is nothing we can do about...
224 if (jogvalue != 0xffff) {
225 //fprintf(stderr, "value=%d jogvalue=%d\n", value, jogvalue);
226 bool wrap = abs(value - jogvalue) > 25;
227 bool rewind = value < jogvalue;
228 bool forward = value > jogvalue;
229 if ((rewind && !wrap) || (forward && wrap))
230 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK1));
231 else if ((forward && !wrap) || (rewind && wrap))
232 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD1));
233 else if (!forward && !rewind && shuttlecounter > 2) {
234 // An event without changing the jog value is sent after each shuttle change.
235 // As the shuttle rest position does not get a shuttle event, only a non-position-changing jog event.
236 // Hence we stop on this when we see 2 non-position-changing jog events in a row.
238 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
242 if (shuttlecounter > 0) shuttlecounter++;
246 JogShuttle::JogShuttle(QString device, QObject *parent) :
252 JogShuttle::~JogShuttle()
257 void JogShuttle::initDevice(QString device)
259 if (m_shuttleProcess.isRunning()) {
260 if (device == m_shuttleProcess.m_device) return;
263 m_shuttleProcess.init(this, device);
264 m_shuttleProcess.start(QThread::LowestPriority);
267 void JogShuttle::stopDevice()
269 if (m_shuttleProcess.isRunning()) {
270 /* the read fd is in blocking mode => stop_me is broken at the moment */
271 m_shuttleProcess.stop_me = true;
272 m_shuttleProcess.exit();
273 /* give the thread some time (ms) to shutdown */
274 m_shuttleProcess.wait(600);
276 /* if still running - do it in the hardcore way */
277 if (m_shuttleProcess.isRunning()) {
278 m_shuttleProcess.terminate();
279 kDebug() << "/// terminate jogshuttle process\n";
284 void JogShuttle::customEvent(QEvent* e)
286 int code = e->type();
288 // Handle simple job events
298 int shuttle_pos = code - JOG_STOP;
299 if (shuttle_pos >= -MAX_SHUTTLE_RANGE && shuttle_pos <= MAX_SHUTTLE_RANGE) {
300 emit shuttlePos(shuttle_pos);
304 // we've got a key event.
305 //fprintf(stderr, "Firing button event for #%d\n", e->type() - KEY_EVENT_OFFSET); // DBG
306 emit button(e->type() - KEY_EVENT_OFFSET);
311 // #include "jogshuttle.moc"