HIDDEV examples

Note

These complete examples will compile with gcc -Wall -W.

If you have updated your linux kernel, and have not updated your header files, you may need to use the -I option to pick up the version of hiddev.h from your new kernel. For example, if you have the kernel in a directory /a/b/c, then you would compile the first example with the command gcc -Wall -W -I/a/b/c/include example1.c -o example1. Naturally, you replace /a/b/c with the directory you really used.

Example 1. HIDIOCVERSION example

/*
 * 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 <linux/hiddev.h>


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

  int fd = -1;
  int version;

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

  /* ioctl() accesses the underlying driver */
  ioctl(fd, HIDIOCGVERSION, &version);

  /* the HIDIOCGVERSION ioctl() returns an int */
  /* so we unpack it and display it */
  printf("hiddev driver version is %d.%d.%d\n",
	 version >> 16, (version >> 8) & 0xff, version & 0xff);

  close(fd);

  exit(0);
}

Example 2. HIDIOCGDEVINFO example

/*
 * 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 <linux/hiddev.h>


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

  int fd = -1;
  struct hiddev_devinfo device_info;

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

  /* suck out some device information */
  ioctl(fd, HIDIOCGDEVINFO, &device_info);

  /* the HIDIOCGDEVINFO ioctl() returns hiddev_devinfo
   * structure - see <linux/hiddev.h> 
   * So we work through the various elements, displaying 
   * each of them 
   */
  printf("vendor 0x%04hx product 0x%04hx version 0x%04hx ",
          device_info.vendor, device_info.product, device_info.version);
  printf("has %i application%s ", device_info.num_applications,
         (device_info.num_applications==1?"":"s"));
  printf("and is on bus: %d devnum: %d ifnum: %d\n",
         device_info.busnum, device_info.devnum, device_info.ifnum);

  close(fd);

  exit(0);
}

Example 3. HIDIOCAPPLICATION example

/*
 * 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 <linux/hiddev.h>


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

  int fd = -1;
  unsigned int yalv;
  int appl;
  struct hiddev_devinfo device_info;

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

  /* suck out some device information */
  ioctl(fd, HIDIOCGDEVINFO, &device_info);

 /* Now that we have the number of applications (in the
  * device_info.num_applications field), 
  * we can retrieve them using the HIDIOCAPPLICATION ioctl()
  * applications are indexed from 0..{num_applications-1}
  */
  for (yalv = 0; yalv < device_info.num_applications; yalv++) {
    appl = ioctl(fd, HIDIOCAPPLICATION, yalv);
    if (appl > 0) {
	printf("Application %i is 0x%x ", yalv, appl);
	/* The magic values come from various usage table specs */
	switch ( appl >> 16)
	    {
	    case 0x01 :
		printf("(Generic Desktop Page)\n");
		break;
	    case 0x0c :
		printf("(Consumer Product Page)\n");
		break;
	    case 0x80 :
		printf("(USB Monitor Page)\n");
		break;
	    case 0x81 :
		printf("(USB Enumerated Values Page)\n");
		break;
	    case 0x82 :
		printf("(VESA Virtual Controls Page)\n");
		break;
	    case 0x83 :
		printf("(Reserved Monitor Page)\n");
		break;
	    case 0x84 :
		printf("(Power Device Page)\n");
		break;
	    case 0x85 :
		printf("(Battery System Page)\n");
		break;
	    case 0x86 :
	    case 0x87 :
		printf("(Reserved Power Device Page)\n");
		break;
	    default :
		printf("(Unknown page - needs to be added)\n");
	    }
    }
  }

  close(fd);

  exit(0);
}