Chinaunix首页 | 论坛 | 博客
  • 博客访问: 699556
  • 博文数量: 255
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 2811
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-09 13:29
个人简介

IT业行者,行者无疆

文章分类

全部博文(255)

文章存档

2011年(121)

2010年(134)

我的朋友

分类: LINUX

2010-09-27 09:04:24

虚拟键盘驱动程序

 
虚拟键盘驱动程序
Saturday, September 12th, 2009 | Author: admin

转载时请注明出处和作者联系方式
文章出处:http://www.limodev.cn/blog
作者联系方式:李先静

前段时间写一个程序,要向系统中注入按键事件,我又不想信赖于具体的GUI,就写一个虚拟键盘设备驱动程序,感觉挺好用的,不过后来发现linux其实有一个uinput驱动程序实现了类似的功能。幸好后来才发现uinput这玩意儿,否则就没有机会去写了这个程序了,有时真是宁愿知识面窄一点,这样才有机会重复发明一些轮子,从中获得一些乐趣。

o 先写一个字符驱动程序。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include     
#include
#include
#include
#include
#include
int vkeyboard_major = 201;
module_param(vkeyboard_major, int, 0);
MODULE_AUTHOR("Li XianJing ");
MODULE_LICENSE("GPL");

static struct input_dev* vkeyboard_idev = NULL;

int vkeyboard_open(struct inode* inode, struct file* filp)
{
return 0;
}

int vkeyboard_release(struct inode* inode, struct file* filp)
{
return 0;
}

ssize_t vkeyboard_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos)
{
printk(KERN_INFO"%s\n", __func__);
return count;
}

struct keyboard_event
{
int press;
int key;
};

ssize_t vkeyboard_write(struct file* filp, const char __user * buf, size_t count, loff_t* f_pos)
{
int ret = 0;
struct keyboard_event event;

while(ret < count)
{
if(copy_from_user(&event, buf + ret, sizeof(event)))
{
return -EFAULT;
}
ret += sizeof(event);

input_event(vkeyboard_idev, EV_MSC, MSC_SCAN, event.key);
input_report_key(vkeyboard_idev, event.key, event.press);
input_sync (vkeyboard_idev);

printk(KERN_INFO"%s p=%d key=%d with scan code\n", __func__, event.press, event.key);
}

return ret;
}

static struct file_operations vkeyboard_fops =
{
.owner = THIS_MODULE,
.open    = vkeyboard_open,
.release = vkeyboard_release,
.read    = vkeyboard_read,
.write   = vkeyboard_write,
};

static int __init vkeyboard_init(void)
{
int result = register_chrdev(vkeyboard_major, "vkeyboard", &vkeyboard_fops);
vkeyboard_input_dev_setup();

return result;
}

static void __exit vkeyboard_cleanup(void)
{
input_unregister_device(vkeyboard_idev);
unregister_chrdev(vkeyboard_major, "vkeyboard");

return;
}

module_init(vkeyboard_init);
module_exit(vkeyboard_cleanup);

o 再实现一个虚拟输入设备。

static int vkeyboard_input_dev_open(struct input_dev* idev)
{
printk(KERN_INFO"%s\n", __func__);

return 0;
}

static void vkeyboard_input_dev_close(struct input_dev* idev)
{
printk(KERN_INFO"%s\n", __func__);

return;
}

static int vkeyboard_input_dev_setup(void)
{
int ret = 0;
int i = 0;
vkeyboard_idev = input_allocate_device();

if(vkeyboard_idev == NULL)
{
return -ENOMEM;
}

vkeyboard_idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) | BIT_MASK(EV_MSC);
vkeyboard_idev->mscbit[0] = BIT_MASK(MSC_SCAN) | BIT_MASK(MSC_SERIAL) | BIT_MASK(MSC_RAW);

bitmap_fill(vkeyboard_idev->keybit, KEY_MAX);
bitmap_fill(vkeyboard_idev->relbit, REL_MAX);
bitmap_fill(vkeyboard_idev->absbit, ABS_MAX);                

vkeyboard_idev->name = "vkeyboard";
vkeyboard_idev->phys = "vkeyboard/input0";
vkeyboard_idev->open = vkeyboard_input_dev_open;
vkeyboard_idev->close = vkeyboard_input_dev_close;

for(i = 32; i < KEY_MAX; i++)
{
input_set_capability(vkeyboard_idev, EV_KEY, i);
}
__set_bit(EV_KEY, vkeyboard_idev->evbit);

ret = input_register_device(vkeyboard_idev);

return ret;
}

o Makefile

obj-m := vkeyboard.o

KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
install:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules_install
clean:
rm -f *.mod.c *.ko *.o Module.markers  modules.order  Module.symvers

加载模块后,向/dev/vkeyboard里写入要注入的事件,事件将会转发到/dev/input/eventX输入设备文件里,系统会从这里自动读取事件。
阅读(2354) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~