]> git.sesse.net Git - kdenlive/blob - src/jogshuttle.cpp
[PATCH by Ray Lehtiniemi] Check return code on read() syscall.
[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         if (read(fd, &ev, sizeof(ev)) < 0) {
101             fprintf(stderr, "Failed to read event from Jog Shuttle FILE DESCRIPTOR\n");
102         }
103         handle_event(ev);
104     }
105     close(fd);
106
107 }
108
109 void ShuttleThread::handle_event(EV ev) {
110     switch (ev.type) {
111     case KEY :
112         key(ev.code, ev.value);
113         break;
114     case JOGSHUTTLE :
115         jogshuttle(ev.code, ev.value);
116         break;
117     }
118 }
119
120 void ShuttleThread::key(unsigned short code, unsigned int value) {
121     if (value == 0) {
122         // Button release (ignored)
123         return;
124     }
125
126     code -= KEY1 - 1;
127
128     // Bound check!
129     if (code > 16)
130         return;
131
132     kDebug() << "Button PRESSED: " << code;
133     QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(20000 + code)));
134
135 }
136
137 void ShuttleThread::shuttle(int value) {
138     //gettimeofday( &last_shuttle, 0 );
139     //need_synthetic_shuttle = value != 0;
140
141     if (value == shuttlevalue)
142         return;
143     shuttlevalue = value;
144     switch (value) {
145     case - 7 :
146     case - 6 :
147     case - 5 :
148     case - 4 :
149     case - 3 :
150         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK_FAST));
151         break;  // Reverse fast
152     case - 2 :
153         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK));
154         break;  // Reverse
155     case - 1 :
156         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK_SLOW));
157         break;  // Reverse slow
158     case  0 :
159         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
160         break;  // Stop!
161     case  1 :
162         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD_SLOW));
163         break;  // Forward slow
164     case  2 :
165         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD));
166         break;  // Normal play
167     case  3 :
168     case  4 :
169     case  5 :
170     case  6 :
171     case  7 :
172         QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD_FAST));
173         break; // Fast forward
174     }
175
176 }
177
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
181     //check_synthetic();
182
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));
188     }
189     jogvalue = value;
190 }
191
192
193 void ShuttleThread::jogshuttle(unsigned short code, unsigned int value) {
194     switch (code) {
195     case JOG :
196         jog(value);
197         break;
198     case SHUTTLE :
199         shuttle(value);
200         break;
201     }
202 }
203
204
205 JogShuttle::JogShuttle(QString device, QObject *parent): QObject(parent) {
206     initDevice(device);
207 }
208
209 JogShuttle::~JogShuttle() {
210     if (m_shuttleProcess.isRunning()) m_shuttleProcess.exit();
211 }
212
213 void JogShuttle::initDevice(QString device) {
214     if (m_shuttleProcess.isRunning()) return;
215     m_shuttleProcess.init(this, device);
216     m_shuttleProcess.start(QThread::LowestPriority);
217 }
218
219 void JogShuttle::stopDevice() {
220     if (m_shuttleProcess.isRunning()) m_shuttleProcess.stop_me = true;
221 }
222
223 void JogShuttle::customEvent(QEvent* e) {
224     switch (e->type()) {
225     case JOG_BACK1:
226         emit rewind1();
227         break;
228     case JOG_FWD1:
229         emit forward1();
230         break;
231     case JOG_BACK:
232         emit rewind(-1);
233         break;
234     case JOG_FWD:
235         emit forward(1);
236         break;
237     case JOG_BACK_SLOW:
238         emit rewind(-0.5);
239         break;
240     case JOG_FWD_SLOW:
241         emit forward(0.5);
242         break;
243     case JOG_BACK_FAST:
244         emit rewind(-2);
245         break;
246     case JOG_FWD_FAST:
247         emit forward(2);
248         break;
249     case JOG_STOP:
250         emit stop();
251         break;
252     default:
253         emit button(e->type() - 20000);
254     }
255 }
256
257
258
259 // #include "jogshuttle.moc"
260