Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
130 | ligi | 1 | #include "evdev_handler.h" |
483 | ligi | 2 | #include "config.h" |
130 | ligi | 3 | |
4 | int retval; |
||
5 | size_t read_bytes; /* how many bytes were read */ |
||
6 | struct input_event ev[64]; /* the events (up to 64 at once) */ |
||
7 | struct input_event led_event; |
||
8 | |||
9 | |||
10 | |||
11 | int counter; |
||
12 | |||
483 | ligi | 13 | int evdevs_count=0; |
14 | int file_select(struct direct *entry) |
||
15 | { |
||
16 | |||
17 | if ((strncmp(entry->d_name, "event",5))==0) |
||
18 | return (TRUE); |
||
19 | else |
||
20 | return(FALSE); |
||
21 | } |
||
22 | |||
23 | |||
24 | void collect_evdev_devices() |
||
25 | { |
||
26 | |||
484 | ligi | 27 | // char event_dev_path[]="/dev/input/"; |
483 | ligi | 28 | |
29 | struct direct **files; |
||
484 | ligi | 30 | evdevs_count = scandir(evdev_path, &files, file_select, NULL); |
483 | ligi | 31 | |
484 | ligi | 32 | printf("%d inputs found in %s\n" , evdevs_count,evdev_path); |
483 | ligi | 33 | |
34 | int i=0; |
||
35 | for (i=1;i<evdevs_count+1;++i) |
||
36 | { |
||
37 | |||
38 | printf("%s",files[i-1]->d_name); |
||
484 | ligi | 39 | sprintf(evdevs[i].fname,"%s%s",evdev_path,files[i-1]->d_name); |
483 | ligi | 40 | int act_fd; |
41 | |||
42 | if ((act_fd = open(evdevs[i].fname, O_RDONLY)) < 0) |
||
43 | { |
||
44 | printf(" cant open %s for reading!",evdevs[i].name); |
||
45 | |||
46 | } |
||
47 | else |
||
48 | { |
||
49 | if(ioctl(act_fd , EVIOCGNAME(sizeof(evdevs[i].name)), evdevs[i].name) < 0) { |
||
50 | perror("evdev ioctl"); |
||
51 | } |
||
52 | |||
53 | printf("EVDEV reports name: %s\n", evdevs[i].name); |
||
54 | close(act_fd); |
||
55 | } |
||
56 | |||
57 | |||
58 | /* |
||
59 | FILE *fp=NULL ; |
||
60 | |||
61 | sprintf(evdevs[i].fname,"/sys/class/input/%s/name",files[i-1]->d_name); |
||
62 | fp = fopen ( evdevs[i].fname, "r") ; |
||
63 | |||
64 | if (fp==NULL) |
||
65 | { |
||
66 | printf("cannot read %s\n",evdevs[i].fname); |
||
67 | // exit(1); |
||
68 | } |
||
69 | int ch=-2; |
||
70 | int str_pos=0; |
||
71 | while ( (ch!=-1) && (ch!='\n') ) |
||
72 | { |
||
73 | |||
74 | ch = fgetc ( fp ) ; |
||
75 | if ((ch!=13)&&(ch!=10))evdevs[i].name[str_pos++]=ch; |
||
76 | printf("%c",ch); |
||
77 | } |
||
78 | |||
79 | fclose(fp); |
||
80 | evdevs[i].name[str_pos++]=0; |
||
81 | */ |
||
82 | |||
83 | } |
||
84 | } |
||
85 | |||
86 | |||
87 | void print_event_str(int id) |
||
88 | { |
||
89 | switch ( id) |
||
90 | { |
||
91 | case EV_KEY : |
||
92 | printf("Keys or Button\n"); |
||
93 | break; |
||
94 | case EV_ABS : |
||
95 | printf("Absolute Axis\n"); |
||
96 | break; |
||
97 | case EV_LED : |
||
98 | printf("LED(s)\n"); |
||
99 | break; |
||
100 | case EV_REP : |
||
101 | printf("Repeat\n"); |
||
102 | break; |
||
103 | case EV_SYN : |
||
104 | printf("Sync?\n"); |
||
105 | break; |
||
106 | case EV_REL : |
||
107 | printf("Relative Axis\n"); |
||
108 | break; |
||
109 | case EV_MSC : |
||
110 | printf("Misc\n"); |
||
111 | break; |
||
112 | default: |
||
113 | printf("Unknown event type ( 0x%04hx)\n", id); |
||
114 | } |
||
115 | |||
116 | return ""; |
||
117 | } |
||
118 | |||
119 | int connect_evdev() |
||
130 | ligi | 120 | { |
121 | |||
483 | ligi | 122 | |
130 | ligi | 123 | struct input_devinfo { |
124 | uint16_t bustype; |
||
125 | uint16_t vendor; |
||
126 | uint16_t product; |
||
127 | uint16_t version; |
||
128 | }; |
||
129 | struct input_devinfo device_info; |
||
130 | |||
483 | ligi | 131 | int i; |
132 | for ( i =0 ; i< input_count;i++) |
||
130 | ligi | 133 | { |
483 | ligi | 134 | inputs[i].evdev_rel_axis = (int *) calloc( 100, sizeof( int ) ); |
135 | inputs[i].evdev_button = (char *)calloc( 500, sizeof( char ) ); |
||
130 | ligi | 136 | |
483 | ligi | 137 | if ((inputs[i].evdev_out_fd = open(inputs[i].fname, O_WRONLY)) < 0) |
138 | { |
||
139 | printf(" cant open %s for writing\n",inputs[i].fname); |
||
140 | |||
141 | } |
||
142 | |||
143 | if ((inputs[i].evdev_in_fd = open(inputs[i].fname, O_RDONLY)) < 0) |
||
144 | { |
||
145 | printf(" cant open %s for reading!",inputs[i].fname); |
||
146 | return 0; |
||
147 | } |
||
130 | ligi | 148 | |
483 | ligi | 149 | ioctl(inputs[i].evdev_in_fd ,EVIOCGID,&device_info); |
130 | ligi | 150 | |
483 | ligi | 151 | printf("vendor 0x%04hx product 0x%04hx version 0x%04hx \n", |
152 | device_info.vendor, device_info.product, |
||
153 | device_info.version); |
||
130 | ligi | 154 | |
155 | |||
483 | ligi | 156 | uint8_t evtype_bitmask[EV_MAX/8 + 1]; |
157 | |||
158 | if(ioctl(inputs[i].evdev_in_fd , EVIOCGBIT(0, sizeof(evtype_bitmask)), evtype_bitmask) < 0) |
||
159 | perror("evdev ioctl"); |
||
160 | |||
161 | printf("Supported event types:\n"); |
||
162 | |||
163 | int i; |
||
164 | for (i = 0; i < EV_MAX; i++) { |
||
165 | if (test_bit(i, evtype_bitmask)) { |
||
166 | /* this means that the bit is set in the event types list */ |
||
167 | printf(" Event type 0x%02x ", i); |
||
168 | |||
169 | print_event_str(i); |
||
170 | }} |
||
171 | } |
||
130 | ligi | 172 | return 1; |
173 | } |
||
174 | |||
483 | ligi | 175 | /* |
130 | ligi | 176 | int init_evdevstatus_led() |
177 | { |
||
178 | led_event.type=EV_LED; |
||
179 | led_event.code = LED_MISC; |
||
180 | led_event.value = 1; |
||
181 | if (evdev_out_fd) |
||
182 | retval = write(evdev_out_fd, &led_event, sizeof(struct input_event)); |
||
183 | return 1; |
||
184 | } |
||
185 | |||
186 | int blink_evdev_led() |
||
187 | { |
||
188 | |||
189 | if (evdev_out_fd) |
||
190 | { |
||
191 | if (led_event.value) |
||
192 | led_event.value = 0; |
||
483 | ligi | 193 | else |
130 | ligi | 194 | led_event.value = 1 ; |
195 | |||
196 | retval = write(evdev_out_fd, &led_event, sizeof(struct input_event)); |
||
197 | } |
||
198 | return 1; |
||
199 | } |
||
483 | ligi | 200 | */ |
130 | ligi | 201 | |
202 | int poll_evdev() |
||
203 | { |
||
204 | |||
483 | ligi | 205 | printf("polling evdev\n"); |
206 | int i; |
||
207 | for ( i =0 ; i< input_count;i++) |
||
130 | ligi | 208 | { |
483 | ligi | 209 | struct timeval tv = {0,5}; |
130 | ligi | 210 | |
483 | ligi | 211 | fd_set rfds; |
212 | FD_ZERO(&rfds); |
||
213 | FD_SET(inputs[i].evdev_in_fd,&rfds); |
||
214 | |||
130 | ligi | 215 | |
483 | ligi | 216 | |
217 | retval = select(inputs[i].evdev_in_fd+1, &rfds, NULL, NULL, &tv); |
||
218 | |||
219 | if (retval==-1) |
||
220 | printf("error in select!!!!!!!!\n"); |
||
221 | else if (retval) |
||
130 | ligi | 222 | { |
483 | ligi | 223 | read_bytes = read(inputs[i].evdev_in_fd, ev, sizeof(struct input_event) * 64); |
224 | |||
225 | if (read_bytes < (int) sizeof(struct input_event)) { |
||
226 | perror("evtest: short read"); |
||
227 | exit (1); |
||
130 | ligi | 228 | |
483 | ligi | 229 | } |
230 | |||
231 | for (counter = 0; counter < (int) (read_bytes / sizeof(struct input_event)); counter++) |
||
232 | { |
||
233 | //print_event_str(ev[counter].type); |
||
234 | // printf(" code:%d val:%d \n",ev[counter].code,ev[counter].value); |
||
235 | |||
236 | // if (ev[counter].type==EV_REL) evdev_rel_axis[ ev[counter].code]= ev[counter].value; |
||
237 | // for logitech problem |
||
238 | if (ev[counter].type==EV_REL) inputs[i].evdev_rel_axis[ ev[counter].code]= ev[counter].value; |
||
239 | if (ev[counter].type==EV_KEY) inputs[i].evdev_button[ ev[counter].code]= ev[counter].value; |
||
240 | } |
||
241 | |||
242 | |||
243 | for (counter=0;counter<20;counter++) |
||
244 | printf("A%d %d -" , counter, inputs[i].evdev_rel_axis[counter] ); |
||
245 | printf("\n"); |
||
246 | |||
247 | for (counter=0;counter<10;counter++) |
||
248 | printf("B%d %d -" , counter, inputs[i].evdev_button[counter] ); |
||
249 | // printf("input read done: nick:%d roll:%d gier:%d gas:%d\n",axis[3] , axis[4] , axis[5] , axis[2]); |
||
130 | ligi | 250 | } |
483 | ligi | 251 | else |
252 | printf("no data from evdev data from evdev: \n"); |
||
130 | ligi | 253 | } |
254 | return 1; |
||
255 | } |