linux input system

Linux input system

 

转载时请注明出处和作者联系方式
文章出处:http://blog.csdn.net/weixu_2008

作者联系方式:徐威weixu_2008@163.com

 

 

本文简单介绍一下linuxinput system并通过一个实际的案例介绍一下在具体的项目中如何实现自己的inputsystem

 

1.  系统结构

 

钻研技术的总是喜欢了解细节以及系统的整个框架,那首先就从linuxinput system的结构开始说起,如下图所示:

Input system 有三大块组成:

  • Drivers:相当于输入设备的驱动程序,负责接收来自硬件的输入中断,并把输入中断转换成相应的输出给Input Core这个部分的实现取决于具体的硬件,也是实际当中我们主要做的部分。

 

  • Input CoreLinux input system中的核心部分,是输入设备的抽象,把来自输入设备的输入输出到相应的Handler,这部分的代码可以看linux的内核代码中Drivers/input/input.c在实际中我们不需要自己去写这部分的代码。

 

  • Handlers:用户空间中的应用程序通过handlers来接收输入,对于用户空间来说,Handler就像一个设备一样,可以从中得到底层的输入。在实际应用中,这块基本上很少会去修改。

 

综上:一个输入的数据流的路径:Drivers → Input Core → Handlers → Applications

 

 

讲完了结构,那在实际中,DriverInput Core Handler Application 是如何联系上的呢?

 

首先说说Driver是怎么和Input CoreHandler联系上的呢?

 

Input Core中,由两个链表:input_dev_listinput_handler_list

当有一个新的driver调用input_register_device的时候,Input Core就会把这个input_dev添加到input_dev_list中,同时还会在input_handler_list中寻找所有匹配的input_handler,把input_handlerinput_dev连接(connect)起来,一旦连接以后,input_dev发生的输入就会通过Input Core 传递到input_handler,用户空间的applications通过input_handler进而得到输入。

 

同样,当有一个新的handler调用input_register_handler的时候,Input Core就会把这个input_handler添加到input_handler_list上面,同时遍历input_dev_list找出所有匹配的input_dev,并且把匹配的input_devinput_handler连接(connect)起来。

 

如果用图来说明的话,input_devinput_dev_handler之间的关系如下:

 

123表示input_dev设备,其通过input_dev->node变量连接到全局输入设备链表input_dev_list中。结点 456表示input_handler处理器,其通过input_handler->node连接到全局handler处理器链表input_handler_list中。结点7是一个input_handle的结构体,其用来连接input_devinput_handlerinput_handledev成员指向了对应的input_dev设备,input_handlehandler成员指向了对应的input_handler。另外,结点7input_handle通过d_node连接到了结点2input_dev上的h_list链表上。另一方面,结点7input_handle通过h_node连接到了结点5input_handlerh_list链表上。通过这种关系,将input_devinput_handler联系了起来。

 

Application又是怎么和Handler联系上的呢?

每一个handler都类似于/dev/下面的一个设备,application需要打开这个设备,使用read方法来读取输入。

而在handler中,又有一个client_list的链表,每当有application打开这个handler的时候,都会建立一个新的client并且添加到这个client_list上面去,这样所有的applications都会接到同样的输入。

 

在系统中,可以通过以下命令来看有哪些input_devinput_dev_handler

cat /proc/bus/input/devices

cat /proc/bus/input/handlers

 

2.  实例

    01  #include <asm/irq.h> 
    02  #include <asm/io.h> 
    03  static struct input_dev *button_dev;    /*输入设备结构体*/  
    04  static irqreturn_t button_interrupt(int irq, void *dummy) /*中断处理函数*/  
    05  {  
    06      input_report_key(button_dev, BTN_0, inb(BUTTON_PORT) & 1);      
    /*向输入子系统报告产生按键事件*/  
    07      input_sync(button_dev);         /*通知接收者,一个报告发送完毕*/  
    08      return IRQ_HANDLED;  
    09  }  
    10  static int __init button_init(void) /*加载函数*/  
    11  {  
    12      int error;  
    13      if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL))  /*申请中断处理函数*/  
    14      {  
    15          /*申请失败,则打印出错信息*/  
    16          printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_  
    irq);  
    17          return -EBUSY;  
    18      }  
    19      button_dev = input_allocate_device();   /*分配一个设备结构体*/  
    20      if (!button_dev)                        /*判断分配是否成功*/  
    21      {  
    22          printk(KERN_ERR "button.c: Not enough memory\n");  
    23          error = -ENOMEM;  
    24          goto err_free_irq;  
    25      }  
    26      button_dev->evbit[0] = BIT_MASK(EV_KEY);    /*设置按键信息*/  
    27      button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);  
    28      error = input_register_device(button_dev);  /*注册一个输入设备*/  
    29      if (error)  
    30      {  
    31          printk(KERN_ERR "button.c: Failed to register device\n");  
    32          goto err_free_dev;  
    33      }  
    34      return 0;  
    35      err_free_dev:                               /*以下是错误处理*/  
    36          input_free_device(button_dev);  
    37      err_free_irq:  
    38          free_irq(BUTTON_IRQ, button_interrupt);  
    39      return error;  
    40  }  
    41  static void __exit button_exit(void)            /*卸载函数*/  
    42  {  
    43      input_unregister_device(button_dev);        /*注销按键设备*/  
    44      free_irq(BUTTON_IRQ, button_interrupt); /*释放按键占用的中断线*/  
    45  }  
    46  module_init(button_init);  
    47  module_exit(button_exit); 


 

 

3. 扩展

 

另外,我们也可以通过往/dev/uinput中写数据来模拟输入。

代码:

 

#include <string.h> 
#include <stdio.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <linux/input.h> 
#include <linux/uinput.h> 
#include <stdio.h> 
#include <sys/time.h> 
#include <sys/types.h> 
#include <unistd.h> 
 
/* Globals */ 
static int uinp_fd = -1; 
struct uinput_user_dev uinp;  // uInput device structure 
struct input_event event;  // Input device structure 
 
/* Setup the uinput device */ 
 
int setup_uinput_device() 
{ 
  // Temporary variable 
  int i=0; 
   
  // Open the input device  
    uinp_fd = open("/dev/uinput", O_WRONLY | O_NDELAY); 
 
  if (uinp_fd == NULL) 
 
  { 
    printf("Unable to open /dev/uinput\n"); 
    return -1; 
  } 
 
  memset(&uinp,0,sizeof(uinp));   // Intialize the uInput device to NULL 
 
  strncpy(uinp.name, "PolyVision Touch Screen", UINPUT_MAX_NAME_SI
  uinp.id.version = 4; 
  uinp.id.bustype = BUS_USB; 
 
  // Setup the uinput device  
  ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY); 
  ioctl(uinp_fd, UI_SET_EVBIT, EV_REL); 
  ioctl(uinp_fd, UI_SET_RELBIT, REL_X); 
  ioctl(uinp_fd, UI_SET_RELBIT, REL_Y); 
 
  for (i=0; i < 256; i++) { 
       ioctl(uinp_fd, UI_SET_KEYBIT, i); 
  } 
 
  ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MOUSE); 
  ioctl(uinp_fd, UI_SET_KEYBIT, BTN_TOUCH); 
 
  ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MOUSE); 
  ioctl(uinp_fd, UI_SET_KEYBIT, BTN_LEFT); 
  ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MIDDLE); 
  ioctl(uinp_fd, UI_SET_KEYBIT, BTN_RIGHT); 
  ioctl(uinp_fd, UI_SET_KEYBIT, BTN_FORWARD); 
  ioctl(uinp_fd, UI_SET_KEYBIT, BTN_BACK); 
 
  /* Create input device into input sub-system */ 
  write(uinp_fd, &uinp, sizeof(uinp)); 
 
  if (ioctl(uinp_fd, UI_DEV_CREATE)) 
  { 
    printf("Unable to create UINPUT device."); 
    return -1; 
  } 
  return 1; 
} 
 
void send_click_events( ) 
{ 
         // Move pointer to (0,0) location 
     
   memset(&event, 0, sizeof(event)); 
  gettimeofday(&event.time, NULL); 
 
        event.type = EV_REL; 
        event.code = REL_X; 
         event.value = 100; 
        write(uinp_fd, &event, sizeof(event)); 
 
        event.type = EV_REL; 
        event.code = REL_Y; 
        event.value = 100; 
        write(uinp_fd, &event, sizeof(event)); 
 
  event.type = EV_SYN; 
        event.code = SYN_REPORT; 
        event.value = 0; 
        write(uinp_fd, &event, sizeof(event)); 
   
  // Report BUTTON CLICK - PRESS event  
 
  memset(&event, 0, sizeof(event)); 
  gettimeofday(&event.time, NULL); 
  event.type = EV_KEY; 
  event.code = BTN_LEFT; 
  event.value = 1; 
  write(uinp_fd, &event, sizeof(event)); 
 
  event.type = EV_SYN; 
  event.code = SYN_REPORT; 
  event.value = 0; 
  write(uinp_fd, &event, sizeof(event)); 
 
  // Report BUTTON CLICK - RELEASE event  
 
  memset(&event, 0, sizeof(event)); 
  gettimeofday(&event.time, NULL); 
  event.type = EV_KEY; 
  event.code = BTN_LEFT; 
  event.value = 0; 
  write(uinp_fd, &event, sizeof(event)); 
 
  event.type = EV_SYN; 
  event.code = SYN_REPORT; 
  event.value = 0; 
  write(uinp_fd, &event, sizeof(event)); 
}   
 
void send_a_button() 
{ 
  // Report BUTTON CLICK - PRESS event  
 
  memset(&event, 0, sizeof(event)); 
  gettimeofday(&event.time, NULL); 
        event.type = EV_KEY; 
        event.code = KEY_A; 
         event.value = 1; 
        write(uinp_fd, &event, sizeof(event)); 
 
        event.type = EV_SYN; 
        event.code = SYN_REPORT; 
        event.value = 0; 
        write(uinp_fd, &event, sizeof(event)); 
 
 
  // Report BUTTON CLICK - RELEASE event  
 
memset(&event, 0, sizeof(event)); 
  gettimeofday(&event.time, NULL); 
        event.type = EV_KEY; 
        event.code = KEY_A; 
        event.value = 0; 
        write(uinp_fd, &event, sizeof(event)); 
 
        event.type = EV_SYN; 
        event.code = SYN_REPORT; 
        event.value = 0; 
        write(uinp_fd, &event, sizeof(event)); 
} 
 
 
/* This function will open the uInput device. Please make  
   sure that you have inserted the uinput.ko into kernel.  */ 
 
int main() 
{ 
  // Return an error if device not found. 
  if (setup_uinput_device() < 0)  
  { 
    printf("Unable to find uinput device\n"); 
    return -1; 
  } 
   
  send_a_button();    // Send a "A" key 
   
  send_click_events();  // Send mouse event 
 
  /* Destroy the input device */ 
  ioctl(uinp_fd, UI_DEV_DESTROY); 
 
  /* Close the UINPUT device */ 
  close(uinp_fd); 
} 
 
 


 

linux中,也并不是所有的input都是通过Input Core的,比如:

USB input system

 

Linux Bluetooth input system

 

Linux ACPI input system

 

现在已经有了很多开源库介绍inputabstract layer可以google下。

 

4.  参考

·       http://www.cnblogs.com/dekun_1986/archive/2011/09/12/2174264.html

·        http://blog.csdn.net/wenny198561/article/details/6309208

·        http://blog.csdn.net/jack0106/article/details/6336136

·        http://blog.chinaunix.net/attachment/attach/25/69/93/2925699329b2cbd71db82a8b965509be27bd20ad46.pdf

·        http://www4.informatik.uni-erlangen.de/~thoenig/thesis/thesis.pdf

·        http://www.einfochips.com/download/dash_jan_tip.pdf

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值