Subversion Repositories Projects

Rev

Rev 140 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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