/
hama_mce.cpp
437 lines (394 loc) · 13.7 KB
/
hama_mce.cpp
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* Copyright (C) 2010-2018 xhaggi, AaYJFG
*
* 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, 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 Kodi event client for Hama MCE Remote Control; see the
* file COPYING. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <fcntl.h>
#include <signal.h>
#include <getopt.h>
#include <libusb-1.0/libusb.h>
#include "xbmcclient.h"
#define VERSION "1.5"
#define msg(x...) do { fprintf(stdout, x); } while (0)
#define err(x...) do { fprintf(stderr, x); } while (0)
#define PCHK(x...) \
do { \
int __err = (x); \
if (__err == -1) { \
err("*** POSIX ERROR *** in %s:%d [%s]: ", __FILE__, __LINE__, #x); \
perror(""); \
abort(); \
} \
} while (0)
#define UCHK(x...) \
do { \
int __err = (x); \
if (__err < 0) \
err("*** USB ERROR *** in %s:%d [%s]: %d\n", __FILE__, __LINE__, #x, __err); \
} while (0)
static CXBMCClient xbmc;
static bool disconnected = false;
static bool quit = false;
static void print_ir_code (const char *prefix, struct libusb_transfer *transfer)
{
msg("** %s : ", prefix);
for (int i = 0; i < transfer->actual_length; i++)
msg("%02x ", transfer->buffer[i] & 0xff);
msg("\n");
}
static void xbmc_action_button (const char *btn)
{
msg("## send action '%s' (button)\n", btn);
xbmc.SendACTION(btn, ACTION_BUTTON);
}
static void xbmc_key (const char *key)
{
msg("## send key (R1) '%s'\n", key);
xbmc.SendButton(key, "R1", BTN_NO_REPEAT);
}
static void xbmc_keyr (const char *key)
{
msg("## send repeat key (R1) '%s'\n", key);
xbmc.SendButton(key, "R1", BTN_DOWN);
}
static void xbmc_release_button ()
{
msg("## send key released\n");
xbmc.SendButton(0x01, BTN_UP);
}
static void emit_left_click (void)
{
msg("## catched button mouse left\n");
xbmc_key("title");
}
static void emit_right_click (void)
{
msg("## catched button mouse right\n");
xbmc_action_button("contextmenu");
}
static void emit_info(void)
{
msg("## catched button info\n");
xbmc_key("info");
}
static void emit_mouse (int dx, int dy)
{
if (dx != 0 || dy != 0) {
const int maxcoord = 65535;
static int x = maxcoord / 2;
static int y = maxcoord / 2;
x += dx * 64;
y += dy * 64;
if (x < 0)
x = 0;
if (y < 0)
y = 0;
if (x > maxcoord)
x = maxcoord;
if (y > maxcoord)
y = maxcoord;
msg("## mouse dx %d, dy %d -- x %d y %d\n", dx, dy, x, y);
xbmc.SendMOUSE(x, y);
}
}
static void transfer0x81_cb (struct libusb_transfer *transfer)
{
static const struct {
char modifier;
char code;
bool can_repeat;
void (*key_fn) (const char *key);
const char *key;
} translation_table [] = {
{ 0x03, 0x17, false, xbmc_key, "mytv" }, /* (yellow) TV */
{ 0x01, 0x10, false, xbmc_key, "mymusic" }, /* (blue) Music */
{ 0x01, 0x0c, false, xbmc_key, "mypictures" }, /* (green) Pictures */
{ 0x01, 0x08, false, xbmc_key, "myvideo" }, /* (red) Videos */
{ 0x01, 0x12, false, xbmc_key, "recordedtv" }, /* grey button under yellow */
{ 0x01, 0x0a, false, xbmc_key, "guide" }, /* grey button under blue */
{ 0x01, 0x17, false, xbmc_key, "livetv" }, /* grey button under green */
{ 0x03, 0x10, false, xbmc_key, "menu" }, /* grey button under red */
{ 0x03, 0x05, false, xbmc_key, "reverse" },
{ 0x03, 0x09, false, xbmc_key, "forward" },
{ 0x01, 0x15, false, xbmc_key, "record" },
{ 0x00, 0x2a, false, xbmc_key, "back" },
{ 0x00, 0x28, false, xbmc_key, "select" }, /* ok botton */
{ 0x00, 0x4f, true, xbmc_keyr, "right" },
{ 0x00, 0x50, true, xbmc_keyr, "left" },
{ 0x00, 0x51, true, xbmc_keyr, "down" },
{ 0x00, 0x52, true, xbmc_keyr, "up" },
{ 0x00, 0x4b, true, xbmc_keyr, "pageplus" },
{ 0x00, 0x4e, true, xbmc_keyr, "pageminus" },
{ 0x0c, 0x28, false, xbmc_key, "start" },
{ 0x00, 0x59, false, xbmc_key, "one" },
{ 0x00, 0x5a, false, xbmc_key, "two" },
{ 0x00, 0x5b, false, xbmc_key, "three" },
{ 0x00, 0x5c, false, xbmc_key, "four" },
{ 0x00, 0x5d, false, xbmc_key, "five" },
{ 0x00, 0x5e, false, xbmc_key, "six" },
{ 0x00, 0x5f, false, xbmc_key, "seven" },
{ 0x00, 0x60, false, xbmc_key, "eight" },
{ 0x00, 0x61, false, xbmc_key, "nine" },
{ 0x00, 0x62, false, xbmc_key, "zero" },
{ 0x00, 0x55, false, xbmc_key, "star" },
{ 0x04, 0x5d, false, xbmc_key, "hash" },
{ 0x00, 0x29, false, xbmc_key, "clear" },
{ 0x04, 0x3d, false, xbmc_key, "subtitle" }, /* close button */
{ 0x04, 0x5b, false, NULL, "Alt+3 [discard]" }, /* discard - hash */
{ 0x00, 0x53, false, NULL, "NumLock [discard]" }, /* discard - key down */
{ 0x0c, 0x00, false, NULL, "start [discard]" }, /* discard - mypictures*/
{ 0x08, 0x00, false, NULL, "start [discard]" }, /* discard - start*/
{ 0x04, 0x00, false, NULL, "Alt [discard]" }, /* discard - hash, close*/
{ 0x03, 0x00, false, NULL, "Shift [discard]" }, /* discard - mytv, menu, rewind, fastforward*/
{ 0x01, 0x00, false, NULL, "Ctrl [discard]" } /* discard - key down */
};
static bool repeating = false;
if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
disconnected = true;
return;
}
if (transfer->actual_length == 8) {
char m = transfer->buffer[0];
char c = transfer->buffer[2];
if (m == 0x00 && c == 0x00) {
if (repeating)
xbmc_release_button();
goto done;
}
for (unsigned int i = 0; i < sizeof(translation_table)/sizeof(translation_table[0]); i++) {
if (translation_table[i].modifier == m && translation_table[i].code == c) {
print_ir_code(translation_table[i].key, transfer);
if (translation_table[i].key_fn != NULL) {
translation_table[i].key_fn(translation_table[i].key);
repeating = translation_table[i].can_repeat;
}
goto done;
}
}
}
if (transfer->actual_length > 0) {
msg("*** unknown keycode: ");
for (int i = 0; i < transfer->actual_length; i++)
msg("%02x ", transfer->buffer[i] & 0xff);
msg("\n");
}
done:
UCHK(libusb_submit_transfer(transfer));
}
static void transfer0x82_cb (struct libusb_transfer *transfer)
{
static const struct {
char code [5];
bool can_repeat;
void (*key_fn) (const char *key);
const char *key;
} translation_table [] = {
{ { 0x03, 0x02, 0x55, 0x55, 0x55 }, false, xbmc_key, "power" },
{ { 0x02, 0x02, 0x00, 0x00, 0x55 }, false, xbmc_key, "display" },
{ { 0x02, (char)0x80, 0x00, 0x00, 0x55 }, false, xbmc_key, "skipminus" },
{ { 0x02, 0x00, 0x02, 0x00, 0x55 }, false, xbmc_key, "skipplus" },
{ { 0x02, 0x00, 0x00, 0x01, 0x55 }, false, xbmc_key, "stop" },
{ { 0x02, 0x00, 0x00, 0x02, 0x55 }, false, xbmc_key, "pause" },
{ { 0x02, 0x00, 0x01, 0x00, 0x55 }, false, xbmc_key, "mute" },
{ { 0x02, 0x10, 0x00, 0x00, 0x55 }, true, xbmc_keyr, "volumeplus" },
{ { 0x02, 0x00, 0x10, 0x00, 0x55 }, true, xbmc_keyr, "volumeminus" }
};
static bool repeating = false;
static int rclick_pending = 0;
if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
disconnected = true;
return;
}
if (transfer->status == LIBUSB_TRANSFER_TIMED_OUT) {
/*
if (rclick_pending == 1) {
print_ir_code("info", transfer);
rclick_pending = 0;
emit_info();
}
*/
goto done;
}
if (transfer->actual_length == 5) {
const char keycode_up [5] = { 0x02, 0x00, 0x00, 0x00, 0x55 };
const char keycode_up_power [5] = { 0x03, 0x00, 0x55, 0x55, 0x55 };
const char keycode_up_mouse [5] = { 0x01, 0x00, 0x00, 0x00, 0x00 };
if (memcmp(transfer->buffer, keycode_up, 5) == 0 ||
memcmp(transfer->buffer, keycode_up_power, 5) == 0 ||
memcmp(transfer->buffer, keycode_up_mouse, 5) == 0) {
if (repeating)
xbmc_release_button();
if (rclick_pending > 0 && --rclick_pending == 0) {
print_ir_code("right click", transfer);
emit_right_click();
}
goto done;
}
if (transfer->buffer[0] == 0x01) {
if (transfer->buffer[1] == 0x00) {
char dx = transfer->buffer[2];
char dy = transfer->buffer[3];
if (dx != 0 || dy != 0) {
print_ir_code("mouse", transfer);
emit_mouse(dx, dy);
}
}
else if (transfer->buffer[1] == 0x01) {
print_ir_code("left click", transfer);
emit_left_click();
}
else if (transfer->buffer[1] == 0x02) {
print_ir_code("catching right click/info", transfer);
rclick_pending = 2;
emit_info();
}
goto done;
}
for (unsigned int i = 0; i < sizeof(translation_table)/sizeof(translation_table[0]); i++) {
if (memcmp(translation_table[i].code, transfer->buffer, 5) == 0) {
print_ir_code(translation_table[i].key, transfer);
if (translation_table[i].key_fn != NULL) {
translation_table[i].key_fn(translation_table[i].key);
repeating = translation_table[i].can_repeat;
}
goto done;
}
}
msg("*** unknown keycode: ");
for (int i = 0; i < transfer->actual_length; i++)
msg("%02x ", transfer->buffer[i] & 0xff);
msg("\n");
}
done:
UCHK(libusb_submit_transfer(transfer));
}
static void handle_exit(int sig)
{
quit = true;
}
void print_usage(char* progname)
{
msg("HAMA MCE remote event client for XBMC\n");
msg("\tUsage: %s [options]\n", progname);
msg("\t\t -h --help\t\tdisplay usage summary\n");
msg("\t\t -v --version\t\tdisplay version\n");
msg("\t\t -d --daemon\t\trun in background\n");
}
int main (int argc, char **argv)
{
bool daemonize = false;
while (true) {
int c;
static struct option long_options[] =
{
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{"daemon", no_argument, NULL, 'd'},
{"fork", no_argument, NULL, 'f'},
{0, 0, 0, 0}
};
c = getopt_long(argc, argv, "hvd", long_options, NULL);
if (c == -1)
break;
switch (c) {
case 'h':
print_usage(argv[0]);
exit(EXIT_SUCCESS);
case 'v':
msg("HAMA MCE remote event client v%s for XBMC\n", VERSION);
exit(EXIT_SUCCESS);
case 'd':
daemonize = true;
break;
default:
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
}
if (optind < (argc - 1)) {
err("%s: too many arguments\n", argv[0]);
exit(EXIT_FAILURE);
}
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_handler = handle_exit;
PCHK(sigaction(SIGINT, &sa, NULL));
PCHK(sigaction(SIGTERM, &sa, NULL));
libusb_context *ctx;
libusb_device_handle *dev;
struct libusb_transfer *transfer0x81 = libusb_alloc_transfer(0);
struct libusb_transfer *transfer0x82 = libusb_alloc_transfer(0);
unsigned char buf0x81 [8];
unsigned char buf0x82 [5];
UCHK(libusb_init(&ctx));
if (!(dev = libusb_open_device_with_vid_pid(ctx, 0x05a4, 0x9881))) {
err("%s: No HAMA MCE remote control found.\n", argv[0]);
exit(EXIT_FAILURE);
}
int exit_code = EXIT_SUCCESS;
if (libusb_kernel_driver_active(dev, 0))
UCHK(libusb_detach_kernel_driver(dev, 0));
if (libusb_kernel_driver_active(dev, 1))
UCHK(libusb_detach_kernel_driver(dev, 1));
UCHK(libusb_claim_interface(dev, 0));
UCHK(libusb_claim_interface(dev, 1));
libusb_fill_interrupt_transfer(transfer0x81, dev, 0x81, buf0x81, sizeof(buf0x81), transfer0x81_cb, NULL, 215);
UCHK(libusb_submit_transfer(transfer0x81));
libusb_fill_interrupt_transfer(transfer0x82, dev, 0x82, buf0x82, sizeof(buf0x82), transfer0x82_cb, NULL, 200);
UCHK(libusb_submit_transfer(transfer0x82));
msg("Connected HAMA MCE Remote\n");
xbmc.SendHELO("HAMA MCE Remote", ICON_NONE);
if (daemonize) {
if (daemon(0,0) == -1) {
err("Failed to fork\n");
perror(argv[0]);
exit_code = EXIT_FAILURE;
goto exit;
}
}
while (!(disconnected || quit)) {
UCHK(libusb_handle_events(ctx));
}
exit:
if (disconnected) {
msg("Disconnected HAMA MCE Remote\n");
xbmc.SendNOTIFICATION("Disconnected", "HAMA MCE Remote", ICON_NONE);
}
else {
msg("Closing HAMA MCE Remote\n");
xbmc.SendNOTIFICATION("Closing", "HAMA MCE Remote", ICON_NONE);
}
libusb_free_transfer(transfer0x81);
libusb_free_transfer(transfer0x82);
if (!disconnected) {
// Release the remote back to the system
UCHK(libusb_release_interface(dev, 0));
UCHK(libusb_release_interface(dev, 1));
UCHK(libusb_attach_kernel_driver(dev, 0));
UCHK(libusb_attach_kernel_driver(dev, 1));
}
libusb_close(dev);
libusb_exit(ctx);
exit(exit_code);
}