-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmDriver.c
494 lines (456 loc) · 14.6 KB
/
tmDriver.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
/*********************************************************************
Tiny Mouse Driver without X
written by Koji Iigura 2019.
please see the README.md.
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <limits.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/inotify.h>
#include <linux/input.h>
#include "tmDriver.h"
static volatile int gMinX=0;
static volatile int gMinY=0;
static volatile int gMaxX=0;
static volatile int gMaxY=0;
static volatile int gMouseX=0;
static volatile int gMouseY=0;
static volatile int gMouseButtonStatus=0;
pthread_mutex_t gMutexForXY=PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t gMutexForButtonStatus=PTHREAD_MUTEX_INITIALIZER;
#define kMaxMonitorThread (256*2)
typedef struct {
int elementIndex;
pthread_t thread;
char *eventFileName; // "eventN"
FILE *fp;
} MonitorInfo;
static MonitorInfo gMonitorArray[kMaxMonitorThread];
pthread_mutex_t gMutexForMonitorArray=PTHREAD_MUTEX_INITIALIZER;
pthread_t gHotPlugMonitorThread;
volatile bool gInitializing=false;
static bool startMonitorForHotPlug();
static void *hotPlugMonitorFunc(void *inDummy);
static bool disposeMonitorThread(const char *inEventFile);
static bool isAlreadyMonitored(const char *inEventFile);
static MonitorInfo *getMonitorSlot(const char *inEventFile);
static struct inotify_event *getEvent(FILE *inINotifyFP);
static bool createMonitorForAlreadyPlugged();
static bool isEventFile(const char *inFileName);
static bool createMonitorThread(const char *inEventFileName);
static MonitorInfo *getEmptyMonitorArraySlot();
static void *mouseWatcherFunc(void *inMonitorInfo);
static void resetMonitorInfo(MonitorInfo *inMonitorInfo);
static bool initMonitorArray();
static int clamp(int inMin,int inMax,int inValue);
bool tmDrvrInit(int inMinX,int inMinY,int inMaxX,int inMaxY) {
if(tmDrvrSetArea(inMinX,inMinY,inMaxX,inMaxY)==false) {
return false;
}
if(tmDrvrSetPosition(0,0)==false) {
return false;
}
gMouseButtonStatus=0;
if(initMonitorArray()==false) {
return false;
}
gInitializing=true;
if(startMonitorForHotPlug()==false) {
return false;
}
if(createMonitorForAlreadyPlugged()==false) {
return false;
}
gInitializing=false;
return true;
}
static bool initMonitorArray() {
int result=pthread_mutex_lock(&gMutexForMonitorArray);
if( result ) {
return false; // can not lock
}
int i;
for(i=0; i<kMaxMonitorThread; i++) {
gMonitorArray[i].elementIndex=i;
gMonitorArray[i].eventFileName=NULL;
gMonitorArray[i].fp=NULL;
}
pthread_mutex_unlock(&gMutexForMonitorArray);
return true;
}
static bool startMonitorForHotPlug() {
int inotify=inotify_init();
if(inotify==-1) {
fprintf(stderr,"libtmdrvr: can not init notification.\n");
return false;
}
int wd=inotify_add_watch(inotify,"/dev/input/",IN_ALL_EVENTS);
if(wd==-1) {
fprintf(stderr,"libtmdrvr: can not watch /dev/input/\n");
return false;
}
// fprintf(stderr,"startMonitorForHotPlug: wd=%d\n",wd);
FILE *inotifyFP=fdopen(inotify,"rb");
if(inotifyFP==NULL) {
fprintf(stderr,"libtmdrvr: can not convert to file pointer.\n");
return false;
}
if( pthread_create(&gHotPlugMonitorThread,NULL,hotPlugMonitorFunc,inotifyFP) ) {
fprintf(stderr,"libtmdrvr: can not start hotplug-monitor thread.\n");
return false;
}
return true;
}
static void *hotPlugMonitorFunc(void *inInotifyFP) {
FILE *inotifyFP=(FILE *)inInotifyFP;
for(;;) {
struct inotify_event *event=getEvent(inotifyFP);
if(event==NULL) {
fprintf(stderr,"libtmdrvr: can not get event.\n");
return NULL;
}
bool isError=false;
if(event->mask & IN_CREATE) {
// device attached
if(isEventFile(event->name)==false) {
goto next;
}
// fprintf(stderr,"ATTACH %s\n",event->name);
pthread_mutex_lock(&gMutexForMonitorArray);
bool create=false;
if( gInitializing ) {
if(isAlreadyMonitored(event->name)==false) {
create=true;
}
} else {
create=true;
}
if( create ) {
if(createMonitorThread(event->name)==false) {
fprintf(stderr,"libtmdrvr: can not create monitor thread.\n");
isError=true;
}
}
pthread_mutex_unlock(&gMutexForMonitorArray);
}
if( isError ) {
goto next;
}
if(event->mask & IN_DELETE) {
// device dettached
if(isEventFile(event->name)==false) {
goto next;
}
//fprintf(stderr,"DETACH %s\n",event->name);
pthread_mutex_lock(&gMutexForMonitorArray);
if(disposeMonitorThread(event->name)==false) {
fprintf(stderr,"libtmdrvr: can not dispose monitor thread.\n");
isError=true;
}
pthread_mutex_unlock(&gMutexForMonitorArray);
}
next:
free(event);
if( isError ) {
break;
}
}
fclose(inotifyFP);
return NULL;
}
static bool disposeMonitorThread(const char *inEventFile) {
MonitorInfo *monitorInfo=getMonitorSlot(inEventFile);
if(monitorInfo==NULL) {
// mouseWatcherFunc detect the mouse removal and terminate by its self.
return true;
}
pthread_cancel(monitorInfo->thread);
resetMonitorInfo(monitorInfo);
return true;
}
static bool isAlreadyMonitored(const char *inEventFile) {
return getMonitorSlot(inEventFile)!=NULL;
}
static MonitorInfo *getMonitorSlot(const char *inEventFile) {
int i;
MonitorInfo *ret=NULL;
for(i=0; i<kMaxMonitorThread; i++) {
if(gMonitorArray[i].fp==NULL || gMonitorArray[i].eventFileName==NULL) {
continue;
}
if(strcmp(inEventFile,gMonitorArray[i].eventFileName)==0) {
ret=gMonitorArray+i;
break;
}
}
return ret;
}
static struct inotify_event *getEvent(FILE *inINotifyFP) {
const size_t fixedAreaSize=offsetof(struct inotify_event,name);
char fixedArea[fixedAreaSize];
int n=fread(fixedArea,sizeof(fixedArea),1,inINotifyFP);
struct inotify_event *ret;
if(n==0) {
fprintf(stderr,"libtmdrvr: can not read inotify event (fixed area).\n");
return NULL;
}
char nameBuffer[PATH_MAX+1];
uint32_t nameSize=((struct inotify_event *)fixedArea)->len;
char *nameAreaTop=nameBuffer;
if(nameSize==0) {
// a case of IN_OPEN.
goto makeReturnValue;
}
if(nameSize>sizeof(nameBuffer)) {
nameAreaTop=(char *)malloc(sizeof(char)*nameSize);
if(nameAreaTop==NULL) {
fprintf(stderr,"libtmdrvr: can not allocate file name memory.\n");
return NULL;
}
} else {
nameAreaTop=nameBuffer;
}
n=fread(nameAreaTop,nameSize,1,inINotifyFP);
if(n==0) {
fprintf(stderr,"libtmdrvr: can not read inotify event (name area).\n");
fprintf(stderr,"errno=%s\n",strerror(errno));
fprintf(stderr,"nameSize=%d\n",nameSize);
fprintf(stderr,"wd=%d\n",((struct inotify_event *)fixedArea)->wd);
fprintf(stderr,"mask=%x\n",((struct inotify_event *)fixedArea)->mask);
return NULL;
}
makeReturnValue:
ret=(struct inotify_event *)malloc(sizeof(struct inotify_event)+nameSize);
memcpy(ret,fixedArea,sizeof(fixedArea));
if(nameSize>0) {
memcpy(&ret->name,nameAreaTop,nameSize);
}
if(nameAreaTop!=nameBuffer) {
free(nameAreaTop);
}
return ret;
}
static bool createMonitorForAlreadyPlugged() {
DIR *dir=opendir("/dev/input/");
if(dir==NULL) {
return false; // can not open directory.
}
bool ret=true;
pthread_mutex_lock(&gMutexForMonitorArray);
struct dirent *entry;
do {
entry=readdir(dir);
if(entry!=NULL && isEventFile(entry->d_name) ) {
if( isAlreadyMonitored(entry->d_name) ) {
continue;
}
if(createMonitorThread(entry->d_name)==false) {
ret=false;
break;
}
}
} while(entry!=NULL);
pthread_mutex_unlock(&gMutexForMonitorArray);
closedir(dir);
return ret;
}
static bool isEventFile(const char *inFileName) {
if( strncmp(inFileName,"event",5) ) {
return false;
}
int i=4;
bool isDigit=true;
do {
i++;
if(isdigit(inFileName[i])==false) {
isDigit=false;
// printf("i=%d isdigit(%c)==false ",i,inFileName[i]);
break;
}
} while(inFileName[i+1]!='\0');
return isDigit;
}
static bool createMonitorThread(const char *inEventFileName) {
//printf("start createMonitorThread %s\n",inEventFileName);
MonitorInfo *monitorInfo=getEmptyMonitorArraySlot();
FILE *fp=NULL;
char *s=(char *)strdup(inEventFileName);
if(s==NULL) {
return false;
}
monitorInfo->eventFileName=s;
char buf[PATH_MAX+1];
sprintf(buf,"/dev/input/%s",inEventFileName);
tryAgain:
fp=fopen(buf,"rb");
if(fp==NULL) {
if(errno==EACCES) {
// Immediately after the USB device connection,
// the permission denided may be occur.
// So wait one second and retry.
sleep(1);
goto tryAgain;
} else {
goto error;
}
}
monitorInfo->fp=fp;
if( pthread_create(&monitorInfo->thread,NULL,mouseWatcherFunc,monitorInfo) ) {
goto error;
}
return true;
error:
resetMonitorInfo(monitorInfo);
return false;
}
static MonitorInfo *getEmptyMonitorArraySlot() {
int i;
for(i=0; i<kMaxMonitorThread; i++) {
if(gMonitorArray[i].eventFileName==NULL
&& gMonitorArray[i].fp==NULL) {
return gMonitorArray+i;
}
}
return NULL;
}
static void *mouseWatcherFunc(void *inMonitorInfo) {
MonitorInfo *monitorInfo=(MonitorInfo*)inMonitorInfo;
FILE *fin=monitorInfo->fp;
//printf("start mouse watcher [%s]\n",monitorInfo->eventFileName);
struct input_event ev;
for(;;) {
int s;
if((s=fread(&ev,sizeof(ev),1,fin))!=1) {
// device detached?
if( pthread_mutex_lock(&gMutexForMonitorArray) ) {
fprintf(stderr,"libtmdrvr: can not lock for monitor info array.\n");
}
resetMonitorInfo(monitorInfo);
pthread_mutex_unlock(&gMutexForMonitorArray);
return NULL;
}
if(ev.type==EV_REL) {
if( pthread_mutex_lock(&gMutexForXY) ) {
fprintf(stderr,"libtmdrvr: can not lock for mouse position.\n");
return NULL;
}
switch(ev.code) {
case REL_X:
gMouseX=clamp(gMinX,gMaxX-1,gMouseX+ev.value);
break;
case REL_Y:
gMouseY=clamp(gMinY,gMaxY-1,gMouseY+ev.value);
break;
}
pthread_mutex_unlock(&gMutexForXY);
} else if(ev.type==EV_KEY) {
if( pthread_mutex_lock(&gMutexForButtonStatus) ) {
fprintf(stderr,"libtmdrvr: can not lock for mouse button.\n");
return NULL;
}
int bitPattern=0;
switch(ev.code) {
case BTN_LEFT:
bitPattern=TMDRVR_LEFT_BUTTON_PRESSED;
break;
case BTN_MIDDLE:
bitPattern=TMDRVR_MIDDLE_BUTTON_PRESSED;
break;
case BTN_RIGHT:
bitPattern=TMDRVR_RIGHT_BUTTON_PRESSED;
}
if( bitPattern ) {
if(ev.value==1) {
gMouseButtonStatus |= bitPattern;
} else {
gMouseButtonStatus &= ~bitPattern;
}
}
pthread_mutex_unlock(&gMutexForButtonStatus);
}
}
return NULL;
}
static void resetMonitorInfo(MonitorInfo *inMonitorInfo) {
//fprintf(stderr,"restMonitorInfo[%s]\n",inMonitorInfo->eventFileName);
if(inMonitorInfo->eventFileName!=NULL) {
free(inMonitorInfo->eventFileName);
inMonitorInfo->eventFileName=NULL;
}
if(inMonitorInfo->fp!=NULL) {
fclose(inMonitorInfo->fp);
inMonitorInfo->fp=NULL;
}
}
static int clamp(int inMin,int inMax,int inValue) {
return inValue<inMin ? inMin
: inValue>inMax ? inMax : inValue;
}
bool tmDrvrSetArea(int inMinX,int inMinY,int inMaxX,int inMaxY) {
if(inMinX>inMaxX || inMinY>inMaxY) {
return false;
}
int result=pthread_mutex_lock(&gMutexForXY);
if( result ) {
return false; // can not lock
}
gMinX=inMinX;
gMinY=inMinY;
gMaxX=inMaxX;
gMaxY=inMaxY;
pthread_mutex_unlock(&gMutexForXY);
return true;
}
bool tmDrvrSetPosition(int inX,int inY) {
int result=pthread_mutex_lock(&gMutexForXY);
if( result ) {
return false; // can not lock
}
gMouseX=clamp(gMinX,gMaxX-1,inX);
gMouseY=clamp(gMinY,gMaxY-1,inY);
pthread_mutex_unlock(&gMutexForXY);
return true;
}
int tmDrvrGetX() {
return gMouseX;
}
int tmDrvrGetY() {
return gMouseY;
}
void tmDrvrGetXY(int *outMouseX,int *outMouseY) {
*outMouseX=gMouseX;
*outMouseY=gMouseY;
}
int tmDrvrGetButton() {
return gMouseButtonStatus;
}
bool tmDrvrIsLeftButtonPressed() {
return (gMouseButtonStatus & TMDRVR_LEFT_BUTTON_PRESSED)!=0;
}
bool tmDrvrIsMiddleButtonPressed() {
return (gMouseButtonStatus & TMDRVR_MIDDLE_BUTTON_PRESSED)!=0;
}
bool tmDrvrIsRightButtonPressed() {
return (gMouseButtonStatus & TMDRVR_RIGHT_BUTTON_PRESSED)!=0;
}
bool tmDrvrDispose() {
if( pthread_mutex_lock(&gMutexForMonitorArray) ) {
return false;
}
int i;
for(i=0; i<kMaxMonitorThread; i++) {
if(gMonitorArray[i].eventFileName!=NULL || gMonitorArray[i].fp!=NULL) {
pthread_cancel(gMonitorArray[i].thread);
resetMonitorInfo(gMonitorArray+i);
}
}
pthread_mutex_unlock(&gMutexForMonitorArray);
return true;
}