]> git.sesse.net Git - kdenlive/blob - src/jogshuttle.cpp
Initial support for Jog Shuttle devices
[kdenlive] / src / jogshuttle.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *   Based on code by Arendt David <admin@prnet.org>                       *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21
22
23 #define DELAY 10
24
25 #define KEY 1
26 #define KEY1 256
27 #define KEY2 257
28 #define KEY3 258
29 #define KEY4 259
30 #define KEY5 260
31 #define KEY6 261
32 #define KEY7 262
33 #define KEY8 263
34 #define KEY9 264
35 #define KEY10 265
36 #define KEY11 266
37 #define KEY12 267
38 #define KEY13 268
39 #define KEY14 269
40 #define KEY15 270
41
42 #define JOGSHUTTLE 2
43 #define JOG 7
44 #define SHUTTLE 8
45
46 #define JOG_BACK1 10001
47 #define JOG_FWD1 10002
48 #define JOG_FWD 10003
49 #define JOG_FWD_SLOW 10004
50 #define JOG_FWD_FAST 10005
51 #define JOG_BACK 10006
52 #define JOG_BACK_SLOW 10007
53 #define JOG_BACK_FAST 10008
54 #define JOG_STOP 10009
55
56
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <unistd.h>
60
61 #include <QFile>
62 #include <QTextStream>
63 #include <QApplication>
64 #include <QEvent>
65
66 #include <KDebug>
67
68
69
70 #include "jogshuttle.h"
71
72
73 /*unsigned short  jogvalue = 0xffff;
74 int   shuttlevalue = 0xffff;
75 struct timeval  last_shuttle;
76 int   need_synthetic_shuttle;
77 */
78
79 void ShuttleThread::init(QObject *parent, QString device) {
80     m_parent = parent;
81     m_device = device;
82     stop_me = false;
83     m_isWorking = false;
84     shuttlevalue = 0xffff;
85     jogvalue = 0xffff;
86 }
87
88 bool ShuttleThread::isWorking() {
89     return m_isWorking;
90 }
91
92 void ShuttleThread::run() {
93     kDebug() << "-------  STARTING SHUTTLE: " << m_device;
94
95     EV ev;
96     FILE * fd = fopen((char *) m_device.toUtf8().data(), "r");
97
98     if (!fd) {
99         fprintf(stderr, "Can't open file\n");
100         exit(1);
101     }
102
103     while (!stop_me) {
104         while (fread(&ev, sizeof(ev), 1, fd) == 1) handle_event(ev);
105     }
106
107
108 }
109
110 void ShuttleThread::handle_event(EV ev) {
111     switch (ev.type) {
112     case KEY :
113         key(ev.code, ev.value);
114         break;
115     case JOGSHUTTLE :
116         jogshuttle(ev.code, ev.value);
117         break;
118     }
119 }
120
121 void ShuttleThread::key(unsigned short code, unsigned int value) {
122     if (value == 0) {
123         // Button release (ignored)
124         return;
125     }
126
127     code -= KEY1 - 1;
128
129     // Bound check!
130     if (code > 16)
131         return;
132
133     kDebug() << "Button PRESSED: " << code;
134     QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(20000 + code)));
135
136 }
137
138 void ShuttleThread::shuttle(int value) {
139     //gettimeofday( &last_shuttle, 0 );
140     //need_synthetic_shuttle = value != 0;
141
142     if (value == shuttlevalue)
143         return;
144     shuttlevalue = value;
145     switch (value) {
146     case - 7 :
147     case - 6 :
148     case - 5 :
149     case - 4 :
150     case - 3 :
151         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK_FAST));
152         break;  // Reverse fast
153     case - 2 :
154         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK));
155         break;  // Reverse
156     case - 1 :
157         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK_SLOW));
158         break;  // Reverse slow
159     case  0 :
160         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
161         break;  // Stop!
162     case  1 :
163         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD_SLOW));
164         break;  // Forward slow
165     case  2 :
166         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD));
167         break;  // Normal play
168     case  3 :
169     case  4 :
170     case  5 :
171     case  6 :
172     case  7 :
173         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD_FAST));
174         break; // Fast forward
175     }
176
177 }
178
179 void ShuttleThread::jog(unsigned int value) {
180     // We should generate a synthetic event for the shuttle going
181     // to the home position if we have not seen one recently
182     //check_synthetic();
183
184     if (jogvalue != 0xffff) {
185         if (value < jogvalue)
186             QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK1));
187         else if (value > jogvalue)
188             QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD1));
189     }
190     jogvalue = value;
191 }
192
193
194 void ShuttleThread::jogshuttle(unsigned short code, unsigned int value) {
195     switch (code) {
196     case JOG :
197         jog(value);
198         break;
199     case SHUTTLE :
200         shuttle(value);
201         break;
202     }
203 }
204
205
206 JogShuttle::JogShuttle(QString device, QObject *parent): QObject(parent) {
207     initDevice(device);
208 }
209
210 JogShuttle::~JogShuttle() {
211     if (m_shuttleProcess.isRunning()) m_shuttleProcess.exit();
212 }
213
214 void JogShuttle::initDevice(QString device) {
215     if (m_shuttleProcess.isRunning()) return;
216     m_shuttleProcess.init(this, device);
217     m_shuttleProcess.start(QThread::LowestPriority);
218 }
219
220 void JogShuttle::stopDevice() {
221     if (m_shuttleProcess.isRunning()) m_shuttleProcess.stop_me = true;
222 }
223
224 void JogShuttle::customEvent(QEvent* e) {
225     switch (e->type()) {
226     case JOG_BACK1:
227         emit rewind1();
228         break;
229     case JOG_FWD1:
230         emit forward1();
231         break;
232     case JOG_BACK:
233         emit rewind(-1);
234         break;
235     case JOG_FWD:
236         emit forward(1);
237         break;
238     case JOG_BACK_SLOW:
239         emit rewind(-0.5);
240         break;
241     case JOG_FWD_SLOW:
242         emit forward(0.5);
243         break;
244     case JOG_BACK_FAST:
245         emit rewind(-2);
246         break;
247     case JOG_FWD_FAST:
248         emit forward(2);
249         break;
250     case JOG_STOP:
251         emit stop();
252         break;
253     default:
254         emit button(e->type() - 20000);
255     }
256 }
257
258
259
260 // #include "jogshuttle.moc"
261