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