Home | History | Annotate | Download | only in toolbox
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <stdint.h>
      5 #include <fcntl.h>
      6 #include <sys/ioctl.h>
      7 //#include <linux/input.h> // this does not compile
      8 #include <errno.h>
      9 
     10 
     11 // from <linux/input.h>
     12 
     13 struct input_event {
     14 	struct timeval time;
     15 	__u16 type;
     16 	__u16 code;
     17 	__s32 value;
     18 };
     19 
     20 #define EVIOCGVERSION		_IOR('E', 0x01, int)			/* get driver version */
     21 #define EVIOCGID		_IOR('E', 0x02, struct input_id)	/* get device ID */
     22 #define EVIOCGKEYCODE		_IOR('E', 0x04, int[2])			/* get keycode */
     23 #define EVIOCSKEYCODE		_IOW('E', 0x04, int[2])			/* set keycode */
     24 
     25 #define EVIOCGNAME(len)		_IOC(_IOC_READ, 'E', 0x06, len)		/* get device name */
     26 #define EVIOCGPHYS(len)		_IOC(_IOC_READ, 'E', 0x07, len)		/* get physical location */
     27 #define EVIOCGUNIQ(len)		_IOC(_IOC_READ, 'E', 0x08, len)		/* get unique identifier */
     28 
     29 #define EVIOCGKEY(len)		_IOC(_IOC_READ, 'E', 0x18, len)		/* get global keystate */
     30 #define EVIOCGLED(len)		_IOC(_IOC_READ, 'E', 0x19, len)		/* get all LEDs */
     31 #define EVIOCGSND(len)		_IOC(_IOC_READ, 'E', 0x1a, len)		/* get all sounds status */
     32 #define EVIOCGSW(len)		_IOC(_IOC_READ, 'E', 0x1b, len)		/* get all switch states */
     33 
     34 #define EVIOCGBIT(ev,len)	_IOC(_IOC_READ, 'E', 0x20 + ev, len)	/* get event bits */
     35 #define EVIOCGABS(abs)		_IOR('E', 0x40 + abs, struct input_absinfo)		/* get abs value/limits */
     36 #define EVIOCSABS(abs)		_IOW('E', 0xc0 + abs, struct input_absinfo)		/* set abs value/limits */
     37 
     38 #define EVIOCSFF		_IOC(_IOC_WRITE, 'E', 0x80, sizeof(struct ff_effect))	/* send a force effect to a force feedback device */
     39 #define EVIOCRMFF		_IOW('E', 0x81, int)			/* Erase a force effect */
     40 #define EVIOCGEFFECTS		_IOR('E', 0x84, int)			/* Report number of effects playable at the same time */
     41 
     42 #define EVIOCGRAB		_IOW('E', 0x90, int)			/* Grab/Release device */
     43 
     44 // end <linux/input.h>
     45 
     46 
     47 
     48 int sendevent_main(int argc, char *argv[])
     49 {
     50     int i;
     51     int fd;
     52     int ret;
     53     int version;
     54     struct input_event event;
     55 
     56     if(argc != 5) {
     57         fprintf(stderr, "use: %s device type code value\n", argv[0]);
     58         return 1;
     59     }
     60 
     61     fd = open(argv[1], O_RDWR);
     62     if(fd < 0) {
     63         fprintf(stderr, "could not open %s, %s\n", argv[optind], strerror(errno));
     64         return 1;
     65     }
     66     if (ioctl(fd, EVIOCGVERSION, &version)) {
     67         fprintf(stderr, "could not get driver version for %s, %s\n", argv[optind], strerror(errno));
     68         return 1;
     69     }
     70     memset(&event, 0, sizeof(event));
     71     event.type = atoi(argv[2]);
     72     event.code = atoi(argv[3]);
     73     event.value = atoi(argv[4]);
     74     ret = write(fd, &event, sizeof(event));
     75     if(ret < sizeof(event)) {
     76         fprintf(stderr, "write event failed, %s\n", strerror(errno));
     77         return -1;
     78     }
     79     return 0;
     80 }
     81