前段时间写一个程序,要向系统中注入按键事件,我又不想信赖于具体的GUI,就写一个虚拟键盘设备驱动程序,感觉挺好用的,不过后来发现linux其实有一个uinput驱动程序实现了类似的功能。幸好后来才发现uinput这玩意儿,否则就没有机会去写了这个程序了,有时真是宁愿知识面窄一点,这样才有机会重复发明一些轮子,从中获得一些乐趣。
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/fcntl.h>
#include <linux/aio.h>
#include <asm/uaccess.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/input.h>
int vkeyboard_major = 201;
module_param(vkeyboard_major, int, 0);
MODULE_AUTHOR("Li XianJing <
xianjimli@hotmail.com>");
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);