]> git.sesse.net Git - kdenlive/blob - src/lib/external/media_ctrl/mediactrl.c
mediactrl: update address of GPL foundation
[kdenlive] / src / lib / external / media_ctrl / mediactrl.c
1 /*
2 * mediactrl.c -- Jog Shuttle device support
3 * Copyright (C) 2001-2007 Dan Dennedy <dan@dennedy.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 #include <stdlib.h>
22 #include <stdio.h>
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/time.h>
27 #include <asm/types.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <errno.h>
33
34 #include <linux/input.h>
35 #include "mediactrl.h"
36
37
38 static char *_shuttle_name = (char*)"Shuttle";
39 static char *_jog_name = (char*)"Jog";
40
41 /*
42         ShuttlePro keys
43 */
44 static struct  media_ctrl_key mc_shuttle_pro_keys[] = {
45         { 0x100, "Button 1", MEDIA_CTRL_F1 },
46         { 0x101, "Button 2", MEDIA_CTRL_F2 },
47         { 0x102, "Button 3", MEDIA_CTRL_F3 },
48         { 0x103, "Button 4", MEDIA_CTRL_F4 },
49         { 0x104, "Button 5", MEDIA_CTRL_B4 },
50         { 0x105, "Button 6", MEDIA_CTRL_B2 },
51         { 0x106, "Button 7", MEDIA_CTRL_B1 },
52         { 0x107, "Button 8", MEDIA_CTRL_B3 },
53         { 0x108, "Button 9", MEDIA_CTRL_B5 },
54         { 0x109, "Button 10", MEDIA_CTRL_B6 },
55         { 0x10a, "Button 11", MEDIA_CTRL_B7 },
56         { 0x10b, "Button 12", MEDIA_CTRL_B8 },
57         { 0x10c, "Button 13", MEDIA_CTRL_B9 },
58         { 0, NULL, 0 }
59 };
60
61
62 /*
63         ShuttleXPress keys
64 */
65 static struct  media_ctrl_key mc_shuttle_xpress_keys[] = {
66     { 0x104, "Button B1", MEDIA_CTRL_B1},
67     { 0x105, "Button B2", MEDIA_CTRL_B2 },
68     { 0x106, "Button B3", MEDIA_CTRL_B3 },
69     { 0x107, "Button B4", MEDIA_CTRL_B4 },
70     { 0x108, "Button B5", MEDIA_CTRL_B5 },
71     { 0, NULL, 0 }
72 };
73
74
75
76 /*
77         JLCooper MCS3 Keys
78 */
79 static struct  media_ctrl_key mc_jlcooper_mcs3_keys[] = {
80         { 0x107, "F1", MEDIA_CTRL_F1 },
81         { 0x101, "F2", MEDIA_CTRL_F2 },
82         { 0x105, "F3", MEDIA_CTRL_F3 },
83         { 0x102, "F4", MEDIA_CTRL_F4 },
84         { 0x103, "F5", MEDIA_CTRL_F5 },
85         { 0x104, "F6", MEDIA_CTRL_F6 },
86         { 0x10d, "W1", MEDIA_CTRL_B6 },
87         { 0x10e, "W2", MEDIA_CTRL_B4 },
88         { 0x100, "W3", MEDIA_CTRL_B2 },
89         { 0x106, "W4", MEDIA_CTRL_B1 },
90         { 0x110, "W5", MEDIA_CTRL_B3 },
91         { 0x111, "W6", MEDIA_CTRL_B5 },
92         { 0x115, "W7", MEDIA_CTRL_B7 },
93         { 0x116, "STICK_LEFT", MEDIA_CTRL_STICK_LEFT },
94         { 0x113, "STICK_RIGHT", MEDIA_CTRL_STICK_RIGHT },
95         { 0x114, "STICK_UP", MEDIA_CTRL_STICK_UP },
96         { 0x112, "STICK_DOWN", MEDIA_CTRL_STICK_DOWN },
97         { 0x10f, "Rewind", MEDIA_CTRL_REWIND },
98         { 0x108, "Fast Forward", MEDIA_CTRL_FAST_FORWARD },
99         { 0x109, "Stop", MEDIA_CTRL_STOP },
100         { 0x10a, "Play", MEDIA_CTRL_PLAY },
101         { 0x10b, "Record", MEDIA_CTRL_RECORD },
102         { 0, NULL, 0 }
103 };
104
105
106 /*
107         Griffin PowerMate
108 */
109 static struct media_ctrl_key mc_powermate_keys[] = {
110         { BTN_0, "Button", MEDIA_CTRL_B1 },
111         { 0, NULL, 0 }
112 };
113
114
115 /*
116         X-Keys Jog/Shuttle
117 */
118 static struct  media_ctrl_key mc_x_keys[] = {
119         { 0x102, "Button L1", MEDIA_CTRL_F1 },
120         { 0x103, "Button L2", MEDIA_CTRL_F9 },
121         { 0x104, "Button L3", MEDIA_CTRL_B1 },
122         { 0x105, "Button L4", MEDIA_CTRL_B3 },
123         { 0x106, "Button L5", MEDIA_CTRL_B5 },
124         { 0x10a, "Button L6", MEDIA_CTRL_F2 },
125         { 0x10b, "Button L7", MEDIA_CTRL_F10 },
126         { 0x10c, "Button L8", MEDIA_CTRL_B2 },
127         { 0x10d, "Button L9", MEDIA_CTRL_B4 },
128         { 0x10e, "Button L10", MEDIA_CTRL_B6 },
129         { 0x112, "Button C1", MEDIA_CTRL_F3 },
130         { 0x11a, "Button C2", MEDIA_CTRL_F4 },
131         { 0x122, "Button C3", MEDIA_CTRL_F5 },
132         { 0x12a, "Button C4", MEDIA_CTRL_F6 },
133         { 0x113, "Button C5", MEDIA_CTRL_F11 },
134         { 0x11b, "Button C6", MEDIA_CTRL_F12 },
135         { 0x123, "Button C7", MEDIA_CTRL_F13 },
136         { 0x12b, "Button C8", MEDIA_CTRL_F14 },
137         { 0x132, "Button R1", MEDIA_CTRL_F7 },
138         { 0x133, "Button R2", MEDIA_CTRL_F15 },
139         { 0x134, "Button R3", MEDIA_CTRL_B7 },
140         { 0x135, "Button R4", MEDIA_CTRL_B9 },
141         { 0x136, "Button R5", MEDIA_CTRL_B11 },
142         { 0x13a, "Button R6", MEDIA_CTRL_F8 },
143         { 0x13b, "Button R7", MEDIA_CTRL_F16 },
144         { 0x13c, "Button R8", MEDIA_CTRL_B8 },
145         { 0x13d, "Button R9", MEDIA_CTRL_B10 },
146         { 0x13e, "Button R10", MEDIA_CTRL_B12 },
147         { 0, NULL, 0 }
148 };
149
150 struct  media_ctrl_key *media_ctrl_get_key(struct media_ctrl *ctrl, int code, int *index)
151 {
152         int i = 0;
153         struct media_ctrl_key *keys = ctrl->device->keys;
154         
155         while ( keys[i].key != 0 ) {
156                 if (keys[i].key == code) {
157                         if (index != NULL)
158                                 *index = i;
159                         return &keys[i];
160                 }
161                 i++;
162         }
163         
164         return NULL;
165 }
166
167 int media_ctrl_get_keys_count(struct media_ctrl *ctrl)
168 {
169     int i = 0;
170     struct media_ctrl_key *keys = ctrl->device->keys;
171
172     while ( keys[i].key != 0 ) {
173         i++;
174     }
175
176     return i;
177 }
178
179 void translate_contour_hid_event(struct media_ctrl *ctrl, struct input_event *ev, struct media_ctrl_event *me) 
180 {
181         
182         int lv, cv;
183         
184         me->type = 0;
185         
186         if (ev->type == EV_REL) {
187                 /* First check the outer dial */
188                 if (ev->code == REL_WHEEL) {
189                         
190                         cv = (signed int)ev->value;
191                         if (cv == 1 || cv == -1 ) cv = 0;
192                         
193                                 
194                         if ( cv == ctrl->lastshu ) return;
195             ctrl->lastshu = cv;
196
197             /* TODO: review this change */
198             if ( cv > 0 ) cv -= 1;
199             if ( cv < 0 ) cv += 1;
200
201                         //printf("Shuttle: %d\n", cv);
202                         me->type  = MEDIA_CTRL_EVENT_SHUTTLE;
203                         me->value = cv*2;
204                         me->name = _shuttle_name;
205                         
206                 } else if  (ev->code == REL_DIAL) {
207                         
208                         if ( ctrl->lastval == -1 ) ctrl->lastval = ev->value;
209                         lv = ctrl->lastval;
210                         cv = ev->value;
211
212                         if ( lv == cv ) return;
213                                 
214                         ctrl->lastval = cv;
215                         
216                         if (cv < 10 && lv > 0xF0) cv +=0x100;
217                         if (lv < 10 && cv > 0xF0) lv +=0x100;
218                         
219                         me->type  = MEDIA_CTRL_EVENT_JOG;
220                         me->value = cv-lv;
221                         me->name = _jog_name;
222                         
223                         ctrl->jogpos += me->value;
224                         //printf("Jog: %06ld (%d)\n", ctrl->jogpos, me->value);
225                 }
226                 return;
227         } else if (ev->type == EV_KEY) {
228                 int index;
229                 struct media_ctrl_key *key = media_ctrl_get_key(ctrl, ev->code, &index);
230                 if ( key == NULL ) return;
231                 
232                 me->type  = MEDIA_CTRL_EVENT_KEY;
233                 me->code = key->code;
234                 me->value = ev->value;
235                 me->name = ( char* )key->name;
236                 me->index = index;
237                  
238                 //printf("Key: %04x %02x: %s\n", ev->code, ev->value, key->name);
239                 
240         }
241         
242 }
243
244 void translate_compliant(struct media_ctrl *ctrl, struct input_event *ev, struct media_ctrl_event *me) 
245 {
246         me->type = 0;
247         
248         // printf("Translate %02x %02x\n", ev->type, ev->code );
249         
250         if (ev->type == EV_REL) {
251                 if  (ev->code == REL_DIAL) {
252                         
253                         me->type  = MEDIA_CTRL_EVENT_JOG;
254                         me->value = (signed int)ev->value;
255                         me->name = _jog_name;
256                         
257                         ctrl->jogpos += me->value;
258                         //printf("Jog: %06ld (%d)\n", ctrl->jogpos, me->value);
259         }
260                 return;
261         } else if (ev->type == EV_ABS) {
262                 // printf("ABS\n" );
263                 if  ( ev->code == 0x1c || ev->code == ABS_THROTTLE ) {
264                         //printf("ABS_MISC\n" );
265                         me->type  = MEDIA_CTRL_EVENT_SHUTTLE;
266                         me->value = (signed int)ev->value;
267                         me->name = _shuttle_name;
268                         
269                         ctrl->shuttlepos = me->value;
270                         //printf("Shuttle: %06d (%d)\n", ctrl->shuttlepos, me->value);
271                 }
272         } else if (ev->type == EV_KEY) {
273                 int index;
274                 struct media_ctrl_key *key = media_ctrl_get_key(ctrl, ev->code, &index);
275                 if ( key == NULL ) return;
276                 
277                 me->type  = MEDIA_CTRL_EVENT_KEY;
278                 me->code = key->code;
279                 me->value = ev->value;
280                 me->name = ( char* )key->name;
281                 me->index = index;
282                  
283                 //printf("Key: %04x %02x: %s\n", ev->code, ev->value, key->name);
284                 
285         }
286 }
287
288 struct media_ctrl_device supported_devices[] = {
289         { 0x0b33, 0x0030, "Contour Design ShuttlePRO v2", mc_shuttle_pro_keys, translate_contour_hid_event },
290         { 0x0b33, 0x0020, "Contour Design ShuttleXpress", mc_shuttle_xpress_keys, translate_contour_hid_event },
291         { 0x0b33, 0x0010, "Contour Design ShuttlePro", mc_shuttle_pro_keys, translate_contour_hid_event },
292         { 0x0b33, 0x0011, "Contour Design ShuttlePro", mc_shuttle_pro_keys, translate_contour_hid_event }, /* Hercules OEM */
293         { 0x05f3, 0x0240, "Contour Design ShuttlePro", mc_shuttle_pro_keys, translate_contour_hid_event },
294         { 0x0760, 0x0001, "JLCooper MCS3", mc_jlcooper_mcs3_keys, translate_compliant },
295         { 0x077d, 0x0410, "Griffin PowerMate", mc_powermate_keys, translate_compliant },
296         { 0x05f3, 0x0241, "X-Keys Editor", mc_x_keys, translate_contour_hid_event },
297         { 0, 0, 0, 0, 0 }
298 };
299
300
301 void media_ctrl_translate(struct media_ctrl *ctrl, struct input_event *ev, struct media_ctrl_event *me) 
302 {
303         if ( ctrl->device ) ctrl->device->translate(ctrl, ev, me);
304 }
305
306
307 void media_ctrl_read_event(struct media_ctrl *ctrl, struct media_ctrl_event *me) 
308 {
309         ssize_t n;
310         struct input_event ev;
311                 
312         // struct media_ctrl_event me;
313         
314         if ( ctrl->fd > 0 ) {
315                 n = read(ctrl->fd, &ev, sizeof(ev));
316         } else {
317                 return;
318         }
319         
320         if (n != sizeof(ev)) {
321                 //printf("JogShuttle::inputCallback: read: (%d) %s\n", errno, strerror(errno));
322                 close(ctrl->fd);
323                 ctrl->fd = -1;
324                 return;
325         }
326         
327         if ( ctrl->device && ctrl->device->translate)
328                 ctrl->device->translate(ctrl, &ev, me);
329         else
330                 me->type = 0;
331         
332         if ( me->type  == MEDIA_CTRL_EVENT_JOG ) {
333                 struct timeval timev;
334                 gettimeofday(&timev, NULL);
335                 unsigned long now = (unsigned long)timev.tv_usec + (1000000*(unsigned long)timev.tv_sec);
336                 if ( now < ctrl->last_jog_time + 40000 ) {
337                         //printf("*** Fast Jog %02d %05d ***\n", me->value, now - ctrl->last_jog_time);
338                         ctrl->jogrel = me->value;
339                         me->type = MEDIA_CTRL_EVENT_NONE;
340                 } else {
341                         me->value += ctrl->jogrel;
342                         ctrl->jogrel = 0;
343                         ctrl->last_jog_time = now;
344                         // printf("*** Jog %02d ***\n", me->value);
345                 }
346         }
347         
348         return;
349         
350 }
351
352
353 int probe_device(struct media_ctrl *mc)
354 {
355         short devinfo[4];
356         int i = 0;
357           
358         if ( ioctl(mc->fd, EVIOCGID, &devinfo) ) {
359                 perror("evdev ioctl");
360                 return 0;
361         }
362         
363         do {
364                 if ( supported_devices[i].vendor == devinfo[1] 
365                         && supported_devices[i].product == devinfo[2] ) {
366                                 
367                         mc->device = &supported_devices[i];
368                         //printf("Success on /dev/input/event%d: %s\n", mc->eventno, mc->device->name);
369                         // mc->fd = fd;
370                         // mc->translate = mc->device.translate_function;
371                         // mc = malloc(sizeof(struct media_ctrl));
372                         mc->jogpos  = 0;
373                         mc->lastval = -1;
374                         mc->last_jog_time = 0;
375                         return 1;
376                 } else {
377                         //mc->device = NULL;
378                 }
379         
380         } while ( supported_devices[++i].vendor != 0 );
381                         
382         return 0;
383 }
384
385
386 void media_ctrl_get_device_list() 
387 {
388         // TBD
389 }
390
391
392
393 void find_first_device(struct media_ctrl *mc) 
394 {
395         char buf[256];
396         int fd, i;
397         
398         for ( i = 0; i < 32; i++ ) {
399                 sprintf(buf, "/dev/input/event%d", i); 
400                 fd = open( buf, O_RDONLY );
401                 if ( fd < 0 ) {
402                         perror(buf);
403                 } else {
404                         mc->fd = fd;
405                         mc->eventno = i;
406                         if( probe_device(mc) ) {
407                                 return;
408                         } else {                
409                                 close(fd);
410                                 mc->fd = -1;
411                         }
412                 }
413         }
414         return;
415 }
416
417
418 void media_ctrl_close(struct media_ctrl *mc)
419 {
420         if (mc->fd > 0)
421                 close( mc->fd );
422         memset( mc, 0, sizeof( struct media_ctrl ) );
423 }
424
425
426 void media_ctrl_open(struct media_ctrl *mc) 
427 {
428         find_first_device(mc);
429 }
430
431 void media_ctrl_open2(struct media_ctrl *mc, const char *devname)
432 {
433     int fd;
434
435     fd = open( devname, O_RDONLY );
436     if ( fd < 0 ) {
437         perror(devname);
438         mc->fd = -1;
439     } else {
440         mc->fd = fd;
441         //mc->eventno = i;
442         if( probe_device(mc) ) {
443             return;
444         } else {
445             close(fd);
446             mc->fd = -1;
447         }
448     }
449 }
450