forked from community-ssu/mce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent-input.c
892 lines (761 loc) · 20.8 KB
/
event-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
/**
* @file event-input.c
* /dev/input event provider for the Mode Control Entity
* <p>
* Copyright © 2004-2010 Nokia Corporation and/or its subsidiary(-ies).
* <p>
* @author David Weinehall <[email protected]>
* @author Ismo Laitinen <[email protected]>
* @author Jonathan Wilson <[email protected]>
*
* mce is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* mce 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mce. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include <gio/gio.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <linux/input.h>
#include "mce.h"
#include "event-input.h"
#include "mce-io.h"
#include "mce-log.h"
#include "datapipe.h"
#include "event-switches.h"
guint16 power_keycode = POWER_BUTTON;
/** ID for touchscreen I/O monitor timeout source */
static guint touchscreen_io_monitor_timeout_cb_id = 0;
/** ID for keypress timeout source */
static guint keypress_repeat_timeout_cb_id = 0;
/** ID for misc timeout source */
static guint misc_io_monitor_timeout_cb_id = 0;
/** List of touchscreen input devices */
static GSList *touchscreen_dev_list = NULL;
/** List of keyboard input devices */
static GSList *keyboard_dev_list = NULL;
/** List of misc input devices */
static GSList *misc_dev_list = NULL;
/** List of switch input devices */
static GSList *switch_dev_list = NULL;
/** GFile pointer for the directory we monitor */
GFile *dev_input_gfp = NULL;
/** GFileMonitor pointer for the directory we monitor */
GFileMonitor *dev_input_gfmp = NULL;
static void update_inputdevices(const gchar *device, gboolean add);
/**
* Wrapper function to call mce_suspend_io_monitor() from g_slist_foreach()
*
* @param io_monitor The I/O monitor to suspend
* @param user_data Unused
*/
static void suspend_io_monitor(gpointer io_monitor, gpointer user_data)
{
(void)user_data;
mce_suspend_io_monitor(io_monitor);
}
/**
* Wrapper function to call mce_resume_io_monitor() from g_slist_foreach()
*
* @param io_monitor The I/O monitor to resume
* @param user_data Unused
*/
static void resume_io_monitor(gpointer io_monitor, gpointer user_data)
{
(void)user_data;
mce_resume_io_monitor(io_monitor);
}
/**
* Wrapper function to call mce_unregister_io_monitor() from g_slist_foreach()
*
* @param io_monitor The I/O monitor to unregister
* @param user_data Unused
*/
static void unregister_io_monitor(gpointer io_monitor, gpointer user_data)
{
/* If we opened an fd to monitor, retrieve it to ensure
* that we can close it after unregistering the I/O monitor
*/
int fd = mce_get_io_monitor_fd(io_monitor);
(void)user_data;
mce_unregister_io_monitor(io_monitor);
/* Close the fd if there is one */
if (fd != -1)
close(fd);
}
/**
* Timeout function for touchscreen I/O monitor reprogramming
*
* @param data Unused
* @return Always returns FALSE, to disable the timeout
*/
static gboolean touchscreen_io_monitor_timeout_cb(gpointer data)
{
(void)data;
touchscreen_io_monitor_timeout_cb_id = 0;
/* Resume I/O monitors */
if (touchscreen_dev_list != NULL) {
g_slist_foreach(touchscreen_dev_list,
(GFunc)resume_io_monitor, NULL);
}
return FALSE;
}
/**
* Cancel timeout for touchscreen I/O monitor reprogramming
*/
static void cancel_touchscreen_io_monitor_timeout(void)
{
if (touchscreen_io_monitor_timeout_cb_id != 0) {
g_source_remove(touchscreen_io_monitor_timeout_cb_id);
touchscreen_io_monitor_timeout_cb_id = 0;
}
}
/**
* Setup timeout for touchscreen I/O monitor reprogramming
*/
static void setup_touchscreen_io_monitor_timeout(void)
{
cancel_touchscreen_io_monitor_timeout();
/* Setup new timeout */
touchscreen_io_monitor_timeout_cb_id =
g_timeout_add_seconds(MONITORING_DELAY,
touchscreen_io_monitor_timeout_cb, NULL);
}
/**
* I/O monitor callback for the touchscreen
*
* @param data The new data
* @param bytes_read The number of bytes read
*/
static void touchscreen_cb(gpointer data, gsize bytes_read)
{
submode_t submode = mce_get_submode_int32();
struct input_event *ev;
ev = data;
/* Don't process invalid reads */
if (bytes_read != sizeof (struct input_event)) {
goto EXIT;
}
/* Ignore unwanted events */
if (ev->type != EV_ABS) {
goto EXIT;
}
/* Generate activity */
(void)execute_datapipe(&device_inactive_pipe, GINT_TO_POINTER(FALSE),
USE_INDATA, CACHE_INDATA);
/* If visual tklock is active or autorelock isn't active,
* suspend I/O monitors
*/
if (((submode & MCE_VISUAL_TKLOCK_SUBMODE) != 0) ||
((submode & MCE_AUTORELOCK_SUBMODE) == 0)) {
if (touchscreen_dev_list != NULL) {
g_slist_foreach(touchscreen_dev_list,
(GFunc)suspend_io_monitor, NULL);
}
/* Setup a timeout I/O monitor reprogramming */
setup_touchscreen_io_monitor_timeout();
}
/* Ignore non-pressure events */
if (ev->code != ABS_PRESSURE) {
goto EXIT;
}
/* For now there's no reason to cache the value,
* or indeed to send any kind of real value at all
*
* If the event eater is active, don't send anything
*/
if ((submode & MCE_EVEATER_SUBMODE) == 0) {
(void)execute_datapipe(&touchscreen_pipe, NULL,
USE_INDATA, DONT_CACHE_INDATA);
}
EXIT:
return;
}
/**
* Timeout function for keypress repeats
* @note Empty function; we check the callback id
* for 0 to know if we've had a timeout or not
*
* @param data Unused
* @return Always returns FALSE, to disable the timeout
*/
static gboolean keypress_repeat_timeout_cb(gpointer data)
{
(void)data;
return FALSE;
}
/**
* Cancel timeout for keypress repeats
*/
static void cancel_keypress_repeat_timeout(void)
{
if (keypress_repeat_timeout_cb_id != 0) {
g_source_remove(keypress_repeat_timeout_cb_id);
keypress_repeat_timeout_cb_id = 0;
}
}
/**
* Setup timeout for touchscreen I/O monitoring
*/
static void setup_keypress_repeat_timeout(void)
{
cancel_keypress_repeat_timeout();
/* Setup new timeout */
keypress_repeat_timeout_cb_id =
g_timeout_add_seconds(MONITORING_DELAY,
keypress_repeat_timeout_cb, NULL);
}
/**
* I/O monitor callback for keypresses
*
* @param data The new data
* @param bytes_read The number of bytes read
*/
static void keypress_cb(gpointer data, gsize bytes_read)
{
submode_t submode = mce_get_submode_int32();
struct input_event *ev;
ev = data;
/* Don't process invalid reads */
if (bytes_read != sizeof (struct input_event)) {
goto EXIT;
}
/* Ignore non-keypress events */
if (ev->type != EV_KEY) {
goto EXIT;
}
if (ev->code == power_keycode) {
ev->code = POWER_BUTTON;
}
/* Generate activity:
* 1 - press (always)
* 2 - repeat (once a second)
*/
if ((ev->value == 0) || (ev->value == 1) ||
((ev->value == 2) && (keypress_repeat_timeout_cb_id == 0))) {
mce_log(LL_DEBUG, "send device_inactive_pipe -> FALSE");
if (!(submode & MCE_EVEATER_SUBMODE))
{
mce_log(LL_DEBUG, "send device_inactive_pipe -> FALSE");
(void)execute_datapipe(&device_inactive_pipe,
GINT_TO_POINTER(FALSE),
USE_INDATA, CACHE_INDATA);
}
if (ev->value == 2) {
setup_keypress_repeat_timeout();
}
}
if ((ev->value == 1) || (ev->value == 0)) {
(void)execute_datapipe(&keypress_pipe, &ev,
USE_INDATA, DONT_CACHE_INDATA);
}
EXIT:
return;
}
static void switch_call_cb(struct input_event *ev, iomon_cb callback,
const char *active, const char *inactive)
{
callback((gpointer)(ev->value ? active : inactive),
ev->value ? strlen(active) : strlen(inactive));
}
/**
* I/O monitor callback for switch
*
* @param data The new data
* @param bytes_read The number of bytes read
*/
static void switch_cb(gpointer data, gsize bytes_read)
{
struct input_event *ev;
ev = data;
/* Don't process invalid reads */
if (bytes_read != sizeof (struct input_event)) {
goto EXIT;
}
if (ev->type == EV_SW) {
switch (ev->code) {
case SW_KEYPAD_SLIDE: {
switch_call_cb(ev, kbd_slide_cb,
MCE_KBD_SLIDE_OPEN,
MCE_KBD_SLIDE_CLOSED);
goto EXIT;
}
case SW_FRONT_PROXIMITY: {
switch_call_cb(ev, proximity_sensor_cb,
MCE_PROXIMITY_SENSOR_CLOSED,
MCE_PROXIMITY_SENSOR_OPEN);
goto EXIT;
}
case SW_CAMERA_LENS_COVER: {
switch_call_cb(ev, camera_launch_button_cb,
MCE_LENS_COVER_CLOSED,
MCE_LENS_COVER_OPEN);
goto EXIT;
}
default:
break;
}
} else if (ev->type == EV_KEY) {
switch (ev->code) {
case KEY_SCREENLOCK: {
switch_call_cb(ev, lockkey_cb,
MCE_FLICKER_KEY_ACTIVE,
MCE_FLICKER_KEY_INACTIVE);
goto EXIT;
}
case KEY_CAMERA: {
switch_call_cb(ev, camera_launch_button_cb,
MCE_CAM_LAUNCH_ACTIVE,
MCE_CAM_LAUNCH_INACTIVE);
goto EXIT;
}
case KEY_CAMERA_FOCUS: {
switch_call_cb(ev, generic_activity_cb,
MCE_CAM_FOCUS_ACTIVE,
MCE_CAM_FOCUS_INACTIVE);
goto EXIT;
}
default:
break;
}
}
EXIT:
return;
}
/**
* Timeout callback for misc event monitoring reprogramming
*
* @param data Unused
* @return Always returns FALSE, to disable the timeout
*/
static gboolean misc_io_monitor_timeout_cb(gpointer data)
{
(void)data;
misc_io_monitor_timeout_cb_id = 0;
/* Resume I/O monitors */
if (misc_dev_list != NULL) {
g_slist_foreach(misc_dev_list,
(GFunc)resume_io_monitor, NULL);
}
return FALSE;
}
/**
* Cancel timeout for misc event I/O monitoring
*/
static void cancel_misc_io_monitor_timeout(void)
{
if (misc_io_monitor_timeout_cb_id != 0) {
g_source_remove(misc_io_monitor_timeout_cb_id);
misc_io_monitor_timeout_cb_id = 0;
}
}
/**
* Setup timeout for misc event I/O monitoring
*/
static void setup_misc_io_monitor_timeout(void)
{
cancel_misc_io_monitor_timeout();
/* Setup new timeout */
misc_io_monitor_timeout_cb_id =
g_timeout_add_seconds(MONITORING_DELAY,
misc_io_monitor_timeout_cb, NULL);
}
/**
* I/O monitor callback for misc /dev/input devices
*
* @param data Unused
* @param bytes_read Unused
*/
static void misc_cb(gpointer data, gsize bytes_read)
{
struct input_event *ev;
ev = data;
/* Don't process invalid reads */
if (bytes_read != sizeof (struct input_event)) {
goto EXIT;
}
/* ev->type for the jack sense is EV_SW */
mce_log(LL_DEBUG, "ev->type: %d", ev->type);
/* Generate activity */
(void)execute_datapipe(&device_inactive_pipe, GINT_TO_POINTER(FALSE),
USE_INDATA, CACHE_INDATA);
/* Suspend I/O monitors */
if (misc_dev_list != NULL) {
g_slist_foreach(misc_dev_list,
(GFunc)suspend_io_monitor, NULL);
}
/* Setup a timeout I/O monitor reprogramming */
setup_misc_io_monitor_timeout();
EXIT:
return;
}
static int match_event_file_by_caps(const gchar *const filename,
const int *const ev_types,
const int *const ev_keys[])
{
int ev_type, p, q;
int version;
unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
int fd;
if ((fd = open(filename, O_NONBLOCK | O_RDONLY)) == -1) {
mce_log(LL_DEBUG, "Failed to open `%s', skipping", filename);
/* Ignore error */
errno = 0;
return -1;
}
/* We use this ioctl to check if this device supports the input
* ioctl's
*/
if (ioctl(fd, EVIOCGVERSION, &version) < 0) {
mce_log(LL_WARN,
"match_event_file_by_caps: can't get version on `%s'",
filename);
goto EXIT;
}
memset(bit, 0, sizeof(bit));
if (ioctl(fd, EVIOCGBIT(0, EV_MAX), bit[0]) < 0) {
mce_log(LL_WARN,
"match_event_file_by_caps: ioctl(EVIOCGBIT, EV_MAX) failed on `%s'",
filename);
goto EXIT;
}
for (p = 0; ev_types[p] != -1; p ++) {
/* TODO: Could check that ev_types[p] is less than EV_MAX */
ev_type = ev_types[p];
/* event type not supported, try the next one */
if (!test_bit(ev_type, bit[0]))
continue;
/* Get bits per event type */
if (ioctl(fd, EVIOCGBIT(ev_type, KEY_MAX), bit[ev_type]) < 0) {
mce_log(LL_WARN,
"match_event_file_by_caps: ioctl(EVIOCGBIT, KEY_MAX) failed on `%s'",
filename);
goto EXIT;
}
for (q = 0; ev_keys[p][q] != -1; q ++) {
/* TODO: Could check that q is less than KEY_MAX */
/* succeed if at least one match is found */
if (test_bit(ev_keys[p][q], bit[ev_type])) {
mce_log(LL_DEBUG,
"match_event_file_by_caps: match found on `%s'", filename);
return fd;
}
}
}
EXIT:
close(fd);
return -1;
}
/**
* Try to match /dev/input event file to a specific driver
*
* @param filename A string containing the name of the event file
* @param drivers An array of driver names
* @return An open file descriptor on success, -1 on failure
*/
static int match_event_file(const gchar *const filename,
const gchar *const *const drivers)
{
static char name[256];
int fd = -1;
int i;
/* If we cannot open the file, abort */
if ((fd = open(filename, O_NONBLOCK | O_RDONLY)) == -1) {
mce_log(LL_DEBUG, "Failed to open `%s', skipping",
filename);
/* Ignore error */
errno = 0;
goto EXIT;
}
for (i = 0; drivers[i] != NULL; i++) {
if (ioctl(fd, EVIOCGNAME(sizeof name), name) >= 0) {
if (strcmp(name, drivers[i]) == 0) {
/* We found our event file */
mce_log(LL_DEBUG,
"`%s' is `%s'",
filename, drivers[i]);
break;
}
} else {
mce_log(LL_WARN,
"ioctl(EVIOCGNAME) failed on `%s'",
filename);
}
}
/* If the scan terminated with drivers[i] == NULL,
* we didn't find any match
*/
if (drivers[i] == NULL) {
close(fd);
fd = -1;
}
EXIT:
return fd;
}
/**
* Custom compare function used to find I/O monitor entries
*
* @param iomon_id An I/O monitor cookie
* @param name The name to search for
*/
static gint iomon_name_compare(gconstpointer iomon_id,
gconstpointer name)
{
const gchar *iomon_name = mce_get_io_monitor_name(iomon_id);
return strcmp(iomon_name, name);
}
static void register_io_monitor_chunk(const gint fd, const gchar *const file,
iomon_cb callback, GSList **devices)
{
gconstpointer iomon = NULL;
iomon = mce_register_io_monitor_chunk(fd, file,
MCE_IO_ERROR_POLICY_WARN, FALSE,
callback,
sizeof (struct input_event));
/* If we fail to register an I/O monitor,
* don't leak the file descriptor,
* and don't add the device to the list
*/
if (iomon == NULL) {
if (fd != -1)
close(fd);
} else {
*devices = g_slist_prepend(*devices, (gpointer)iomon);
}
}
/**
* Match and register I/O monitor
*/
static void match_and_register_io_monitor(const gchar *filename)
{
int fd;
gboolean match = FALSE;
if ((fd = match_event_file(filename, driver_blacklist)) != -1) {
/* If the driver for the event file is blacklisted, skip it */
close(fd);
goto EXIT;
} else if ((fd = match_event_file(filename,
touchscreen_event_drivers)) != -1) {
register_io_monitor_chunk(fd, filename, touchscreen_cb,
&touchscreen_dev_list);
match = TRUE;
} else if ((fd = match_event_file(filename,
keyboard_event_drivers)) != -1) {
register_io_monitor_chunk(fd, filename, keypress_cb,
&keyboard_dev_list);
match = TRUE;
}
if ((fd = match_event_file_by_caps(filename, switch_event_types,
switch_event_keys)) != -1) {
register_io_monitor_chunk(fd, filename, switch_cb,
&switch_dev_list);
match = TRUE;
}
if (!match) {
register_io_monitor_chunk(fd, filename, misc_cb,
&misc_dev_list);
}
EXIT:;
}
static void remove_input_device(GSList **devices, const gchar *device)
{
gconstpointer iomon_id = NULL;
GSList *list_entry = NULL;
/* Try to find a matching device I/O monitor */
list_entry = g_slist_find_custom(*devices, device,
iomon_name_compare);
/* If we find one, obtain the iomon ID,
* remove the entry and finally unregister the I/O monitor
*/
if (list_entry != NULL) {
iomon_id = list_entry->data;
*devices = g_slist_remove(*devices, iomon_id);
mce_unregister_io_monitor(iomon_id);
}
}
/**
* Update list of input devices
* Remove the I/O monitor for the specified device (if existing)
* and (re)open it if available
*
* @param device The device to add/remove
* @param add TRUE if the device was added, FALSE if it was removed
* @return TRUE on success, FALSE on failure
*/
static void update_inputdevices(const gchar *device, gboolean add)
{
/* Try to find a matching touchscreen I/O monitor */
remove_input_device(&touchscreen_dev_list, device);
/* Try to find a matching keyboard I/O monitor */
remove_input_device(&keyboard_dev_list, device);
/* Try to find a matching switch I/O monitor */
remove_input_device(&switch_dev_list, device);
/* Try to find a matching misc I/O monitor */
remove_input_device(&misc_dev_list, device);
if (add == TRUE)
match_and_register_io_monitor(device);
}
/**
* Scan /dev/input for input event devices
* @return TRUE on success, FALSE on failure
*/
static gboolean scan_inputdevices(void)
{
DIR *dir = NULL;
struct dirent *direntry = NULL;
gboolean status = FALSE;
if ((dir = opendir(DEV_INPUT_PATH)) == NULL) {
mce_log(LL_ERR, "opendir() failed; %s", g_strerror(errno));
errno = 0;
goto EXIT;
}
for (direntry = readdir(dir);
(direntry != NULL && telldir(dir));
direntry = readdir(dir)) {
gchar *filename = NULL;
if (strncmp(direntry->d_name, EVENT_FILE_PREFIX,
strlen(EVENT_FILE_PREFIX)) != 0) {
mce_log(LL_DEBUG,
"`%s/%s' skipped",
DEV_INPUT_PATH,
direntry->d_name);
continue;
}
filename = g_strconcat(DEV_INPUT_PATH, "/",
direntry->d_name, NULL);
match_and_register_io_monitor(filename);
g_free(filename);
}
/* Report, but ignore, errors when closing directory */
if (closedir(dir) == -1) {
mce_log(LL_ERR,
"closedir() failed; %s", g_strerror(errno));
errno = 0;
}
status = TRUE;
EXIT:
return status;
}
/**
* Unregister monitors for input devices allocated by scan_inputdevices
*/
static void unregister_inputdevices(void)
{
if (touchscreen_dev_list != NULL) {
g_slist_foreach(touchscreen_dev_list,
(GFunc)unregister_io_monitor, NULL);
g_slist_free(touchscreen_dev_list);
touchscreen_dev_list = NULL;
}
if (keyboard_dev_list != NULL) {
g_slist_foreach(keyboard_dev_list,
(GFunc)unregister_io_monitor, NULL);
g_slist_free(keyboard_dev_list);
keyboard_dev_list = NULL;
}
if (switch_dev_list != NULL) {
g_slist_foreach(switch_dev_list,
(GFunc)unregister_io_monitor, NULL);
g_slist_free(switch_dev_list);
switch_dev_list = NULL;
}
if (misc_dev_list != NULL) {
g_slist_foreach(misc_dev_list,
(GFunc)unregister_io_monitor, NULL);
g_slist_free(misc_dev_list);
misc_dev_list = NULL;
}
}
/**
* Callback for directory changes
*
* @param monitor Unused
* @param file The file that changed
* @param other_file Unused
* @param event_type The event that occured
* @param user_data Unused
*/
static void dir_changed_cb(GFileMonitor *monitor,
GFile *file, GFile *other_file,
GFileMonitorEvent event_type, gpointer user_data)
{
(void)monitor;
(void)other_file;
(void)user_data;
switch (event_type) {
case G_FILE_MONITOR_EVENT_CREATED:
update_inputdevices(g_file_get_path(file), TRUE);
break;
case G_FILE_MONITOR_EVENT_DELETED:
update_inputdevices(g_file_get_path(file), FALSE);
break;
default:
break;
}
}
/**
* Init function for the /dev/input event component
*
* @return TRUE on success, FALSE on failure
*/
gboolean mce_input_init(void)
{
GError *error = NULL;
gboolean status = FALSE;
g_type_init();
/* Retrieve a GFile pointer to the directory to monitor */
dev_input_gfp = g_file_new_for_path(DEV_INPUT_PATH);
/* Monitor the directory */
if ((dev_input_gfmp = g_file_monitor_directory(dev_input_gfp,
G_FILE_MONITOR_NONE,
NULL, &error)) == NULL) {
mce_log(LL_ERR,
"Failed to add monitor for directory `%s'; %s",
DEV_INPUT_PATH, error->message);
goto EXIT;
}
/* XXX: There is a race condition here; if a file (dis)appears
* after this scan, but before we start monitoring,
* then we'll miss that device. The race is miniscule though,
* and any workarounds are likely to be cumbersome
*/
/* Find the initial set of input devices */
if ((status = scan_inputdevices()) == FALSE) {
g_file_monitor_cancel(dev_input_gfmp);
dev_input_gfmp = NULL;
goto EXIT;
}
/* Connect "changed" signal for the directory monitor */
g_signal_connect(G_OBJECT(dev_input_gfmp), "changed",
G_CALLBACK(dir_changed_cb), NULL);
EXIT:
g_clear_error(&error);
return status;
}
/**
* Exit function for the /dev/input event component
*/
void mce_input_exit(void)
{
if (dev_input_gfmp != NULL)
g_file_monitor_cancel(dev_input_gfmp);
unregister_inputdevices();
/* Remove all timer sources */
cancel_touchscreen_io_monitor_timeout();
cancel_keypress_repeat_timeout();
cancel_misc_io_monitor_timeout();
return;
}