-
Notifications
You must be signed in to change notification settings - Fork 259
/
ratbagd-button.c
547 lines (459 loc) · 14.2 KB
/
ratbagd-button.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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/***
This file is part of ratbagd.
Copyright 2016 Red Hat, Inc.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
***/
#include "config.h"
#include <assert.h>
#include <errno.h>
#include <libratbag.h>
#include <libudev.h>
#include <stdio.h>
#include <stdlib.h>
#include <systemd/sd-bus.h>
#include <systemd/sd-event.h>
#include <linux/input.h>
#include "ratbagd.h"
#include "shared-macro.h"
#include "libratbag-util.h"
struct ratbagd_button {
struct ratbagd_device *device;
struct ratbag_button *lib_button;
unsigned int index;
char *path;
};
static int ratbagd_button_get_button(sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error)
{
struct ratbagd_button *button = userdata;
unsigned int b;
b = ratbag_button_get_button(button->lib_button);
verify_unsigned_int(b);
return sd_bus_message_append(reply, "(uv)",
RATBAG_BUTTON_ACTION_TYPE_BUTTON,
"u", b);
}
static int ratbagd_button_set_button(sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *m,
void *userdata,
sd_bus_error *error)
{
struct ratbagd_button *button = userdata;
unsigned int map;
int r;
r = sd_bus_message_read(m, "v", "u", &map);
if (r < 0)
return r;
if (map == 0 || map > 30)
return 0;
r = ratbag_button_set_button(button->lib_button, map);
if (r == 0) {
sd_bus_emit_properties_changed(bus,
button->path,
RATBAGD_NAME_ROOT ".Button",
"Mapping",
NULL);
}
return 0;
}
static int ratbagd_button_get_special(sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error)
{
struct ratbagd_button *button = userdata;
enum ratbag_button_action_special special;
special = ratbag_button_get_special(button->lib_button);
if (special == RATBAG_BUTTON_ACTION_SPECIAL_INVALID)
special = RATBAG_BUTTON_ACTION_SPECIAL_UNKNOWN;
verify_unsigned_int(special);
CHECK_CALL(sd_bus_message_append(reply, "(uv)",
RATBAG_BUTTON_ACTION_TYPE_SPECIAL,
"u",
special));
return 0;
}
static int ratbagd_button_set_special(sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *m,
void *userdata,
sd_bus_error *error)
{
struct ratbagd_button *button = userdata;
enum ratbag_button_action_special special;
int r;
CHECK_CALL(sd_bus_message_read(m, "v", "u", &special));
r = ratbag_button_set_special(button->lib_button, special);
if (r == 0) {
sd_bus_emit_properties_changed(bus,
button->path,
RATBAGD_NAME_ROOT ".Button",
"Mapping",
NULL);
}
return 0;
}
static int ratbagd_button_get_key(sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error)
{
struct ratbagd_button *button = userdata;
const unsigned int key = ratbag_button_get_key(button->lib_button);
verify_unsigned_int(key);
CHECK_CALL(sd_bus_message_append(reply, "(uv)",
RATBAG_BUTTON_ACTION_TYPE_KEY,
"u",
key));
return 0;
}
static int ratbagd_button_set_key(sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *m,
void *userdata,
sd_bus_error *error)
{
struct ratbagd_button *button = userdata;
unsigned int key;
int r;
CHECK_CALL(sd_bus_message_read(m, "v", "u", &key));
r = ratbag_button_set_key(button->lib_button, key);
if (r == 0) {
sd_bus_emit_properties_changed(bus,
button->path,
RATBAGD_NAME_ROOT ".Button",
"Mapping",
NULL);
}
return 0;
}
DEFINE_TRIVIAL_CLEANUP_FUNC(struct ratbag_button_macro *, ratbag_button_macro_unref);
static int ratbagd_button_get_macro(sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error)
{
struct ratbagd_button *button = userdata;
_cleanup_(ratbag_button_macro_unrefp) struct ratbag_button_macro *macro = NULL;
unsigned int idx;
CHECK_CALL(sd_bus_message_open_container(reply, SD_BUS_TYPE_STRUCT, "uv"));
CHECK_CALL(sd_bus_message_append(reply, "u", RATBAG_BUTTON_ACTION_TYPE_MACRO));
CHECK_CALL(sd_bus_message_open_container(reply, 'v', "a(uu)"));
CHECK_CALL(sd_bus_message_open_container(reply, 'a', "(uu)"));
macro = ratbag_button_get_macro(button->lib_button);
if (!macro)
goto out;
for (idx = 0; idx < ratbag_button_macro_get_num_events(macro); idx++) {
enum ratbag_macro_event_type type;
int value;
type = ratbag_button_macro_get_event_type(macro, idx);
switch (type) {
case RATBAG_MACRO_EVENT_INVALID:
abort();
break;
case RATBAG_MACRO_EVENT_NONE:
goto out;
case RATBAG_MACRO_EVENT_KEY_PRESSED:
case RATBAG_MACRO_EVENT_KEY_RELEASED:
value = ratbag_button_macro_get_event_key(macro, idx);
break;
case RATBAG_MACRO_EVENT_WAIT:
value = ratbag_button_macro_get_event_timeout(macro, idx);
break;
default:
abort();
}
verify_unsigned_int(type);
verify_unsigned_int(value);
CHECK_CALL(sd_bus_message_append(reply, "(uu)", type, value));
}
out:
CHECK_CALL(sd_bus_message_close_container(reply)); /* a(uu) */
CHECK_CALL(sd_bus_message_close_container(reply)); /* v */
CHECK_CALL(sd_bus_message_close_container(reply)); /* ) */
return 0;
}
static int ratbagd_button_set_macro(sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *m,
void *userdata,
sd_bus_error *error)
{
struct ratbagd_button *button = userdata;
unsigned int type, value;
int r, idx = 0;
_cleanup_(ratbag_button_macro_unrefp) struct ratbag_button_macro *macro = NULL;
CHECK_CALL(sd_bus_message_enter_container(m, 'v', "a(uu)"));
CHECK_CALL(sd_bus_message_enter_container(m, 'a', "(uu)"));
macro = ratbag_button_macro_new("macro");
while ((r = sd_bus_message_read(m, "(uu)", &type, &value)) > 0) {
r = ratbag_button_macro_set_event(macro, idx++, type, value);
if (r < 0) {
r = ratbagd_device_resync(button->device, bus);
if (r < 0)
return r;
}
}
if (r < 0)
return r;
CHECK_CALL(sd_bus_message_exit_container(m)); /* (uu) */
CHECK_CALL(sd_bus_message_exit_container(m)); /* a(uu) */
r = ratbag_button_set_macro(button->lib_button, macro);
if (r < 0) {
r = ratbagd_device_resync(button->device, bus);
if (r < 0)
return r;
}
if (r == 0) {
sd_bus_emit_properties_changed(bus,
button->path,
RATBAGD_NAME_ROOT ".Button",
"Mapping",
NULL);
}
return 0;
}
static int ratbagd_button_get_none(sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error)
{
CHECK_CALL(sd_bus_message_append(reply, "(uv)",
RATBAG_BUTTON_ACTION_TYPE_NONE,
"u",
0));
return 0;
}
static int ratbagd_button_set_none(sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *m,
void *userdata,
sd_bus_error *error)
{
struct ratbagd_button *button = userdata;
int r;
_unused_ unsigned int zero;
CHECK_CALL(sd_bus_message_read(m, "v", "u", &zero));
r = ratbag_button_disable(button->lib_button);
if (r < 0) {
r = ratbagd_device_resync(button->device, bus);
if (r < 0)
return r;
}
if (r == 0) {
sd_bus_emit_properties_changed(bus,
button->path,
RATBAGD_NAME_ROOT ".Button",
"Mapping",
NULL);
}
return 0;
}
static int ratbagd_button_get_mapping(sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error)
{
struct ratbagd_button *button = userdata;
enum ratbag_button_action_type type;
type = ratbag_button_get_action_type(button->lib_button);
verify_unsigned_int(type);
switch (type) {
case RATBAG_BUTTON_ACTION_TYPE_NONE:
return ratbagd_button_get_none(bus, path, interface, property,
reply, userdata, error);
case RATBAG_BUTTON_ACTION_TYPE_BUTTON:
return ratbagd_button_get_button(bus, path, interface, property,
reply, userdata, error);
case RATBAG_BUTTON_ACTION_TYPE_SPECIAL:
return ratbagd_button_get_special(bus, path, interface, property,
reply, userdata, error);
case RATBAG_BUTTON_ACTION_TYPE_KEY:
return ratbagd_button_get_key(bus, path, interface, property,
reply, userdata, error);
case RATBAG_BUTTON_ACTION_TYPE_MACRO:
return ratbagd_button_get_macro(bus, path, interface, property,
reply, userdata, error);
default:
return sd_bus_message_append(reply, "(uv)",
RATBAG_BUTTON_ACTION_TYPE_UNKNOWN,
"u", 0);
}
return 0;
}
static int ratbagd_button_set_mapping(sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *m,
void *userdata,
sd_bus_error *error)
{
enum ratbag_button_action_type type;
CHECK_CALL(sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, "uv"));
CHECK_CALL(sd_bus_message_read(m, "u", &type));
switch (type) {
case RATBAG_BUTTON_ACTION_TYPE_NONE:
CHECK_CALL(ratbagd_button_set_none(bus, path, interface, property,
m, userdata, error));
break;
case RATBAG_BUTTON_ACTION_TYPE_BUTTON:
CHECK_CALL(ratbagd_button_set_button(bus, path, interface, property,
m, userdata, error));
break;
case RATBAG_BUTTON_ACTION_TYPE_SPECIAL:
CHECK_CALL(ratbagd_button_set_special(bus, path, interface, property,
m, userdata, error));
break;
case RATBAG_BUTTON_ACTION_TYPE_KEY:
CHECK_CALL(ratbagd_button_set_key(bus, path, interface, property,
m, userdata, error));
break;
case RATBAG_BUTTON_ACTION_TYPE_MACRO:
CHECK_CALL(ratbagd_button_set_macro(bus, path, interface, property,
m, userdata, error));
break;
default:
/* FIXME */
return 1;
}
CHECK_CALL(sd_bus_message_exit_container(m));
return 0;
}
static int ratbagd_button_get_action_types(sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error)
{
struct ratbagd_button *button = userdata;
enum ratbag_button_action_type types[] = {
RATBAG_BUTTON_ACTION_TYPE_NONE,
RATBAG_BUTTON_ACTION_TYPE_BUTTON,
RATBAG_BUTTON_ACTION_TYPE_SPECIAL,
RATBAG_BUTTON_ACTION_TYPE_KEY,
RATBAG_BUTTON_ACTION_TYPE_MACRO
};
enum ratbag_button_action_type *t;
CHECK_CALL(sd_bus_message_open_container(reply, 'a', "u"));
ARRAY_FOR_EACH(types, t) {
if (!ratbag_button_has_action_type(button->lib_button, *t))
continue;
verify_unsigned_int(*t);
CHECK_CALL(sd_bus_message_append(reply, "u", *t));
}
CHECK_CALL(sd_bus_message_close_container(reply));
return 0;
}
const sd_bus_vtable ratbagd_button_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY("Index", "u", NULL, offsetof(struct ratbagd_button, index), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_WRITABLE_PROPERTY("Mapping", "(uv)",
ratbagd_button_get_mapping,
ratbagd_button_set_mapping,
0, SD_BUS_VTABLE_UNPRIVILEGED | SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("ActionTypes", "au", ratbagd_button_get_action_types, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_VTABLE_END,
};
int ratbagd_button_new(struct ratbagd_button **out,
struct ratbagd_device *device,
struct ratbagd_profile *profile,
struct ratbag_button *lib_button,
unsigned int index)
{
_cleanup_(ratbagd_button_freep) struct ratbagd_button *button = NULL;
char profile_buffer[DECIMAL_TOKEN_MAX(unsigned int) + 1],
button_buffer[DECIMAL_TOKEN_MAX(unsigned int) + 1];
int r;
assert(out);
assert(lib_button);
button = zalloc(sizeof(*button));
button->device = device;
button->lib_button = lib_button;
button->index = index;
sprintf(profile_buffer, "p%u", ratbagd_profile_get_index(profile));
sprintf(button_buffer, "b%u", index);
r = sd_bus_path_encode_many(&button->path,
RATBAGD_OBJ_ROOT "/button/%/%/%",
ratbagd_device_get_sysname(device),
profile_buffer,
button_buffer);
if (r < 0)
return r;
*out = button;
button = NULL;
return 0;
}
const char *ratbagd_button_get_path(struct ratbagd_button *button)
{
assert(button);
return button->path;
}
struct ratbagd_button *ratbagd_button_free(struct ratbagd_button *button)
{
if (!button)
return NULL;
button->path = mfree(button->path);
button->lib_button = ratbag_button_unref(button->lib_button);
return mfree(button);
}
int ratbagd_button_resync(sd_bus *bus,
struct ratbagd_button *button)
{
return sd_bus_emit_properties_changed(bus,
button->path,
RATBAGD_NAME_ROOT ".Button",
"ButtonMapping",
"SpecialMapping",
"Macro",
"ActionType",
NULL);
}