Linux系统编程应用 Linux输入子系统(二)

Posted itcast0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux系统编程应用 Linux输入子系统(二)相关的知识,希望对你有一定的参考价值。


1. 设备ID信息结构体

结构体如下:

/*
* IOCTLs (0x00 - 0x7f)
*/

struct input_id
__u16 bustype;
__u16 vendor;
__u16 product;
__u16 version;
;

struct input_absinfo
__s32 value;
__s32 minimum;
__s32 maximum;
__s32 fuzz;
__s32 flat;
;


2. 相关ioctl操作

#define EVIOCGVERSION       _IOR(E, 0x01, int)            /* get driver version */
#define EVIOCGID _IOR(E, 0x02, struct input_id) /* get device ID */
#define EVIOCGREP _IOR(E, 0x03, int[2]) /* get repeat settings */
#define EVIOCSREP _IOW(E, 0x03, int[2]) /* set repeat settings */
#define EVIOCGKEYCODE _IOR(E, 0x04, int[2]) /* get keycode */
#define EVIOCSKEYCODE _IOW(E, 0x04, int[2]) /* set keycode */

#define EVIOCGNAME(len) _IOC(_IOC_READ, E, 0x06, len) /* get device name */
#define EVIOCGPHYS(len) _IOC(_IOC_READ, E, 0x07, len) /* get physical location */
#define EVIOCGUNIQ(len) _IOC(_IOC_READ, E, 0x08, len) /* get unique identifier */

#define EVIOCGKEY(len) _IOC(_IOC_READ, E, 0x18, len) /* get global keystate */
#define EVIOCGLED(len) _IOC(_IOC_READ, E, 0x19, len) /* get all LEDs */
#define EVIOCGSND(len) _IOC(_IOC_READ, E, 0x1a, len) /* get all sounds status */
#define EVIOCGSW(len) _IOC(_IOC_READ, E, 0x1b, len) /* get all switch states */

#define EVIOCGBIT(ev,len) _IOC(_IOC_READ, E, 0x20 + ev, len) /* get event bits */
#define EVIOCGABS(abs) _IOR(E, 0x40 + abs, struct input_absinfo) /* get abs value/limits */
#define EVIOCSABS(abs) _IOW(E, 0xc0 + abs, struct input_absinfo) /* set abs value/limits */

#define EVIOCSFF _IOC(_IOC_WRITE, E, 0x80, sizeof(struct ff_effect)) /* send a force effect to a force feedback device */
#define EVIOCRMFF _IOW(E, 0x81, int) /* Erase a force effect */
#define EVIOCGEFFECTS _IOR(E, 0x84, int) /* Report number of effects playable at the same time */

#define EVIOCGRAB _IOW(E, 0x90, int) /* Grab/Release device */


3. 相关ID值

/*
* IDs.
*/

#define ID_BUS 0
#define ID_VENDOR 1
#define ID_PRODUCT 2
#define ID_VERSION 3

#define BUS_PCI 0x01
#define BUS_ISAPNP 0x02
#define BUS_USB 0x03
#define BUS_HIL 0x04
#define BUS_BLUETOOTH 0x05

#define BUS_ISA 0x10
#define BUS_I8042 0x11
#define BUS_XTKBD 0x12
#define BUS_RS232 0x13
#define BUS_GAMEPORT 0x14
#define BUS_PARPORT 0x15
#define BUS_AMIGA 0x16
#define BUS_ADB 0x17
#define BUS_I2C 0x18
#define BUS_HOST 0x19
#define BUS_GSC 0x1A


4. 相关ioctl操作示例

参考代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>

#define SIZE 128

int main(void)

int ret = -1;
int fd = -1;
int value;
int repeat[2];

struct input_id id;
char name[SIZE];

fd = open("/dev/input/event0", O_RDONLY);
if (-1 == fd)

perror("open");
goto err0;


//get driver version
value = 0;
ret = ioctl(fd, EVIOCGVERSION, &value);
if (-1 == ret)

perror("ioctl");
goto err0;

printf("version: %d\\n", value);

//get device ID
memset(&id, 0, sizeof(id));
ret = ioctl(fd, EVIOCGID, &id);
if (-1 == ret)

perror("ioctl");
goto err0;

printf("bustype: %hu vendor: %hu product: %hu version: %hu\\n", id.bustype, id.vendor, id.product, id.version);

//get or set repead
memset(repeat, 0, sizeof(repeat));
ret = ioctl(fd, EVIOCGREP, repeat);
if (-1 == ret)

perror("ioctl");
goto err0;

printf("repeat[0]: %d repeat[1]: %d\\n", repeat[0], repeat[1]);

//get device name
ret = ioctl(fd, EVIOCGNAME(SIZE), name);
if (-1 == ret)

perror("ioctl");
goto err0;

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

//get physical location
memset(name, 0, SIZE);
ret = ioctl(fd, EVIOCGPHYS(SIZE), name);
if (-1 == ret)

perror("ioctl");
goto err0;

printf("physical location: %s\\n", name);

//get unique identifier
memset(name, 0, SIZE);
ret = ioctl(fd, EVIOCGUNIQ(SIZE), name);
if (-1 == ret)

perror("ioctl");
goto err0;

printf("unique identifier: %s\\n", name);


close(fd);

return 0;
err0:
return -1;


执行结果:

【Linux系统编程应用】



以上是关于Linux系统编程应用 Linux输入子系统(二)的主要内容,如果未能解决你的问题,请参考以下文章

Linux input子系统input设备应用编程

Linux系统编程应用 Linux Input子系统

02_先学习输入系统应用编程

Linux系统GPIO应用编程

LINUX系统编程之进程

Linux高性能server编程——信号及应用