Subversion Repositories Projects

Rev

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

#include "evdev_handler.h"

int evdev_fd;
int evdev_out_fd;
int retval;
size_t read_bytes;  /* how many bytes were read */
struct input_event ev[64]; /* the events (up to 64 at once) */
struct input_event led_event;



int  *evdev_rel_axis=NULL;
char *evdev_button=NULL;

int counter;

int connect_evdev(char* input_evdev_name)
{
  evdev_rel_axis = (int *) calloc( 100, sizeof( int ) );
  evdev_button = (char *)calloc( 100, sizeof( char ) );

  struct input_devinfo {
    uint16_t bustype;
    uint16_t vendor;
    uint16_t product;
    uint16_t version;
  };
  struct input_devinfo device_info;

  if ((evdev_out_fd = open(input_evdev_name, O_WRONLY)) < 0)
    {
      printf(" cant open %s for writing\n",input_evdev_name);
     
    }

  if ((evdev_fd = open(input_evdev_name, O_RDONLY)) < 0)
    {
      printf(" cant open %s for reading!",input_evdev_name);
      return 0;
    }
 
  ioctl(evdev_fd,EVIOCGID,&device_info);
   
  printf("vendor 0x%04hx product 0x%04hx version 0x%04hx \n",
         device_info.vendor, device_info.product,
         device_info.version);


  char name[256]= "Unknown";

  if(ioctl(evdev_fd, EVIOCGNAME(sizeof(name)), name) < 0) {
    perror("evdev ioctl");
  }

  printf("EVDEV reports name: %s\n", name);

  /* this macro is used to tell if "bit" is set in "array"
   * it selects a byte from the array, and does a boolean AND
   * operation with a byte that only has the relevant bit set.
   * eg. to check for the 12th bit, we do (array[1] & 1<<4)
   */



    uint8_t evtype_bitmask[EV_MAX/8 + 1];

    if(ioctl(evdev_fd, EVIOCGBIT(0, sizeof(evtype_bitmask)), evtype_bitmask) < 0)
      perror("evdev ioctl");

     printf("Supported event types:\n");

     int i;
     for (i = 0; i < EV_MAX; i++) {
       if (test_bit(i, evtype_bitmask)) {
         /* this means that the bit is set in the event types list */
         printf("  Event type 0x%02x ", i);
         switch ( i)
           {
           case EV_KEY :
             printf(" (Keys or Buttons)\n");
             break;
           case EV_ABS :
             printf(" (Absolute Axes)\n");
             break;
           case EV_LED :
             printf(" (LEDs)\n");
             break;
           case EV_REP :
             printf(" (Repeat)\n");
             break;
           case EV_SYN :
             printf(" (Sync?)\n");
             break;
           case EV_REL :
             printf(" (Relative Axis)\n");
             break;
           case EV_MSC :
             printf(" (Misc)\n");
             break;
           default:
             printf(" (Unknown event type: 0x%04hx)\n", i);
           }}}

  return 1;
}

int init_evdevstatus_led()
{
  led_event.type=EV_LED;
  led_event.code = LED_MISC;
  led_event.value = 1;
  if (evdev_out_fd)
    retval = write(evdev_out_fd, &led_event, sizeof(struct input_event));
  return 1;
}

int blink_evdev_led()
{

  if (evdev_out_fd)
    {
      if (led_event.value)
        led_event.value = 0;
      else
        led_event.value = 1 ;
     
      retval = write(evdev_out_fd, &led_event, sizeof(struct input_event));
    }
  return 1;
}

int poll_evdev()
{

  struct timeval tv;

  fd_set rfds;
  FD_ZERO(&rfds);
  FD_SET(evdev_fd,&rfds);

  tv.tv_sec = 0;
  tv.tv_usec = 5;
 
  retval = select(evdev_fd+1, &rfds, NULL, NULL, &tv);
 
  if (retval==-1)
    printf("error in select!!!!!!!!\n");
  else if (retval)
    {
      read_bytes = read(evdev_fd, ev, sizeof(struct input_event) * 64);
     
      if (read_bytes < (int) sizeof(struct input_event)) {
        perror("evtest: short read");
        exit (1);
      }
     
      for (counter = 0; counter < (int) (read_bytes / sizeof(struct input_event)); counter++)
        {
         
          printf("%d type:%d code:%d val:%d \n",counter,ev[counter].type,ev[counter].code,ev[counter].value);
          if (ev[counter].type==EV_REL) evdev_rel_axis[ ev[counter].code]= ev[counter].value;
          if (ev[counter].type==EV_KEY) evdev_button[ ev[counter].code-256]= ev[counter].value;
        }
     
     
      for (counter=0;counter<10;counter++)
        printf("A%d %d -" , counter, evdev_rel_axis[counter] );
      printf("\n");
     
      for (counter=0;counter<10;counter++)
        printf("B%d %d -" , counter, evdev_button[counter] );
      //                printf("input read done: nick:%d roll:%d gier:%d gas:%d\n",axis[3] , axis[4] , axis[5] , axis[2]);
    }
  else
    printf("no data from evdev data from evdev: \n");
 
  return 1;
}