Rev 130 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 130 | Rev 140 | ||
---|---|---|---|
1 | #include "evdev_handler.h" |
1 | #include "evdev_handler.h" |
2 | 2 | ||
3 | int evdev_fd; |
3 | int evdev_fd; |
4 | int evdev_out_fd; |
4 | int evdev_out_fd; |
5 | int retval; |
5 | int retval; |
6 | size_t read_bytes; /* how many bytes were read */ |
6 | size_t read_bytes; /* how many bytes were read */ |
7 | struct input_event ev[64]; /* the events (up to 64 at once) */ |
7 | struct input_event ev[64]; /* the events (up to 64 at once) */ |
8 | struct input_event led_event; |
8 | struct input_event led_event; |
9 | 9 | ||
10 | 10 | ||
11 | 11 | ||
12 | int *evdev_rel_axis=NULL; |
12 | int *evdev_rel_axis=NULL; |
13 | char *evdev_button=NULL; |
13 | char *evdev_button=NULL; |
14 | 14 | ||
15 | int counter; |
15 | int counter; |
16 | 16 | ||
17 | int connect_evdev(char* input_evdev_name) |
17 | int connect_evdev(char* input_evdev_name) |
18 | { |
18 | { |
19 | evdev_rel_axis = (int *) calloc( 100, sizeof( int ) ); |
19 | evdev_rel_axis = (int *) calloc( 100, sizeof( int ) ); |
20 | evdev_button = (char *)calloc( 100, sizeof( char ) ); |
20 | evdev_button = (char *)calloc( 100, sizeof( char ) ); |
21 | 21 | ||
22 | struct input_devinfo { |
22 | struct input_devinfo { |
23 | uint16_t bustype; |
23 | uint16_t bustype; |
24 | uint16_t vendor; |
24 | uint16_t vendor; |
25 | uint16_t product; |
25 | uint16_t product; |
26 | uint16_t version; |
26 | uint16_t version; |
27 | }; |
27 | }; |
28 | struct input_devinfo device_info; |
28 | struct input_devinfo device_info; |
29 | 29 | ||
30 | if ((evdev_out_fd = open(input_evdev_name, O_WRONLY)) < 0) |
30 | if ((evdev_out_fd = open(input_evdev_name, O_WRONLY)) < 0) |
31 | { |
31 | { |
32 | printf(" cant open %s for writing\n",input_evdev_name); |
32 | printf(" cant open %s for writing\n",input_evdev_name); |
33 | 33 | ||
34 | } |
34 | } |
35 | 35 | ||
36 | if ((evdev_fd = open(input_evdev_name, O_RDONLY)) < 0) |
36 | if ((evdev_fd = open(input_evdev_name, O_RDONLY)) < 0) |
37 | { |
37 | { |
38 | printf(" cant open %s for reading!",input_evdev_name); |
38 | printf(" cant open %s for reading!",input_evdev_name); |
39 | return 0; |
39 | return 0; |
40 | } |
40 | } |
41 | 41 | ||
42 | ioctl(evdev_fd,EVIOCGID,&device_info); |
42 | ioctl(evdev_fd,EVIOCGID,&device_info); |
43 | 43 | ||
44 | printf("vendor 0x%04hx product 0x%04hx version 0x%04hx \n", |
44 | printf("vendor 0x%04hx product 0x%04hx version 0x%04hx \n", |
45 | device_info.vendor, device_info.product, |
45 | device_info.vendor, device_info.product, |
46 | device_info.version); |
46 | device_info.version); |
47 | 47 | ||
48 | 48 | ||
49 | char name[256]= "Unknown"; |
49 | char name[256]= "Unknown"; |
50 | 50 | ||
51 | if(ioctl(evdev_fd, EVIOCGNAME(sizeof(name)), name) < 0) { |
51 | if(ioctl(evdev_fd, EVIOCGNAME(sizeof(name)), name) < 0) { |
52 | perror("evdev ioctl"); |
52 | perror("evdev ioctl"); |
53 | } |
53 | } |
54 | 54 | ||
55 | printf("EVDEV reports name: %s\n", name); |
55 | printf("EVDEV reports name: %s\n", name); |
56 | 56 | ||
57 | /* this macro is used to tell if "bit" is set in "array" |
57 | /* this macro is used to tell if "bit" is set in "array" |
58 | * it selects a byte from the array, and does a boolean AND |
58 | * it selects a byte from the array, and does a boolean AND |
59 | * operation with a byte that only has the relevant bit set. |
59 | * operation with a byte that only has the relevant bit set. |
60 | * eg. to check for the 12th bit, we do (array[1] & 1<<4) |
60 | * eg. to check for the 12th bit, we do (array[1] & 1<<4) |
61 | */ |
61 | */ |
62 | 62 | ||
63 | 63 | ||
64 | uint8_t evtype_bitmask[EV_MAX/8 + 1]; |
64 | uint8_t evtype_bitmask[EV_MAX/8 + 1]; |
65 | 65 | ||
66 | if(ioctl(evdev_fd, EVIOCGBIT(0, sizeof(evtype_bitmask)), evtype_bitmask) < 0) |
66 | if(ioctl(evdev_fd, EVIOCGBIT(0, sizeof(evtype_bitmask)), evtype_bitmask) < 0) |
67 | perror("evdev ioctl"); |
67 | perror("evdev ioctl"); |
68 | 68 | ||
69 | printf("Supported event types:\n"); |
69 | printf("Supported event types:\n"); |
70 | 70 | ||
71 | int i; |
71 | int i; |
72 | for (i = 0; i < EV_MAX; i++) { |
72 | for (i = 0; i < EV_MAX; i++) { |
73 | if (test_bit(i, evtype_bitmask)) { |
73 | if (test_bit(i, evtype_bitmask)) { |
74 | /* this means that the bit is set in the event types list */ |
74 | /* this means that the bit is set in the event types list */ |
75 | printf(" Event type 0x%02x ", i); |
75 | printf(" Event type 0x%02x ", i); |
76 | switch ( i) |
76 | switch ( i) |
77 | { |
77 | { |
78 | case EV_KEY : |
78 | case EV_KEY : |
79 | printf(" (Keys or Buttons)\n"); |
79 | printf(" (Keys or Buttons)\n"); |
80 | break; |
80 | break; |
81 | case EV_ABS : |
81 | case EV_ABS : |
82 | printf(" (Absolute Axes)\n"); |
82 | printf(" (Absolute Axes)\n"); |
83 | break; |
83 | break; |
84 | case EV_LED : |
84 | case EV_LED : |
85 | printf(" (LEDs)\n"); |
85 | printf(" (LEDs)\n"); |
86 | break; |
86 | break; |
87 | case EV_REP : |
87 | case EV_REP : |
88 | printf(" (Repeat)\n"); |
88 | printf(" (Repeat)\n"); |
89 | break; |
89 | break; |
90 | case EV_SYN : |
90 | case EV_SYN : |
91 | printf(" (Sync?)\n"); |
91 | printf(" (Sync?)\n"); |
92 | break; |
92 | break; |
93 | case EV_REL : |
93 | case EV_REL : |
94 | printf(" (Relative Axis)\n"); |
94 | printf(" (Relative Axis)\n"); |
95 | break; |
95 | break; |
96 | case EV_MSC : |
96 | case EV_MSC : |
97 | printf(" (Misc)\n"); |
97 | printf(" (Misc)\n"); |
98 | break; |
98 | break; |
99 | default: |
99 | default: |
100 | printf(" (Unknown event type: 0x%04hx)\n", i); |
100 | printf(" (Unknown event type: 0x%04hx)\n", i); |
101 | }}} |
101 | }}} |
102 | 102 | ||
103 | return 1; |
103 | return 1; |
104 | } |
104 | } |
105 | 105 | ||
106 | int init_evdevstatus_led() |
106 | int init_evdevstatus_led() |
107 | { |
107 | { |
108 | led_event.type=EV_LED; |
108 | led_event.type=EV_LED; |
109 | led_event.code = LED_MISC; |
109 | led_event.code = LED_MISC; |
110 | led_event.value = 1; |
110 | led_event.value = 1; |
111 | if (evdev_out_fd) |
111 | if (evdev_out_fd) |
112 | retval = write(evdev_out_fd, &led_event, sizeof(struct input_event)); |
112 | retval = write(evdev_out_fd, &led_event, sizeof(struct input_event)); |
113 | return 1; |
113 | return 1; |
114 | } |
114 | } |
115 | 115 | ||
116 | int blink_evdev_led() |
116 | int blink_evdev_led() |
117 | { |
117 | { |
118 | 118 | ||
119 | if (evdev_out_fd) |
119 | if (evdev_out_fd) |
120 | { |
120 | { |
121 | if (led_event.value) |
121 | if (led_event.value) |
122 | led_event.value = 0; |
122 | led_event.value = 0; |
123 | else |
123 | else |
124 | led_event.value = 1 ; |
124 | led_event.value = 1 ; |
125 | 125 | ||
126 | retval = write(evdev_out_fd, &led_event, sizeof(struct input_event)); |
126 | retval = write(evdev_out_fd, &led_event, sizeof(struct input_event)); |
127 | } |
127 | } |
128 | return 1; |
128 | return 1; |
129 | } |
129 | } |
130 | 130 | ||
131 | int poll_evdev() |
131 | int poll_evdev() |
132 | { |
132 | { |
133 | 133 | ||
134 | struct timeval tv; |
134 | struct timeval tv; |
135 | 135 | ||
136 | fd_set rfds; |
136 | fd_set rfds; |
137 | FD_ZERO(&rfds); |
137 | FD_ZERO(&rfds); |
138 | FD_SET(evdev_fd,&rfds); |
138 | FD_SET(evdev_fd,&rfds); |
139 | 139 | ||
140 | tv.tv_sec = 0; |
140 | tv.tv_sec = 0; |
141 | tv.tv_usec = 5; |
141 | tv.tv_usec = 5; |
142 | 142 | ||
143 | retval = select(evdev_fd+1, &rfds, NULL, NULL, &tv); |
143 | retval = select(evdev_fd+1, &rfds, NULL, NULL, &tv); |
144 | 144 | ||
145 | if (retval==-1) |
145 | if (retval==-1) |
146 | printf("error in select!!!!!!!!\n"); |
146 | printf("error in select!!!!!!!!\n"); |
147 | else if (retval) |
147 | else if (retval) |
148 | { |
148 | { |
149 | read_bytes = read(evdev_fd, ev, sizeof(struct input_event) * 64); |
149 | read_bytes = read(evdev_fd, ev, sizeof(struct input_event) * 64); |
150 | 150 | ||
151 | if (read_bytes < (int) sizeof(struct input_event)) { |
151 | if (read_bytes < (int) sizeof(struct input_event)) { |
152 | perror("evtest: short read"); |
152 | perror("evtest: short read"); |
153 | exit (1); |
153 | exit (1); |
154 | } |
154 | } |
155 | 155 | ||
156 | for (counter = 0; counter < (int) (read_bytes / sizeof(struct input_event)); counter++) |
156 | for (counter = 0; counter < (int) (read_bytes / sizeof(struct input_event)); counter++) |
157 | { |
157 | { |
158 | 158 | ||
159 | printf("%d type:%d code:%d val:%d \n",counter,ev[counter].type,ev[counter].code,ev[counter].value); |
159 | printf("%d type:%d code:%d val:%d \n",counter,ev[counter].type,ev[counter].code,ev[counter].value); |
- | 160 | ||
- | 161 | // if (ev[counter].type==EV_REL) evdev_rel_axis[ ev[counter].code]= ev[counter].value; |
|
- | 162 | // for logitech problem |
|
160 | if (ev[counter].type==EV_REL) evdev_rel_axis[ ev[counter].code]= ev[counter].value; |
163 | if (ev[counter].type==EV_REL) evdev_rel_axis[ ev[counter].code]= ev[counter].value; |
161 | if (ev[counter].type==EV_KEY) evdev_button[ ev[counter].code-256]= ev[counter].value; |
164 | if (ev[counter].type==EV_KEY) evdev_button[ ev[counter].code-256]= ev[counter].value; |
162 | } |
165 | } |
163 | 166 | ||
164 | 167 | ||
165 | for (counter=0;counter<10;counter++) |
168 | for (counter=0;counter<20;counter++) |
166 | printf("A%d %d -" , counter, evdev_rel_axis[counter] ); |
169 | printf("A%d %d -" , counter, evdev_rel_axis[counter] ); |
167 | printf("\n"); |
170 | printf("\n"); |
168 | 171 | ||
169 | for (counter=0;counter<10;counter++) |
172 | for (counter=0;counter<10;counter++) |
170 | printf("B%d %d -" , counter, evdev_button[counter] ); |
173 | printf("B%d %d -" , counter, evdev_button[counter] ); |
171 | // printf("input read done: nick:%d roll:%d gier:%d gas:%d\n",axis[3] , axis[4] , axis[5] , axis[2]); |
174 | // printf("input read done: nick:%d roll:%d gier:%d gas:%d\n",axis[3] , axis[4] , axis[5] , axis[2]); |
172 | } |
175 | } |
173 | else |
176 | else |
174 | printf("no data from evdev data from evdev: \n"); |
177 | printf("no data from evdev data from evdev: \n"); |
175 | 178 | ||
176 | return 1; |
179 | return 1; |
177 | } |
180 | } |
178 | 181 |