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>
60 #define JOG_BACK1 10001
61 #define JOG_FWD1 10002
63 #define JOG_FWD_SLOW 10004
64 #define JOG_FWD_FAST 10005
65 #define JOG_BACK 10006
66 #define JOG_BACK_SLOW 10007
67 #define JOG_BACK_FAST 10008
68 #define JOG_STOP 10009
71 void ShuttleThread::init(QObject *parent, QString device)
77 shuttlevalue = 0xffff;
81 bool ShuttleThread::isWorking()
86 void ShuttleThread::run()
88 kDebug() << "------- STARTING SHUTTLE: " << m_device;
90 const int fd = KDE_open((char *) m_device.toUtf8().data(), O_RDONLY);
92 fprintf(stderr, "Can't open Jog Shuttle FILE DESCRIPTOR\n");
97 if (ioctl(fd, EVIOCGRAB, 1) < 0) {
98 fprintf(stderr, "Can't get exclusive access on Jog Shuttle FILE DESCRIPTOR\n");
103 if (read(fd, &ev, sizeof(ev)) < 0) {
104 fprintf(stderr, "Failed to read event from Jog Shuttle FILE DESCRIPTOR\n");
112 void ShuttleThread::handle_event(EV ev)
116 key(ev.code, ev.value);
119 jogshuttle(ev.code, ev.value);
124 void ShuttleThread::key(unsigned short code, unsigned int value)
127 // Button release (ignored)
137 kDebug() << "Button PRESSED: " << code;
138 QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(20000 + code)));
142 void ShuttleThread::shuttle(int value)
144 //gettimeofday( &last_shuttle, 0 );
145 //need_synthetic_shuttle = value != 0;
147 if (value == shuttlevalue)
149 shuttlevalue = value;
156 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK_FAST));
157 break; // Reverse fast
159 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK));
162 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK_SLOW));
163 break; // Reverse slow
165 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
168 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD_SLOW));
169 break; // Forward slow
171 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD));
172 break; // Normal play
178 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD_FAST));
179 break; // Fast forward
184 void ShuttleThread::jog(unsigned int value)
186 // We should generate a synthetic event for the shuttle going
187 // to the home position if we have not seen one recently
190 if (jogvalue != 0xffff) {
191 if (value < jogvalue)
192 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK1));
193 else if (value > jogvalue)
194 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD1));
200 void ShuttleThread::jogshuttle(unsigned short code, unsigned int value)
213 JogShuttle::JogShuttle(QString device, QObject *parent) :
219 JogShuttle::~JogShuttle()
221 if (m_shuttleProcess.isRunning()) m_shuttleProcess.exit();
224 void JogShuttle::initDevice(QString device)
226 if (m_shuttleProcess.isRunning()) return;
227 m_shuttleProcess.init(this, device);
228 m_shuttleProcess.start(QThread::LowestPriority);
231 void JogShuttle::stopDevice()
233 if (m_shuttleProcess.isRunning()) m_shuttleProcess.stop_me = true;
236 void JogShuttle::customEvent(QEvent* e)
267 emit button(e->type() - 20000);
273 // #include "jogshuttle.moc"