Listing 17. Checking Global State by Axis

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */


#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <asm/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>

#include <linux/input.h>

/* 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)
 */
#define test_bit(bit, array)    (array[bit/8] & (1<<(bit%8)))

int main (int argc, char **argv) {

  int fd = -1;
  uint8_t abs_bitmask[ABS_MAX/8 + 1];
  int yalv;
  struct input_absinfo abs_features;

  /* ioctl() requires a file descriptor, so we check we got one, and then open it */
  if (argc != 2) {
    fprintf(stderr, "usage: %s event-device - probably /dev/input/event0\n", argv[0]);
    exit(1);
  }
  if ((fd = open(argv[1], O_RDONLY)) < 0) {
    perror("evdev open");
    exit(1);
  }

  memset(abs_bitmask, 0, sizeof(abs_bitmask));
  if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) < 0) {
      perror("evdev ioctl");
  }

  printf("Supported Absolute axes:\n");

  for (yalv = 0; yalv < ABS_MAX; yalv++) {
      if (test_bit(yalv, abs_bitmask)) {
	  /* this means that the bit is set in the axes list */
	  printf("  Absolute axis 0x%02x ", yalv);
	  switch ( yalv)
	      {
	      case ABS_X :
		  printf(" (X Axis) ");
		  break;
	      case ABS_Y :
		  printf(" (Y Axis) ");
		  break;
	      case ABS_Z :
		  printf(" (Z Axis) ");
		  break;
	      case ABS_RX :
		  printf(" (X Rate Axis) ");
		  break;
	      case ABS_RY :
		  printf(" (Y Rate Axis) ");
		  break;
	      case ABS_RZ :
		  printf(" (Z Rate Axis) ");
		  break;
	      case ABS_THROTTLE :
		  printf(" (Throttle) ");
		  break;
	      case ABS_RUDDER :
		  printf(" (Rudder) ");
		  break;
	      case ABS_WHEEL :
		  printf(" (Wheel) ");
		  break;
	      case ABS_GAS :
		  printf(" (Accelerator) ");
		  break;
	      case ABS_BRAKE :
		  printf(" (Brake) ");
		  break;
	      case ABS_HAT0X :
		  printf(" (Hat zero, x axis) ");
		  break;
	      case ABS_HAT0Y :
		  printf(" (Hat zero, y axis) ");
		  break;
	      case ABS_HAT1X :
		  printf(" (Hat one, x axis) ");
		  break;
	      case ABS_HAT1Y :
		  printf(" (Hat one, y axis) ");
		  break;
	      case ABS_HAT2X :
		  printf(" (Hat two, x axis) ");
		  break;
	      case ABS_HAT2Y :
		  printf(" (Hat two, y axis) ");
		  break;
	      case ABS_HAT3X :
		  printf(" (Hat three, x axis) ");
		  break;
	      case ABS_HAT3Y :
		  printf(" (Hat three, y axis) ");
		  break;
	      case ABS_PRESSURE :
		  printf(" (Pressure) ");
		  break;
	      case ABS_DISTANCE :
		  printf(" (Distance) ");
		  break;
	      case ABS_TILT_X :
		  printf(" (Tilt, X axis) ");
		  break;
	      case ABS_TILT_Y :
		  printf(" (Tilt, Y axis) ");
		  break;
	      case ABS_MISC :
		  printf(" (Miscellaneous) ");
		  break;
	      default:
		  printf(" (Unknown absolute feature) ");
	      }
	  if(ioctl(fd, EVIOCGABS(yalv), &abs_features)) {
	      perror("evdev EVIOCGABS ioctl");
	  }
	  printf("%d (min: %d, max: %d, flatness: %d, fuzz: %d)\n",
		 abs_features.value,
		 abs_features.minimum,
		 abs_features.maximum,
		 abs_features.flat,
		 abs_features.fuzz);
      }
  }

  close(fd);

  exit(0);
}
