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
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
|
/*
*
* olpc-kbdshim.c -- low-level support for XO:
* - mouse-based scrolling use the XO grab keys
* - touchpad and dpad rotation (to match screen rotation)
* - user activity monitoring
NOTE: this version of the code is a standalone daemon,
pre-dating the HAL addon version. There are newer features
implemented in olpc-kbdshim-hal which are not implemented here.
In particular, the -hal version can monitor USB devices automatically,
where this version cannot. There are other smaller differences
as well.
*
* Copyright (C) 2009, Paul G Fox, inspired in places
* by code from mouseemu, by Colin Leroy and others.
*
* 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.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <wait.h>
#include <syslog.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sched.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <time.h>
#include <utime.h>
#include <limits.h>
static void inject_uinput_event(unsigned int type, unsigned int code,
int value);
/* are we still in the foreground? */
int daemonized;
/* log to syslog (instead of stderr) */
int logtosyslog;
/* higher values for more debug */
int debug;
/* suppress any actual tranmission or injection of data */
int noxmit;
/* which keys do grabbing? */
#define NGRABKEYS 4
struct keypress {
int code;
int pass;
int pressed;
} grabkey[NGRABKEYS];
int ngrab;
/* command fifo */
char *command_fifo;
int fifo_fd = -1;
/* sysactive path -- where we send user activity/idle events */
char *sysactive_path = "/var/run/powerevents";
#define MAXTIMERS 3
/* list of idle deltas. last timer should stay zero */
int idledeltas[MAXTIMERS + 1] = { 10 * 60, 5 * 60, 5 * 60, 0 };;
/* are we in user-idle state? */
int idleness;
int wasidle;
/* external commands bound to rotate and brightness keys:
*
* the rotate command should inform us via our command fifo when
* a rotate has happened, so that we can adjust the touchpad and
* d-pad rotation.
*
* the brightness command should accept a single argument of
* "up", "down", "min", or "max" (the last two when the alt
* modifier is in effect).
*
* these bindings are done here for convenience, and could be
* done anywhere else in the system just as well. binding
* them here effectively "steals" them from sugar, which has
* internal implementations of rotation and brightness control
* which are no longer correct when this package and olpc-powerd
* are used.
*
* the brightness and volume keys appear on F9/F10 and F11/F12.
* to accomodate gnome apps, which want full access to the F-keys,
* kbdshim will (as of july 2010, see d.l.o. #10213) bind to the
* Fn-modified version of those keys as well, which we've arranged
* to be F21/F22 and F23/F24.
*
* either of the F9-F12 or F21-F42 set of bindings can be disabled
* independently, using the following kbdshim commands:
* f9 to disable capture of F9-F12,
* F9 to enable capture of F9-F12,
* f21 to disable capture of F21-F24
* F21 to enable capture of F21-F24
*/
char *brightness_cmd; /* probably "olpc-brightness" */
char *volume_cmd; /* probably "olpc-volume" */
char *rotate_cmd; /* probably "olpc-rotate" */
int reflect_x, reflect_y; /* inverted mouse (e.g., for ebook mode) */
enum rotation {
ROT_NORMAL,
ROT_LEFT,
ROT_INVERT,
ROT_RIGHT
} rotation;
/* are we scrolling, or not */
int scrolling;
#define SCROLL_QUANTUM 20
#define SCROLL_FILTER_PERCENT 33
int cumul_x, cumul_y;
/* "distance traveled" before we emulate a scroll button event */
int quantum = SCROLL_QUANTUM;
int ratio = SCROLL_FILTER_PERCENT;
/* if true, finger moves scrollbars, instead of window contents */
int reverse;
/* should we capture F9 through F12 for brightness and volume? */
static int use_F9_F12 = 0;
/* likewise for F21 through F14 for brightness and volume */
static int use_BRT_VOL = 1;
/* product, vendor id for keyboard and touchpad */
struct input_id kbd_id = {
bustype: BUS_I8042,
product: 0xffff,
vendor: 0xffff
};
struct input_id tpad_id = {
bustype: BUS_I8042,
product: 0xffff,
vendor: 0xffff
};
extern char *optarg;
extern int optind, opterr, optopt;
/* output events device */
int uinp_fd = -1;
/* input event devices */
int kbd_fd = -1;
int tpad_fd = -1;
/* bit array ops */
#define bits2bytes(x) ((((x)-1)/8)+1)
#define test_bit(bit, array) ( array[(bit)/8] & (1 << (bit)%8) )
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
/* how many /dev/input/eventX devices to check */
#define EVENTDEVICES 20
typedef struct input_id input_id_t;
char *me;
void
usage(void)
{
fprintf(stderr,
"usage: %s [options]\n"
" Grab-scroll configuration:\n"
" '-g N','-G N' Specify which key(s) are used for grabbing.\n"
" A maximum of %d keys can be specified. (Use \"showkey\"\n"
" at the console to find the keycode.) With '-g', the\n"
" key will be processed normally (in addition to instituting\n"
" scrolling). '-G' will cause scrolling but suppress the\n"
" key's normal action. Default is '-g 125 -g 126'. (The\n"
" \"meta\" keys, labeled as \"grab\" keys on the XO laptop.\n"
" Use '-g 0' to suppress grab-scoll support.\n"
" '-v' to reverse the relative motion of mouse and window\n"
" By default the mouse/finger slides the window, but\n"
" with -r it effectively slides the scrollbars instead.\n"
" '-q N' to set how far the mouse/finger must move before a\n"
" scrolling event is generated"
" (default is %d).\n"
" '-n N' If the x or y motion is less than N percent of the\n"
" other (default N is %d) then the smaller of the two is\n"
" dropped. This reduces sideways jitter when scrolling\n"
" vertically,and vice versa.\n"
"\n"
" Touchpad/D-pad rotation:\n"
" '-R <fifoname>' If set, this gives the name of a fifo which\n"
" will be created and monitored for the purpose of rotating\n"
" and reflecting the touchpad and the D-pad. This can be\n"
" used to make their function match the screen's rotation.\n"
" This fifo will also receive setup commands related to\n"
" user activity monitoring (see below).\n"
"\n"
" Keyboard/touchpad identification:\n"
" '-K BB:VV:PP' Specifies the bustype, vendor, and product id\n"
" for the keyboard to be monitored. BB, VV, and PP are hex\n"
" numbers. ffff can be used as a wildcard for any of them.\n"
" Default is '11:ffff:ffff' for the i8042 bus, which will find\n"
" the local keyboard. '03:ffff:ffff' finds any USB keyboard.\n"
" '-T BB:VV:PP' Same as -K, but for the pointer device.\n"
"\n"
" User activity monitor:\n"
" '-A PATH' Specifies path where user idle/activity events will\n"
" be written (default is /var/run/powerevents).\n"
"\n"
" Key bindings:\n"
" '-r rotate-cmd' If set, the command to be run when the XO\n"
" 'rotate' button is detected. (Will be passed no arguments.)\n"
" '-b brightness-cmd' If set, the command to be run when the XO\n"
" brightness keys are used. (Should accept 'min', 'max', 'up'\n"
" or 'down' as the sole argument.)\n"
" '-V volume-cmd' If set, the command to be run when the XO\n"
" volume keys are used. (Should accept 'min', 'max', 'up'\n"
" or 'down' as the sole argument.)\n"
"\n"
" Daemon options:\n"
" '-f' to keep program in foreground.\n"
" '-l' use syslog, rather than stderr, for messages.\n"
" '-s' to run with elevated (sched_fifo) scheduling priority.\n"
" '-d' for debugging (repeate for more verbosity).\n"
" '-X' don't actually pass on received keystrokes (for debug).\n"
"(olpc-kbdshim version %d)\n"
, me, NGRABKEYS, SCROLL_QUANTUM, SCROLL_FILTER_PERCENT, VERSION);
exit(1);
}
static void
report(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (logtosyslog && debug <= 1) {
vsyslog(LOG_NOTICE, fmt, ap);
} else {
fprintf(stderr, "%s: ", me);
vfprintf(stderr, fmt, ap);
fputc('\n', stderr);
}
}
static void
dbg(int level, const char *fmt, ...)
{
va_list ap;
if (debug < level) return;
va_start(ap, fmt);
if (logtosyslog && debug <= 1) {
vsyslog(LOG_NOTICE, fmt, ap);
} else {
fputc(' ', stderr);
vfprintf(stderr, fmt, ap);
fputc('\n', stderr);
}
}
void
die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (logtosyslog && debug <= 1) {
vsyslog(LOG_ERR, fmt, ap);
syslog(LOG_ERR, "exiting -- %m");
} else {
fprintf(stderr, "%s: ", me);
vfprintf(stderr, fmt, ap);
fprintf(stderr, " - %s", strerror(errno));
fputc('\n', stderr);
}
exit(1);
}
/* Manage the uinput device */
void
deinit_uinput_device(void)
{
if (uinp_fd < 0) return;
/* Destroy the input device */
if (ioctl(uinp_fd, UI_DEV_DESTROY) < 0)
die("destroy of uinput dev failed\n");
/* Close the UINPUT device */
close(uinp_fd);
uinp_fd = -1;
}
int
setup_uinput(void)
{
struct uinput_user_dev uinp;
int e, i;
uinp_fd = open("/dev/input/uinput", O_WRONLY | O_NDELAY);
if (uinp_fd < 0) {
uinp_fd = open("/dev/uinput", O_WRONLY | O_NDELAY);
if (uinp_fd < 0) {
report(
"Unable to open either /dev/input/uinput or /dev/uinput\n");
return -1;
}
}
atexit(deinit_uinput_device);
memset(&uinp, 0, sizeof(uinp));
strncpy(uinp.name, "olpc-kbdshim virtual input", UINPUT_MAX_NAME_SIZE);
uinp.id.bustype = BUS_VIRTUAL;
uinp.id.vendor = 'p';
uinp.id.product = 'g';
uinp.id.version = 'f';
for (i = 0; i <= KEY_UNKNOWN; i++) {
if (ioctl(uinp_fd, UI_SET_KEYBIT, i) != 0) {
report("uinput setup failed, code %d\n", i);
return -1;
}
}
e = 0;
if (!++e || ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY) < 0 || // keys
!++e || ioctl(uinp_fd, UI_SET_EVBIT, EV_REP) < 0 ||
!++e || ioctl(uinp_fd, UI_SET_EVBIT, EV_REL) < 0 || // mouse
!++e || ioctl(uinp_fd, UI_SET_RELBIT, REL_X) < 0 ||
!++e || ioctl(uinp_fd, UI_SET_RELBIT, REL_Y) < 0 ||
!++e || ioctl(uinp_fd, UI_SET_RELBIT, REL_WHEEL) < 0 ||
!++e || ioctl(uinp_fd, UI_SET_RELBIT, REL_HWHEEL) < 0 ||
!++e || ioctl(uinp_fd, UI_SET_KEYBIT, BTN_LEFT) < 0 ||
!++e || ioctl(uinp_fd, UI_SET_KEYBIT, BTN_RIGHT) < 0 ||
!++e || ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MIDDLE) < 0 ||
!++e || write(uinp_fd, &uinp, sizeof(uinp)) < 0 || // device
!++e || ioctl(uinp_fd, UI_DEV_CREATE) < 0) {
report("uinput setup failed, step %d\n", e);
return -1;
}
/* disable timer-based autorepeat, see http://dev.laptop.org/ticket/9690 */
inject_uinput_event(EV_REP, REP_DELAY, 0);
inject_uinput_event(EV_REP, REP_PERIOD, 0);
return 0;
}
int
match_input_id(input_id_t *id, input_id_t *tmpl)
{
return id->bustype == tmpl->bustype &&
(id->vendor == tmpl->vendor || tmpl->vendor == 0xffff) &&
(id->product == tmpl->product || tmpl->product == 0xffff);
}
int
setup_input()
{
int i;
int dfd;
char devname[128];
unsigned char bit[bits2bytes(EV_MAX)];
struct input_id id;
for (i = 0; i < EVENTDEVICES; i++) {
snprintf(devname, sizeof(devname), "/dev/input/event%d", i);
if ((dfd = open(devname, O_RDONLY)) < 0)
continue;
if (ioctl(dfd, EVIOCGID, &id) < 0) {
report("failed ioctl EVIOCGID on %d", i);
close(dfd);
continue;
}
if (ioctl(dfd, EVIOCGBIT(0, EV_MAX), bit) < 0) {
report("failed ioctl EVIOCGBIT on %d", i);
close(dfd);
continue;
}
if (kbd_fd < 0 &&
test_bit(EV_KEY, bit) &&
test_bit(EV_REP, bit) &&
match_input_id(&id, &kbd_id)) {
kbd_fd = dfd;
if (ioctl(kbd_fd, EVIOCGRAB, 1) < 0)
die("ioctl EVIOCGRAB on keyboard");
report("found keyboard %s (%02x:%02x:%02x)", devname,
id.bustype, id.vendor, id.product);
continue;
}
if (tpad_fd < 0 &&
test_bit(EV_REL, bit) &&
match_input_id(&id, &tpad_id)) {
tpad_fd = dfd;
if (ioctl(tpad_fd, EVIOCGRAB, 1) < 0)
die("ioctl EVIOCGRAB on touchpad");
report("found touchpad %s (%02x:%02x:%02x)", devname,
id.bustype, id.vendor, id.product);
continue;
}
close(dfd);
}
if (kbd_fd == -1)
report("didn't find keyboard");
if (tpad_fd == -1)
report("didn't find touchpad");
return (kbd_fd >= 0 && tpad_fd >= 0);
}
void
inject_uinput_event(unsigned int type, unsigned int code, int value)
{
struct input_event event;
gettimeofday(&event.time, NULL);
event.type = type;
event.code = code;
event.value = value;
if (!noxmit && write(uinp_fd, &event, sizeof(event)) < 0) {
report("warning: inject event failed, t/c/v 0x%x/0x%x/0x%x\n",
type, code, value);
}
}
void
send_a_scroll(int x, int y)
{
if (reverse) {
x = -x;
y = -y;
}
if (x) {
dbg(1, "scroll %s", y > 0 ? "left" : "right");
inject_uinput_event(EV_REL, REL_HWHEEL, x < 0 ? 1 : -1);
}
if (y) {
dbg(1, "scroll %s", y > 0 ? "up" : "down");
inject_uinput_event(EV_REL, REL_WHEEL, y < 0 ? -1 : 1);
}
inject_uinput_event(EV_SYN, SYN_REPORT, 0);
}
unsigned char dpad[] = { KEY_KP8, KEY_KP6, KEY_KP2, KEY_KP4,
KEY_KP8, KEY_KP6, KEY_KP2, KEY_KP4};
int keyboard_event(void)
{
struct input_event ev[1];
int pass = 1;
int i;
static int altdown;
if (read(kbd_fd, ev, sizeof(ev)) != sizeof(ev))
die("bad read from keyboard");
dbg(3, "evtype %d code %d", ev->type, ev->code);
if (ev->type == EV_KEY) {
for (i = 0; i < ngrab; i++) {
if (ev->code == grabkey[i].code) {
/* keep track of how many grab keys are down */
if (ev->value) {
if (grabkey[i].pressed++ == 0)
scrolling++;
} else {
grabkey[i].pressed = 0;
if (--scrolling == 0)
cumul_x = cumul_y = 0;
}
pass = grabkey[i].pass;
dbg(1, "scrolling %d", scrolling);
break;
}
}
}
/* implement arrow key grab-scrolling */
if (scrolling && ev->value) {
switch(ev->code) {
case KEY_UP: send_a_scroll( 0,-1); pass = 0; break;
case KEY_DOWN: send_a_scroll( 0, 1); pass = 0; break;
case KEY_LEFT: send_a_scroll(-1, 0); pass = 0; break;
case KEY_RIGHT: send_a_scroll( 1, 0); pass = 0; break;
}
}
/* rotate the dpad keys on the screen bezel */
i = 0;
switch(ev->code) {
case KEY_KP4: i++;
case KEY_KP2: i++;
case KEY_KP6: i++;
case KEY_KP8:
ev->code = dpad[rotation + i];
break;
case KEY_SWITCHVIDEOMODE: // rotate button
dbg(3, "might rotate");
if (rotate_cmd && ev->value == 1) { /* ignore value 2 (repeats) */
// command given, so don't pass the keystroke through.
pass = 0;
// if absolute, we can check for presence cheaply
if (*rotate_cmd != '/' || access(rotate_cmd, X_OK) == 0) {
dbg(1, "invoking %s", rotate_cmd);
if (system(rotate_cmd) == 0)
;
}
}
break;
case KEY_F9: // brightness down key (or minimize, with alt)
case KEY_F10: // brightness up key (or maximize, with alt)
dbg(3, "F9 or F10, %d", use_F9_F12);
if (!use_F9_F12)
break;
goto brightness;
case KEY_BRIGHTNESSDOWN:
case KEY_BRIGHTNESSUP:
dbg(3, "BRTD or BRTU, %d", use_BRT_VOL);
if (!use_BRT_VOL)
break;
brightness:
dbg(3, "might brighten");
if (brightness_cmd && ev->value) {
char cmd[256];
char *arg;
int kreduce = (ev->code == KEY_F9 || ev->code == KEY_BRIGHTNESSDOWN);
// command given, so don't pass the keystroke through.
pass = 0;
// if absolute, we can check for presence cheaply
if (*brightness_cmd != '/' ||
access(brightness_cmd, X_OK) == 0) {
if (altdown) {
arg = kreduce ? "min" : "max";
} else {
arg = kreduce ? "down" : "up";
}
if (snprintf(cmd, sizeof(cmd), "%s %s",
brightness_cmd, arg) <= sizeof(cmd)) {
dbg(1, "invoking %s", cmd);
if (system(cmd) == 0)
;
}
}
}
break;
case KEY_F11: // volume down key (or minimize, with alt)
case KEY_F12: // volume up key (or maximize, with alt)
if (!use_F9_F12)
break;
goto volume;
case KEY_VOLUMEDOWN:
case KEY_VOLUMEUP:
if (!use_BRT_VOL)
break;
volume:
dbg(3, "might adjust volume");
if (volume_cmd && ev->value) {
char cmd[256];
char *arg;
int kreduce = (ev->code == KEY_F11 || ev->code == KEY_VOLUMEDOWN);
// command given, so don't pass the keystroke through.
pass = 0;
// if absolute, we can check for presence cheaply
if (*volume_cmd != '/' ||
access(volume_cmd, X_OK) == 0) {
if (altdown) {
arg = kreduce ? "min" : "max";
} else {
arg = kreduce ? "down" : "up";
}
if (snprintf(cmd, sizeof(cmd), "%s %s",
volume_cmd, arg) <= sizeof(cmd)) {
dbg(1, "invoking %s", cmd);
if (system(cmd) == 0)
;
}
}
}
break;
case KEY_LEFTALT:
case KEY_RIGHTALT:
if (ev->value == 0)
altdown--;
else if (ev->value == 1)
altdown++;
else
/* ignore repeats with ev->value == 2 */ ;
if (altdown > 2) altdown = 2; // safety
else if (altdown < 0) altdown = 0; // safety
break;
}
if (pass && !noxmit && write(uinp_fd, ev, sizeof(ev)) < 0) {
report("uinput write failed: %s\n", strerror(errno));
}
return !!(ev->value); /* indicates press */
}
void touchpad_event(void)
{
struct input_event ev[1];
if (read(tpad_fd, ev, sizeof(ev)) != sizeof(ev))
die("bad read from touchpad");
dbg(3, "evtype %d code %d", ev->type, ev->code);
if ((reflect_x && ev->code == REL_X) ||
(reflect_y && ev->code == REL_Y)) {
ev->value = -ev->value;
}
switch(rotation) {
case ROT_NORMAL:
break;
case ROT_RIGHT:
if (ev->code == REL_Y) {
ev->code = REL_X;
} else if (ev->code == REL_X) {
ev->code = REL_Y;
ev->value = -ev->value;
}
break;
case ROT_LEFT:
if (ev->code == REL_Y) {
ev->code = REL_X;
ev->value = -ev->value;
} else if (ev->code == REL_X) {
ev->code = REL_Y;
}
break;
case ROT_INVERT:
ev->value = -ev->value;
break;
}
if (scrolling && ev->type == EV_REL && ev->code == REL_X) {
cumul_x += ev->value;
} else if (scrolling && ev->type == EV_REL && ev->code == REL_Y) {
cumul_y += ev->value;
} else if (scrolling && ev->type == EV_SYN) {
/* emit our current scroll input */
int x = 0, y = 0;
if (abs(cumul_y) > quantum) {
if (abs(cumul_x) < ratio * abs(cumul_y) / 100)
cumul_x = 0;
y = cumul_y;
cumul_y = 0;
}
if (abs(cumul_x) > quantum) {
if (abs(cumul_y) < ratio * abs(cumul_x) / 100)
cumul_y = 0;
x = cumul_x;
cumul_x = 0;
}
send_a_scroll(x, y);
} else {
/* passthrough */
if (!noxmit && write(uinp_fd, ev, sizeof(ev)) < 0) {
report("uinput write failed: %s\n", strerror(errno));
}
}
}
void
send_event(char *e)
{
int fd, n;
char evtbuf[128];
fd = open(sysactive_path, O_RDWR|O_NONBLOCK);
if (fd >= 0) {
n = snprintf(evtbuf, 128, "%s %d\n", e, (int)time(0));
n = write(fd, evtbuf, n);
close(fd);
}
}
void
indicate_activity(int newactivity)
{
static char useridle[] = "useridleX";
if (newactivity) {
if (wasidle)
send_event("useractive");
idleness = 0;
wasidle = 0;
} else { /* an idle timer expired */
useridle[8] = idleness + '1';
if (idleness < MAXTIMERS) idleness++;
send_event(useridle);
wasidle = 1;
}
}
void
get_command(void)
{
int n;
unsigned int a, b, c;
char event[128];
n = read(fifo_fd, event, sizeof(event));
if (n < 1) {
if (n < 0)
die("read from rotation fifo");
close(fifo_fd);
fifo_fd = -1;
}
switch (event[0]) {
case 'x': reflect_x = 0; break;
case 'y': reflect_y = 0; break;
case 'z': reflect_x = 0;
reflect_y = 0; break;
case 'X': reflect_x = 1; break;
case 'Y': reflect_y = 1; break;
case 'Z': reflect_x = 1;
reflect_y = 1; break;
case 'n': rotation = ROT_NORMAL; break;
case 'r': rotation = ROT_RIGHT; break;
case 'i': rotation = ROT_INVERT; break;
case 'l': rotation = ROT_LEFT; break;
case 'I':
n = sscanf(event, "I %u %u %u", &a, &b, &c);
if (n < 1) {
a = b = c = 0;
} else {
if (n < 2 || b < a + 1)
b = a + 1;
if (n < 3 || c < b + 1)
c = b + 1;
}
{
static unsigned int old_a, old_b, old_c;
if (a != old_a || b != old_b || c != old_c) {
report("idle timers changed to %d %d %d", a, b, c);
} else {
dbg(1,"idle timers set to %d %d %d", a, b, c);
}
old_a = a; old_b = b; old_c = c;
}
idledeltas[3] = 0; /* always, so our last sleep is untimed */
idledeltas[2] = c - b;
idledeltas[1] = b - a;
idledeltas[0] = a;
idleness = 0;
wasidle = 1;
break;
case 'f':
case 'F':
/* F9/f9 to enable/disable capture of F9-F12,
* FBV/fbv to enable/disable capture of Fn-BRt, Fn-Vol keys
*/
if (!strncasecmp(event, "F9", 2)) {
use_F9_F12 = (event[0] == 'F');
} else if (!strncasecmp(event, "FBV", 3)) {
use_BRT_VOL = (event[0] == 'F');
}
dbg(1,"fkey, now: use_F9_F12 %d, use_BRT_VOL %d", use_F9_F12, use_BRT_VOL);
break;
default:
rotation = ROT_NORMAL;
reflect_x = 0; reflect_y = 0;
break;
}
dbg(1, "r/x/y is %d/%d/%d", rotation, reflect_x, reflect_y);
}
void
data_loop(void)
{
fd_set inputs, errors;
int maxfd, r;
struct timeval tv;
struct timeval *tvp;
int pressed;
indicate_activity(1);
maxfd = MAX(fifo_fd, MAX(kbd_fd, tpad_fd));
while (1) {
FD_ZERO(&inputs);
FD_SET(kbd_fd, &inputs);
FD_SET(tpad_fd, &inputs);
if (fifo_fd >= 0) FD_SET(fifo_fd, &inputs);
FD_ZERO(&errors);
FD_SET(kbd_fd, &errors);
FD_SET(tpad_fd, &errors);
if (fifo_fd >= 0) FD_SET(fifo_fd, &errors);
if (idledeltas[idleness]) {
tv.tv_sec = idledeltas[idleness];
tv.tv_usec = 0;
tvp = &tv;
} else {
tvp = 0;
}
r = select(maxfd+1, &inputs, NULL, &errors, tvp);
if (r < 0)
die("select failed");
if (r == 0) {
indicate_activity(0);
continue;
}
pressed = 0;
if (fifo_fd >= 0 && FD_ISSET(fifo_fd, &errors))
die("select reports error on rotation event fifo");
if (FD_ISSET(kbd_fd, &errors))
die("select reports error on keyboard");
if (FD_ISSET(tpad_fd, &errors))
die("select reports error on touchpad");
if (fifo_fd >= 0 && FD_ISSET(fifo_fd, &inputs))
get_command();
if (FD_ISSET(kbd_fd, &inputs))
pressed |= keyboard_event();
if (FD_ISSET(tpad_fd, &inputs)) {
touchpad_event();
pressed = 1;
}
if (pressed)
indicate_activity(1);
}
}
int
init_fifo(char *fifo_node)
{
int fd;
struct stat sbuf;
#define fifomode 0622 /* allow anyone to adjust the rotation */
if (mkfifo(fifo_node, fifomode)) {
if (errno != EEXIST) {
report("mkfifo of %s failed", fifo_node);
return -1;
}
/* the path exists. is it a fifo? */
if (stat(fifo_node, &sbuf) < 0) {
report("stat of %s failed", fifo_node);
return -1;
}
/* if not, remove and recreate */
if (!S_ISFIFO(sbuf.st_mode)) {
unlink(fifo_node);
if (mkfifo(fifo_node, fifomode)) {
report("recreate of %s failed", fifo_node);
return -1;
}
}
}
/* mkfifo was affected by umask */
if (chmod(fifo_node, fifomode)) {
report("chmod of %s to 0%o failed", fifo_node, fifomode);
return -1;
}
/* open for read/write, since writers are itinerant */
fd = open(fifo_node, O_RDWR);
if (fd < 0) {
report("open %s failed", fifo_node);
return -1;
}
return fd;
}
void
deinit_fifo(void)
{
close(fifo_fd);
fifo_fd = -1;
unlink(command_fifo);
}
void
sighandler(int sig)
{
deinit_uinput_device();
deinit_fifo();
die("got signal %d", sig);
}
int
main(int argc, char *argv[])
{
int foreground = 0;
int sched_realtime = 0;
unsigned short b, v, p;
char *cp, *eargp;
int c;
me = argv[0];
cp = strrchr(argv[0], '/');
if (cp) me = cp + 1;
while ((c = getopt(argc, argv, "flsdXvq:n:K:T:g:G:R:A:r:b:V:")) != -1) {
switch (c) {
/* daemon options */
case 'f':
foreground = 1;
break;
case 'l':
logtosyslog = 1;
break;
case 's':
sched_realtime = 1;
break;
case 'd':
debug++;
break;
case 'X':
noxmit = 1;
break;
/* algorithmic tuning */
case 'v':
reverse = 1;
break;
case 'q':
quantum = strtol(optarg, &eargp, 10);
if (*eargp != '\0' || quantum <= 0)
usage();
break;
case 'n':
ratio = strtol(optarg, &eargp, 10);
if (*eargp != '\0' || ratio <= 0)
usage();
break;
case 'K':
if (sscanf(optarg, "%hx:%hx:%hx", &b, &v, &p) != 3)
usage();
kbd_id.bustype = b;
kbd_id.vendor = v;
kbd_id.product = p;
break;
case 'T':
if (sscanf(optarg, "%hx:%hx:%hx", &b, &v, &p) != 3)
usage();
tpad_id.bustype = b;
tpad_id.vendor = v;
tpad_id.product = p;
break;
case 'g':
case 'G':
if (ngrab >= NGRABKEYS) {
fprintf(stderr, "%s: too many grab keys specified, %d max\n",
me, NGRABKEYS);
exit(1);
}
grabkey[ngrab].code = strtol(optarg, &eargp, 10);
if (*eargp != '\0')
usage();
grabkey[ngrab].pass = (c == 'g');
ngrab++;
break;
case 'R':
command_fifo = optarg;
break;
case 'A':
sysactive_path = optarg;
break;
case 'r':
rotate_cmd = optarg;
break;
case 'b':
brightness_cmd = optarg;
break;
case 'V':
volume_cmd = optarg;
break;
default:
usage();
break;
}
}
if (optind < argc) {
report("found non-option argument(s)");
usage();
}
/* default to XO grab keys, with passthrough. since they're
* just modifiers, nothing will happen with them unless X
* wants it to.
*/
if (ngrab == 0) {
grabkey[0].code = KEY_LEFTMETA;
grabkey[0].pass = 1;
grabkey[1].code = KEY_RIGHTMETA;
grabkey[1].pass = 1;
ngrab = 2;
}
report("starting %s version %d", me, VERSION);
if (setup_input() < 0)
die("%s: unable to find input devices\n", me);
/* initialize uinput, if needed */
if (!noxmit && setup_uinput() < 0)
die("%s: unable to find uinput device\n", me);
if (command_fifo) {
fifo_fd = init_fifo(command_fifo);
if (fifo_fd < 0)
report("no fifo for rotation control");
}
signal(SIGTERM, sighandler);
signal(SIGHUP, sighandler);
signal(SIGINT, sighandler);
signal(SIGQUIT, sighandler);
signal(SIGABRT, sighandler);
signal(SIGUSR1, sighandler);
signal(SIGUSR2, sighandler);
if (sched_realtime) {
struct sched_param sparam;
int min, max;
/* first, lock down all our memory */
long takespace[1024];
memset(takespace, 0, sizeof(takespace)); /* force paging */
if (mlockall(MCL_CURRENT|MCL_FUTURE) < 0)
die("unable to mlockall");
/* then, raise our scheduling priority */
min = sched_get_priority_min(SCHED_FIFO);
max = sched_get_priority_max(SCHED_FIFO);
sparam.sched_priority = (min + max)/2; /* probably always 50 */
if (sched_setscheduler(0, SCHED_FIFO, &sparam))
die("unable to set SCHED_FIFO");
report("memory locked, scheduler priority set");
}
if (!foreground) {
if (daemon(0, 0) < 0)
die("failed to daemonize");
daemonized = 1;
}
data_loop();
return 0;
}
|