]> git.sesse.net Git - kdenlive/blob - src/jogshuttle.cpp
Apply patch from P. Fleury to improve jog shuttle speed handling.
[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 #define DELAY 10
37
38 #define KEY 1
39 #define KEY1 256
40 #define KEY2 257
41 #define KEY3 258
42 #define KEY4 259
43 #define KEY5 260
44 #define KEY6 261
45 #define KEY7 262
46 #define KEY8 263
47 #define KEY9 264
48 #define KEY10 265
49 #define KEY11 266
50 #define KEY12 267
51 #define KEY13 268
52 #define KEY14 269
53 #define KEY15 270
54
55 // Constants for the returned events when reading them from the device
56 #define JOGSHUTTLE 2
57 #define JOG 7
58 #define SHUTTLE 8
59
60 // Constants for the signals sent.
61 #define JOG_BACK1 10001
62 #define JOG_FWD1 10002
63
64 // middle value for shuttle, will be +/-MAX_SHUTTLE_RANGE
65 #define JOG_STOP 10010
66 #define MAX_SHUTTLE_RANGE 7
67
68 // TODO(fleury): this should probably be a user configuration parameter (at least the max speed).
69 const double SPEEDS[MAX_SHUTTLE_RANGE + 1] = {0.0, 0.5, 1.0, 2.0, 4.0, 8.0, 16.0, 32.0};
70
71
72 void ShuttleThread::init(QObject *parent, QString device)
73 {
74     m_parent = parent;
75     m_device = device;
76     stop_me = false;
77     m_isWorking = false;
78     shuttlevalue = 0xffff;
79     shuttlechange = false;
80     jogvalue = 0xffff;
81 }
82
83 bool ShuttleThread::isWorking()
84 {
85     return m_isWorking;
86 }
87
88 void ShuttleThread::run()
89 {
90     kDebug() << "-------  STARTING SHUTTLE: " << m_device;
91
92     const int fd = KDE_open((char *) m_device.toUtf8().data(), O_RDONLY);
93     if (fd < 0) {
94         fprintf(stderr, "Can't open Jog Shuttle FILE DESCRIPTOR\n");
95         return;;
96     }
97     EV ev;
98
99     if (ioctl(fd, EVIOCGRAB, 1) < 0) {
100         fprintf(stderr, "Can't get exclusive access on  Jog Shuttle FILE DESCRIPTOR\n");
101         return;;
102     }
103
104     int num_warnings = 0;
105     while (!stop_me) {
106         if (read(fd, &ev, sizeof(ev)) < 0) {
107             if (num_warnings % 100 == 0)
108                 fprintf(stderr, "Failed to read event from Jog Shuttle FILE DESCRIPTOR (repeated %d times)\n", num_warnings + 1);
109             num_warnings++;
110         }
111         handle_event(ev);
112     }
113     close(fd);
114
115 }
116
117 void ShuttleThread::handle_event(EV ev)
118 {
119     switch (ev.type) {
120     case KEY :
121         key(ev.code, ev.value);
122         break;
123     case JOGSHUTTLE :
124         if (ev.code == JOG)
125             jog(ev.value);
126         if (ev.code == SHUTTLE)
127             shuttle(ev.value);
128         break;
129     }
130 }
131
132 void ShuttleThread::key(unsigned short code, unsigned int value)
133 {
134     if (value == 0) {
135         // Button release (ignored)
136         return;
137     }
138
139     // Check key index
140     code -= KEY1 - 1;
141     if (code > 16)
142         return;
143
144     kDebug() << "Button PRESSED: " << code;
145     QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(20000 + code)));
146
147 }
148
149 void ShuttleThread::shuttle(int value)
150 {
151     //gettimeofday( &last_shuttle, 0 );
152     //need_synthetic_shuttle = value != 0;
153
154     if (value == shuttlevalue)
155         return;
156
157     if (value > MAX_SHUTTLE_RANGE || value < -MAX_SHUTTLE_RANGE) {
158         fprintf(stderr, "Jog Shuttle returned value of %d (should be between -%d ad +%d)", value, MAX_SHUTTLE_RANGE, MAX_SHUTTLE_RANGE);
159         return;
160     }
161     shuttlevalue = value;
162     shuttlechange = true;
163     QApplication::postEvent(m_parent, new QEvent((QEvent::Type)(JOG_STOP + value)));
164 }
165
166 void ShuttleThread::jog(unsigned int value)
167 {
168     // generate a synthetic event for the shuttle going
169     // to the home position if we have not seen one recently.
170     //if (shuttlevalue != 0) {
171     //  QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
172     //  shuttlevalue = 0;
173     //}
174
175     // This code takes care of wrapping around the limits of the jog dial number (it is represented as a single byte, hence
176     // wraps at the 0/255 boundary). I used 25 as the difference to make sure that even in heavy load, with the jog dial
177     // turning fast and we miss some events that we do not mistakenly reverse the direction.
178     // Note also that at least the Contour ShuttlePRO v2 does not send an event for the value 0, so at the wrap it will
179     // need 2 events to go forward. But that is nothing we can do about...
180     if (jogvalue != 0xffff) {
181         //fprintf(stderr, "value=%d jogvalue=%d\n", value, jogvalue);
182         bool wrap = abs(value - jogvalue) > 25;
183         bool rewind = value < jogvalue;
184         bool forward = value > jogvalue;
185         if ((rewind && !wrap) || (forward && wrap))
186             QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_BACK1));
187         else if ((forward && !wrap) || (rewind && wrap))
188             QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_FWD1));
189         else if (!forward && !rewind && !shuttlechange)
190             // An event without changing the jog value is sent after each shuttle change.
191             // As the shuttle rest position does not get a shuttle event, only a non-position-changing jog event.
192             // Hence we stop on this when we see 2 non-position-changing jog events in a row.
193             QApplication::postEvent(m_parent, new QEvent((QEvent::Type) JOG_STOP));
194     }
195     jogvalue = value;
196     shuttlechange = false;
197 }
198
199
200 JogShuttle::JogShuttle(QString device, QObject *parent) :
201         QObject(parent)
202 {
203     initDevice(device);
204 }
205
206 JogShuttle::~JogShuttle()
207 {
208     if (m_shuttleProcess.isRunning()) m_shuttleProcess.exit();
209 }
210
211 void JogShuttle::initDevice(QString device)
212 {
213     if (m_shuttleProcess.isRunning()) {
214         if (device == m_shuttleProcess.m_device) return;
215         stopDevice();
216     }
217     m_shuttleProcess.init(this, device);
218     m_shuttleProcess.start(QThread::LowestPriority);
219 }
220
221 void JogShuttle::stopDevice()
222 {
223     if (m_shuttleProcess.isRunning())
224         m_shuttleProcess.stop_me = true;
225 }
226
227 void JogShuttle::customEvent(QEvent* e)
228 {
229     int code = e->type();
230
231     // Handle the job events
232     if (code == JOG_BACK1) {
233         emit rewind1();
234         return;
235     }
236     if (code == JOG_FWD1) {
237         emit forward1();
238         return;
239     }
240
241     //handle the shuttle events
242     if (code == JOG_STOP) {
243         // TODO(fleury): to make sure stop() has an effect, as it doesn't when in rewind mode, we set it to forward with
244         // a value that will for sure not move to the next frame.
245         if (m_shuttleProcess.shuttlevalue < 0)
246             emit forward(0.01);
247         emit stop();
248         return;
249     }
250
251     int shuttle = code - JOG_STOP;
252     if (shuttle >= -MAX_SHUTTLE_RANGE && shuttle <= MAX_SHUTTLE_RANGE) {
253         if (shuttle < 0)
254             emit rewind(-SPEEDS[abs(shuttle)]);
255         else
256             emit forward(SPEEDS[abs(shuttle)]);
257         return;
258     }
259
260     // we've got a key event.
261     emit button(e->type() - 20000);
262 }
263
264
265
266 // #include "jogshuttle.moc"
267