uinput模块分析 -- 2 初步分析驱动代码

 源码位置

kernel/driver/misc/uinput.c

#define UINPUT_NAME		"uinput"
static const struct file_operations uinput_fops = {
	.owner		= THIS_MODULE,
	.open		= uinput_open,
	.release	= uinput_release,
	.read		= uinput_read,
	.write		= uinput_write,
	.poll		= uinput_poll,
	.unlocked_ioctl	= uinput_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl	= uinput_compat_ioctl,
#endif
	.llseek		= no_llseek,
};

static struct miscdevice uinput_misc = {
	.fops		= &uinput_fops,
	.minor		= UINPUT_MINOR,
	.name		= UINPUT_NAME,
};

 从驱动代码看,uinput是注册的混杂设备,节点是/dev/uinput

对于其open,read,write,ioctl这些方法,打算放到下一篇文章和应用一起分析

为了增加一些篇幅,这里简单的看一下uinput_device结构体

struct uinput_device {
	struct input_dev	*dev;
	struct mutex		mutex;
	enum uinput_state	state;
	wait_queue_head_t	waitq;
	unsigned char		ready;
	unsigned char		head;
	unsigned char		tail;
	struct input_event	buff[UINPUT_BUFFER_SIZE];
	unsigned int		ff_effects_max;

	struct uinput_request	*requests[UINPUT_NUM_REQUESTS];
	wait_queue_head_t	requests_waitq;
	spinlock_t		requests_lock;
};

可以看到,uinput是包含input_dev的,另外还有head,tail,buff,看起来又是一套缓冲区,对于request相关的,暂时看不出来什么信息。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个使用C语言编写的uinput模拟绝对坐标的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <linux/uinput.h> int main() { int uinput_fd; struct uinput_user_dev uidev; struct input_event ev; // 打开uinput设备文件 uinput_fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK); if (uinput_fd < 0) { perror("open"); exit(EXIT_FAILURE); } // 设置设备属性 ioctl(uinput_fd, UI_SET_EVBIT, EV_ABS); ioctl(uinput_fd, UI_SET_ABSBIT, ABS_X); ioctl(uinput_fd, UI_SET_ABSBIT, ABS_Y); memset(&uidev, 0, sizeof(uidev)); snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "My uinput device"); uidev.id.bustype = BUS_USB; uidev.id.vendor = 0x1234; uidev.id.product = 0x5678; uidev.id.version = 1; write(uinput_fd, &uidev, sizeof(uidev)); ioctl(uinput_fd, UI_DEV_CREATE); // 模拟绝对坐标输入事件 ev.type = EV_ABS; ev.code = ABS_X; ev.value = 1000; write(uinput_fd, &ev, sizeof(ev)); ev.code = ABS_Y; ev.value = 500; write(uinput_fd, &ev, sizeof(ev)); ev.type = EV_SYN; ev.code = SYN_REPORT; ev.value = 0; write(uinput_fd, &ev, sizeof(ev)); // 关闭uinput设备文件 ioctl(uinput_fd, UI_DEV_DESTROY); close(uinput_fd); return 0; } ``` 这段代码使用了Linux系统提供的uinput用户空间接口,通过ioctl和write系统调用来控制uinput设备。在模拟绝对坐标输入事件时,我们需要构造一个input_event结构体,并设置事件类型、事件码和事件值。最后,我们通过发送SYN_REPORT事件来告诉系统这是一个完整的输入事件。 注意,为了编译通过,你需要在代码文件添加头文件#include <string.h>和#include <linux/input.h>。另外,编译时需要链接libudev库,可以使用以下命令进行编译: ``` gcc -o uinput_abs uinput_abs.c -ludev ``` 其,uinput_abs是编译生成的可执行文件名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dianlong_lee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值