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