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
|
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include "utils.h"
#include "shared.h"
#include "rcm-server-main.h"
#include "rcm-server-keymap.h"
#include "rcm-server-lirc.h"
#include "rcm-server-evdev.h"
#include "rcm-server-rcdev.h"
struct rc_event {
uint16_t type;
uint16_t code;
uint32_t val;
} __attribute__((packed));
/* rc_event.type value */
#define RC_DEBUG 0x0
#define RC_CORE 0x1
#define RC_KEY 0x2
#define RC_IR 0x3
/* RC_CORE codes */
#define RC_CORE_DROPPED 0x0
/* RC_KEY codes */
#define RC_KEY_REPEAT 0x0
#define RC_KEY_PROTOCOL 0x1
#define RC_KEY_SCANCODE 0x2
#define RC_KEY_SCANCODE_PART 0x3
#define RC_KEY_TOGGLE 0x4
/* RC_IR codes */
#define RC_IR_SPACE 0x0
#define RC_IR_PULSE 0x1
#define RC_IR_START 0x2
#define RC_IR_STOP 0x3
#define RC_IR_RESET 0x4
#define RC_IR_CARRIER 0x5
#define RC_IR_DUTY_CYCLE 0x6
static int
rcdev_read(sd_event_source *s, int fd, uint32_t revents, void *userdata)
{
struct rc_device *rcdev = userdata;
if (fd != rcdev->rcdev_fd)
fprintf(stderr, "rcdev fd mismatch: %i != %i\n", rcdev->rcdev_fd, fd);
if (revents & EPOLLHUP) {
fprintf(stderr, "rcdev connection closed!\n");
rcdev_close(rcdev);
return 0;
}
if (!(revents & EPOLLIN)) {
fprintf(stderr, "unexpected rcdev event: %" PRIu32 "\n", revents);
return 0;
}
while (true) {
struct rc_event ev;
ssize_t r;
r = read(rcdev->rcdev_fd, &ev, sizeof(ev));
if (r < 0) {
fprintf(stderr, "rcdev read failed: %s\n", strerror(errno));
break;
}
if (r != sizeof(ev)) {
fprintf(stderr, "rcdev read failed, got %zi bytes, expected %zu\n", r, sizeof(ev));
break;
}
printf("rcdev event - type ");
switch (ev.type) {
case RC_DEBUG:
printf("RC_DEBUG; code 0x%0" PRIx16 "; val 0x%0" PRIx32 "\n",
ev.code, ev.val);
break;
case RC_CORE:
printf("RC_CORE; code ");
if (ev.code == RC_CORE_DROPPED)
printf("RC_CORE_DROPPED");
else
printf("UNKNOWN (0x%0" PRIx16 ")", ev.code);
printf("; val 0x%0" PRIx32 "\n", ev.val);
break;
case RC_KEY:
printf("RC_KEY; code ");
switch (ev.code) {
case RC_KEY_REPEAT:
printf("RC_KEY_REPEAT");
break;
case RC_KEY_PROTOCOL:
printf("RC_KEY_PROTOCOL");
break;
case RC_KEY_SCANCODE:
printf("RC_KEY_SCANCODE");
break;
case RC_KEY_SCANCODE_PART:
printf("RC_KEY_SCANCODE_PART");
break;
case RC_KEY_TOGGLE:
printf("RC_KEY_TOGGLE");
break;
default:
printf("UNKNOWN (0x%0" PRIx16 ")", ev.code);
break;
}
printf("; val 0x%0" PRIx32 "\n", ev.val);
break;
case RC_IR:
printf("RC_IR; code ");
switch (ev.code) {
case RC_IR_SPACE:
printf("RC_IR_SPACE");
break;
case RC_IR_PULSE:
printf("RC_IR_PULSE");
break;
case RC_IR_START:
printf("RC_IR_START");
break;
case RC_IR_STOP:
printf("RC_IR_STOP");
break;
case RC_IR_RESET:
printf("RC_IR_RESET");
break;
case RC_IR_CARRIER:
printf("RC_IR_CARRIER");
break;
case RC_IR_DUTY_CYCLE:
printf("RC_IR_DUTY_CYCLE");
break;
default:
printf("UNKNOWN (0x%0" PRIx16 ")", ev.code);
break;
}
printf("; val 0x%0" PRIx32 "\n", ev.val);
break;
default:
printf("type UNKNOWN (0x%0" PRIx16 "); code 0x%0" PRIx16 "; val 0x%0" PRIx32 "\n",
ev.type, ev.code, ev.val);
break;
}
}
return 0;
}
struct rc_device *
rcdev_create(struct manager *mgr, const char *name)
{
struct rc_device *rcdev;
if (!name)
return NULL;
rcdev = malloc(sizeof(*rcdev));
if (!rcdev) {
fprintf(stderr, "malloc failed: %m\n");
return NULL;
}
if (asprintf(&rcdev->path, "/org/gnome/RemoteControlManager/%s", name) < 0) {
fprintf(stderr, "asprintf failed: %m\n");
free(rcdev);
return NULL;
}
list_init(&rcdev->keymaps);
rcdev->mgr = mgr;
rcdev->name = rcdev->path + strlen("/org/gnome/RemoteControlManager/");
rcdev->rcdev_fd = -1;
rcdev->evdev_fd = -1;
rcdev->lirc_fd = -1;
rcdev->error = NULL;
rcdev->input_name = NULL;
rcdev->driver_name = NULL;
rcdev->keymap_name = NULL;
if (keymaps_load(rcdev) < 0) {
fprintf(stderr, "failed to load keymaps: %m\n");
rcdev_free(rcdev);
return NULL;
}
return rcdev;
}
void
rcdev_free(struct rc_device *rcdev)
{
if (!rcdev)
return;
list_del(&rcdev->list);
lirc_close(rcdev);
evdev_close(rcdev);
rcdev_close(rcdev);
free(rcdev->path);
free(rcdev->input_name);
free(rcdev->driver_name);
free(rcdev->keymap_name);
while (!list_empty(&rcdev->keymaps)) {
struct keymap *keymap = list_first_entry(&rcdev->keymaps, typeof(*keymap), list);
list_del(&keymap->list);
free(keymap->name);
free(keymap);
}
free(rcdev);
}
int
rcdev_setup(struct rc_device *rcdev, const char *path)
{
int r;
if (!rcdev)
return -EINVAL;
if (rcdev->rcdev_fd >= 0) {
printf("Multiple rcdev devices!?\n");
return 0;
}
rcdev->rcdev_fd = open(path, O_RDONLY | O_NONBLOCK);
if (rcdev->rcdev_fd < 0) {
printf("Failed to open rcdev device %s: %s\n", path, strerror(errno));
return -errno;
}
if (sd_event_add_io(rcdev->mgr->event, &rcdev->rcdev_ev,
rcdev->rcdev_fd, EPOLLIN, rcdev_read, rcdev) < 0) {
printf("Failed to add event source for rcdev device %s: %s\n",
path, strerror(errno));
r = -errno;
rcdev_close(rcdev);
return r;
}
return 0;
}
void
rcdev_close(struct rc_device *rcdev)
{
if (!rcdev)
return;
if (rcdev->rcdev_fd < 0)
return;
close(rcdev->rcdev_fd);
rcdev->rcdev_fd = -1;
}
|