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