/
input.c
2200 lines (2046 loc) · 64.1 KB
/
input.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
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
/*
* Copyright (C) 2009-2010 Paul Bender.
*
* This file is part of eventlircd.
*
* eventlircd 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 of the License, or
* (at your option) any later version.
*
* eventlircd 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 eventlircd. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Single Unix Specification Version 3 headers.
*/
#include <errno.h> /* C89 */
#include <fcntl.h> /* POSIX */
#include <stdbool.h> /* C99 */
#include <stddef.h> /* C89 */
#include <stdio.h> /* C89 */
#include <stdint.h> /* POSIX */
#include <stdlib.h> /* C89 */
#include <string.h> /* C89 */
#include <sys/time.h> /* POSIX */
#include <syslog.h> /* XSI */
#include <unistd.h> /* POSIX */
/*
* Linux headers.
*/
#include <linux/input.h> /* */
#include <linux/limits.h> /* */
#include <linux/types.h> /* */
#include <linux/uinput.h> /* */
/*
* libudev headers.
*/
#include <libudev.h>
/*
* eventlircd headers (autogenerated).
*/
#include "event_name_to_code.h"
#include "evkey_code_to_name.h"
#include "evkey_type.h"
/*
* eventlircd headers.
*/
#include "input.h"
#include "lircd.h"
#include "monitor.h"
/*
* The lircd_handler does not use the id parameter, so we need to let gcc's
* -Wused know that it is ok.
*/
#ifdef UNUSED
# error cannot define UNUSED because it is already defined
#endif
#if defined(__GNUC__)
# define UNUSED(x) x __attribute__((unused))
#else
# define UNUSED(x) x
#endif
#define DEVICE_NAME "eventlircd"
#define DEVICE_BUSTYPE 0
#define DEVICE_VENDOR 0
#define DEVICE_PRODUCT 0
#define DEVICE_VERSION 0
#define EVENTLIRCD_EVMAP_LOCK_OFFSET (28)
#define EVENTLIRCD_EVMAP_LOCK_MASK ((0x7U ) << EVENTLIRCD_EVMAP_LOCK_OFFSET)
#define EVENTLIRCD_EVMAP_LOCK_CAPS ((0x1U << 2) << EVENTLIRCD_EVMAP_LOCK_OFFSET)
#define EVENTLIRCD_EVMAP_LOCK_NUM ((0x1U << 1) << EVENTLIRCD_EVMAP_LOCK_OFFSET)
#define EVENTLIRCD_EVMAP_LOCK_SCROLL ((0x1U << 0) << EVENTLIRCD_EVMAP_LOCK_OFFSET)
#define EVENTLIRCD_EVMAP_MOD_OFFSET (24)
#define EVENTLIRCD_EVMAP_MOD_MASK ((0xfU ) << EVENTLIRCD_EVMAP_MOD_OFFSET)
#define EVENTLIRCD_EVMAP_MOD_CTRL ((0x1U << 3) << EVENTLIRCD_EVMAP_MOD_OFFSET)
#define EVENTLIRCD_EVMAP_MOD_SHIFT ((0x1U << 2) << EVENTLIRCD_EVMAP_MOD_OFFSET)
#define EVENTLIRCD_EVMAP_MOD_ALT ((0x1U << 1) << EVENTLIRCD_EVMAP_MOD_OFFSET)
#define EVENTLIRCD_EVMAP_MOD_META ((0x1U << 0) << EVENTLIRCD_EVMAP_MOD_OFFSET)
#define EVENTLIRCD_EVMAP_TYPE_OFFSET (16)
#define EVENTLIRCD_EVMAP_TYPE_MASK ((0xffU ) << EVENTLIRCD_EVMAP_TYPE_OFFSET)
#define EVENTLIRCD_EVMAP_CODE_OFFSET (0)
#define EVENTLIRCD_EVMAP_CODE_MASK ((0xffffU ) << EVENTLIRCD_EVMAP_CODE_OFFSET)
#if EV_MAX >= 65534
# error cannot define EVENTLIRCD_EV_NULL because EV_MAX exceeds 65534
#endif
#define EVENTLIRCD_EV_NULL 65535
#if KEY_MAX >= 65534U
# error cannot define EVENTLIRCD_EVMAP_NULL because KEY_MAX exceeds 65534
#endif
#define EVENTLIRCD_EVMAP_NULL 65535U
/*
* Macros for reading ioctl bit fields.
*/
#define BITFIELD_BITS_PER_LONG (sizeof(long) * 8)
#define BITFIELD_LONGS_PER_ARRAY(x) ((((x) - 1) / BITFIELD_BITS_PER_LONG) + 1)
#define BITFIELD_TEST(bit, array) ((array[((bit) / BITFIELD_BITS_PER_LONG)] >> ((bit) % BITFIELD_BITS_PER_LONG)) & 0x1)
/*
* The 'input_device_event' structure is used by the 'current' and
* 'previous_list' members of the 'input_device' structure. It is used to hold
* information associated with the current event (when used by the
* 'input_device' structure's 'current' member) and the previous key press
* events (when used by the 'input_device' structure's 'previous_list' member).
* The current event is needed so that it can be forwared to either the lircd
* socket or the mouse/joystick event output device. The previous key press
* events are needed so that current input key repeat and release events can be
* mapped to the same output key event used by the corresponding key press
* event.
*/
struct input_device_event {
struct input_event event_in; /* The input event. */
struct input_event event_out; /* The output event corresponding to the input event. */
unsigned int repeat_count; /* The number of times the output key event has been repeated. */
struct input_device_event *next; /* Pointer to the next input device event. */
};
/*
* The 'input_device_evmap' structure is used by the 'evmap' member of the
* 'input_device' structure. It is used to hold information associated with
* each mapping of an input keyboard shortcut key sequence code (zero or more
* lock or modifier keys) followed by a base (non-lock and non-modifier) key
* converted to a code) to an output key code.
*/
struct input_device_evmap {
uint32_t code_in; /* The event map's input shortcut key sequence code. */
__u16 code_out; /* The event map's output code. */
};
/*
* The 'input_device' structure is used by the 'device_list' member of the
* 'eventlircd_input' variable. It is used to hold information associated with
* each input device handled by eventlircd. In addition to holding the evmap,
* current event and previous key press events information for the input device,
* it holds the information associated with the input device's mouse/joystick
* event output device.
*/
struct input_device {
int fd; /* The input device's file descriptor. */
char *path; /* The input device's path in the device file system. */
struct input_device_evmap *evmap; /* The input device's event map table. */
size_t evmap_size; /* The input device's event map table size. */
bool repeat_filter; /* The input device's repeat filter flag. */
uint32_t lock_state; /* The input device's current lock key state. */
uint32_t modifier_state; /* The input device's current modifier key state. */
struct input_device_event current; /* The input device's current event. */
struct input_device_event *previous_list; /* The input device's previous key events. */
struct {
bool capslock;
bool numlock;
bool scrolllock;
} led;
char *remote; /* The remote control name used in lircd socket output. */
struct { /* The input device's mouse/joystick event output device. */
int fd; /* The output device's file descriptor. */
struct uinput_user_dev dev; /* The output device. */
bool syn_report; /* The output device has a pending synchronization report event. */
} output;
struct input_device *next; /* Pointer to the next input device in the linked list. */
};
/*
* The 'lirudevd_input' variable holds information associated with input event
* devices handled by eventlircd. It contains the information with associated
* with monitoring udev for input event devices and a list of the input devices
* currently being handled by eventlircd. The udev monitor file descriptor and
* each of the the input device file descriptors are registered with monitor
* so that input is notified of any udev and input device events that eventlircd
* might need to handle.
*/
struct {
char *evmap_dir; /* The name of the directory containing event map files. */
bool repeat_filter; /* The flag indicating whether or not repeat filtering is enabled. */
struct {
int fd;
struct udev_monitor *monitor;
} udev;
struct input_device *device_list; /* The linked list of udev detected input devices. */
} eventlircd_input = {
.evmap_dir = NULL,
.repeat_filter = false,
.udev = {
.fd = -1,
.monitor = NULL
},
.device_list = NULL
};
static struct input_device_event *input_device_previous_get(struct input_device *device)
{
struct input_device_event *previous;
if (device == NULL) {
errno = EINVAL;
return NULL;
}
for (previous = device->previous_list
; (previous != NULL) &&
((previous->event_in.type != device->current.event_in.type) ||
(previous->event_in.code != device->current.event_in.code))
; previous = previous->next);
return previous;
}
static int input_device_previous_pop(struct input_device *device)
{
struct input_device_event **previous_ptr;
struct input_device_event *previous;
if (device == NULL) {
errno = EINVAL;
return -1;
}
previous_ptr = &(device->previous_list);
while (*previous_ptr != NULL) {
previous = *previous_ptr;
if ((previous->event_in.type == device->current.event_in.type) &&
(previous->event_in.code == device->current.event_in.code)) {
*previous_ptr = previous->next;
free(previous);
} else {
previous_ptr = &((*previous_ptr)->next);
}
}
return 0;
}
static int input_device_previous_push(struct input_device *device)
{
struct input_device_event *previous;
if (device == NULL) {
errno = EINVAL;
return -1;
}
/*
* Pop any events with the same type,code pair off the list. This would only
* occur were a previous key release event associated with this key lost.
* However, without this, the loss of key release events would cause the
* growth in the number of elements in 'previous_list' to be unbounded.
*/
input_device_previous_pop(device);
if ((previous = (struct input_device_event *)malloc(sizeof(struct input_device_event))) == NULL) {
syslog(LOG_ERR,
"failed to allocate memory for previous input device event: %s\n",
strerror(errno));
return -1;
}
*previous = device->current;
previous->next = device->previous_list;
device->previous_list = previous;
return 0;
}
static int input_device_send(struct input_device *device, const struct input_event *event)
{
if (device == NULL) {
errno = EINVAL;
return -1;
}
if (event == NULL) {
errno = EINVAL;
return -1;
}
if ((event->type == EV_SYN) && (event->code == SYN_REPORT) && (event->value == 0)) {
if (device->output.syn_report == true) {
if (write(device->output.fd, event, sizeof(*event)) != sizeof(*event)) {
syslog(LOG_ERR,
"failed to flush event for %s: %s\n",
device->output.dev.name,
strerror(errno));
return -1;
}
device->output.syn_report = false;
}
} else {
if (write(device->output.fd, event, sizeof(*event)) != sizeof(*event)) {
syslog(LOG_ERR,
"failed to send event (%u %u %d) for %s: %s\n",
(unsigned int)event->type,
(unsigned int)event->code,
(int)event->value,
device->output.dev.name,
strerror(errno));
return -1;
}
device->output.syn_report = true;
}
return 0;
}
static int input_device_evmap_compare(const void *evmap_a, const void *evmap_b)
{
__s32 code_a = (__s32)(((const struct input_device_evmap *)evmap_a)->code_in);
__s32 code_b = (__s32)(((const struct input_device_evmap *)evmap_b)->code_in);
return (code_a - code_b);
}
static int input_device_evmap_exit(struct input_device *device)
{
if (device == NULL) {
errno = EINVAL;
return -1;
}
device->evmap_size = 0;
if (device->evmap == NULL) {
return 0;
}
free(device->evmap);
device->evmap = NULL;
return 0;
}
static int input_device_evmap_init(struct input_device *device, const char *evmap_dir, const char *evmap_file)
{
char evmap_path[PATH_MAX + 1];
FILE *fp;
char *line;
size_t line_len;
unsigned int line_number;
size_t evmap_index;
char *comment;
char name_in[128];
char name_out[128];
char *name_in_part;
char *name_in_part_state;
bool evmap_valid;
size_t i;
if (device == NULL) {
errno = EINVAL;
return -1;
}
device->evmap = NULL;
device->evmap_size = 0;
if (evmap_dir == NULL) {
errno = EINVAL;
return -1;
}
if (evmap_file == NULL) {
return 0;
}
if (strnlen(evmap_dir, PATH_MAX + 1) == PATH_MAX + 1) {
errno = ENAMETOOLONG;
syslog(LOG_ERR,
"event map file directory '%s': %s\n",
evmap_path,
strerror(errno));
return -1;
}
if (strnlen(evmap_file, PATH_MAX + 1) == PATH_MAX + 1) {
errno = ENAMETOOLONG;
syslog(LOG_ERR,
"event map file name '%s': %s\n",
evmap_file,
strerror(errno));
return -1;
}
if (strchr(evmap_file, '/')) {
syslog(LOG_ERR,
"event map file name '%s' contains a directory\n",
evmap_file);
return -1;
}
if (evmap_dir[strlen(evmap_dir) - 1] == '/') {
if (snprintf(evmap_path, PATH_MAX + 1, "%s%s", evmap_dir, evmap_file) > PATH_MAX) {
errno = ENAMETOOLONG;
syslog(LOG_ERR,
"event map file path name '%s%s': %s\n",
evmap_dir,
evmap_file,
strerror(errno));
return -1;
}
} else {
if (snprintf(evmap_path, PATH_MAX + 1, "%s/%s", evmap_dir, evmap_file) > PATH_MAX) {
errno = ENAMETOOLONG;
syslog(LOG_ERR,
"event map file path name '%s/%s': %s\n",
evmap_dir,
evmap_file,
strerror(errno));
return -1;
}
}
if ((fp = fopen(evmap_path, "r")) == NULL) {
syslog(LOG_ERR,
"failed to open event map file '%s': %s\n",
evmap_path,
strerror(errno));
return -1;
}
line_len = 0;
evmap_index = 0;
for (evmap_index = 0 ; getline(&line, &line_len, fp) >= 0 ; evmap_index++);
device->evmap_size = evmap_index;
if (device->evmap_size == 0) {
return 0;
}
/*
* Allocate memory for the event map table.
*/
if ((device->evmap = (struct input_device_evmap *)malloc(device->evmap_size * sizeof(struct input_device_evmap))) == NULL) {
syslog(LOG_ERR,
"failed to allocate memory for the input device map %s: %s\n",
evmap_path,
strerror(errno));
device->evmap_size = 0;
return -1;
}
rewind(fp);
line_number = 0;
evmap_index = 0;
while ((evmap_index < device->evmap_size) && (getline(&line, &line_len, fp) >= 0)) {
device->evmap[evmap_index].code_in = 0;
device->evmap[evmap_index].code_out = 0;
line_number++;
/*
* End the line at the first comment character.
*/
comment = strchr(line, '#');
if (comment != NULL) {
*comment = '\0';
}
/*
* Skip blank (whitespace only) lines.
*/
if (sscanf(line, " %127s", name_in) != 1) {
continue;
}
/*
* Split event map line into input keyboard shortcut and output key name.
*/
if (sscanf(line, " %127[a-zA-Z0-9_+] = %127[a-zA-Z0-9_] ", name_in, name_out) != 2) {
syslog(LOG_WARNING,
"%s:%u: format is not <name-in> = <name-out>\n",
evmap_path,
line_number);
continue;
}
name_in[127] = '\0';
name_out[127] = '\0';
if (strlen(name_in) < 1) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: name is empty",
evmap_path,
line_number);
continue;
}
if (strlen(name_out) < 1) {
syslog(LOG_WARNING,
"%s:%u:<name-out>: name is empty",
evmap_path,
line_number);
continue;
}
/*
* parse input keyboard shortcut, validate lock key tockens, modifer
* key tokens and base key name, and determine corresponding input code.
* While the the tokens and names need not be treated as case sesitive,
* they are treated as case sesitive in order to force a more consistant
* file format.
*/
name_in_part = strtok_r(name_in, "+", &name_in_part_state);
if (name_in_part == NULL) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: keyboard shortcut could not be parsed\n",
evmap_path,
line_number);
continue;
}
evmap_valid = true;
while (name_in_part) {
if (strcmp(name_in_part, "capslock") == 0) {
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_CODE_MASK) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: the '%s' lock key token appeared after the base key name\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_LOCK_CAPS) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: the '%s' lock key token appeared more than once\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
device->evmap[evmap_index].code_in |= EVENTLIRCD_EVMAP_LOCK_CAPS;
} else if (strcmp(name_in_part, "numlock") == 0) {
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_CODE_MASK)
{
syslog(LOG_WARNING,
"%s:%u:<name-in>: the '%s' lock key token appeared after the base key name\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_LOCK_NUM)
{
syslog(LOG_WARNING,
"%s:%u:<name-in>: the '%s' lock key token appeared more than once\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
device->evmap[evmap_index].code_in |= EVENTLIRCD_EVMAP_LOCK_NUM;
} else if (strcmp(name_in_part, "scrolllock") == 0) {
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_CODE_MASK) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: the '%s' lock key token appeared after the base key name\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_LOCK_SCROLL) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: the '%s' lock key token appeared more than once\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
device->evmap[evmap_index].code_in |= EVENTLIRCD_EVMAP_LOCK_SCROLL;
} else if (strcmp(name_in_part, "ctrl") == 0) {
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_CODE_MASK) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: the '%s' modifier key token appeared after the base key name\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_MOD_CTRL) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: the '%s' modifier key token appeared more than once\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
device->evmap[evmap_index].code_in |= EVENTLIRCD_EVMAP_MOD_CTRL;
} else if (strcmp(name_in_part, "shift") == 0) {
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_CODE_MASK) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: the '%s' modifier key token appeared after the base key name\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_MOD_SHIFT) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: the '%s' modifier key token appeared more than once\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
device->evmap[evmap_index].code_in |= EVENTLIRCD_EVMAP_MOD_SHIFT;
} else if (strcmp(name_in_part, "alt") == 0) {
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_CODE_MASK) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: the '%s' modifier key token appeared after the base key name\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_MOD_ALT) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: the '%s' modifier key token appeared more than once\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
device->evmap[evmap_index].code_in |= EVENTLIRCD_EVMAP_MOD_ALT;
} else if (strcmp(name_in_part, "meta") == 0) {
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_CODE_MASK) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: the '%s' modifier key token appeared after the base key name\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_MOD_META) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: the '%s' modifier key token appeared more than once\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
device->evmap[evmap_index].code_in |= EVENTLIRCD_EVMAP_MOD_META;
} else {
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_CODE_MASK) {
evmap_valid = false;
break;
}
for (i = 0 ; (event_name_to_code[i].name != NULL) && (strcmp(name_in_part, event_name_to_code[i].name) != 0) ; i++);
if (event_name_to_code[i].name == NULL) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: '%s' is not a known key name\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
if ((strcmp(name_in_part, "KEY_CAPSLOCK" ) == 0) ||
(strcmp(name_in_part, "KEY_NUMLOCK" ) == 0) ||
(strcmp(name_in_part, "KEY_SCROLLLOCK") == 0)) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: '%s' is a key name that is part of the lock key token.\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
if ((strcmp(name_in_part, "KEY_LEFTCTRL" ) == 0) || (strcmp(name_in_part, "KEY_RIGHTCTRL" ) == 0) ||
(strcmp(name_in_part, "KEY_LEFTSHIFT" ) == 0) || (strcmp(name_in_part, "KEY_RIGHTSHIFT" ) == 0) ||
(strcmp(name_in_part, "KEY_LEFTALT" ) == 0) || (strcmp(name_in_part, "KEY_RIGHTALT" ) == 0) ||
(strcmp(name_in_part, "KEY_LEFTMETA" ) == 0) || (strcmp(name_in_part, "KEY_RIGHTMETA" ) == 0)) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: '%s' is a key name that is part of the modifier key token.\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
break;
}
if ((strncmp(name_in_part, "KEY_", strlen("KEY_")) != 0) &&
(strncmp(name_in_part, "BTN_", strlen("BTN_")) != 0)) {
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_LOCK_MASK) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: '%s' lock key applied to non-key event.\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
}
if (device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_MOD_MASK) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: '%s' modifier key applied to non-key event.\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
}
if (strcmp(name_out, "NULL") != 0) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: '%s' non-key event mapped to non-null value.\n",
evmap_path,
line_number,
name_in_part);
evmap_valid = false;
}
}
device->evmap[evmap_index].code_in |= ((uint32_t)(event_name_to_code[i].type)) << EVENTLIRCD_EVMAP_TYPE_OFFSET;
device->evmap[evmap_index].code_in |= ((uint32_t)(event_name_to_code[i].code)) << EVENTLIRCD_EVMAP_CODE_OFFSET;
}
name_in_part = strtok_r(NULL, "+", &name_in_part_state);
}
if (evmap_valid == false) {
continue;
}
if ((device->evmap[evmap_index].code_in & EVENTLIRCD_EVMAP_CODE_MASK) == 0) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: no key in keyboard shortcut.\n",
evmap_path,
line_number);
evmap_valid = false;
continue;
}
for (i = 0 ; i < evmap_index ; i++) {
if (device->evmap[evmap_index].code_in == device->evmap[i].code_in) {
syslog(LOG_WARNING,
"%s:%u:<name-in>: duplicate keyboard shortcut.\n",
evmap_path,
line_number);
evmap_valid = false;
break;
}
}
if (evmap_valid == false) {
continue;
}
if (strcmp(name_out, "NULL") == 0) {
device->evmap[evmap_index].code_out = EVENTLIRCD_EVMAP_NULL;
}
else {
if ((strncmp(name_out, "KEY_", strlen("KEY_")) != 0) &&
(strncmp(name_out, "BTN_", strlen("BTN_")) != 0)) {
syslog(LOG_WARNING,
"%s:%u:<name-out>: '%s' is not a valid key name\n",
evmap_path,
line_number,
name_out);
evmap_valid = false;
continue;
}
for (i = 0 ; (event_name_to_code[i].name != NULL) && (strcmp(name_out, event_name_to_code[i].name) != 0) ; i++);
if (event_name_to_code[i].name == NULL) {
syslog(LOG_WARNING,
"%s:%u:<name-out>: '%s' is not a valid key name\n",
evmap_path,
line_number,
name_out);
evmap_valid = false;
continue;
}
device->evmap[evmap_index].code_out = event_name_to_code[i].code;
}
evmap_index++;
}
device->evmap_size = evmap_index;
syslog(LOG_DEBUG,
"%s: using %u valid keyboard shortcut mappings\n",
evmap_path,
(unsigned int)device->evmap_size);
/*
* Sort the event map so that later look-ups can be done with a binary search.
*/
qsort(device->evmap, device->evmap_size, sizeof(struct input_device_evmap), input_device_evmap_compare);
free(line);
fclose(fp);
return 0;
}
static int input_device_evmap_run(struct input_device *device)
{
struct input_device_evmap search;
struct input_device_evmap *result;
if (device == NULL) {
errno = EINVAL;
return -1;
}
device->current.event_out = device->current.event_in;
if (device->evmap == NULL) {
if (evkey_type[device->current.event_out.code] == EVENTLIRCD_EVKEY_TYPE_NULL) {
device->current.event_out.code = (__u16)(EVENTLIRCD_EVMAP_NULL);
}
return 0;
}
search.code_in = (uint32_t)(device->current.event_out.code);
search.code_in |= (uint32_t)(device->current.event_out.type << 16);
/*
* Lock and modifier states are applied to keys only.
*/
if (device->current.event_out.type == EV_KEY) {
search.code_in |= device->modifier_state;
search.code_in |= device->lock_state;
}
result = bsearch(&search, device->evmap, device->evmap_size, sizeof(struct input_device_evmap), input_device_evmap_compare);
if (result == NULL) {
if (evkey_type[device->current.event_out.code] == EVENTLIRCD_EVKEY_TYPE_NULL) {
device->current.event_out.type = (__u16)(EVENTLIRCD_EVMAP_NULL);
}
return 0;
}
device->current.event_out.code = result->code_out;
return 0;
}
static int input_device_event_update(struct input_device *device, const struct input_event *event)
{
struct input_device_event *previous;
long time_delta;
if (device == NULL) {
errno = EINVAL;
return -1;
}
if (event == NULL) {
errno = EINVAL;
return -1;
}
device->current.event_in = *event;
device->current.event_out = device->current.event_in;
device->current.repeat_count = 0;
device->current.next = NULL;
/*
* Map the current event.
*/
if (input_device_evmap_run(device) < 0) {
return -1;
}
if (device->current.event_out.code == EVENTLIRCD_EVMAP_NULL) {
memset(&(device->current.event_out), 0, sizeof(struct input_event));
device->current.event_out.type = EVENTLIRCD_EV_NULL;
device->current.repeat_count = 0;
return 0;
}
/*
* When the device let's us know that the device's capslock, numlock or
* scrolllock state has changed, make sure that our state matches the
* device's state.
*/
if (device->current.event_in.type == EV_LED) {
switch (device->current.event_in.code) {
case LED_CAPSL:
if (device->current.event_in.value == 0)
device->lock_state &= ~EVENTLIRCD_EVMAP_LOCK_CAPS;
else
device->lock_state |= EVENTLIRCD_EVMAP_LOCK_CAPS;
device->current.event_out.type = EVENTLIRCD_EV_NULL;
device->current.repeat_count = 0;
return 0;
break;
case LED_NUML:
if (device->current.event_in.value == 0)
device->lock_state &= ~EVENTLIRCD_EVMAP_LOCK_NUM;
else
device->lock_state |= EVENTLIRCD_EVMAP_LOCK_NUM;
device->current.event_out.type = EVENTLIRCD_EV_NULL;
device->current.repeat_count = 0;
return 0;
break;
case LED_SCROLLL:
if (device->current.event_in.value == 0)
device->lock_state &= ~EVENTLIRCD_EVMAP_LOCK_SCROLL;
else
device->lock_state |= EVENTLIRCD_EVMAP_LOCK_SCROLL;
device->current.event_out.type = EVENTLIRCD_EV_NULL;
device->current.repeat_count = 0;
return 0;
break;
}
}
/*
* The remaining processing is for key events.
*/
if (device->current.event_in.type != EV_KEY) {
device->current.event_out = device->current.event_in;
device->current.repeat_count = 0;
return 0;
}
/*
* If it is a lock key code, then update the lock state, null
* the current output event (as it has been handled) and return.
*/
if (device->current.event_in.code == KEY_CAPSLOCK) {
if (device->current.event_in.value == 1) {
/*
* Toggle the capslock status.
*/
device->lock_state ^= EVENTLIRCD_EVMAP_LOCK_CAPS;
/*
* Update the device's capslock status.
*/
if (device->led.capslock == true) {
struct input_event event = {
.type = EV_LED,
.code = LED_CAPSL,
.value = ((device->lock_state & EVENTLIRCD_EVMAP_LOCK_CAPS) == 0) ? 0 : 1
};
if (write(device->fd, &event, sizeof(event)) != sizeof(event)) {
syslog(LOG_WARNING,
"failed to update capslock LED state for device %s: %s\n",
device->path,
strerror(errno));
}
}
}
device->current.event_out.type = EVENTLIRCD_EV_NULL;
device->current.repeat_count = 0;
return 0;
}
if (device->current.event_in.code == KEY_NUMLOCK) {
if (device->current.event_in.value == 1) {
/*
* Toggle the numlock status.
*/
device->lock_state ^= EVENTLIRCD_EVMAP_LOCK_NUM;
/*
* Update the device's numlock status.
*/
if (device->led.numlock == true) {
struct input_event event = {
.type = EV_LED,
.code = LED_NUML,
.value = ((device->lock_state & EVENTLIRCD_EVMAP_LOCK_NUM) == 0) ? 0 : 1
};
if (write(device->fd, &event, sizeof(event)) != sizeof(event)) {
syslog(LOG_WARNING,
"failed to update numlock LED state for device %s: %s\n",
device->path,
strerror(errno));
}
}
}
device->current.event_out.type = EVENTLIRCD_EV_NULL;
device->current.repeat_count = 0;
return 0;
}
if (device->current.event_in.code == KEY_SCROLLLOCK) {
if (device->current.event_in.value == 1) {
/*
* Toggle the scrolllock status.
*/
device->lock_state ^= EVENTLIRCD_EVMAP_LOCK_SCROLL;
/*
* Update the device's scrolllock status.
*/
if (device->led.scrolllock == true) {
struct input_event event = {
.type = EV_LED,
.code = LED_SCROLLL,
.value = ((device->lock_state & EVENTLIRCD_EVMAP_LOCK_SCROLL) == 0) ? 0 : 1
};
if (write(device->fd, &event, sizeof(event)) != sizeof(event)) {
syslog(LOG_WARNING,
"failed to update scrolllock LED state for device %s: %s\n",
device->path,
strerror(errno));
}
}
}
device->current.event_out.type = EVENTLIRCD_EV_NULL;
device->current.repeat_count = 0;
return 0;
}
/*