Input子系统代码

本文深入探讨Linux操作系统的Input子系统,包括输入驱动的实现原理与应用层交互的示例代码,帮助读者理解如何处理来自键盘、鼠标等设备的输入事件。
摘要由CSDN通过智能技术生成

input驱动代码

#include <linux/input.h>
#include <linux/module.h>
#include <linux/timer.h>
#include <linux/init.h>

static struct input_dev *event_input_dev = NULL;
static struct timer_list timer;

static int event_open(struct input_dev *dev)
{
   
    printk(KERN_INFO "event_open\n");  
    return 0; 
}

static void event_close(struct input_dev *dev)
{
   
    printk(KERN_INFO "event_close\n");   
}


static void   input_event_timer_callback(struct timer_list * list)
{
   
    printk(KERN_INFO "input_event\n");
    static int value = 20;
    input_report_rel(event_input_dev,REL_X,value++);
    input_report_rel(event_input_dev,REL_Y,value++);
    input_sync(event_input_dev);
    mod_timer(&timer,jiffies + msecs_to_jiffies(1000));
}

static int __init event_input_init(void)
{
   
    int error;
    event_input_dev = input_allocate_device();
    if (!event_input_dev) {
   
	    printk(KERN_ERR "Not enough memory\n");
	    error = -ENOMEM;
    }
    event_input_dev->open = event_open;
    event_input_dev->close = event_close;
    event_input_dev->name  = "event_test";

     __set_bit(EV_REL,event_input_dev->evbit);
     __set_bit(REL_X,event_input_dev->relbit);
     __set_bit(REL_Y,event_input_dev->relbit);
     error = input_register_device(event_input_dev);
     if (error) {
   
	     printk(KERN_ERR "Failed to register device\n");
	     goto err_free_dev;
     }
     timer_setup(&timer,input_event_timer_callback,0);
     add_timer(&timer);
     mod_timer(&timer,jiffies + msecs_to_jiffies(1000));

	return 0;

err_free_dev:
	input_free_device(event_input_dev);
	return error;
}

static void __exit event_input_exit(void)
{
   
    del_timer(&timer);
    input_unregister_device(event_input_dev);
}

module_init(event_input_init);
module_exit(event_input_exit);
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Carl");

应用层示例代码:

/*
 * $Id: evtest.c,v 1.23 2005/02/06 13:51:42 vojtech Exp $
 *
 *  Copyright (c) 1999-2000 Vojtech Pavlik
 *
 *  Event device test program
 */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * Should you need to contact me, the author, you can do so either by
 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
 */
#include <stdint.h>
#include <linux/input.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#ifndef EV_SYN
#define EV_SYN 0
#endif
char *events[EV_MAX + 1] = {
   
	[0 ... EV_MAX] = NULL,
	[EV_SYN] = "Sync",			[EV_KEY] = "Key",
	[EV_REL] = "Relative",			[EV_ABS] = "Absolute",
	[EV_MSC] = "Misc",			[EV_LED] = "LED",
	[EV_SND] = "Sound",			[EV_REP] = "Repeat",
	[EV_FF] = "ForceFeedback",		[EV_PWR] = "Power",
	[EV_FF_STATUS] = "ForceFeedbackStatus",
};
char *keys[KEY_MAX + 1] = {
   
	[0 ... KEY_MAX] = NULL,
	[KEY_RESERVED] = "Reserved",		[KEY_ESC] = "Esc",
	[KEY_1] = "1",				[KEY_2] = "2",
	[KEY_3] = "3",				[KEY_4] = "4",
	[KEY_5] = "5",				[KEY_6] = "6",
	[KEY_7] = "7",				[KEY_8] = "8",
	[KEY_9] = "9",				[KEY_0] = "0",
	[KEY_MINUS] = "Minus",			[KEY_EQUAL] = "Equal",
	[KEY_BACKSPACE] = "Backspace",		[KEY_TAB] = "Tab",
	[KEY_Q] = "Q",				[KEY_W] = "W",
	[KEY_E] = "E",				[KEY_R] = "R",
	[KEY_T] = "T",				[KEY_Y] = "Y",
	[KEY_U] = "U",				[KEY_I] = "I",
	[KEY_O] = "O",				[KEY_P] = "P",
	[KEY_LEFTBRACE] = "LeftBrace",		[KEY_RIGHTBRACE] = "RightBrace",
	[KEY_ENTER] = "Enter",			[KEY_LEFTCTRL] = "LeftControl",
	[KEY_A] = "A",				[KEY_S] = "S",
	[KEY_D] = "D",				[KEY_F] = "F",
	[KEY_G] = "G",				[KEY_H] = "H",
	[KEY_J] = "J",				[KEY_K] = "K",
	[KEY_L] = "L",				[KEY_SEMICOLON] = "Semicolon",
	[KEY_APOSTROPHE] = "Apostrophe",	[KEY_GRAVE] = "Grave",
	[KEY_LEFTSHIFT] = "LeftShift",		[KEY_BACKSLASH] = "BackSlash",
	[KEY_Z] = "Z",				[KEY_X] = "X",
	[KEY_C] = "C",				[KEY_V] = "V",
	[KEY_B] = "B",				[KEY_N]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值