summaryrefslogtreecommitdiff
path: root/rcm-server-main.c
blob: 33ef8a7ef71164db984d0b25d9ade62f73eca41b (plain)
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <systemd/sd-bus.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>

#include "utils.h"
#include "shared.h"
#include "rcm-server-main.h"
#include "rcm-server-udev.h"
#include "rcm-server-keymap.h"
#include "rcm-server-evdev.h"
#include "rcm-server-lirc.h"
#include "rcm-server-kdb.h"

static struct device *
find_device_by_path(struct manager *mgr, const char *path)
{
	struct device *dev;

	list_for_each_entry(dev, &mgr->devices, list)
		if (!strcmp(dev->path, path))
			return dev;

	return NULL;
}

static int
property_get(sd_bus *bus, const char *path, const char *interface,
	     const char *property, sd_bus_message *reply, void *userdata,
	     sd_bus_error *error)
{
	struct manager *mgr = userdata;
	struct device *dev;

	dev = find_device_by_path(mgr, path);
	if (!dev) {
		sd_bus_error_set_const(error, "org.gnome.RemoteControlManager.InvalidDevice", "Sorry, invalid device");
		return -EINVAL;
	}

	if (!strcmp(property, "SysName"))
		return sd_bus_message_append(reply, "s", dev->name);
	else if (!strcmp(property, "Description"))
		return sd_bus_message_append(reply, "s", dev->input_name);
	else if (!strcmp(property, "DriverName"))
		return sd_bus_message_append(reply, "s", dev->driver_name);
	else if (!strcmp(property, "KernelKeymapName"))
		return sd_bus_message_append(reply, "s", dev->keymap_name);
	else if (!strcmp(property, "HardwareType"))
		return sd_bus_message_append(reply, "s", "ir");
	else if (!strcmp(property, "Error"))
		return sd_bus_message_append(reply, "s", dev->error);

	sd_bus_error_set_const(error, "org.gnome.RemoteControlManager.InvalidProperty", "Sorry, invalid property");
	return -EINVAL;
}

static int
sdbus_get_psk_triplet(sd_bus_message *m, unsigned *ret_protocol,
		      uint64_t *ret_scancode, struct linux_input_keycode **ret_keycode)
{
	const char *protocol = NULL;
	unsigned protocol_numeric;
	uint64_t scancode;
	bool scancode_found = false;
	struct linux_input_keycode *lik = NULL;
	const char *keycode = NULL;
	unsigned i;
	int r;

	r = sd_bus_message_enter_container(m, 'a', "{sv}");
	if (r < 1)
		return r;

	while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
		const char *name;
		const char *contents;
		char type;

		r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &name);
		if (r < 0)
			return r;

		r = sd_bus_message_peek_type(m, NULL, &contents);
		if (r < 0)
			return r;

		r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
		if (r < 0)
			return r;

		r = sd_bus_message_peek_type(m, &type, &contents);
		if (r < 0)
			return r;

		if (!strcmp(name, "protocol")) {
			if (type != SD_BUS_TYPE_STRING) {
				printf("Invalid type for protocol (%c)\n", type);
				return -EINVAL;
			}

			if (protocol) {
				printf("Protocol specified more than once\n");
				return -EINVAL;
			}

			r = sd_bus_message_read_basic(m, type, &protocol);
			if (r < 0)
				return r;

			for (i = 0; rc_protocols[i]; i++) {
				if (!strcmp(rc_protocols[i], protocol))
					break;
			}

			if (!rc_protocols[i]) {
				printf("Invalid protocol name (%s)\n", protocol);
				return -EINVAL;
			}

			protocol_numeric = i;

		} else if (!strcmp(name, "scancode")) {
			if (type != SD_BUS_TYPE_UINT64) {
				printf("Invalid type for scancode (%c)\n", type);
				return -EINVAL;
			}

			if (scancode_found) {
				printf("Scancode specified more than once\n");
				return -EINVAL;
			}

			r = sd_bus_message_read_basic(m, type, &scancode);
			if (r < 0)
				return r;

			scancode_found = true;

		} else if (!strcmp(name, "keycode")) {
			if (type != SD_BUS_TYPE_STRING) {
				printf("Invalid type for keycode (%c)\n", type);
				return -EINVAL;
			}

			if (keycode) {
				printf("Keycode specified more than once\n");
				return -EINVAL;
			}

			r = sd_bus_message_read_basic(m, type, &keycode);
			if (r < 0)
				return r;

			if (strcmp(keycode, "KEY_RESERVED")) {
				lik = get_linux_keycode_by_name(keycode);
				if (!lik) {
					printf("Invalid keycode name (%s)\n", keycode);
					return -EINVAL;
				}
			}

		} else {
			r = sd_bus_message_skip(m, contents);
			if (r < 0)
				return r;
		}

		r = sd_bus_message_exit_container(m);
		if (r < 0)
			return r;

		r = sd_bus_message_exit_container(m);
		if (r < 0)
			return r;
	}

	if (r < 0)
		return r;

	r = sd_bus_message_exit_container(m);
	if (r < 0)
		return r;

	if (!protocol) {
		printf("Protocol missing\n");
		return -EINVAL;
	}

	if (!scancode_found) {
		printf("Scancode missing\n");
		return -EINVAL;
	}

	if (!keycode) {
		printf("Keycode missing\n");
		return -EINVAL;
	}

	*ret_protocol = protocol_numeric;
	*ret_scancode = scancode;
	*ret_keycode = lik;
	return 1;
}

static int
method_getkernelmappings(sd_bus_message *m, void *userdata, sd_bus_error *error)
{
	_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
	struct manager *mgr = userdata;
	struct device *dev;
	unsigned protocol_numeric;
	uint64_t scancode;
	struct linux_input_keycode *lik;
	unsigned i = 0;
	int r;

	dev = find_device_by_path(mgr, sd_bus_message_get_path(m));
	if (!dev) {
		sd_bus_error_set_const(error, "org.gnome.RemoteControlManager.InvalidDevice", "Sorry, invalid device");
		return -EINVAL;
	}

	if (dev->evdev_fd < 0) {
		sd_bus_error_set_const(error, "org.gnome.RemoteControlManager.EvdevError", "Evdev device not available");
		r = -EINVAL;
		goto out;
	}

	r = sd_bus_message_new_method_return(m, &reply);
	if (r < 0)
		goto out;

	r = sd_bus_message_open_container(reply, 'a', "a{sv}");
	if (r < 0)
		goto out;

	while ((r = evdev_get_mapping(dev, i, &protocol_numeric, &scancode, &lik)) > 0) {
		r = sd_bus_message_append(reply, "a{sv}", 3,
					  "protocol", "s", rc_protocols[protocol_numeric],
					  "scancode", "t", scancode,
					  "keycode", "s", lik->name);
		if (r < 0)
			goto out;

		i++;
	}

	if (r < 0)
		goto out;

	r = sd_bus_message_close_container(reply);
	if (r < 0)
		goto out;

	r = sd_bus_send(NULL, reply, NULL);
	if (r < 0)
		goto out;

	return 1;

out:
	return sd_bus_error_set_errno(error, r);
}

static int
method_setkernelmappings(sd_bus_message *m, void *userdata, sd_bus_error *error)
{
	_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
	struct manager *mgr = userdata;
	struct device *dev;
	unsigned protocol;
	uint64_t scancode;
	struct linux_input_keycode *keycode;
	int r;

	dev = find_device_by_path(mgr, sd_bus_message_get_path(m));
	if (!dev) {
		sd_bus_error_set_const(error, "org.gnome.RemoteControlManager.InvalidDevice", "Sorry, invalid device");
		r = -EINVAL;
		goto out;
	}

	if (dev->evdev_fd < 0) {
		sd_bus_error_set_const(error, "org.gnome.RemoteControlManager.EvdevError", "Evdev device not available");
		r = -EINVAL;
		goto out;
	}

	r = sd_bus_message_enter_container(m, 'a', "a{sv}");
	if (r < 0)
		goto out;

	while ((r = sdbus_get_psk_triplet(m, &protocol, &scancode, &keycode)) > 0) {
		printf("%s kernel mapping %s:0x%08lx:%s\n",
		       keycode ? "Setting" : "Unsetting", rc_protocols[protocol],
		       scancode, keycode ? keycode->name : "<na>");
		evdev_set_mapping(dev, protocol, scancode, keycode);
	}

	if (r < 0)
		goto out;

	r = sd_bus_message_exit_container(m);
	if (r < 0)
		goto out;

	return sd_bus_reply_method_return(m, NULL);

out:
	return sd_bus_error_set_errno(error, r);
}

static int
method_listkeymaps(sd_bus_message *m, void *userdata, sd_bus_error *error)
{
	_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
	struct manager *mgr = userdata;
	struct device *dev;
	struct keymap *keymap;
	int r;

	dev = find_device_by_path(mgr, sd_bus_message_get_path(m));
	if (!dev) {
		sd_bus_error_set_const(error, "org.gnome.RemoteControlManager.InvalidDevice", "Sorry, invalid device");
		return -EINVAL;
	}

	r = sd_bus_message_new_method_return(m, &reply);
	if (r < 0)
		goto out;

	r = sd_bus_message_open_container(reply, 'a', "s");
	if (r < 0)
		goto out;

	list_for_each_entry(keymap, &dev->keymaps, list) {
		printf("Listing keymaps for %s: %s\n", dev->name, keymap->name);
		r = sd_bus_message_append(reply, "s", keymap->id);
		if (r < 0)
			goto out;
	}

	r = sd_bus_message_close_container(reply);
	if (r < 0)
		goto out;

	r = sd_bus_send(NULL, reply, NULL);
	if (r < 0)
		goto out;

	return 1;

out:
	return sd_bus_error_set_errno(error, r);
}

static int
method_getkeymap(sd_bus_message *m, void *userdata, sd_bus_error *error)
{
	_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
	struct manager *mgr = userdata;
	struct device *dev;
	struct keymap *keymap;
	const char *id;
	int r;
	unsigned i;

	dev = find_device_by_path(mgr, sd_bus_message_get_path(m));
	if (!dev) {
		sd_bus_error_set_const(error, "org.gnome.RemoteControlManager.InvalidDevice", "Sorry, invalid device");
		return -EINVAL;
	}

	r = sd_bus_message_read(m, "s", &id);
	if (r < 0) {
		fprintf(stderr, "Failed to parse parameters: %s\n", strerror(-r));
		return r;
	}

	keymap = find_keymap_by_id(dev, id);
	if (!keymap) {
		sd_bus_error_set_const(error, "org.gnome.RemoteControlManager.InvalidKeymap", "Sorry, unknown keymap");
		return -EINVAL;
	}

	r = sd_bus_message_new_method_return(m, &reply);
	if (r < 0)
		goto out;

	r = sd_bus_message_append(reply, "s", keymap->name);
	if (r < 0)
		goto out;

	r = sd_bus_message_append(reply, "s", keymap->description);
	if (r < 0)
		goto out;

	r = sd_bus_message_append(reply, "q", keymap->cols);
	if (r < 0)
		goto out;

	r = sd_bus_message_append(reply, "q", keymap->rows);
	if (r < 0)
		goto out;

	/* Keymap */
	r = sd_bus_message_open_container(reply, 'a', "a{sv}");
	if (r < 0)
		goto out;

	for (i = 0; i < keymap->keycode_count; i++) {
		r = sd_bus_message_append(reply, "a{sv}", 3,
					  "protocol", "s", rc_protocols[keymap->keycodes[i].protocol],
					  "scancode", "t", keymap->keycodes[i].scancode,
					  "keycode", "s", keymap->keycodes[i].lik->name);
		if (r < 0)
			goto out;
	}

	r = sd_bus_message_close_container(reply);
	if (r < 0)
		goto out;

	/* Layout */
	r = sd_bus_message_open_container(reply, 'a', "a{sv}");
	if (r < 0)
		goto out;

	for (i = 0; i < (keymap->rows * keymap->cols); i++) {
		if (!keymap->layout[i])
			r = sd_bus_message_append(reply, "a{sv}", 1,
						  "type", "s", "blank");
		else
			r = sd_bus_message_append(reply, "a{sv}", 2,
						  "type", "s", "button",
						  "keycode", "s", keymap->layout[i]->lik->name);
		if (r < 0)
			goto out;
	}

	r = sd_bus_message_close_container(reply);
	if (r < 0)
		goto out;

	r = sd_bus_send(NULL, reply, NULL);
	if (r < 0)
		goto out;

	return 1;

out:
	return sd_bus_error_set_errno(error, r);
}

static int
method_setkeymap_parse_dbus_msg(sd_bus_message *m, struct keymap *keymap,
				const char **ret_id, unsigned *ret_keycode_count,
				uint16_t *ret_cols, uint16_t *ret_rows)
{
	const char *id;
	const char *name;
	const char *description;
	unsigned protocol;
	uint64_t scancode;
	struct linux_input_keycode *lik;
	uint16_t cols, rows;
	unsigned keycode_count = 0;
	unsigned layout_count = 0;
	unsigned i;
	int r;

	r = sd_bus_message_rewind(m, 1);
	if (r < 0)
		return r;

	r = sd_bus_message_read(m, "sssqq", &id, &name, &description, &cols, &rows);
	if (r < 0) {
		fprintf(stderr, "Failed to parse parameters: %s\n", strerror(-r));
		return r;
	}

	if (!id) {
		printf("Id missing\n");
		return -EINVAL;
	}

	if (!name) {
		printf("Name missing\n");
		return -EINVAL;
	}

	if (!description) {
		printf("Description missing\n");
		return -EINVAL;
	}

	if (cols < 1 || cols > REMOTE_LAYOUT_MAX_WIDTH) {
		printf("Invalid column size\n");
		return -EINVAL;
	}

	if (rows < 1 || rows > REMOTE_LAYOUT_MAX_HEIGHT) {
		printf("Invalid row size\n");
		return -EINVAL;
	}

	if (keymap) {
		keymap->id = strdup(id);
		keymap->name = strdup(name);
		keymap->description = strdup(description);
		keymap->cols = cols;
		keymap->rows = rows;

		if (!keymap->id || !keymap->name || !keymap->description)
			return -ENOMEM;
	}

	r = sd_bus_message_enter_container(m, 'a', "a{sv}");
	if (r < 0)
		return r;

	while ((r = sdbus_get_psk_triplet(m, &protocol, &scancode, &lik)) > 0) {
		if (!lik) {
			printf("Keycode entry without keycode\n");
			return -EINVAL;
		}

		if (keymap) {
			keymap->keycodes[keycode_count].protocol = protocol;
			keymap->keycodes[keycode_count].scancode = scancode;
			keymap->keycodes[keycode_count].lik = lik;
		}

		keycode_count++;
	}

	if (r < 0)
		return r;

	r = sd_bus_message_exit_container(m);
	if (r < 0)
		return r;

	r = sd_bus_message_enter_container(m, 'a', "a{sv}");
	if (r < 0)
		return r;

	while ((r = sd_bus_message_enter_container(m, 'a', "{sv}")) > 0) {
		bool blank = false;
		bool button = false;
		struct linux_input_keycode *lik = NULL;

		while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
			const char *name;
			const char *contents;
			const char *button_type;
			const char *keycode;
			char type;

			r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &name);
			if (r < 0)
				return r;

			r = sd_bus_message_peek_type(m, NULL, &contents);
			if (r < 0)
				return r;

			r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
			if (r < 0)
				return r;

			r = sd_bus_message_peek_type(m, &type, &contents);
			if (r < 0)
				return r;

			if (!strcmp(name, "type")) {
				if (type != SD_BUS_TYPE_STRING) {
					printf("Invalid type for type (%c)\n", type);
					return -EINVAL;
				}

				r = sd_bus_message_read_basic(m, type, &button_type);
				if (r < 0)
					return r;

				if (button || blank) {
					printf("Multiple types specified in layout\n");
					return -EINVAL;
				}

				if (!strcasecmp(button_type, "button"))
					button = true;
				else if (!strcasecmp(button_type, "blank"))
					blank = true;
				else {
					printf("Invalid button type: %s\n", button_type);
					return -EINVAL;
				}

			} else if (!strcmp(name, "keycode")) {
				if (type != SD_BUS_TYPE_STRING) {
					printf("Invalid type for keycode (%c)\n", type);
					return -EINVAL;
				}

				if (lik) {
					printf("Multiple keycodes specified in layout\n");
					return -EINVAL;
				}

				r = sd_bus_message_read_basic(m, type, &keycode);
				if (r < 0)
					return r;

				lik = get_linux_keycode_by_name(keycode);
				if (!lik) {
					printf("Invalid keycode name in layout (%s)\n", keycode);
					return -EINVAL;
				}

			} else {
				r = sd_bus_message_skip(m, contents);
				if (r < 0)
					return r;
			}

			r = sd_bus_message_exit_container(m);
			if (r < 0)
				return r;

			r = sd_bus_message_exit_container(m);
			if (r < 0)
				return r;
		}

		if (r < 0)
			return r;

		if ((!blank && !button) || (blank && button) ||
		    (button && !lik) || (blank && lik)) {
			printf("Invalid layout specification\n");
			return -EINVAL;
		}

		if (keymap) {
			if (blank)
				keymap->layout[layout_count] = NULL;
			else {
				for (i = 0; i < keycode_count; i++) {
					if (keymap->keycodes[i].lik == lik)
						break;
				}

				if (i >= keycode_count) {
					printf("Invalid keycode in layout: '%s'\n", lik->name);
					return -EINVAL;
				}

				keymap->layout[layout_count] = &keymap->keycodes[i];
			}
		}

		layout_count++;

		r = sd_bus_message_exit_container(m);
		if (r < 0)
			return r;
	}

	if (r < 0)
		return r;

	r = sd_bus_message_exit_container(m);
	if (r < 0)
		return r;

	if (!sd_bus_message_at_end(m, 1)) {
		printf("Trailing message parts\n");
		return -EINVAL;
	}

	if (keycode_count < 1) {
		printf("No keycodes specified\n");
		return -EINVAL;
	}

	if (layout_count < 1) {
		printf("No layout specified\n");
		return -EINVAL;
	}

	if (rows * cols != layout_count) {
		fprintf(stderr, "Layout (%u) does not match rows x cols (%ux%u)\n",
			layout_count, rows, cols);
		return -EINVAL;
	}

	if (ret_id)
		*ret_id = id;

	if (ret_keycode_count)
		*ret_keycode_count = keycode_count;

	if (keymap)
		keymap->keycode_count = keycode_count;

	if (ret_cols)
		*ret_cols = cols;

	if (ret_rows)
		*ret_rows = rows;

	return 0;
}

static int
method_setkeymap(sd_bus_message *m, void *userdata, sd_bus_error *error)
{
	struct manager *mgr = userdata;
	struct device *dev;
	struct keymap *old_keymap;
	struct keymap *new_keymap;
	const char *id;
	unsigned keycode_count;
	uint16_t cols, rows;
	int r;

	dev = find_device_by_path(mgr, sd_bus_message_get_path(m));
	if (!dev)
		return sd_bus_error_set_const(error, "org.gnome.RemoteControlManager.InvalidDevice", "Sorry, invalid device");

	r = method_setkeymap_parse_dbus_msg(m, NULL, &id, &keycode_count, &cols, &rows);
	if (r < 0)
		goto out;

	old_keymap = find_keymap_by_id(dev, id);
	if (!old_keymap)
		return sd_bus_error_set_const(error, "org.gnome.RemoteControlManager.InvalidKeymap", "Sorry, unknown keymap");

	printf("Asked to update keymap '%s'\n", id);

	new_keymap = zmalloc(sizeof(*new_keymap) + sizeof(struct keycode) * keycode_count);
	if (!new_keymap) {
		r = -ENOMEM;
		goto out;
	}

	new_keymap->layout = zmalloc(sizeof(struct keycode *) * cols * rows);
	if (!new_keymap->layout) {
		r = -ENOMEM;
		goto out;
	}

	r = method_setkeymap_parse_dbus_msg(m, new_keymap, NULL, NULL, NULL, NULL);
	if (r < 0)
		goto out;

	printf("\tUpdated keymap: name (%s), desc (%s), keycodes(%u), rows(%u), cols(%u)\n",
	       new_keymap->name, new_keymap->description,
	       new_keymap->keycode_count,
	       new_keymap->rows, new_keymap->cols);

	r = keymap_write(new_keymap);
	if (r < 0)
		goto out;

	list_replace(&old_keymap->list, &new_keymap->list);
	keymap_free(old_keymap);

	return sd_bus_reply_method_return(m, NULL);

out:
	keymap_free(new_keymap);
	return sd_bus_error_set_errno(error, r);
}

static int
method_transmit(sd_bus_message *m, void *userdata, sd_bus_error *error)
{
	_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
	struct manager *mgr = userdata;
	struct device *dev;
	const void *v;
	size_t l;
	int r;
	const uint32_t *values;
	size_t count;

	printf("Transmit method called\n");
	dev = find_device_by_path(mgr, sd_bus_message_get_path(m));
	if (!dev) {
		sd_bus_error_set_const(error, "org.gnome.RemoteControlManager.InvalidDevice", "Sorry, invalid device");
		return -EINVAL;
	}

	r = sd_bus_message_read_array(m, SD_BUS_TYPE_UINT32, &v, &l);
	if (r < 0) {
		goto out;
	} else if (r == 0 || l % sizeof(uint32_t) != 0) {
		r = -EBADMSG;
		goto out;
	}

	values = v;
	count = l / sizeof(uint32_t);
	lirc_write(dev->lirc_fd, values, count);


	return sd_bus_reply_method_return(m, NULL);

out:
	return sd_bus_error_set_errno(error, r);
}

static const sd_bus_vtable device_vtable[] = {
	SD_BUS_VTABLE_START(0),
	SD_BUS_PROPERTY("SysName", "s", property_get, 0, 0),
	SD_BUS_PROPERTY("Description", "s", property_get, 0, 0),
	SD_BUS_PROPERTY("DriverName", "s", property_get, 0, 0),
	SD_BUS_PROPERTY("KernelKeymapName", "s", property_get, 0, 0),
	SD_BUS_PROPERTY("HardwareType", "s", property_get, 0, 0),
	SD_BUS_PROPERTY("Error", "s", property_get, 0, 0),
	SD_BUS_METHOD("GetKernelMappings", NULL, "aa{sv}", method_getkernelmappings, SD_BUS_VTABLE_UNPRIVILEGED),
	SD_BUS_METHOD("SetKernelMappings", "aa{sv}", NULL, method_setkernelmappings, SD_BUS_VTABLE_UNPRIVILEGED),
	SD_BUS_METHOD("ListKeymaps", NULL, "as", method_listkeymaps, SD_BUS_VTABLE_UNPRIVILEGED),
	SD_BUS_METHOD("GetKeymap", "s", "ssqqaa{sv}aa{sv}", method_getkeymap, SD_BUS_VTABLE_UNPRIVILEGED),
	SD_BUS_METHOD("SetKeymap", "sssqqaa{sv}aa{sv}", NULL, method_setkeymap, SD_BUS_VTABLE_UNPRIVILEGED),
	SD_BUS_METHOD("Transmit", "au", NULL, method_transmit, SD_BUS_VTABLE_UNPRIVILEGED),
	SD_BUS_SIGNAL("KeyPressed", "sts", 0),
	SD_BUS_SIGNAL("KeyReleased", "s", 0),
	SD_BUS_SIGNAL("Event", "s", 0),
	SD_BUS_VTABLE_END
};

static int
enumerator(sd_bus *bus, const char *path, void *userdata, char ***retnodes, sd_bus_error *error)
{
	_cleanup_strv_free_ char **nodes = NULL;
	struct manager *mgr = userdata;
	struct device *device;
	int i = 0;

	if (!path)
		return 0;

	nodes = zmalloc((mgr->num_devices + 1) * sizeof(char *));
	if (!nodes)
		return -ENOMEM;

	list_for_each_entry(device, &mgr->devices, list) {
		nodes[i] = strdup(device->path);
		if (!nodes[i])
			return -ENOMEM;
		i++;
	}

	nodes[i++] = NULL;
	*retnodes = nodes;
	nodes = NULL;

	return 1;
}

static void
free_manager(struct manager *mgr) {
	if (!mgr)
		return;

	kdb_close(mgr);
	sd_event_source_unref(mgr->udev_ev);
	sd_bus_detach_event(mgr->bus);
	sd_event_unref(mgr->event);
	udev_close(mgr);

	while (!list_empty(&mgr->devices)) {
		struct device *dev = list_first_entry(&mgr->devices, typeof(*dev), list);
		device_free(dev);
	}
	free(mgr);
}

static int
block_signals(void)
{
	sigset_t sigset;
	int r;

	r = sigemptyset(&sigset);
	if (r < 0)
		return r;

	r = sigaddset(&sigset, SIGINT);
	if (r < 0)
		return r;

	r = sigaddset(&sigset, SIGTERM);
	if (r < 0)
		return r;

	return sigprocmask(SIG_BLOCK, &sigset, NULL);
}

int
main(int argc, char **argv)
{
	int r;
	struct manager *mgr;
	_cleanup_bus_flush_close_unref_ sd_bus *bus = NULL;
	_cleanup_bus_slot_unref_ struct sd_bus_slot *vtable_slot = NULL;
	_cleanup_bus_slot_unref_ struct sd_bus_slot *enumerator_slot = NULL;
	_cleanup_bus_slot_unref_ struct sd_bus_slot *objm_slot = NULL;
	_cleanup_event_source_unref_ sd_event_source *sigint_ev = NULL;
	_cleanup_event_source_unref_ sd_event_source *sigterm_ev = NULL;

	mgr = zmalloc(sizeof(*mgr));
	if (!mgr) {
		fprintf(stderr, "Failed to allocate memory: %m\n");
		goto finish;
	}
	list_init(&mgr->devices);

	r = sd_event_default(&mgr->event);
	if (r < 0) {
		fprintf(stderr, "Failed to create sd_event: %s\n", strerror(-r));
		goto finish;
	}

	r = kdb_init(mgr, "./rcm.sqlite");
	if (r < 0) {
		fprintf(stderr, "Failed to open/create sqlite database\n");
		goto finish;
	}

	r = kdb_add_directory(mgr, "./keymaps/db");
	if (r < 0) {
		fprintf(stderr, "Failed to add keymap directory: %s\n", strerror(-r));
		goto finish;
	}

	r = sd_bus_open_user(&bus);
	if (r < 0) {
		fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
		goto finish;
	}
	mgr->bus = bus;

	r = sd_bus_request_name(mgr->bus, "org.gnome.RemoteControlManager", 0);
	if (r < 0) {
		fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-r));
		goto finish;
	}

	r = sd_bus_add_fallback_vtable(mgr->bus, &vtable_slot, "/org/gnome/RemoteControlManager",
				       "org.gnome.RemoteControlManager.Device",
				       device_vtable, NULL, mgr);
	if (r < 0) {
		fprintf(stderr, "Failed to add vtable: %s\n", strerror(-r));
		goto finish;
	}

	r = sd_bus_add_node_enumerator(mgr->bus, &enumerator_slot, "/org/gnome/RemoteControlManager", enumerator, mgr);
	if (r < 0) {
		fprintf(stderr, "Failed to create node enumerator: %s\n", strerror(-r));
		goto finish;
	}

	r = sd_bus_add_object_manager(mgr->bus, &objm_slot, "/org/gnome/RemoteControlManager");
	if (r < 0) {
		fprintf(stderr, "Failed to create object manager: %s\n", strerror(-r));
		goto finish;
	}

	r = sd_bus_attach_event(mgr->bus, mgr->event, 0);
	if (r < 0) {
		fprintf(stderr, "Failed to attach bus to event loop: %s\n", strerror(-r));
		goto finish;
	}

	r = udev_setup(mgr);
	if (r < 0) {
		fprintf(stderr, "Failed to setup udev monitoring: %s\n", strerror(-r));
		goto finish;
	}

	r = block_signals();
	if (r < 0) {
		fprintf(stderr, "Failed to block signals: %m\n");
		goto finish;
	}

	sd_event_add_signal(mgr->event, &sigint_ev, SIGINT, NULL, NULL);
	sd_event_add_signal(mgr->event, &sigterm_ev, SIGTERM, NULL, NULL);

	r = sd_event_loop(mgr->event);
	if (r < 0) {
		fprintf(stderr, "Event loop failed: %s\n", strerror(-r));
		goto finish;
	}

	sd_event_get_exit_code(mgr->event, &r);

finish:
	free_manager(mgr);

	return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}