Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4042609
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: 嵌入式

2012-12-02 15:30:56

uinput is a linux kernel module that allows to handle the input subsystem from user land. It can be used to create and to handle input devices from an application. It creates a character device in /dev/input directory. The device is a virtual interface, it doesn't belong to a physical device.

In this document, we will see how to create a such input device and how it can be used.


1. Creating an input device

Once the uinput module is installed (via modprobe or insmod), a character device is created, named as /dev/input/uinput (or /dev/uinput on some systems). This device represents the interface between the application and the kernel input subsystem.

To use uinput, we need to open the character device in write-only and non-blocking mode:

#include
#include
...

int fd;
fd = open("/dev/input/uinput", O_WRONLY | O_NONBLOCK);
if(fd < 0)
{ ... exit(EXIT_FAILURE); }

Now the device is opened, we will configure our input device. First, we need to inform the input subsystem which types of input event we want to use. Types of events are defined in /usr/include/linux/input.h:

/usr/include/linux/input.h
...
#define EV_KEY 0x01
#define EV_REL 0x02
#define EV_ABS 0x03 ...
  • EV_KEY type represents key press and release events,
  • EV_REL type represents relative axis events (such as mouse movements),
  • EV_ABS type represents absolute axis events (such as touchscreen movements),

The ioctl request UI_SET_EVBIT applied on the uinput file descriptor is used to enable a type of event. The two following lines enable key press/release and synchronization events.

ret = ioctl(fd, UI_SET_EVBIT, EV_KEY);
...
ret = ioctl(fd, UI_SET_EVBIT, EV_SYN);

When enabling EV_KEY events, we need to describe which keycodes are allowed to be sent via the input subsystem.

As linux/input.h defines the 'd' key as KEY_D, we can enable the keycode representing the 'd' key by using:

ret = ioctl(fd, UI_SET_KEYBIT, KEY_D);
 ...

Now some basic features have been enabled, we need to finish the configuration by using the struct uinput_user_dev from linux/uinput.h. This structure is defined as:

/usr/include/linux/uinput.h
#define UINPUT_MAX_NAME_SIZE 80
struct uinput_user_dev
{
char name[UINPUT_MAX_NAME_SIZE];
struct input_id id;
int ff_effects_max;
 int absmax[ABS_MAX + 1];
 int absmin[ABS_MAX + 1];
 int absfuzz[ABS_MAX + 1];
int absflat[ABS_MAX + 1];
 };

The most important fields are:

  • name is the given name to the input device we will create,
  • id is a linux internal structure that describes the device bustype, vendor id, product id and version,
  • absmin and absmax are integer array that defines mininal and maximal values for an absolute axis (i.e absmin[ABS_X] = 0, absmax[ABS_X] = 1024 for the X axis on a touchscreen device).

Now, we can fill this structure with appropriate values:

struct uinput_user_dev uidev;
 memset(&uidev, 0, sizeof(uidev));
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
uidev.id.bustype = BUS_USB;
uidev.id.vendor = 0x1234;
uidev.id.product = 0xfedc;
uidev.id.version = 1;

Then, we write this structure in the uinput file descriptor.

ret = write(fd, &uidev, sizeof(uidev));

Last step is to request the creation of the device via the UI_DEV_CREATE ioctl request on the file descriptor:

ret = ioctl(fd, UI_DEV_CREATE);

Now, the file descriptor fd represents the end-point file descriptor of the new input device.


2. Injecting events in the input subsystem

The following block code injects a key press event in the input subsystem. The input_event structure contains 3 important fields:

  • type: is an event type (EV_KEY, EV_ABS, EV_REL, ...),
  • code: could be either a key code when using EV_KEY, or an axis for EV_ABS and EV_REL,
  • value: may be 1 (press) or 0 (release) for EV_KEY, or any values for others (positive integer for EV_ABS, signed integer for EV_REL, etc…).

To inject a press event on the 'd' key:

struct input_event ev;
memset(&ev, 0, sizeof(ev));
ev.type = EV_KEY;
 ev.code = KEY_D;
ev.value = 1;
 ret = write(fd, &ev, sizeof(ev));


3. Destroying an input device

ret = ioctl(fd, UI_DEV_DESTROY);


4. Handling absolute axis events

If we want to inject absolute events, we first need to activate EV_ABS event and the desired axes support with ioctl requests. The following ioctl requests enable X and Y absolute axes:

ret = ioctl(fd, UI_SET_EVBIT, EV_ABS);
...
 ret = ioctl(fd, UI_SET_ABSBIT, ABS_X);
...
ret = ioctl(fd, UI_SET_ABSBIT, ABS_Y);

Then we need to defined a range of values for each axis with absmin and absmax fields from the uinput_user_dev structure:

uidev.absmin[ABS_X] = 0;
 uidev.absmax[ABS_X] = 1023;

Event injection follows the same method as for any other events.

struct input_event ev[2];
 memset(ev, 0, sizeof(ev));
ev[0].type = EV_ABS;
ev[0].code = ABS_X;
ev[0].value = 1023;
 ev[1].type = EV_ABS;
ev[1].code = ABS_Y;
ev[1].value = 767;
ret = write(fd, ev, sizeof(ev));


5. Sample code

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <linux/input.h>
  8. #include <linux/uinput.h>

  9. #define die(str, args...) do { \
  10.         perror(str); \
  11.         exit(EXIT_FAILURE); \
  12.     } while(0)

  13. int
  14. main(void)
  15. {
  16.     int fd;
  17.     struct uinput_user_dev uidev;
  18.     struct input_event ev;
  19.     int dx, dy;
  20.     int i;

  21.     fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
  22.     if(fd < 0)
  23.         die("error: open");

  24.     if(ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0)
  25.         die("error: ioctl");
  26.     if(ioctl(fd, UI_SET_KEYBIT, BTN_LEFT) < 0)
  27.         die("error: ioctl");

  28.     if(ioctl(fd, UI_SET_EVBIT, EV_REL) < 0)
  29.         die("error: ioctl");
  30.     if(ioctl(fd, UI_SET_RELBIT, REL_X) < 0)
  31.         die("error: ioctl");
  32.     if(ioctl(fd, UI_SET_RELBIT, REL_Y) < 0)
  33.         die("error: ioctl");

  34.     memset(&uidev, 0, sizeof(uidev));
  35.     snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
  36.     uidev.id.bustype = BUS_USB;
  37.     uidev.id.vendor = 0x1;
  38.     uidev.id.product = 0x1;
  39.     uidev.id.version = 1;

  40.     if(write(fd, &uidev, sizeof(uidev)) < 0)
  41.         die("error: write");

  42.     if(ioctl(fd, UI_DEV_CREATE) < 0)
  43.         die("error: ioctl");

  44.     sleep(2);

  45.     srand(time(NULL));

  46.     while(1) {
  47.         switch(rand() % 4) {
  48.         case 0:
  49.             dx = -10;
  50.             dy = -1;
  51.             break;
  52.         case 1:
  53.             dx = 10;
  54.             dy = 1;
  55.             break;
  56.         case 2:
  57.             dx = -1;
  58.             dy = 10;
  59.             break;
  60.         case 3:
  61.             dx = 1;
  62.             dy = -10;
  63.             break;
  64.         }

  65.         for(i = 0; i < 20; i++) {
  66.             memset(&ev, 0, sizeof(struct input_event));
  67.             ev.type = EV_REL;
  68.             ev.code = REL_X;
  69.             ev.value = dx;
  70.             if(write(fd, &ev, sizeof(struct input_event)) < 0)
  71.                 die("error: write");

  72.             memset(&ev, 0, sizeof(struct input_event));
  73.             ev.type = EV_REL;
  74.             ev.code = REL_Y;
  75.             ev.value = dy;
  76.             if(write(fd, &ev, sizeof(struct input_event)) < 0)
  77.                 die("error: write");

  78.             memset(&ev, 0, sizeof(struct input_event));
  79.             ev.type = EV_SYN;
  80.             ev.code = 0;
  81.             ev.value = 0;
  82.             if(write(fd, &ev, sizeof(struct input_event)) < 0)
  83.                 die("error: write");

  84.             usleep(15000);
  85.         }

  86.         sleep(5);
  87.     }

  88.     sleep(2);

  89.     if(ioctl(fd, UI_DEV_DESTROY) < 0)
  90.         die("error: ioctl");

  91.     close(fd);

  92.     return 0;
  93. }
阅读(2637) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~