-
Notifications
You must be signed in to change notification settings - Fork 5
/
airboard-ir.c
1433 lines (1241 loc) · 43.1 KB
/
airboard-ir.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
/*
*
*
* airboard.c, based on avrlirc2udp.c
*
* the latest copy of this program is probably available here:
* https://github.com/foxharp/avrlirc
*
* this daemon accepts infrared (IR) data from an Airboard
* keyboard, also known as an SK-7100, made by Silitek, or
* LiteOn, and rebadged by Motorola and gateway, perhaps among
* others.
*
* as sold, the airboard has its own IR receiver. the receiver
* works well enough, except that a) the keyboard is PS/2-only (a
* "dumb" PS/2-to-USB adapter won't convert it), and worse, b),
* the mouse is serial-only -- a serial to PS/2 adapter won't
* help. with the disappearance of serial ports, and even of PS/2
* ports, this otherwise excellent keyboard is becoming obsolete.
* having recently purchased a USB-only computer, and having USB
* IR receivers on hand (avrlirc), it made sense to write a
* "driver" for this keyboard.
*
* data comes from an avrlirc device (specified with '-t').
* perhaps it could also be read from other lirc compatible input
* devices, but no others have been tried. (for more information
* on avrlirc, see <http://www.foxharp.boston.ma.us/avrlirc>.)
*
* output from this program goes to three separate destinations:
*
* - if '-a' is given, keyboard and mouse events will be
* injected into the kernel using the uinput driver, where
* they will automatically become available to the rest of the
* system.
*
* - if '-s' is also used, it specifies a destination for the
* "special" multimedia buttons; their names will be sent to
* the given host/port pair. (otherwise they will be
* "injected", along with the rest of the keys.)
*
* - consumer IR codes will be sent to the lircd host and
* port, specified with '-h' and '-p'.
*
* regarding the path of the consumer IR codes: all of the
* functionality of avrlirc2udp remains. namely, this will
* transfer characters from an avrlirc device on a serial port to
* an lircd daemon running the udp "driver". the output of the
* arvlirc device matches the expected lircd input pretty much
* exactly. we try to read pairs of bytes using the VMIN
* parameter of the tty driver, but other than that, it's mainly
* a data copy.
*
* with '-g', if the blue Fn key is held, the joystick sends scrolling
* events rather than motion events.
*
* invocation: as a critical system service, this program should be
* invoked from inittab, or from /etc/event.d on systems running
* upstart (ubuntu, possibly modern fedora). a sample invocation line
* might be:
* /usr/local/bin/airboard -f -r -t /dev/ttyUSB0 -w 10 -a -g -h lircdhost
*
* because the keyboard and mouse on a system are a somewhat critical
* service, provision is made for assigning elevated scheduling
* priority to this program (-r).
*
*
**********
*
* Copyright (C) 2009, Paul G Fox
*
* 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.
*
* A copy of the GNU General Public License is appended at the bottom
* of this file.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <termios.h>
#include <signal.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sched.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <sys/stat.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <linux/input.h>
char *me;
void
usage(void)
{
fprintf(stderr,
"usage: %s [options] -t <ttydev>\n"
" tty options:\n"
" '-H' for high speed tty (115200 instead of 38400).\n"
" '-w <S>' to poll the creation of ttydev at S second intervals.\n"
" airboard options:\n"
" '-a' to include support for the airboard keyboard.\n"
" '-s <host>:<port>' to divert airboard multimedia keys to\n"
" the specified UDP socket.\n"
" '-m NN,NN,NN,NN' to specify mouse acceleration parameters.\n"
" All 4 levels must be specified. Default is 1,5,10,25.\n"
" '-g' to enable grab scrolling (using blue 'Fn' key)\n"
" lircd options:\n"
" '-h <lircd_host>' to specify the lircd host for CIR decoding\n"
" '-p <lircd_port>] (defaults to 8765).\n"
" '-T' make a TCP connection for CIR rather than UDP (uncommon).\n"
" daemon options:\n"
" '-f' to keep program in foreground.\n"
" '-l' use syslog, rather than stderr, for messages.\n"
" '-r' to run with elevated (sched_fifo) scheduling priority.\n"
" '-d' for debugging (repeate for more verbosity).\n"
" '-X' don't forward any IR commands or keystrokes (for debug).\n"
" (%s requires root privileges if -a or -r is used.)\n"
" This is airboard-ir version %s\n"
, me, me, VERSION);
exit(1);
}
/* 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;
/* handle input from the airboark sk-7100 silitek (and
* motorola and gateway rebadged) keyboard.
*/
int airboard;
/* send "special" keys from the airboard keyboard
* (i.e. the multimedia keys) to this socket instead
* to uinput (which is where the regular keys go).
*/
char *spec_host;
int spec_port;
/* mouse acceleration values. should really be done by the
* window system
*/
int mouse_speeds[] = {0, 1, 5, 10, 25};
/* special key (currently the blue "Fn" key) enables grab scrolling */
int do_grabscroll;
int scrolling;
#define SCROLL_QUANTUM 15 /* "distance" before we emulate a scroll button */
int cumul_x_scroll, cumul_y_scroll;
/* default port for reports to lircd */
#define LIRCD_UDP_PORT 8765
/* see the "keyboard protocol" and "mouse protocol" comment blocks
* for descriptions of the actual binary reports.
*/
#define KEY_WORD_LEN 19
#define MOUSE_WORD_LEN 30
#define IR_UP_MASK 0x801
#define IR_REPEAT 0x656d5
#define IR_MOUSE_PREFIX 0x7e6
#define IR_MOUSE_PREFIX_LEN 11
typedef struct key_desc {
long ir_code; /* IR code the airboard sends (9 bits) */
char *name; /* for debug, and actually sent for special keys */
int event_code; /* the event code for the uinput subsystem */
char type; /* regular/mouse/special */
} key_desc_t;
#define NUM_KEYS 128
#define TYPE_MOUSE 1
#define TYPE_SPECIAL 2
#define TYPE_REPEAT 3
#define TYPE_ALL_UP 4
#define TYPE_GRAB 5
extern key_desc_t keys[NUM_KEYS]; /* declared below */
extern char *optarg;
extern int optind, opterr, optopt;
static int uinp_fd = -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);
}
}
static void
dbgchar(int level, int c)
{
if (debug < level) return;
if (logtosyslog && debug <= 1) {
syslog(LOG_NOTICE, "%c", c); // shouldn't happen
} else {
fputc(c, 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;
key_desc_t *keyp;
int e;
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)); // Intialize the uInput device to NULL
strncpy(uinp.name, "airboard IR driver", UINPUT_MAX_NAME_SIZE);
uinp.id.version = 4;
uinp.id.bustype = BUS_VIRTUAL;
for (keyp = keys; keyp < &keys[sizeof(keys)/sizeof(keys[0])]; keyp++) {
if (!keyp->event_code)
continue;
if (ioctl(uinp_fd, UI_SET_KEYBIT, keyp->event_code) < 0) {
report("uinput setup failed, key '%s'\n", keyp->name);
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 || 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;
}
return 0;
}
void
input_event(unsigned int type, unsigned int code, int value)
{
struct input_event event;
memset(&event, 0, sizeof(event));
gettimeofday(&event.time, NULL);
event.type = type;
event.code = code;
event.value = value;
if (write(uinp_fd, &event, sizeof(event)) < 0) {
report("warning: input event failed, t/c/v 0x%x/0x%x/0x%x\n",
type, code, value);
}
}
void
send_a_motion(int x, int y)
{
input_event(EV_REL, REL_X, x);
input_event(EV_REL, REL_Y, y);
input_event(EV_SYN, SYN_REPORT, 0);
}
void
send_a_scroll(int x, int y)
{
if (x) {
dbg(1, "scroll %s", y > 0 ? "left" : "right");
input_event(EV_REL, REL_HWHEEL, x > 0 ? -1 : 1);
}
if (y) {
dbg(1, "scroll %s", y > 0 ? "up" : "down");
input_event(EV_REL, REL_WHEEL, y > 0 ? -1 : 1);
}
input_event(EV_SYN, SYN_REPORT, 0);
}
void
send_a_key(int key_event_code, int down)
{
input_event(EV_KEY, key_event_code, !!down);
input_event(EV_SYN, SYN_REPORT, 0);
}
int socket_init(int tcp, char *host, int port)
{
struct sockaddr_in sa[1];
struct hostent *hent;
int s;
hent = gethostbyname(host);
if (!hent) {
report("gethostbyname failed for %s", host);
return -1;
}
if (tcp) {
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
die("tcp socket open");
} else {
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
die("udp socket open");
}
memset((char *) sa, 0, sizeof(*sa));
sa->sin_family = AF_INET;
sa->sin_port = htons(port);
sa->sin_addr = *((struct in_addr *)hent->h_addr);
if (connect(s, (struct sockaddr *)sa, sizeof(*sa)) < 0) {
report("connect failed to %s:%d", host, port);
close(s);
return -1;
}
return s;
}
/* shared by tty_init()/tty_restore() */
struct termios prev_tios;
int tty_fd;
void
tty_restore(void)
{
if (tty_fd >= 0)
tcsetattr(tty_fd, TCSADRAIN, &prev_tios);
}
void
sighandler(int sig)
{
tty_restore();
deinit_uinput_device();
die("got signal %d", sig);
}
int tty_init(char *term, int wait_term, int speed)
{
int s;
struct termios tios;
int flags;
long fflags;
int logged = 0;
while(1) {
tty_fd = open(term, O_RDWR|O_NDELAY);
if (tty_fd >= 0 || errno != ENOENT || !wait_term)
break;
if (!logged)
report("waiting for tty creation");
logged = 1;
sleep(wait_term);
}
if (logged)
report("found tty");
if (tty_fd < 0)
die("can't open tty '%s'", term);
if (!isatty(tty_fd))
die("%s is not a tty", term);
fflags = fcntl(tty_fd, F_GETFL );
fcntl (tty_fd, F_SETFL, fflags & ~O_NDELAY );
s = tcgetattr(tty_fd, &prev_tios);
if (s < 0)
die("tcgetattr on %s", term);
/* set up restore hooks quickly */
atexit(tty_restore);
tios = prev_tios;
tios.c_oflag = 0; /* no output flags at all */
tios.c_lflag = 0; /* no line flags at all */
tios.c_cflag &= ~PARENB; /* disable parity, both in and out */
tios.c_cflag |= CSTOPB|CLOCAL|CS8|CREAD; /* two stop bits on transmit */
/* no modem control, 8bit chars, */
/* receiver enabled, */
tios.c_iflag = IGNBRK | IGNPAR; /* ignore break, ignore parity errors */
tios.c_cc[VMIN] = 2;
tios.c_cc[VTIME] = 0;
tios.c_cc[VSUSP] = _POSIX_VDISABLE;
tios.c_cc[VSTART] = _POSIX_VDISABLE;
tios.c_cc[VSTOP] = _POSIX_VDISABLE;
s = cfsetspeed (&tios, speed);
if (s < 0)
die("cfsetspeed on %s", term);
s = tcsetattr(tty_fd, TCSAFLUSH, &tios);
if (s < 0)
die("tcsetattr on %s", term);
/* make sure RTS and DTR lines are high, since device may be
* phantom-powered. no termios/posix way to do this, that i
* know of.
*/
if (ioctl(tty_fd, TIOCMGET, &flags) >= 0) {
flags &= ~TIOCM_RTS;
flags &= ~TIOCM_DTR;
ioctl(tty_fd, TIOCMSET, &flags);
}
return tty_fd;
}
int
timed_read(int from, unsigned char *buf, int n, int block)
{
if (block) {
return read(from, buf, n);
} else {
int ret;
fd_set readfd;
struct timeval to;
FD_ZERO(&readfd);
FD_SET(from, &readfd);
to.tv_sec = 0;
to.tv_usec = 20 * 1000; // 20ms
ret = select(from+1, &readfd, 0, &readfd, &to);
if (ret == 0)
return -2; // timeout
return read(from, buf, n);
}
}
void send_hotkey(char *name)
{
static int s = -1;
static char keyname[25];
if (s < 0)
s = socket_init(0, spec_host, spec_port);
if (s < 0) {
report("failed to init hotkey socket");
return;
}
strncpy(keyname, name, sizeof(keyname)-2);
keyname[sizeof(keyname)-1] = '\0';
strcat(keyname, "\n");
if (write(s, keyname, strlen(keyname)) < 0) {
close(s);
s = -1;
}
}
void set_scrolling(void)
{
dbg(1, "scrolling");
scrolling = 1;
}
void reset_scrolling(void)
{
dbg(1, "NOT scrolling");
scrolling = cumul_x_scroll = cumul_y_scroll = 0;
}
/*
* keyboard protocol:
*
* keypress reports are 19 bits long. they are preceded by a 0
* start bit, and are (usually) followed by a trailing stop bit.
* (the stop bit will be missing if two keycodes arrive in
* direct succession, such as a "key-up" report immediately
* followed by "all-up" they are essentially fully identified
* after 7 bits -- the rest are redundancy, and press/release
* bits.
*
* 18 0
* III|iiii|d110|IIIe|eeeu
*
* - bits 12-18 (the top 7) are the keycode ("IIIiiii").
* - bit 11 ('d') is 1 for "press", 0 for "release".
* - bits 8-10 are a constant pattern, 110.
* - bits 5-7 are a repeat of the upper 3 bits of the keycode ("III").
* - bits 1-4 ('eeee') are the value "eeee = (15 - iiii)".
* - bit 0 ('u') is 0 for press, 1 for release.
*
* however, since we don't get a bit at a time (we get a variable
* number at a time) it's easiest to just put all 19 bits of the
* keycode into the lookup table. (besides, that's how the code
* was initially written, before the keycode format was fully decoded.)
*
*/
key_desc_t *lookup_key(long ir_code)
{
#if DO_FULL_SANITY_CHECK
/* not needed, since all the bits are in the table */
int I1, I2, i, e, d, u;
if ((ir_code >> 12) > 0x7f) {
dbg(3, "keycode out of range");
return 0;
}
if ((ir_code & 0x700) != 0x600) {
dbg(3, "keycode bad bits");
return 0;
}
I1 = (ir_code >> 16) & 0x7;
I2 = (ir_code >> 5) & 0x7;
if (I1 != I2) {
dbg(3, "keycode III mismatch");
return 0;
}
i = (ir_code >> 12) & 0xf;
e = (ir_code >> 1) & 0xf;
if (e != 15 - i) {
dbg(3, "keycode iiii/eeee mismatch");
return 0;
}
d = (ir_code >> 11) & 1;
u = ir_code & 1;
if ((u ^ d) != 1) {
dbg(3, "keycode u/d mismatch");
return 0;
}
return &keys[ir_code >> 12];
#else
key_desc_t *keyp = &keys[ir_code >> 12];
if ((keyp->ir_code == ir_code) ||
(keyp->ir_code == (ir_code ^ IR_UP_MASK ))) {
return keyp;
}
return 0;
#endif
}
void emitkey(long ir_code)
{
#define N_KEYS_PRESSED 21 // all fingers + all toes + 1
static key_desc_t *key_pressed[N_KEYS_PRESSED];
key_desc_t *keyp;
int i;
dbg(1, " 0x%05lx", ir_code);
keyp = lookup_key(ir_code);
if (!keyp) return;
if (keyp->type == TYPE_ALL_UP) {
dbg(1, "got all-up");
if (0) dbg(1, "skipping");
/* cancel all outstanding keypresses, for safety */
for (i = 0; i < N_KEYS_PRESSED; i++) {
if (key_pressed[i]) {
dbg(1, "forcing %d / %s up", i, key_pressed[i]->name);
if (!noxmit)
send_a_key(key_pressed[i]->event_code, 0);
key_pressed[i] = 0;
}
}
reset_scrolling();
} else if (keyp->type == TYPE_REPEAT) {
/* ignore the repeat code */ ;
} else if (keyp->ir_code == ir_code) {
dbg(1, "%s pressed", keyp->name);
/* check for already pressed */
for (i = 0; i < N_KEYS_PRESSED; i++) {
if (key_pressed[i] == keyp) {
dbg(1, "ignoring %s already pressed at %d", keyp->name, i);
return;
}
}
/* record what's pressed */
for (i = 0; i < N_KEYS_PRESSED; i++) {
if (!key_pressed[i]) {
if (do_grabscroll && keyp->type == TYPE_GRAB) {
set_scrolling();
} else if (!noxmit) {
if (spec_host && keyp->type == TYPE_SPECIAL)
send_hotkey(keyp->name);
else
send_a_key(keyp->event_code, 1);
}
dbg(1, "marking %s pressed at %d", keyp->name, i);
key_pressed[i] = keyp;
break;
}
}
} else if (keyp->ir_code == (ir_code ^ IR_UP_MASK)) {
int mark_released = 0;
dbg(1, "%s released", keyp->name);
/* keep track of what's been released */
for (i = 0; i < N_KEYS_PRESSED; i++) {
if (key_pressed[i] == keyp) {
if (do_grabscroll && keyp->type == TYPE_GRAB) {
reset_scrolling();
} else if (!noxmit) {
if (spec_host && keyp->type == TYPE_SPECIAL)
;
else
send_a_key(keyp->event_code, 0);
}
dbg(1, "marking %s released at %d", keyp->name, i);
mark_released = 1;
key_pressed[i] = 0;
break;
}
}
if (!mark_released) {
dbg(1, "no matching press for %s release, ignoring", keyp->name);
}
} else {
dbg(1, "%s: key lookup error: 0x%lx", me, ir_code);
}
}
/*
* mouse protocol:
*
* mouse motion reports are 30 bits long. they are preceded by a
* 0 start bit, and are (sometimes) followed by a trailing stop
* bit. the stop bit may not always be present. each report
* contains both x and y motion information. mouse buttons are
* not in the mouse motion reports -- they're reported as
* keypresses.
*
* 30 0
* 11|1111|0011|0xxx|xxXX|X110|yyyy|yYYY
*
* - bits 19-30 are the constant prefix 0x7e6.
* - bits 14-18 are the X motion value.
* - bits 11-13 are the X direction. ("111" == left, "000" == right)
* - bits 8-10 are the constant "110".
* - bits 3-7 are the Y motion value.
* - bits 0-2 are the Y direction. ("111" == up, "000" == down)
*
* both the X and Y motion values ("xxxxx" and "yyyyy") work the same.
* the least significant bits are to the left, the most significant
* are to the right (i.e., backwards). when traveling in a negative
* direction (up or to the left), the motion value should be inverted.
* 0 4
* mmmmm
*
* the "mmmmm" values reported are not very accurate. the
* observed speed levels are:
* speed 4: bit 4 set -- fastest
* speed 3: bit 3, plus any of 0-3
* speed 2: bit 3
* speed 1: any of bits 0-2 (they're erratic, and don't count nicely)
* no motion: all bits zero
*
*/
int
motion_fixup(int raw, int *lastspeedp)
{
int mdir, n, goal, last;
mdir = ((raw & 7) == 7) ? -1 : 1;
raw >>= 3;
if (mdir < 0) raw = ~raw & 0x1f;
n = 0;
if (raw & 0x01) {
n = 4;
} else if (raw & 0x02) {
n = 2;
if (raw & 0x1c)
n = 3;
} else if ((raw & 0x1c) || mdir < 0) {
n = 1;
}
/* map the reported levels to better values */
goal = mouse_speeds[n];
/* transition between speeds gently -- only change speed by 1
* for each mouse packet received.
*/
last = *lastspeedp;
dbg(2, "r 0x%x, n %d, g %d, last %d", raw, n, goal, last);
if (goal == 0) last = 0;
else if (last > goal * 2) last -= 2;
else if (last > goal) last -= 1;
else if (last < goal * 2) last += 2;
else if (last < goal) last += 1;
*lastspeedp = last;
return last * mdir;
}
void emitmouse(long mousecode, int *last_y_motion)
{
int x, y;
int nx, ny;
static int lastxspeed, lastyspeed;
if ((mousecode & 0x700) != 0x600)
return;
x = (mousecode >> 11) & 0xff;
y = mousecode & 0xff;
dbgchar(2, '\n');
nx = motion_fixup(x, &lastxspeed);
ny = motion_fixup(y, &lastyspeed);
dbg(1, " mouse x,y 0x%02x,0x%02x %2d,%2d", x,y, nx,ny);
if (scrolling) {
dbg(2, "would scroll");
cumul_x_scroll += nx;
if (abs(cumul_x_scroll) > SCROLL_QUANTUM) {
send_a_scroll(cumul_x_scroll, 0);
cumul_x_scroll = 0;
}
cumul_y_scroll += ny;
if (abs(cumul_y_scroll) > SCROLL_QUANTUM) {
send_a_scroll(0, cumul_y_scroll);
cumul_y_scroll = 0;
}
} else if (!noxmit) {
send_a_motion(nx, ny);
}
*last_y_motion = ny;
}
void
data_loop(int from, int tcp, char *lircdhost, int lircdport)
{
unsigned char b[2];
int n;
int prevhilo = -1;
int pulse;
long time = 0;
int bits = 0, hilo = 0, totbits = 0;
long bitaccum = 0;
int in_gap = 1;
int wordlen = KEY_WORD_LEN;
int block = 1;
int last_y_motion;
int phase_err_count = 0;
static int to = -1;
setbuf(stdout, NULL); // for timely debug messages
while (1) {
/* leave extra space in buf for extra bytes we may read below */
if ((n = timed_read(from, b, 2, block)) == -1)
die("timed_read");
/* in my experience, this results from a USB serial
* device being unplugged */
if (n == 0)
return;
if (n == 1) { /* short read -- force it even */
if (timed_read(from, &b[1], 1, 1) == -1)
die("timed_read");
n++;
}
block = 1;
if (n > 0) {
pulse = (b[1] << 8) + b[0];
hilo = pulse & 0x8000;
time = pulse & 0x7fff;
dbg(4, "%s: %s 0x%04x (%ld) (%ldus)\n", me,
hilo ? "pulse" : "space",
pulse, time, 1000000 * time / 16384);
if (hilo == prevhilo) {
/*
* if we somehow start our reads "halfway" through one
* of the 16 bit data words, we'll forever be
* out-of-sync. we try to correct this by noticing
* two things: a) if the data contains two zero bytes
* in succession, then they're definitely a pair
* (since this is the out-of-band data prefix), and b)
* the 16 bit values that we read should have
* alternating high bits: 0x8000, 0x0000, 0x8000,
* etc.
*/
if (phase_err_count++ > 10) {
report("too many phase corrections, re-opening tty");
return;
}
report("phase correction");
b[0] = b[1];
if (timed_read(from, &b[1], 1, 1) <= 0)
die("timed_read");
hilo = (b[1] & 0x80);
}
prevhilo = hilo;
}
/* send lircd data to lircdhost */
if (!noxmit && lircdhost) {
if (to < 0)
to = socket_init(tcp, lircdhost, lircdport);
if (to >= 0) {
if (write(to, b, 2) < 0) {
if (errno != ECONNREFUSED)
die("write");
close(to);
to = -1;
}
}
}
if (airboard) {
if (n > 0) {
#define BITTIME 13653 /* 1200 baud: 1/1200 * 16384 * 1000 */
// report("h:%d t:%d ", hilo, time);
bits = ((1000 * time) + BITTIME/2) / BITTIME;
if (in_gap && hilo) { // skip marking during gap
// while (bits--)
dbgchar(2, 'S');
continue;
} else if (in_gap && !hilo) {
if (bits > 0) bits--; // skip the start bit
dbgchar(2,'\t');
dbgchar(2,'s');
totbits = 0;
}
in_gap = 0;
} else {
// timeout -- fill in with 1's
bits = wordlen - totbits;
hilo = 1;
in_gap = 1;
dbgchar(2,'f');
}
if (hilo && bits > 12) {
if (totbits > 2) {
bits = wordlen - totbits;
//report(".0x%05llx", bitaccum);
} else if (totbits) {
bitaccum = 0;
totbits = 0;
continue;
} else {
continue;
}
}
while (bits--) {
totbits++;