Mercurial > remote-gamepad-server
comparison uinput.c @ 0:e70ea46d6073
Initial import from http://wouhanegaine.free.fr/dev/DSPad02b_neo07.zip
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 22 Feb 2015 01:38:06 +0100 |
parents | |
children | f362b20de51e |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e70ea46d6073 |
---|---|
1 #include "uinput.h" | |
2 #include "ds.h" | |
3 #include "service.h" | |
4 | |
5 #include <stdio.h> | |
6 #include <fcntl.h> | |
7 #include <string.h> | |
8 #include <sys/ioctl.h> | |
9 #include <unistd.h> | |
10 #include <stdbool.h> | |
11 | |
12 | |
13 #define DS_NAME "NintendoDS Wireless Gamepad" | |
14 | |
15 /* Not found these values... but it's not important! */ | |
16 #define DS_VENDOR 0xdead | |
17 #define DS_PRODUCT 0xbeef | |
18 #define DS_VERSION 0x00 | |
19 | |
20 | |
21 | |
22 /* | |
23 Create a new uinput device | |
24 Returns: file descriptor (-1 if error) | |
25 */ | |
26 int init_uinput_device(void){ | |
27 struct uinput_user_dev dev; | |
28 int fd = -1; | |
29 | |
30 if(uinput_dev == NULL){ | |
31 fd = open("/dev/uinput", O_RDWR); | |
32 | |
33 if(fd < 0) | |
34 fd = open("/dev/misc/uinput", O_RDWR); | |
35 | |
36 if(fd < 0) | |
37 fd = open("/dev/input/uinput", O_RDWR); | |
38 } | |
39 else fd = open(uinput_dev, O_RDWR); //from xml | |
40 | |
41 if(fd < 0){ | |
42 fprintf(stderr, " [%d] Unable to open uinput device ; hint: 'modprobe uinput' ?!\n", curdev); | |
43 return -1; | |
44 } | |
45 | |
46 | |
47 | |
48 memset(&dev, 0, sizeof(dev)); | |
49 strncpy(dev.name, DS_NAME, UINPUT_MAX_NAME_SIZE); | |
50 dev.id.bustype = 0; | |
51 dev.id.vendor = DS_VENDOR; | |
52 dev.id.product = DS_PRODUCT; | |
53 dev.id.version = DS_VERSION; | |
54 | |
55 dev.absmax[ABS_X] = DS_MAX_X; | |
56 dev.absmin[ABS_X] = DS_MIN_X; | |
57 dev.absfuzz[ABS_X] = 4; | |
58 dev.absflat[ABS_X] = 2; | |
59 | |
60 dev.absmax[ABS_Y] = DS_MAX_Y; | |
61 dev.absmin[ABS_Y] = DS_MIN_Y; | |
62 dev.absfuzz[ABS_X] = 4; | |
63 dev.absflat[ABS_X] = 2; | |
64 | |
65 | |
66 if(write(fd, &dev, sizeof(dev)) < (ssize_t)sizeof(dev)){ | |
67 fprintf(stderr, " [%d] Registering device at uinput failed\n", curdev); | |
68 return -1; | |
69 } | |
70 | |
71 /* Keys [01] (found in <linux/input.h>) */ | |
72 if( ioctl(fd, UI_SET_EVBIT, EV_KEY) ) return -1; | |
73 if( ioctl(fd, UI_SET_KEYBIT, BTN_A) ) return -1; | |
74 if( ioctl(fd, UI_SET_KEYBIT, BTN_B) ) return -1; | |
75 if( ioctl(fd, UI_SET_KEYBIT, BTN_X) ) return -1; | |
76 if( ioctl(fd, UI_SET_KEYBIT, BTN_Y) ) return -1; | |
77 if( ioctl(fd, UI_SET_KEYBIT, BTN_TL) ) return -1; | |
78 if( ioctl(fd, UI_SET_KEYBIT, BTN_TR) ) return -1; | |
79 if( ioctl(fd, UI_SET_KEYBIT, BTN_START) ) return -1; | |
80 if( ioctl(fd, UI_SET_KEYBIT, BTN_SELECT)) return -1; | |
81 | |
82 /* D-Pad [-0xFFFF - 0xFFFF] */ | |
83 if( ioctl(fd, UI_SET_EVBIT, EV_ABS) ) return -1; | |
84 if( ioctl(fd, UI_SET_ABSBIT, ABS_X) ) return -1; | |
85 if( ioctl(fd, UI_SET_ABSBIT, ABS_Y) ) return -1; | |
86 | |
87 | |
88 /* Register device */ | |
89 if( ioctl(fd, UI_DEV_CREATE) ) return -1; | |
90 | |
91 return fd; | |
92 } | |
93 | |
94 | |
95 /* | |
96 Send an event into the uinput device | |
97 Returns: TRUE, FALSE | |
98 */ | |
99 int do_uinput(int fd, unsigned short key, int pressed, unsigned short event_type){ | |
100 struct input_event event; | |
101 memset(&event, 0 , sizeof(event)); | |
102 | |
103 event.type = event_type; | |
104 event.code = key; | |
105 event.value = pressed; | |
106 | |
107 if(write(fd,&event,sizeof(event)) != sizeof(event)){ | |
108 fprintf(stderr, " [%d] Writing event to uinput driver failed ; Aborting\n", curdev); | |
109 return false; | |
110 } | |
111 return true; | |
112 } | |
113 | |
114 | |
115 /* | |
116 Synchonize events | |
117 */ | |
118 void flush_uinput(int fd){ | |
119 struct input_event event; | |
120 memset(&event, 0 , sizeof(event)); | |
121 | |
122 event.type = EV_SYN; | |
123 event.code = SYN_REPORT; | |
124 | |
125 if(fd){ | |
126 if(write(fd, &event, sizeof(event) != sizeof(event))) | |
127 fprintf(stderr, " [%d] Flushing uinput failed\n", curdev); | |
128 } | |
129 } | |
130 | |
131 | |
132 | |
133 | |
134 |