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