-
Notifications
You must be signed in to change notification settings - Fork 11
/
egi_input.c
1737 lines (1453 loc) · 56.6 KB
/
egi_input.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
( Input Handlers: cat /proc/bus/input/devices )
Example:
I: Bus=0003 Vendor=062a Product=4c01 Version=0110
N: Name="xxx 2.4G INPUT DEVICE"
P: Phys=usb-0000:00:14.0-1/input0
S: Sysfs=/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/0003:062A:4C01.0003/input/input14
U: Uniq=
H: Handlers=sysrq kbd event13 leds
B: PROP=0
B: EV=120233
B: KEY=4000000000 0 1000700000000 2102400 0 0
B: MSC=10
B: LED=4
...
(linux/input.h)
more details: https://www.kernel.org/doc/html/latest/input/input-programming.html
--- Event types ---
#define EV_SYN 0x00
#define EV_KEY 0x01
#define EV_REL 0x02 -- Relative value
#define EV_ABS 0x03 -- Absolute value
#define EV_MSC 0x04
#define EV_SW 0x05
#define EV_LED 0x11
#define EV_SND 0x12
#define EV_REP 0x14
#define EV_FF 0x15
#define EV_PWR 0x16
#define EV_FF_STATUS 0x17
#define EV_MAX 0x1f
#define EV_CNT (EV_MAX+1)
--- Relative Axes ---
#define REL_X 0x00
#define REL_Y 0x01
#define REL_Z 0x02
#define REL_RX 0x03
#define REL_RY 0x04
#define REL_RZ 0x05
#define REL_HWHEEL 0x06
#define REL_DIAL 0x07
#define REL_WHEEL 0x08
#define REL_MISC 0x09
#define REL_MAX 0x0f
#define REL_CNT (REL_MAX+1)
--- Button Value ---
...
#define BTN_MOUSE 0x110
#define BTN_LEFT 0x110
#define BTN_RIGHT 0x111
#define BTN_MIDDLE 0x112
#define BTN_SIDE 0x113
#define BTN_EXTRA 0x114
#define BTN_FORWARD 0x115
#define BTN_BACK 0x116
#define BTN_TASK 0x117
(With reference to: https://wiki.osdev.org/PS/2_Mouse )
--- 1. PS2 DATA ---
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
--------------------------------------------------------------------------------
Byte 1: Y_overflow X_overflow Y_sign X_sign 1 Mid_Btn Right_Btn Left_Btn
Byte 2: X_Movement
Byte 3: Y_Movement
--- 2. Intellimouse #1 ---
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
--------------------------------------------------------------------------------
Byte 1: Y_overflow X_overflow Y_sign X_sign 1 Mid_Btn Right_Btn Left_Btn
Byte 2: X_Movement
Byte 3: Y_Movement
Byte 4: Z_Movement (wheel) (in 2's complement. valid value are -8 to +7)
SETUP MAGIC: { 0xf3, 200, 0xf3, 100, 0xf3, 80 }
--- 3. Intellimouse #2 ---
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
--------------------------------------------------------------------------------
Byte 1: Y_overflow X_overflow Y_sign X_sign 1 Mid_Btn Right_Btn Left_Btn
Byte 2: X_Movement
Byte 3: Y_Movement
Byte 4: 0 0 5th_Btn 4th_Btn Z_Movement(Bit0-3)
SETUP MAGIC: { 0xf3, 200, 0xf3, 200, 0xf3, 80 }
Journal
2021-03-14:
1. egi_mouse_loopread(): When select() time out, instead of return and wait,
go on to calculate mostatus with sustained data of mouse_data[0] &= 0x0F.
So that if the mouse keeps idle after a click (in such case, no data from the
input event after the click), the loopread can still generate mostatus! For example,
to deduce a LeftKeyDownHold status after the click.
TODO: it continue generating mostatu at idle time.....
2021-03-18:
1. egi_mouse_loopread(): Adjust mostatus.DX/DY as per mostatus.mouseX/mouseY variation,
with consideration of mouse movement limit at four sides.
2021-03-26:
1. egi_mouse_loopread(): Add var. status_sustain to check if idle DOWNHOLD happens when select() returns 0.
2021-04-03:
1. egi_mouse_loopread():
1.A. Reset mostatus.RightKeyDownHold=false if status=0b00.
1.B. Clear mouse_data after set mouse type to Intellimouse.
1.C. Case 2 0b01: mostatus.RightKeyDownHold=true --modified to--> mostatus.RightKeyDownHold=false
1.D. Case 2 0b10: mostatus.LeftKeyDownHold=false --modified to--> mostatus.RightKeyDownHold=false;
2021-05-10:
XXX 1. egi_mouse_loopread(): Cancel while(checkRequest) {} at end of loop.
TODO: If mouse is disconnected, the mouse_callback() will be hung up.
2021-06-11:
1. Add EGI_KBD_STATUS
2. Add egi_read_kbdcode().
2021-06-12:
1. Add Control_key status in EGI_KBD_STATUS and revise egi_read_kbdcode().
2021-06-15:
1. Add EGI_CONKEYS_STAUTS, and revise egi_read_kbdcode().
2021-06-17:
1. egi_read_kbdcode(): Sort kstat->conkeys.conkeyseq[] according to input timeval.
2021-06-18:
1. egi_read_kbdcode():
1.1 Close(fd) will also flush buffer of event, so male fd[] static.
1.2 Check integerity of fd[] before ioctl and read.
2021-06-21:
1. Add member 'asciikey' to EGI_CONKEY_STATUS, as the only ASCII type conkey!
and modify egi_read_kbdcode() accordingly.
2021-06-22:
1. egi_mouse_loopread(): DO NOT STOP loopreading even if the mouse is disconnected,
replace while(mouse_fd<0){} with if(mouse_fd<0){}.
2. egi_read_kbdcode(): Call egi_valid_fd() to check/validate set_fds in each round.
2021-06-23:
1. egi_read_kbdcode():
1.1 Add concept and definition.
1.2 Clear KBD STATUS when it detects an input device becomes disconnected/invalid! <-----
2021-06-24:
1.egi_read_kbdcode(): Add ascii_conkey to conkeseq[].
2021-07-03:
1. Add member 'abskey' and 'absvalue' for EGI_CONKEY_STATUS.
Modify egi_read_kbdcode() accordingly.
2021-07-07:
1. (Itme 11.) If abskey value keeps as IDLE(0x7F) aft. readkbd, reset baskey=ABS_MAX to make it invalid!.
2. (Item 12.) If press_lastkey keeps FALSE, clear laskey code!
2022-03-10:
1. egi_mouse_loopread(): TEST touchscreen data (handled to /dev/input/mice) maps to mouse movement DX/DY.
Midas Zhou
[email protected](Not in use since 2022_03_01)
--------------------------------------------------------------------------------------*/
#include "egi_debug.h"
#include "egi_input.h"
#include "egi_timer.h"
#include "egi_fbdev.h"
#include "egi_utils.h"
#include <stdio.h>
#include <string.h>
#include <termio.h>
#include <fcntl.h> /* open */
#include <unistd.h> /* read */
#include <errno.h>
#include <stdbool.h>
#include <linux/input.h>
/* 1. Inpute device: for generic input events */
static int input_fd=-1;
static struct input_event inevent;
static EGI_INEVENT_CALLBACK inevent_callback; /* To be assigned */
/* ??? mutex lock for inevent_callback */
static bool tok_loopread_event_running; /* token for loopread is running if true */
static bool cmd_end_loopread_event; /* command to end loopread if true */
static pthread_t thread_loopread_event;
static void *egi_input_loopread(void *arg);
#if 0 ///////////////////
static pthread_cond_t cond_request; /* To indicate that mouse event request */
static pthread_mutex_t mutex_lockCond; /* mutex lock for pthread cond */
static int flag_cond=-1; /* predicate for pthread cond, set in egi_touch_loopread()
* flag_cond: == 0, request=false, ==1 request=true.
* flag_cond: == -1, idle.
*/
#endif/////////////////
/* 2. Input device: for mouse */
#if 1 /* set mouse type to Intellimouse #1 */
static unsigned char setup_data[6]={0xf3,200,0xf3,100,0xf3,80};
#else /* set mouse type to Intellimouse #2 */
static unsigned char setup_data[6]={0xf3,200,0xf3,200,0xf3,80};
#endif
static int mouse_fd=-1;
static unsigned char mouse_data[4];
static EGI_MOUSE_CALLBACK mouse_callback; /* To be assigned */
/* ??? mutex lock for mouse_callback */
static bool tok_loopread_mouse_running; /* token for loopread is running if true */
static bool cmd_end_loopread_mouse; /* command to end loopread if true */
static pthread_t thread_loopread_mouse;
static void *egi_mouse_loopread(void *arg);
/* Mouse status and data */
static EGI_MOUSE_STATUS mostatus={ .LeftKeyUpHold=true, .RightKeyUpHold=true, .MidKeyUpHold=true, .KeysIdle=true, };
/* Keyboard status and data */
static EGI_KBD_STATUS kbdstatus;
/* Conkey ID to its Name */
const char* CONKEY_NAME[]=
{
[CONKEYID_NONE] = "None",
[CONKEYID_CTRL] = "CTRL",
[CONKEYID_ALT] = "ALT",
[CONKEYID_SHIFT] = "SHIFT",
[CONKEYID_SUPER] = "SUPER",
[CONKEYID_ASCII] = "ASCII",
};
/*---------------------------------------------------
Call this function before loopread input device
----------------------------------------------------*/
void egi_input_setCallback(EGI_INEVENT_CALLBACK callback)
{
inevent_callback=callback;
}
/*-------------------------------
Run inevent loopread in a thread
Return:
0 OK
<0 Fails
------------------------------*/
int egi_start_inputread(const char *dev_name)
{
if(tok_loopread_event_running==true) {
printf("%s: inevent loopread has been running already!\n",__func__);
return -1;
}
cmd_end_loopread_event=false;
/* start touch_read thread */
if( pthread_create(&thread_loopread_event, NULL, (void *)egi_input_loopread, (void *)dev_name) !=0 )
{
printf("%s: Fail to create inevent loopread thread!\n", __func__);
return -1;
}
/* reset token */
tok_loopread_event_running=true;
return 0;
}
/*-------------------------------
End inevent loopread.
Return:
0 OK
<0 Fails
------------------------------*/
int egi_end_inputread(void)
{
if(tok_loopread_event_running==false) {
printf("%s: inevent loopread has been ceased already!\n",__func__);
return -1;
}
/* Set indicator to end loopread */
cmd_end_loopread_event=true;
/* Wait to join touch_loopread thread */
if( pthread_join(thread_loopread_event, NULL ) !=0 ) {
printf("%s:Fail to join thread_loopread_event.\n", __func__);
return -1;
}
/* reset cmd and token */
cmd_end_loopread_event=false;
tok_loopread_event_running=false;
/* close fd */
close(input_fd);
input_fd=-1;
return 0;
}
/*---------------------------------------------------
Call this function before loopread mice/mouseX device
----------------------------------------------------*/
void egi_mouse_setCallback(EGI_MOUSE_CALLBACK callback)
{
mouse_callback=callback;
}
/*----------------------------------------------
Run loopread mice/mouseX data in a thread.
@dev_name: Mouse dev name
NULL, default "/dev/input/mice"
Return:
0 OK
<0 Fails
-----------------------------------------------*/
EGI_MOUSE_STATUS* egi_start_mouseread(const char *dev_name, EGI_MOUSE_CALLBACK callback )
{
if(tok_loopread_mouse_running==true) {
printf("%s: inevent loopread has been running already!\n",__func__);
return NULL;
}
//cmd_end_loopread_mouse=false;
mostatus.cmd_end_loopread_mouse=false;
/* Set callback */
mouse_callback=callback;
/* Init mostatus mutex */
if(pthread_mutex_init(&mostatus.mutex,NULL) != 0)
{
printf("%s: Fail to initiate mostatus.mutex.\n",__func__);
return NULL;
}
/* Start loopread_mouse thread */
if( pthread_create(&thread_loopread_mouse, NULL, (void *)egi_mouse_loopread, (void *)dev_name) !=0 )
{
printf("%s: Fail to create mouse loopread thread!\n", __func__);
if(pthread_mutex_destroy(&mostatus.mutex) !=0 )
printf("%s: Fail to destroy mostatus.mutex!\n",__func__);
return NULL;
}
/* reset token */
tok_loopread_mouse_running=true;
return &mostatus;
//return 0;
}
/*-------------------------------
End inevent loopread.
Return:
0 OK
<0 Fails
------------------------------*/
int egi_end_mouseread(void)
{
if(tok_loopread_mouse_running==false) {
printf("%s: inevent loopread has been ceased already!\n",__func__);
return -1;
}
/* Set indicator to end loopread */
//cmd_end_loopread_mouse=true;
/* Set mostatus, otherwise it may be trapped in mouse_callback. */
mostatus.cmd_end_loopread_mouse=true;
/* Wait to join touch_loopread thread */
if( pthread_join(thread_loopread_mouse, NULL ) !=0 ) {
printf("%s: Fail to join thread_loopread_event.\n", __func__);
return -1;
}
/* Destroy mostatus mutex */
if(pthread_mutex_destroy(&mostatus.mutex) !=0 )
printf("%s: Fail to destroy mostatus.mutex!\n",__func__);
/* reset cmd and token */
//cmd_end_loopread_mouse=false;
mostatus.cmd_end_loopread_mouse=false;
tok_loopread_mouse_running=false;
/* close fd */
close(mouse_fd);
mouse_fd=-1;
return 0;
}
/*---------------------------------------------------------------------
A thread fucntion
Read input device in a loop, handle callback if
event reaches.
NOTE:
1. A mouse KeyHoldDown event will NOT be polled independently, it's only
updated together with other events, such as mouse moving/rolling
etc.
@devname: Name of the input device
----------------------------------------------------------------------*/
static void *egi_input_loopread( void* arg )
{
fd_set rfds;
struct timeval tmval;
int retval;
const char *dev_name=(const char *)arg;
if(dev_name==NULL )
return (void *)-1;
/* -------------- Buffer ??? ---------------------------
FILE *fp;
fp=open("/dev/input/event0", "r");
if(fp==NULL) {
printf("%s: Open input event: %s", __func__, strerror(errno));
return -1;
}
while(1) {
//Read input event in buffer way
fread((void *)&inevent, sizeof(inevent), 1, fp);
if(ferror(fp)) {
printf("%s: fread input event:%s\n",__func__, strerror(errno));
return -2;
}
printf("Event_type: %d, Event_code: %d, Event_value: %d, Event_Time: %dsec, %dusec\n",
inevent.type, inevent.code, inevent.value, (int)inevent.time.tv_sec, (int)inevent.time.tv_usec );
}
------------------------------------------ */
/* Loop input select read */
while(1) {
/* Check end_loopread command */
if(cmd_end_loopread_event)
break;
/* Check fd, and try to re_open if necessary. */
while(input_fd<0) {
input_fd=open( dev_name, O_RDWR|O_CLOEXEC);
if(input_fd<0) {
printf("%s: Open '%s' fail: %s.\n",__func__, dev_name, strerror(errno));
tm_delayms(250);
}
else if(input_fd>0) {
printf("%s: succeed to open '%s'.\n",__func__, dev_name);
/* set mouse type to Intellimouse
* If the eventX device is mapped to /dev/input/mice or mouseX etc.., it will fail!?
* Instead, you should set the type to /dev/input/mice or mouseX etc..
*/
retval=write(input_fd, setup_data, sizeof(setup_data));
if(retval<=0) {
printf("%s: Fail to set as Intellimouse! retval=%d. \n",__func__, retval);
} else {
/* Read out reply to discard */
printf("%s: Succeed to set as Intellimouse! \n",__func__);
read(input_fd, &inevent, sizeof(struct input_event));
}
}
}
/* To select fd */
FD_ZERO(&rfds);
FD_SET( input_fd, &rfds);
tmval.tv_sec=1;
tmval.tv_usec=0;
retval=select( input_fd+1, &rfds, NULL, NULL, &tmval );
if(retval<0) {
printf("%s: select event: errno=%d, %s \n",__func__, errno, strerror(errno));
continue;
}
else if(retval==0) {
//printf("Time out\n");
continue;
}
/* Read input fd and trigger callback */
if(FD_ISSET(input_fd, &rfds)) {
retval=read(input_fd, &inevent, sizeof(struct input_event));
if(retval<0 && errno==ENODEV) { /* Device is offline */
printf("%s: retval=%d, read inevent :%s\n",__func__, retval, strerror(errno));
if(close(input_fd)!=0)
printf("%s: close input_fd :%s\n",__func__, strerror(errno));
input_fd=-1; /* close() will not reset it */
printf("%s: Device '%s' is offline. close input_fd and reset it to %d. \n", __func__, dev_name, input_fd);
tm_delayms(100);
continue;
}
else if(retval<0) {
printf("%s: read inevent errno=%d : %s\n",__func__, errno, strerror(errno));
continue;
}
/* Call back */
if(inevent_callback!=NULL) {
//inevent_callback(inevent.type, inevent.code, inevent.value);
inevent_callback(&inevent);
}
}
}
close(input_fd);
input_fd=-1;
return (void *)0;
}
/*--------------------------------------------------------
A thread function
Read mouse input data and upate MODULE.mostatus in loop.
Note:
1. If gv_fb_dev is NOT initialized! then gv_fb_dev.pos_xres
and gv_fb_dev.pos_yres HAVE TO be set manually!
TODO:
1. There is chance that the mice dev MAY miss/omit events!?
Example: LeftKeyDownHold transfers to LeftKeyDown directly,
and omit LeftKeyUp status.
2.
@arg: devname for the mouse device.
if NULL, "/dev/input/mice"
---------------------------------------------------------*/
static void *egi_mouse_loopread( void* arg )
{
fd_set rfds;
struct timeval tmval;
int retval;
int tmp;
unsigned char old_mouse_data[4]={0};
bool status_sustain=false;
int status=0;/* 0b00(0): Released_hold
0b01(1): Key Down / Raising edge
0b10(2): Key Up / Falling edge
0b11(3): Pressed_hold
*/
const char *dev_name="/dev/input/mice";
if(arg!=NULL)
dev_name=(const char *)arg;
/* Loop input select read */
while(1) {
/* Check end_loopread command */
//if(cmd_end_loopread_mouse)
if(mostatus.cmd_end_loopread_mouse)
break;
/* Check fd, and try to re_open if necessary. */
#if 0
while(mouse_fd<0) {
#else
if(mouse_fd<0) {
#endif
mouse_fd=open( dev_name, O_RDWR|O_CLOEXEC);
if(mouse_fd<0) {
printf("%s: Open '%s' fail: %s.\n",__func__, dev_name, strerror(errno));
tm_delayms(50); //TODO: if() continue...
}
else if(mouse_fd>0) {
printf("%s: Succeed to open '%s'.\n",__func__, dev_name);
memset(mouse_data,0,sizeof(mouse_data));
/* set mouse type to Intellimouse */
retval=write( mouse_fd, setup_data,sizeof(setup_data));
if(retval<=0) {
printf("%s: Fail to set to as Intellimouse! retval=%d.\n",__func__, retval);
} else {
printf("%s: Succeed to set as Intellimouse!\n",__func__);
/* Read out reply to discard */
read( mouse_fd, mouse_data, sizeof(mouse_data));
}
/* Clear mouse_data! */
memset(mouse_data,0,sizeof(mouse_data));
}
}
/* To select fd */
FD_ZERO(&rfds);
FD_SET( mouse_fd, &rfds);
tmval.tv_sec=0;
tmval.tv_usec=50000;
//egi_dpstd("Start select mouse..\n");
retval=select( mouse_fd+1, &rfds, NULL, NULL, &tmval );
if(retval<0) {
printf("%s: select event: errno=%d, %s \n",__func__, errno, strerror(errno));
continue;
}
else if(retval==0) {
//printf("Time out\n");
if(status_sustain==false) { /* Check if its DOWNHOLD idle */
/* Keep status, clear Movement */
/* EXAMPLE: If previous status is Click down. .....HOLD */
mouse_data[0] &= 0x0F;
mouse_data[1]=0;
mouse_data[2]=0;
mouse_data[3]=0;
status_sustain=true;
//continue; GO ON ....
}
else /* Sustained status: DOWNHOLD etc. */
continue;
}
/* Reset, status is changing. */
status_sustain=false;
/* Read input fd and trigger callback */
if( retval>0 && FD_ISSET(mouse_fd, &rfds) ) {
retval=read(mouse_fd, mouse_data, sizeof(mouse_data));
/* For /dev/input/eventX and /dev/input/mouseX, ENODEV means device is offline.
* For /dev/input/mice, it alway exists, so ENODEV case never happens.
*/
if(retval<0 && errno==ENODEV) {
printf("%s: read input mouse to get retval=%d, :%s \n",__func__, retval, strerror(errno));
if(close(mouse_fd)!=0)
printf("%s: close mouse_fd :%s\n",__func__, strerror(errno));
mouse_fd=-1; /* close() will not reset it */
printf("%s: Device '%s' is offline. close mouse_fd and reset it to %d. \n", __func__, dev_name, mouse_fd);
tm_delayms(100);
continue;
}
else if(retval<0) {
printf("%s: read mouse to get retval=%d, errno=%d : %s\n",__func__, retval, errno, strerror(errno));
continue;
}
else if(retval==0) {
printf("%s: read mouse to get retval=0, errno=%d : %s\n",__func__, errno, strerror(errno));
continue;
}
} /* END FD_ISSET */
/* NOW: retval>0 OR TIME_OUT */
/* Get mostatus mutex lock: Waiting for test_surfman to egi_mouse_putRequest(pmostat) */
if( pthread_mutex_lock(&mostatus.mutex) !=0 )
printf("%s: Fail to mutex lock mostatus.mutex!\n",__func__);
/* --- >>> Critical Zone */
/* HK2022-05-07 Clear mostatus tokens -----> TODO clear more. */
//memset(&mostatus, 0, sizeof(mostatus));
mostatus.LeftKeyDown=false; mostatus.RightKeyDown=false; mostatus.MidKeyDown=false;
//mostatus.LeftKeyUpHold=true; mostatus.RightKeyUpHold=true;
//mostatus.MidKeyUpHold=true; mostatus.KeysIdle=true;
/* ------ Cal. mouse status/data --------- */
/* 1. Renew status for Left Key */
status=((old_mouse_data[0]&0x1)<<1) + (mouse_data[0]&0x1);
switch( status ) {
case 0b01:
mostatus.LeftKeyDown=true;
mostatus.LeftKeyUpHold=false;
mostatus.LeftKeyDownHold=false;
printf("-Leftkey Down!\n");
break;
case 0b10:
mostatus.LeftKeyUp=true;
mostatus.LeftKeyDown=false; /* MAY transfer from 0b01 ! */
mostatus.LeftKeyDownHold=false; /* <--------- */
printf("-Leftkey Up!\n");
break;
case 0b11:
mostatus.LeftKeyDownHold=true;
mostatus.LeftKeyDown=false;
mostatus.LeftKeyUpHold=false;
printf("-Leftkey DownHold!\n");
break;
default: /* 0b00: Leftkey UpHold */
/* TODO: Necesssary? May miss status transitions sometimes, restore KeyUp status */
if(mostatus.LeftKeyDown || mostatus.LeftKeyDownHold ) {
mostatus.LeftKeyUp=true;
printf("--Leftkey Up!\n");
}
else
mostatus.LeftKeyUp=false;
mostatus.LeftKeyUpHold=true;
mostatus.LeftKeyDown=false; /* May miss some status??? */
mostatus.LeftKeyDownHold=false;
break;
}
/* 2. Renew status for Right Key */
status=(old_mouse_data[0]&0x2) + ((mouse_data[0]&0x2)>>1);
switch( status ) {
case 0b01:
mostatus.RightKeyDown=true;
mostatus.RightKeyUpHold=false;
mostatus.RightKeyDownHold=false;
printf("-Rightkey Down!\n");
break;
case 0b10:
mostatus.RightKeyUp=true;
mostatus.RightKeyDown=false;
mostatus.RightKeyDownHold=false;
printf("-Rightkey Up!\n");
break;
case 0b11:
mostatus.RightKeyDownHold=true;
mostatus.RightKeyDown=false;
printf(" ---Rightkey DownHold!\n");
break;
default: /* 0b00: UpHold */
mostatus.RightKeyUpHold=true;
mostatus.RightKeyUp=false;
mostatus.RightKeyDown=false;
mostatus.RightKeyDownHold=false;
break;
}
/* 3. Renew status for Mid Key */
status=((old_mouse_data[0]&0x4)>>1) + ((mouse_data[0]&0x4)>>2);
switch( status ) {
case 0b01:
mostatus.MidKeyDown=true;
mostatus.MidKeyUpHold=false;
// printf("-Midkey Down!\n");
break;
case 0b10:
mostatus.MidKeyUp=true;
mostatus.MidKeyDown=false;
mostatus.MidKeyDownHold=false;
// printf("-Midkey Up!\n");
break;
case 0b11:
mostatus.MidKeyDownHold=true;
mostatus.MidKeyDown=false;
//printf(" ---Midkey DownHold!\n");
break;
default: /* 0b00: UpHold */
mostatus.MidKeyUpHold=true;
mostatus.MidKeyUp=false;
mostatus.MidKeyDown=false; /* May miss some status??? */
break;
}
/* 4. Renew status for KeysIdle */
mostatus.KeysIdle=(mostatus.LeftKeyUpHold) && (mostatus.RightKeyUpHold) && (mostatus.MidKeyUpHold);
#define TEST_TOUCHSCREEN_MOUSE 0
#if TEST_TOUCHSCREEN_MOUSE /* TEST: Re_map touchscreen moustX/Y for Landscape Mode, mouse XY COORD
Note:
* 1. XXX Mouse origin NOT at left top corner, instead at left down corner. ---ok, + for 0!
* 2. XXX Screen touch data NOT in sequence occasionally, it makes mouse skip??! ---ok + for 0!
*/
unsigned char tok1=mouse_data[0]&0x10; /* X sign */
unsigned char tok2=mouse_data[0]&0x20; /* Y sign */
unsigned char data1=mouse_data[1]; /* mouseDX */
unsigned char data2=mouse_data[2]; /* mouseDY */
#if 0 /* screen DX maps to touch -DY */
if(tok2) {
mouse_data[0] &= ~(0x10); /* + */
mouse_data[1] = (~data2)+1; /* - -> + 负数的反码+1就是补码, 即其相反的正数 */
} else {
if(data2!=0) {
mouse_data[0] |= 0x10; /* - */
mouse_data[1]= 0x100-data2; /* 补码表示负数 */
}
else {
mouse_data[0] &= ~(0x10); /* + for 0! */
mouse_data[1] =0;
}
}
#else /* screen DX maps to touch DY */
if(tok2)
mouse_data[0] |= 0x10; /* - */
else
mouse_data[0] &= ~(0x10); /* + */
mouse_data[1]=data2;
#endif
/* screen DY maps to touch -DX !!! MOUSE COORD, NOT sCREEN coord !!! */
if(tok1) {
mouse_data[0] &= ~(0x20); /* + */
mouse_data[2] = (~data1)+1;
}
else {
if(data1!=0) {
mouse_data[0] |= 0x20; /* - */
mouse_data[2] = 0x100-data1; /* 补码表示负数 */
}
else { /* !!! data1==0 */
mouse_data[0] &= ~(0x20); /* + for 0! */
mouse_data[2] =0;
}
}
#endif
/* Get mouseDX/DY */
mostatus.mouseDX = (mouse_data[0]&0x10) ? mouse_data[1]-256 : mouse_data[1];
mostatus.mouseDY = -( (mouse_data[0]&0x20) ? mouse_data[2]-256 : mouse_data[2] );
#if TEST_TOUCHSCREEN_MOUSE /* TOUCHSCREEN speed adjust to flow up with displaying image, as origin touch ABS_X/Y in 12bits!? */
mostatus.mouseDX = mostatus.mouseDX*8/5; // 320:240
mostatus.mouseDY = mostatus.mouseDY*5/6; // 240:320
#endif
/* 5. Get mouse X */
//mostatus.mouseDX = (mouse_data[0]&0x10) ? mouse_data[1]-256 : mouse_data[1];
tmp=mostatus.mouseX;
mostatus.mouseX += mostatus.mouseDX;
if( mostatus.mouseX > gv_fb_dev.pos_xres -5)
mostatus.mouseX=gv_fb_dev.pos_xres -5;
else if( mostatus.mouseX<0)
mostatus.mouseX=0;
/* Adjust DX accordingly! */
mostatus.mouseDX = mostatus.mouseX-tmp;
/* 6. Get mouse Y: Notice LCD Y direction! Minus for down movement, Plus for up movement!
* !!! For eventX: Minus for up movement, Plus for down movement!
*/
//mostatus.mouseDY = -( (mouse_data[0]&0x20) ? mouse_data[2]-256 : mouse_data[2] );
tmp=mostatus.mouseY;
mostatus.mouseY +=mostatus.mouseDY;
/* Limit mouseY */
if( mostatus.mouseY > gv_fb_dev.pos_yres -5)
mostatus.mouseY=gv_fb_dev.pos_yres -5;
else if(mostatus.mouseY<0)
mostatus.mouseY=0;
/* Adjust DY accordingly! */
mostatus.mouseDY = mostatus.mouseY-tmp;
/* 7. Get mouse Z */
mostatus.mouseDZ = (mouse_data[3]&0x80) ? mouse_data[3]-256 : mouse_data[3] ;
mostatus.mouseZ += mostatus.mouseDZ;
/* 8. Renew old mouse data */
memcpy(old_mouse_data, mouse_data, sizeof(old_mouse_data));
/* ------ END: Cal. mouse status/data --------- */
/* --- <<< Critical Zone */
/* Put mutex lock */
pthread_mutex_unlock(&mostatus.mutex);
/* Call back. mostauts.request is set in the callback! */
if(mouse_callback!=NULL) {
/* If callback trapped here, then cmd_end_loopread_mouse signal will NOT be responded!
* mostauts.request is set in the callback!
*/
//egi_dpstd("RKDHold=%s\n", mostatus.RightKeyDownHold ? "TRUE" : "FALSE" );
mouse_callback(mouse_data, sizeof(mouse_data), &mostatus);
}
#if 0 /* --- TEST ---- */
/* Parse mouse data */
printf("Left:%d, Right:%d, Middle:%d, dx=%d, dy=%d, (wheel)dz=%d\n",
mouse_data[0]&0x1, (mouse_data[0]&0x2) > 0, (mouse_data[0]&0x4) > 0, /* 3 KEYL L.R.M */
(mouse_data[0]&0x10) ? mouse_data[1]-256 : mouse_data[1], /* dx */
(mouse_data[0]&0x20) ? mouse_data[2]-256 : mouse_data[2], /* dy */
(mouse_data[3]&0x80) ? mouse_data[3]-256 : mouse_data[3] /* dz, 2's complement */
);
#endif
/* Wait for MEVETN request to be proceed, TODO: use pthread_cond_wait() instead. */
/* Note: (mostauts.request is set in the callback!)
* 1. This is to ensure each MEVENT request is parsed/responded.
* 2. Request usually is parsed/responded by another thread, other than mouse_callback(), Example: test_surfman.c */
while( egi_mouse_checkRequest(&mostatus) && !mostatus.cmd_end_loopread_mouse ) { tm_delayms(5); };
}
close(mouse_fd);
mouse_fd=-1;
return (void *)0;
}
/*-------------------------------------------------
Return TURE if mostat.request==TRUE
Return FALSE if it fails or mostat.request==FALSE
Unlock moutex.
--------------------------------------------------*/
bool egi_mouse_checkRequest(EGI_MOUSE_STATUS *mostat)
{
bool request;
if(mostat==NULL)
return false;
/* Get mostatus mutex lock */
if( pthread_mutex_lock(&mostat->mutex) !=0 ) {
printf("%s: Fail to mutex lock mostatus.mutex!\n",__func__);
return false;
}
request=mostat->request;
/* Put mutex lock */
pthread_mutex_unlock(&mostat->mutex);
return request;
}
/*---------------------------------------------------------
Return TURE if mostat.request==TRUE and keep mutex locked!
Return FALSE if mostat.request==FALSE or it fails.
--------------------------------------------------------*/
bool egi_mouse_getRequest(EGI_MOUSE_STATUS *mostat)
{
if(mostat==NULL)
return false;
/* Get mostatus mutex lock */
if( pthread_mutex_lock(&mostat->mutex) !=0 ) {
printf("%s: Fail to mutex lock mostatus.mutex!\n",__func__);
return false;
}
if(mostat->request) {
return true;
/* --- Keep mostatus mutex locked! --- */
}
else { /* Put mutex lock */
pthread_mutex_unlock(&mostat->mutex);
return false;
}
}
/*-----------------------------------------------
Reset mostat.request to FALSE, and unlock mutex.
Return:
0 OK
<0 Fails
------------------------------------------------*/
int egi_mouse_putRequest(EGI_MOUSE_STATUS *mostat)
{
if(mostat==NULL)
return -1;
/* Reset request */
mostat->request=false;
/* Put mutex lock */
pthread_mutex_unlock(&mostat->mutex);
return 0;
}
/* =================== Terminal IO setttings =============== */
static struct termios old_termioset;
static speed_t old_termispeed;
static speed_t old_termospeed;
/*------------------------------------
Set terminal IO attributes, and try to
set io speed as expected.
-------------------------------------*/
void egi_set_termios(void)
{
struct termios new_termioset;
#if 0 /* TEST: Call cfmakeraw() */
tcgetattr(STDIN_FILENO, &old_termioset);
new_termioset=old_termioset;