-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhidraw.c
More file actions
189 lines (157 loc) · 4.25 KB
/
Copy pathhidraw.c
File metadata and controls
189 lines (157 loc) · 4.25 KB
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
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/hidraw.h>
#include "hid.h"
#include "hidraw.h"
#include "remote.h"
#include "utils.h"
/* Keymaps */
static const char *keymap_strings[] = {
[0x00] = "KEY_1", /* number pad */
[0x01] = "KEY_2",
[0x02] = "KEY_3",
[0x03] = "KEY_4",
[0x04] = "KEY_5",
[0x05] = "KEY_6",
[0x06] = "KEY_7",
[0x07] = "KEY_8",
[0x08] = "KEY_9",
[0x09] = "KEY_0",
[0x0b] = "KEY_ENTER", /* enter */
[0x1a] = "KEY_M", /* top menu */
[0x30] = "KEY_PREVIOUSSONG", /* step back */
[0x31] = "KEY_NEXTSONG", /* step forward */
[0x32] = "KEY_PLAY", /* play */
[0x33] = "KEY_REWIND", /* rewind */
[0x34] = "KEY_FASTFORWARD", /* fast forward */
[0x38] = "KEY_STOP", /* stop */
[0x39] = "KEY_PAUSE", /* pause */
[0x40] = "KEY_P", /* guide/popup */
[0x43] = "KEY_H", /* home */
[0x50] = "KEY_L", /* select/list */
[0x53] = "KEY_A", /* start/aspect */
[0x54] = "KEY_UP", /* d-pad up */
[0x55] = "KEY_RIGHT", /* d-pad right */
[0x56] = "KEY_DOWN", /* d-pad down */
[0x57] = "KEY_LEFT", /* d-pad left */
[0x5c] = "KEY_V", /* triangle */
[0x5d] = "KEY_O", /* circle */
[0x5e] = "KEY_X", /* cross */
[0x5f] = "KEY_U", /* square */
[0x60] = "KEY_PREVIOUSSONG", /* step back */
[0x61] = "KEY_NEXTSONG", /* step forward */
[0x63] = "KEY_S", /* subt */
[0x64] = "KEY_D", /* audio */
[0x65] = "KEY_N", /* angle */
[0x70] = "KEY_I", /* info */
[0x80] = "KEY_B", /* blue/move */
[0x81] = "KEY_R", /* red/pip */
[0x82] = "KEY_G", /* green/swap */
[0x83] = "KEY_Y", /* yellow */
};
static const uint16_t keymap_buttons[] = {
[0x00] = KEY_1, /* number pad */
[0x01] = KEY_2,
[0x02] = KEY_3,
[0x03] = KEY_4,
[0x04] = KEY_5,
[0x05] = KEY_6,
[0x06] = KEY_7,
[0x07] = KEY_8,
[0x08] = KEY_9,
[0x09] = KEY_0,
[0x0b] = KEY_ENTER, /* enter */
[0x1a] = KEY_M, /* top menu */
[0x30] = KEY_PREVIOUSSONG, /* step back */
[0x31] = KEY_NEXTSONG, /* step forward */
[0x32] = KEY_PLAY, /* play */
[0x33] = KEY_REWIND, /* rewind */
[0x34] = KEY_FASTFORWARD, /* fast forward */
[0x38] = KEY_STOP, /* stop */
[0x39] = KEY_PAUSE, /* pause */
[0x40] = KEY_P, /* guide/popup */
[0x43] = KEY_H, /* home */
[0x50] = KEY_L, /* select/list */
[0x53] = KEY_A, /* start/aspect */
[0x54] = KEY_UP, /* d-pad up */
[0x55] = KEY_RIGHT, /* d-pad right */
[0x56] = KEY_DOWN, /* d-pad down */
[0x57] = KEY_LEFT, /* d-pad left */
[0x5c] = KEY_V, /* triangle */
[0x5d] = KEY_O, /* circle */
[0x5e] = KEY_X, /* cross */
[0x5f] = KEY_U, /* square */
[0x60] = KEY_PREVIOUSSONG, /* step back */
[0x61] = KEY_NEXTSONG, /* step forward */
[0x63] = KEY_S, /* subt */
[0x64] = KEY_D, /* audio */
[0x65] = KEY_N, /* angle */
[0x70] = KEY_I, /* info */
[0x80] = KEY_B, /* blue/move */
[0x81] = KEY_R, /* red/pip */
[0x82] = KEY_G, /* green/swap */
[0x83] = KEY_Y, /* yellow */
};
int
verify_id (struct udev_device *dev)
{
const char *hid_id;
struct udev_device *parent;
parent = udev_device_get_parent_with_subsystem_devtype(dev, "hid", NULL);
hid_id = udev_device_get_property_value(parent, "HID_ID");
if (strncmp(hid_id, REMOTE_HID_ID, 22)) {
fprintf(stderr, "open_hidraw: hid_id mismatch (%s)\n", hid_id);
return 0;
}
return 1;
}
/*
* open_hidraw
* Returns a FD for reading the hidraw, or -1 if the hidraw did not match.
*/
int
open_hidraw (struct udev_device *dev)
{
int fd;
const char *devnode;
if (!verify_id(dev)) {
return -1;
}
/* Open device for reading */
devnode = udev_device_get_devnode(dev);
fd = open(devnode, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
perror("open_hidraw");
return -1;
}
fprintf(stderr, "Opened %s.\n", devnode);
DEBUG_FN(print_hidinfo(fd));
return fd;
}
static void
debug_scancode (struct report *usage)
{
int size;
int i;
size = sizeof(struct report);
printf("<=");
for (i=size - 2; i>=0; i-=2) {
printf(" 0x%04x", (*(uint16_t*)((uint8_t*)usage+i)));
}
putchar('\n');
}
uint16_t
read_hidraw (int fd)
{
struct report usage;
memset(&usage, 0, sizeof usage);
read(fd, &usage, sizeof usage);
DEBUG_FN(debug_scancode(&usage));
if (usage.key == 0xff) {
return RELEASE;
}
debug_print("got 0x%02x = %s\n", usage.key, keymap_strings[usage.key]);
return keymap_buttons[usage.key];
}