-
Notifications
You must be signed in to change notification settings - Fork 49
/
kevent.c
1265 lines (1156 loc) · 28.4 KB
/
kevent.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
/*
* This file is part of the Advance project.
*
* Copyright (C) 2003, 2005 Andrea Mazzoleni
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* In addition, as a special exception, Andrea Mazzoleni
* gives permission to link the code of this program with
* the MAME library (or with modified versions of MAME that use the
* same license as MAME), and distribute linked combinations including
* the two. You must obey the GNU General Public License in all
* respects for all of the code used other than MAME. If you modify
* this file, you may extend this exception to your version of the
* file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*/
#include "portable.h"
#include "kevent.h"
#include "log.h"
#include "oslinux.h"
#include "target.h"
#include "error.h"
#include "event.h"
#include <linux/input.h>
#if HAVE_TERMIOS_H
#include <termios.h>
#endif
#if HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#if HAVE_SYS_KD_H
#include <sys/kd.h>
#endif
#if HAVE_SYS_VT_H
#include <sys/vt.h>
#endif
#define EVENT_KEYBOARD_MAX 8
#define EVENT_KEYBOARD_DEVICE_MAX 32
struct keyboard_item_context {
int fe; /**< Handle of the event interface. */
unsigned vendor;
unsigned product;
unsigned version;
unsigned bus;
unsigned keys;
unsigned order;
unsigned char evtype_bitmask[EV_MAX / 8 + 1];
unsigned char key_bitmask[KEY_MAX / 8 + 1];
adv_bool state[KEY_MAX];
unsigned led_state;
};
#define LOW_INVALID ((unsigned)0xFFFFFFFF)
struct keyb_event_context {
struct termios old0_kdbtermios;
struct termios old1_kdbtermios;
int old0_kdbmode;
int old0_terminalmode;
int f0; /**< Handle of the local TTY interface. */
int f1; /**< Handle of the process TTY interface. It may be the same or different than ::f0. */
adv_bool disable_special_flag; /**< Disable special hotkeys. */
unsigned map_up_to_low[KEYB_MAX];
unsigned mac;
struct keyboard_item_context map[EVENT_KEYBOARD_MAX];
adv_bool graphics_flag; /**< Set the terminal in graphics mode. */
adv_bool passive_flag; /**< Be passive on some actions. Required for compatibility with other libs. */
};
static struct keyb_pair {
unsigned up_code;
unsigned low_code;
} KEYS[] = {
#ifdef KEY_A
{ KEYB_A, KEY_A },
#endif
#ifdef KEY_B
{ KEYB_B, KEY_B },
#endif
#ifdef KEY_C
{ KEYB_C, KEY_C },
#endif
#ifdef KEY_D
{ KEYB_D, KEY_D },
#endif
#ifdef KEY_E
{ KEYB_E, KEY_E },
#endif
#ifdef KEY_F
{ KEYB_F, KEY_F },
#endif
#ifdef KEY_G
{ KEYB_G, KEY_G },
#endif
#ifdef KEY_H
{ KEYB_H, KEY_H },
#endif
#ifdef KEY_I
{ KEYB_I, KEY_I },
#endif
#ifdef KEY_J
{ KEYB_J, KEY_J },
#endif
#ifdef KEY_K
{ KEYB_K, KEY_K },
#endif
#ifdef KEY_L
{ KEYB_L, KEY_L },
#endif
#ifdef KEY_M
{ KEYB_M, KEY_M },
#endif
#ifdef KEY_N
{ KEYB_N, KEY_N },
#endif
#ifdef KEY_O
{ KEYB_O, KEY_O },
#endif
#ifdef KEY_P
{ KEYB_P, KEY_P },
#endif
#ifdef KEY_Q
{ KEYB_Q, KEY_Q },
#endif
#ifdef KEY_R
{ KEYB_R, KEY_R },
#endif
#ifdef KEY_S
{ KEYB_S, KEY_S },
#endif
#ifdef KEY_T
{ KEYB_T, KEY_T },
#endif
#ifdef KEY_U
{ KEYB_U, KEY_U },
#endif
#ifdef KEY_V
{ KEYB_V, KEY_V },
#endif
#ifdef KEY_W
{ KEYB_W, KEY_W },
#endif
#ifdef KEY_X
{ KEYB_X, KEY_X },
#endif
#ifdef KEY_Y
{ KEYB_Y, KEY_Y },
#endif
#ifdef KEY_Z
{ KEYB_Z, KEY_Z },
#endif
#ifdef KEY_0
{ KEYB_0, KEY_0 },
#endif
#ifdef KEY_1
{ KEYB_1, KEY_1 },
#endif
#ifdef KEY_2
{ KEYB_2, KEY_2 },
#endif
#ifdef KEY_3
{ KEYB_3, KEY_3 },
#endif
#ifdef KEY_4
{ KEYB_4, KEY_4 },
#endif
#ifdef KEY_5
{ KEYB_5, KEY_5 },
#endif
#ifdef KEY_6
{ KEYB_6, KEY_6 },
#endif
#ifdef KEY_7
{ KEYB_7, KEY_7 },
#endif
#ifdef KEY_8
{ KEYB_8, KEY_8 },
#endif
#ifdef KEY_9
{ KEYB_9, KEY_9 },
#endif
#ifdef KEY_KP0
{ KEYB_0_PAD, KEY_KP0 },
#endif
#ifdef KEY_KP1
{ KEYB_1_PAD, KEY_KP1 },
#endif
#ifdef KEY_KP2
{ KEYB_2_PAD, KEY_KP2 },
#endif
#ifdef KEY_KP3
{ KEYB_3_PAD, KEY_KP3 },
#endif
#ifdef KEY_KP4
{ KEYB_4_PAD, KEY_KP4 },
#endif
#ifdef KEY_KP5
{ KEYB_5_PAD, KEY_KP5 },
#endif
#ifdef KEY_KP6
{ KEYB_6_PAD, KEY_KP6 },
#endif
#ifdef KEY_KP7
{ KEYB_7_PAD, KEY_KP7 },
#endif
#ifdef KEY_KP8
{ KEYB_8_PAD, KEY_KP8 },
#endif
#ifdef KEY_KP9
{ KEYB_9_PAD, KEY_KP9 },
#endif
#ifdef KEY_F1
{ KEYB_F1, KEY_F1 },
#endif
#ifdef KEY_F2
{ KEYB_F2, KEY_F2 },
#endif
#ifdef KEY_F3
{ KEYB_F3, KEY_F3 },
#endif
#ifdef KEY_F4
{ KEYB_F4, KEY_F4 },
#endif
#ifdef KEY_F5
{ KEYB_F5, KEY_F5 },
#endif
#ifdef KEY_F6
{ KEYB_F6, KEY_F6 },
#endif
#ifdef KEY_F7
{ KEYB_F7, KEY_F7 },
#endif
#ifdef KEY_F8
{ KEYB_F8, KEY_F8 },
#endif
#ifdef KEY_F9
{ KEYB_F9, KEY_F9 },
#endif
#ifdef KEY_F10
{ KEYB_F10, KEY_F10 },
#endif
#ifdef KEY_F11
{ KEYB_F11, KEY_F11 },
#endif
#ifdef KEY_F12
{ KEYB_F12, KEY_F12 },
#endif
#ifdef KEY_ESC
{ KEYB_ESC, KEY_ESC },
#endif
#ifdef KEY_GRAVE
{ KEYB_BACKQUOTE, KEY_GRAVE },
#endif
#ifdef KEY_MINUS
{ KEYB_MINUS, KEY_MINUS },
#endif
#ifdef KEY_EQUAL
{ KEYB_EQUALS, KEY_EQUAL },
#endif
#ifdef KEY_BACKSPACE
{ KEYB_BACKSPACE, KEY_BACKSPACE },
#endif
#ifdef KEY_TAB
{ KEYB_TAB, KEY_TAB },
#endif
#ifdef KEY_LEFTBRACE
{ KEYB_OPENBRACE, KEY_LEFTBRACE },
#endif
#ifdef KEY_RIGHTBRACE
{ KEYB_CLOSEBRACE, KEY_RIGHTBRACE },
#endif
#ifdef KEY_ENTER
{ KEYB_ENTER, KEY_ENTER },
#endif
#ifdef KEY_SEMICOLON
{ KEYB_SEMICOLON, KEY_SEMICOLON },
#endif
#ifdef KEY_APOSTROPHE
{ KEYB_QUOTE, KEY_APOSTROPHE },
#endif
#ifdef KEY_103RD
{ KEYB_BACKSLASH, KEY_103RD }, /* KEY_BACKSLASH */
#endif
#ifdef KEY_102ND
{ KEYB_LESS, KEY_102ND },
#endif
#ifdef KEY_COMMA
{ KEYB_COMMA, KEY_COMMA },
#endif
#ifdef KEY_DOT
{ KEYB_PERIOD, KEY_DOT },
#endif
#ifdef KEY_SLASH
{ KEYB_SLASH, KEY_SLASH },
#endif
#ifdef KEY_SPACE
{ KEYB_SPACE, KEY_SPACE },
#endif
#ifdef KEY_INSERT
{ KEYB_INSERT, KEY_INSERT },
#endif
#ifdef KEY_DELETE
{ KEYB_DEL, KEY_DELETE },
#endif
#ifdef KEY_HOME
{ KEYB_HOME, KEY_HOME },
#endif
#ifdef KEY_END
{ KEYB_END, KEY_END },
#endif
#ifdef KEY_PAGEUP
{ KEYB_PGUP, KEY_PAGEUP },
#endif
#ifdef KEY_PAGEDOWN
{ KEYB_PGDN, KEY_PAGEDOWN },
#endif
#ifdef KEY_LEFT
{ KEYB_LEFT, KEY_LEFT },
#endif
#ifdef KEY_RIGHT
{ KEYB_RIGHT, KEY_RIGHT },
#endif
#ifdef KEY_UP
{ KEYB_UP, KEY_UP },
#endif
#ifdef KEY_DOWN
{ KEYB_DOWN, KEY_DOWN },
#endif
#ifdef KEY_KPSLASH
{ KEYB_SLASH_PAD, KEY_KPSLASH },
#endif
#ifdef KEY_KPASTERISK
{ KEYB_ASTERISK, KEY_KPASTERISK },
#endif
#ifdef KEY_KPMINUS
{ KEYB_MINUS_PAD, KEY_KPMINUS },
#endif
#ifdef KEY_KPPLUS
{ KEYB_PLUS_PAD, KEY_KPPLUS },
#endif
#ifdef KEY_KPDOT
{ KEYB_PERIOD_PAD, KEY_KPDOT },
#endif
#ifdef KEY_KPENTER
{ KEYB_ENTER_PAD, KEY_KPENTER },
#endif
#ifdef KEY_SYSRQ
{ KEYB_PRTSCR, KEY_SYSRQ },
#endif
#ifdef KEY_PAUSE
{ KEYB_PAUSE, KEY_PAUSE },
#endif
#ifdef KEY_LEFTSHIFT
{ KEYB_LSHIFT, KEY_LEFTSHIFT },
#endif
#ifdef KEY_RIGHTSHIFT
{ KEYB_RSHIFT, KEY_RIGHTSHIFT },
#endif
#ifdef KEY_LEFTCTRL
{ KEYB_LCONTROL, KEY_LEFTCTRL },
#endif
#ifdef KEY_RIGHTCTRL
{ KEYB_RCONTROL, KEY_RIGHTCTRL },
#endif
#ifdef KEY_LEFTALT
{ KEYB_ALT, KEY_LEFTALT },
#endif
#ifdef KEY_RIGHTALT
{ KEYB_ALTGR, KEY_RIGHTALT },
#endif
#ifdef KEY_LEFTMETA
{ KEYB_LWIN, KEY_LEFTMETA },
#endif
#ifdef KEY_RIGHTMETA
{ KEYB_RWIN, KEY_RIGHTMETA },
#endif
#ifdef KEY_MENU
{ KEYB_MENU, KEY_MENU },
#endif
#ifdef KEY_SCROLLLOCK
{ KEYB_SCRLOCK, KEY_SCROLLLOCK },
#endif
#ifdef KEY_NUMLOCK
{ KEYB_NUMLOCK, KEY_NUMLOCK },
#endif
#ifdef KEY_CAPSLOCK
{ KEYB_CAPSLOCK, KEY_CAPSLOCK },
#endif
#ifdef KEY_STOP
{ KEYB_STOP, KEY_STOP },
#endif
#ifdef KEY_AGAIN
{ KEYB_AGAIN, KEY_AGAIN },
#endif
#ifdef KEY_PROPS
{ KEYB_PROPS, KEY_PROPS },
#endif
#ifdef KEY_UNDO
{ KEYB_UNDO, KEY_UNDO },
#endif
#ifdef KEY_FRONT
{ KEYB_FRONT, KEY_FRONT },
#endif
#ifdef KEY_COPY
{ KEYB_COPY, KEY_COPY },
#endif
#ifdef KEY_OPEN
{ KEYB_OPEN, KEY_OPEN },
#endif
#ifdef KEY_PASTE
{ KEYB_PASTE, KEY_PASTE },
#endif
#ifdef KEY_FIND
{ KEYB_FIND, KEY_FIND },
#endif
#ifdef KEY_CUT
{ KEYB_CUT, KEY_CUT },
#endif
#ifdef KEY_HELP
{ KEYB_HELP, KEY_HELP },
#endif
#ifdef KEY_MENU
{ KEYB_MENU, KEY_MENU },
#endif
#ifdef KEY_CALC
{ KEYB_CALC, KEY_CALC },
#endif
#ifdef KEY_SETUP
{ KEYB_SETUP, KEY_SETUP },
#endif
#ifdef KEY_SLEEP
{ KEYB_SLEEP, KEY_SLEEP },
#endif
#ifdef KEY_WAKEUP
{ KEYB_WAKEUP, KEY_WAKEUP },
#endif
#ifdef KEY_FILE
{ KEYB_FILE, KEY_FILE },
#endif
#ifdef KEY_SENDFILE
{ KEYB_SENDFILE, KEY_SENDFILE },
#endif
#ifdef KEY_DELETEFILE
{ KEYB_DELETEFILE, KEY_DELETEFILE },
#endif
#ifdef KEY_XFER
{ KEYB_XFER, KEY_XFER },
#endif
#ifdef KEY_PROG1
{ KEYB_PROG1, KEY_PROG1 },
#endif
#ifdef KEY_PROG2
{ KEYB_PROG2, KEY_PROG2 },
#endif
#ifdef KEY_WWW
{ KEYB_WWW, KEY_WWW },
#endif
#ifdef KEY_MSDOS
{ KEYB_MSDOS, KEY_MSDOS },
#endif
#ifdef KEY_COFFEE
{ KEYB_COFFEE, KEY_COFFEE },
#endif
#ifdef KEY_DIRECTION
{ KEYB_DIRECTION, KEY_DIRECTION },
#endif
#ifdef KEY_CYCLEWINDOWS
{ KEYB_CYCLEWINDOWS, KEY_CYCLEWINDOWS },
#endif
#ifdef KEY_MAIL
{ KEYB_MAIL, KEY_MAIL },
#endif
#ifdef KEY_BOOKMARKS
{ KEYB_BOOKMARKS, KEY_BOOKMARKS },
#endif
#ifdef KEY_COMPUTER
{ KEYB_COMPUTER, KEY_COMPUTER },
#endif
#ifdef KEY_BACK
{ KEYB_BACK, KEY_BACK },
#endif
#ifdef KEY_FORWARD
{ KEYB_FORWARD, KEY_FORWARD },
#endif
#ifdef KEY_CLOSECD
{ KEYB_CLOSECD, KEY_CLOSECD },
#endif
#ifdef KEY_EJECTCD
{ KEYB_EJECTCD, KEY_EJECTCD },
#endif
#ifdef KEY_EJECTCLOSECD
{ KEYB_EJECTCLOSECD, KEY_EJECTCLOSECD },
#endif
#ifdef KEY_NEXTSONG
{ KEYB_NEXTSONG, KEY_NEXTSONG },
#endif
#ifdef KEY_PLAYPAUSE
{ KEYB_PLAYPAUSE, KEY_PLAYPAUSE },
#endif
#ifdef KEY_PREVIOUSSONG
{ KEYB_PREVIOUSSONG, KEY_PREVIOUSSONG },
#endif
#ifdef KEY_STOPCD
{ KEYB_STOPCD, KEY_STOPCD },
#endif
#ifdef KEY_RECORD
{ KEYB_RECORD, KEY_RECORD },
#endif
#ifdef KEY_REWIND
{ KEYB_REWIND, KEY_REWIND },
#endif
#ifdef KEY_PHONE
{ KEYB_PHONE, KEY_PHONE },
#endif
#ifdef KEY_ISO
{ KEYB_ISO, KEY_ISO },
#endif
#ifdef KEY_CONFIG
{ KEYB_CONFIG, KEY_CONFIG },
#endif
#ifdef KEY_HOMEPAGE
{ KEYB_HOMEPAGE, KEY_HOMEPAGE },
#endif
#ifdef KEY_REFRESH
{ KEYB_REFRESH, KEY_REFRESH },
#endif
#ifdef KEY_EXIT
{ KEYB_EXIT, KEY_EXIT },
#endif
#ifdef KEY_MOVE
{ KEYB_MOVE, KEY_MOVE },
#endif
#ifdef KEY_EDIT
{ KEYB_EDIT, KEY_EDIT },
#endif
#ifdef KEY_SCROLLUP
{ KEYB_SCROLLUP, KEY_SCROLLUP },
#endif
#ifdef KEY_SCROLLDOWN
{ KEYB_SCROLLDOWN, KEY_SCROLLDOWN },
#endif
#ifdef KEY_KPLEFTPAREN
{ KEYB_KPLEFTPAREN, KEY_KPLEFTPAREN },
#endif
#ifdef KEY_KPRIGHTPAREN
{ KEYB_KPRIGHTPAREN, KEY_KPRIGHTPAREN },
#endif
#ifdef KEY_INTL1
{ KEYB_INTL1, KEY_INTL1 },
#endif
#ifdef KEY_INTL2
{ KEYB_INTL2, KEY_INTL2 },
#endif
#ifdef KEY_INTL3
{ KEYB_INTL3, KEY_INTL3 },
#endif
#ifdef KEY_INTL4
{ KEYB_INTL4, KEY_INTL4 },
#endif
#ifdef KEY_INTL5
{ KEYB_INTL5, KEY_INTL5 },
#endif
#ifdef KEY_INTL6
{ KEYB_INTL6, KEY_INTL6 },
#endif
#ifdef KEY_INTL7
{ KEYB_INTL7, KEY_INTL7 },
#endif
#ifdef KEY_INTL8
{ KEYB_INTL8, KEY_INTL8 },
#endif
#ifdef KEY_INTL9
{ KEYB_INTL9, KEY_INTL9 },
#endif
#ifdef KEY_LANG1
{ KEYB_LANG1, KEY_LANG1 },
#endif
#ifdef KEY_LANG2
{ KEYB_LANG2, KEY_LANG2 },
#endif
#ifdef KEY_LANG3
{ KEYB_LANG3, KEY_LANG3 },
#endif
#ifdef KEY_LANG4
{ KEYB_LANG4, KEY_LANG4 },
#endif
#ifdef KEY_LANG5
{ KEYB_LANG5, KEY_LANG5 },
#endif
#ifdef KEY_LANG6
{ KEYB_LANG6, KEY_LANG6 },
#endif
#ifdef KEY_LANG7
{ KEYB_LANG7, KEY_LANG7 },
#endif
#ifdef KEY_LANG8
{ KEYB_LANG8, KEY_LANG8 },
#endif
#ifdef KEY_LANG9
{ KEYB_LANG9, KEY_LANG9 },
#endif
#ifdef KEY_PLAYCD
{ KEYB_PLAYCD, KEY_PLAYCD },
#endif
#ifdef KEY_PAUSECD
{ KEYB_PAUSECD, KEY_PAUSECD },
#endif
#ifdef KEY_PROG3
{ KEYB_PROG3, KEY_PROG3 },
#endif
#ifdef KEY_PROG4
{ KEYB_PROG4, KEY_PROG4 },
#endif
#ifdef KEY_SUSPEND
{ KEYB_SUSPEND, KEY_SUSPEND },
#endif
#ifdef KEY_CLOSE
{ KEYB_CLOSE, KEY_CLOSE },
#endif
#ifdef KEY_BRIGHTNESSDOWN
{ KEYB_BRIGHTNESSDOWN, KEY_BRIGHTNESSDOWN },
#endif
#ifdef KEY_BRIGHTNESSUP
{ KEYB_BRIGHTNESSUP, KEY_BRIGHTNESSUP },
#endif
#ifdef KEY_MACRO
{ KEYB_MACRO, KEY_MACRO },
#endif
#ifdef KEY_MUTE
{ KEYB_MUTE, KEY_MUTE },
#endif
#ifdef KEY_VOLUMEDOWN
{ KEYB_VOLUMEDOWN, KEY_VOLUMEDOWN },
#endif
#ifdef KEY_VOLUMEUP
{ KEYB_VOLUMEUP, KEY_VOLUMEUP },
#endif
#ifdef KEY_POWER
{ KEYB_POWER, KEY_POWER },
#endif
#ifdef KEY_COMPOSE
{ KEYB_COMPOSE, KEY_COMPOSE },
#endif
#ifdef KEY_F13
{ KEYB_F13, KEY_F13 },
#endif
#ifdef KEY_F14
{ KEYB_F14, KEY_F14 },
#endif
#ifdef KEY_F15
{ KEYB_F15, KEY_F15 },
#endif
#ifdef KEY_F16
{ KEYB_F16, KEY_F16 },
#endif
#ifdef KEY_F17
{ KEYB_F17, KEY_F17 },
#endif
#ifdef KEY_F18
{ KEYB_F18, KEY_F18 },
#endif
#ifdef KEY_F19
{ KEYB_F19, KEY_F19 },
#endif
#ifdef KEY_F20
{ KEYB_F20, KEY_F20 },
#endif
#ifdef KEY_F21
{ KEYB_F21, KEY_F21 },
#endif
#ifdef KEY_F22
{ KEYB_F22, KEY_F22 },
#endif
#ifdef KEY_F23
{ KEYB_F23, KEY_F23 },
#endif
#ifdef KEY_F24
{ KEYB_F24, KEY_F24 },
#endif
{ KEYB_MAX, 0 }
};
static struct keyb_event_context event_state;
static adv_device DEVICE[] = {
{ "auto", -1, "Linux input-event keyboard" },
{ 0, 0, 0 }
};
static void keyb_event_clear(void)
{
unsigned i, j;
for (i = 0; i < event_state.mac; ++i) {
for (j = 0; j < KEY_MAX; ++j) {
event_state.map[i].state[j] = 0;
event_state.map[i].led_state = 0;
}
}
}
static adv_error keyb_event_setup(struct keyboard_item_context* item, int f)
{
item->fe = f;
memset(item->key_bitmask, 0, sizeof(item->key_bitmask));
if (event_test_bit(EV_KEY, item->evtype_bitmask)) {
if (ioctl(f, EVIOCGBIT(EV_KEY, sizeof(item->key_bitmask)), item->key_bitmask) < 0) {
log_std(("event: error in ioctl(EVIOCGBIT(EV_KEY,%d))\n", (int)KEY_MAX));
return -1;
}
}
return 0;
}
static int event_state_compare(const void* void_a, const void* void_b)
{
const struct keyboard_item_context* a = void_a;
const struct keyboard_item_context* b = void_b;
if (a->order < b->order)
return -1;
if (a->order > b->order)
return 1;
if (a->keys > b->keys)
return -1;
if (a->keys < b->keys)
return 1;
return 0;
}
adv_error keyb_event_init(int keyb_id, adv_bool disable_special)
{
struct keyb_pair* j;
unsigned i;
adv_bool eacces = 0;
struct event_location map[EVENT_KEYBOARD_DEVICE_MAX];
unsigned mac;
log_std(("keyb:event: keyb_event_init(id:%d, disable_special:%d)\n", keyb_id, (int)disable_special));
if (target_wm()) {
error_set("Unsupported in X.\n");
return -1;
}
for (i = 0; i < KEYB_MAX; ++i) {
event_state.map_up_to_low[i] = LOW_INVALID;
}
for (j = KEYS; j->up_code != KEYB_MAX; ++j) {
event_state.map_up_to_low[j->up_code] = j->low_code;
}
mac = event_locate(map, EVENT_KEYBOARD_DEVICE_MAX, "event", &eacces);
event_state.mac = 0;
for (i = 0; i < mac; ++i) {
int f;
unsigned j;
struct keyboard_item_context* item = &event_state.map[event_state.mac];
if (event_state.mac >= EVENT_KEYBOARD_MAX)
continue;
f = event_open(map[i].file, item->evtype_bitmask, sizeof(item->evtype_bitmask));
if (f == -1)
continue;
if (!event_is_keyboard(f, item->evtype_bitmask)) {
log_std(("keyb:event: not a keyboard on device %s\n", map[i].file));
event_close(f);
continue;
}
if (keyb_event_setup(item, f) != 0) {
event_close(f);
continue;
}
/* put before real keyboard */
if (event_is_joystick(f, item->evtype_bitmask))
item->order = 2;
else if (event_is_mouse(f, item->evtype_bitmask))
item->order = 1;
else
item->order = 0;
/* count number of keys */
item->keys = 0;
for (j = 0; j < KEY_MAX; ++j) {
if (event_test_bit(j, item->key_bitmask))
++item->keys;
}
item->vendor = map[i].vendor;
item->product = map[i].product;
item->version = map[i].version;
item->bus = map[i].bus;
++event_state.mac;
}
if (!event_state.mac) {
error_set("No keyboard found.\n");
return -1;
}
/* sort the device by importance */
qsort(event_state.map, event_state.mac, sizeof(event_state.map[0]), event_state_compare);
event_state.disable_special_flag = disable_special;
return 0;
}
void keyb_event_done(void)
{
unsigned i;
log_std(("keyb:event: keyb_event_done()\n"));
for (i = 0; i < event_state.mac; ++i)
event_close(event_state.map[i].fe);
event_state.mac = 0;
}
adv_error keyb_event_enable(adv_bool graphics)
{
struct termios kdbtermios;
log_std(("keyb:event: keyb_event_enable(graphics:%d)\n", (int)graphics));
#if defined(USE_VIDEO_SDL)
if (os_internal_sdl_is_video_active()) {
error_set("The event keyboard driver cannot be used with the SDL video driver\n");
return -1;
}
#endif
event_state.passive_flag = 0;
#ifdef USE_VIDEO_SVGALIB
/* SVGALIB already set the terminal in KD_GRAPHICS mode and */
/* it waits on a signal on a vt switch */
if (os_internal_svgalib_is_video_mode_active()) {
event_state.passive_flag = 1;
}
#endif
event_state.graphics_flag = graphics;
/*
* Open the local active console with /dev/tty0.
* This one is always local, but when connected remotely you can open it only as root.
*/
event_state.f0 = open("/dev/tty0", O_RDONLY | O_NONBLOCK);
if (event_state.f0 == -1)
log_std(("keyb:event: Failed to open /dev/tty0. %s\n", strerror(errno)));
/*
* Open the process console with /dev/tty.
* This one could be either local or remote.
*/
event_state.f1 = open("/dev/tty", O_RDONLY | O_NONBLOCK);
if (event_state.f1 == -1)
log_std(("keyb:event: Failed to open /dev/tty. %s\n", strerror(errno)));
/* if only tty, assign it back to tty0, assuming it's the local console */
if (event_state.f0 == -1) {
event_state.f0 = event_state.f1;
event_state.f1 = -1;
}
/* at least one is needed */
if (event_state.f0 == -1) {
error_set("Not able to open the local console. Try running as root.\n");
goto err_close;
}
if (ioctl(event_state.f0, KDGKBMODE, &event_state.old0_kdbmode) != 0) {
if (errno == ENOTTY) {
error_set("Not able to query the local console. Try running as root.\n");
} else {
error_set("Error enabling the even keyboard driver. Function ioctl(/dev/tty0, KDGKBMODE) failed. %s\n", strerror(errno));
}
goto err_close;
}
if (tcgetattr(event_state.f0, &event_state.old0_kdbtermios) != 0) {
error_set("Error enabling the event keyboard driver. Function tcgetattr(/dev/tty0) failed.\n");
goto err_close;
}
if (event_state.f1 != -1 && tcgetattr(event_state.f1, &event_state.old1_kdbtermios) != 0) {
error_set("Error enabling the event keyboard driver. Function tcgetattr(/dev/tty) failed.\n");
goto err_close;
}
if (event_state.f1 != -1) {
/*
* Disable ECHO on /dev/tty.
* We want do avoid keypresses to show in the remote consoles,
* but still having control of it, like when pressing Ctrl+C
*/
kdbtermios = event_state.old1_kdbtermios;
kdbtermios.c_lflag &= ~ECHO;
log_std(("keyb:event: tcsetattr(/dev/tty, TCSAFLUSH, %sICANON %sECHO %sISIG)\n", (kdbtermios.c_lflag & ICANON) ? "" : "~", (kdbtermios.c_lflag & ECHO) ? "" : "~", (kdbtermios.c_lflag & ISIG) ? "" : "~"));
if (tcsetattr(event_state.f1, TCSAFLUSH, &kdbtermios) != 0) {
error_set("Error enabling the event keyboard driver. Function tcsetattr(/dev/tty, TCSAFLUSH) failed.\n");
goto err_close;
}
}
/*
* Set for graphics mode /dev/tty0.
* We disable any interaction as the keyboard is going to be used directly.
* Settings taken from SVGALIB.
*/
kdbtermios = event_state.old0_kdbtermios;
kdbtermios.c_lflag &= ~(ICANON | ECHO | ISIG);
kdbtermios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
kdbtermios.c_cc[VMIN] = 0;
kdbtermios.c_cc[VTIME] = 0;
log_std(("keyb:event: tcsetattr(/dev/tty0, TCSAFLUSH, %sICANON %sECHO %sISIG)\n", (kdbtermios.c_lflag & ICANON) ? "" : "~", (kdbtermios.c_lflag & ECHO) ? "" : "~", (kdbtermios.c_lflag & ISIG) ? "" : "~"));
if (tcsetattr(event_state.f0, TCSAFLUSH, &kdbtermios) != 0) {
error_set("Error enabling the event keyboard driver. Function tcsetattr(/dev/tty0, TCSAFLUSH) failed.\n");
goto err_term1;
}
if (event_state.disable_special_flag) {
/* enter in raw mode only to disable the ALT+Fx sequences */
log_std(("keyb:event: ioctl(/dev/tty0, KDSKBMODE, K_MEDIUMRAW)\n"));
if (ioctl(event_state.f0, KDSKBMODE, K_MEDIUMRAW) != 0) {
error_set("Error enabling the event keyboard driver. Function ioctl(/dev/tty0, KDSKBMODE) failed.\n");
goto err_term0;
}
}
if (event_state.graphics_flag) {
if (ioctl(event_state.f0, KDGETMODE, &event_state.old0_terminalmode) != 0) {
error_set("Error enabling the event keyboard driver. Function ioctl(/dev/tty0, KDGETMODE) failed. %s\n", strerror(errno));
goto err_mode;
}
if (event_state.old0_terminalmode == KD_GRAPHICS) {
log_std(("WARNING:keyb:event: terminal already in KD_GRAPHICS mode\n"));
}
/* set the console in graphics mode (disable cursor) */
log_std(("keyb:event: ioctl(/dev/tty0, KDSETMODE, KD_GRAPHICS)\n"));
if (ioctl(event_state.f0, KDSETMODE, KD_GRAPHICS) < 0) {
log_std(("keyb:event: ioctl(/dev/tty0, KDSETMODE, KD_GRAPHICS) failed\n"));
error_set("Error setting /dev/tty0 in graphics mode.\n");
goto err_mode;
}
}
keyb_event_clear();
return 0;
err_mode:
log_std(("keyb:event: ioctl(/dev/tty0, KDSKBMODE, %s)\n",
event_state.old0_kdbmode == K_RAW ? "K_RAW" :
event_state.old0_kdbmode == K_XLATE ? "K_XLATE" :
event_state.old0_kdbmode == K_MEDIUMRAW ? "K_MEDIUMRAW" :
event_state.old0_kdbmode == K_UNICODE ? "K_UNICODE" :
event_state.old0_kdbmode == K_OFF ? "K_OFF" :
"other"
));
if (ioctl(event_state.f0, KDSKBMODE, event_state.old0_kdbmode) < 0) {
/* ignore error */
log_std(("keyb:event: ioctl(/dev/tty0, KDSKBMODE, old) failed\n"));
}
err_term0:
log_std(("keyb:event: tcsetattr(/dev/tty0, TCSAFLUSH, %sICANON %sECHO %sISIG)\n", (event_state.old0_kdbtermios.c_lflag & ICANON) ? "" : "~", (event_state.old0_kdbtermios.c_lflag & ECHO) ? "" : "~", (event_state.old0_kdbtermios.c_lflag & ISIG) ? "" : "~"));
if (tcsetattr(event_state.f0, TCSAFLUSH, &event_state.old0_kdbtermios) != 0) {