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) {
76 shuttlevalue = 0xffff;
80 bool ShuttleThread::isWorking() {
84 void ShuttleThread::run() {
85 kDebug() << "------- STARTING SHUTTLE: " << m_device;
87 const int fd = KDE_open((char *) m_device.toUtf8().data(), O_RDONLY);
89 fprintf(stderr, "Can't open Jog Shuttle FILE DESCRIPTOR\n");
94 if (ioctl(fd, EVIOCGRAB, 1) < 0) {
95 fprintf(stderr, "Can't get exclusive access on Jog Shuttle FILE DESCRIPTOR\n");
100 if (read(fd, &ev, sizeof(ev)) < 0) {
101 fprintf(stderr, "Failed to read event from Jog Shuttle FILE DESCRIPTOR\n");
109 void ShuttleThread::handle_event(EV ev) {
112 key(ev.code, ev.value);
115 jogshuttle(ev.code, ev.value);
120 void ShuttleThread::key(unsigned short code, unsigned int value) {
122 // Button release (ignored)
132 kDebug() << "Button PRESSED: " << code;
133 QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(20000 + code)));
137 void ShuttleThread::shuttle(int value) {
138 //gettimeofday( &last_shuttle, 0 );
139 //need_synthetic_shuttle = value != 0;
141 if (value == shuttlevalue)
143 shuttlevalue = value;
150 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK_FAST));
151 break; // Reverse fast
153 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK));
156 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK_SLOW));
157 break; // Reverse slow
159 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
162 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD_SLOW));
163 break; // Forward slow
165 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD));
166 break; // Normal play
172 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD_FAST));
173 break; // Fast forward
178 void ShuttleThread::jog(unsigned int value) {
179 // We should generate a synthetic event for the shuttle going
180 // to the home position if we have not seen one recently
183 if (jogvalue != 0xffff) {
184 if (value < jogvalue)
185 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK1));
186 else if (value > jogvalue)
187 QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD1));
193 void ShuttleThread::jogshuttle(unsigned short code, unsigned int value) {
205 JogShuttle::JogShuttle(QString device, QObject *parent): QObject(parent) {
209 JogShuttle::~JogShuttle() {
210 if (m_shuttleProcess.isRunning()) m_shuttleProcess.exit();
213 void JogShuttle::initDevice(QString device) {
214 if (m_shuttleProcess.isRunning()) return;
215 m_shuttleProcess.init(this, device);
216 m_shuttleProcess.start(QThread::LowestPriority);
219 void JogShuttle::stopDevice() {
220 if (m_shuttleProcess.isRunning()) m_shuttleProcess.stop_me = true;
223 void JogShuttle::customEvent(QEvent* e) {
253 emit button(e->type() - 20000);
259 // #include "jogshuttle.moc"