Subversion Repositories Projects

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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