1. inotify和epoll

怎么监测键盘接入与拔出?

(1)hotplug机制:内核发现键盘接入/拔出==>启动hotplug进程==>发消息给输入系统

(2)inotify机制:输入系统使用inotify来监测目录/dev/input

android使用inofity机制

当插入多个键盘时,系统怎么知道哪个键盘被按下?

android下使用epoll,可以同时监控多个文件,当文件发生改变,其会知道谁变化了

参考代码:
frameworks\native\services\inputflinger\EventHub.cpp

参考文章:
《深入理解Android 卷III》第五章 深入理解Android输入系统 
http://blog.csdn.net/innost/article/details/47660387

inotify的使用(监测目录或者文件的变化)

(1)fd = inotify_init()

(2)inotify_add_watch(目录名字/文件名字,创建/删除)

(3)read(fd),平时目录和文件没有创建或者删除时,会休眠,发生变化后read返回多个inotify_event结构体

inotify_event.name保存了名字,inotify_event.len表示名字的长度,inotify_event.mask表示发生了说明变化(创建还是删除)

inotify.c编写(Usage:inotify <dir> 这个目录下发生的变化)

#include <unistd.h>

#include <stdio.h>

#include <sys/inotify.h>

#include <string.h>

#include <errno.h>

int read_process_inotify_fd(int fd){

  int res;

  char event_buf[512];

  int event_size;

  int event_pos = 0;

  struct inotify_event *event;

  res = read(fd,event_buf,sizeof(event_buf));

  if(res < (int)sizeof(*event)){

    if(errno == EINTR)

      return 0;

    printf("could not get event ,%s\n",strerror(errno));

    return -1;

  }

  //处理数据,读到的数据是一个或多个inotify_event,他们len不一样,逐个处理

  while(res >= (int)sizeof(*event)){

    event = (struct inotify_event *)(event_buf+event_pos);

    if(event->len){

      if(event->mask & IN_CREATE){

          printf("create file : %s\n",event->name);

      }else{

          printf("delete file : %s\n",event->name);

      }

    }

    event_size = sizeof(*event)+event->len;

    res -= event_size;

    event_pos += event_size;

  }

  return 0;

}

int main(int argc,char **argv)

{

  int mINotifyFd;

  int result;

  if(argc != 2)

  {

    printf("Usage:%s <dir>\n",argv[0]);

    return -1;

  }

  mINotifyFd = inotify_init(argv[0]);

  result = inotify_add_watch(mINotifyFd,argv[1],IN_DELETE | IN_CREATE);

  while(1)

  {

    read_process_inotify_fd(mINotifyFd);

  }

  return 0;

}
gcc -o inotify inotify.c
mkdir tmp
./inotify tmp &

echo > tmp/1
echo > tmp/2
rm tmp/1 tmp/2

epoll用来检测多个文件有无数据供读出、有无空间供写入

(1)epoll_create//创建fd

(2)对每个文件执行epoll_ctl(......,EPOLL_CTL_ADD,) 表示要监测它

(3)epoll_wait//等待某个文件可用

(4)不在想监测某文件可用执行epoll_ctl(......,EPOLL_CTL_DEL,)

epoll , fifo :
http://stackoverflow.com/questions/15055065/o-rdwr-on-named-pipes-with-poll

使用fifo是, 我们的epoll程序是reader
echo aa > tmp/1 是writer
a.
如果reader以 O_RDONLY|O_NONBLOCK打开FIFO文件,
当writer写入数据时, epoll_wait会立刻返回;
当writer关闭FIFO之后, reader再次调用epoll_wait, 它也会立刻返回(原因是EPPLLHUP, 描述符被挂断)
b.
如果reader以 O_RDWR打开FIFO文件
当writer写入数据时, epoll_wait会立刻返回;
当writer关闭FIFO之后, reader再次调用epoll_wait, 它并不会立刻返回, 而是继续等待有数据

epoll.c

/*Usage:epoll <file1> [file2] [file3]*/

#include <sys/epoll.h>

#include <unistd.h>

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <string.h>

#define DATA_MAX_LEN 500

int add_to_epoll(int fd,int epollFd)

{

  int result;

  struct epoll_event eventItem;

  memset(&eventItem,0,sizeof(eventItem));

  eventItem.events = EPOLLIN;//表示监测其有数据

  eventItem.data.fd=fd;

  result = epoll_ctl(epollFd,EPOLL_CTL_ADD,fd,&eventItem);

  return result;

}

void rm_from_epoll(int fd,int epollFd)

{

  result = epoll_ctl(epollFd,EPOLL_CTL_DEL,fd,NULL);

}

int main(int argc,char **argv)

{

  int mEpollFd;

  int i;

  char buf[DATA_MAX_LEN];

  static const int EPOLL_MAX_EVENTS = 16;//epoll_wait一次最大监测事件数

  struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];

  if(argc < 2)

  {

    printf("Usage:%s<file1> [file2] [file3] \n",argv[0]);

    return -1;

  }

  mEpollFd = epoll_create(8);

  /*for each file:open it /add it to epoll*/

  for(i = 1;i < argc;i++)

  {

    int tmpFd = open(argv[i],O_RDWR);

    add_to_epoll(tmpFd,mEpollFd);

  }

  /*epoll_wait*/

  while(1){

    int pollResult = epoll_wait(mEpollFd ,mPendingEventItems,EPOLL_MAX_EVENTS ,-1);//-1表示永远监测不退出

    for(i=0;i<pollResult;i++)

    {

      int len =read(mPendingEventItems[i].data.fd,buf,DATA_MAX_LEN);

      buf[len] = '\0';

      printf("get data:%s\n",buf);

    }

  }

  return 0;

}

gcc -o epoll epoll.c
mkdir tmp
mkfifo tmp/1 tmp/2 tmp/3
./epoll tmp/1 tmp/2 tmp/3 &
echo aaa > tmp/1
echo bbb > tmp/2

课后作业:
编写 inotify_epoll.c, 用它来监测tmp/目录: 有文件被创建/删除, 有文件可读出数据
a. 当在tmp/下创建文件时, 会立刻监测到,并且使用epoll监测该文件
b. 当文件有数据时,读出数据
c. 当tmp/下文件被删除时,会立刻监测到,并且把它从epoll中移除不再监测

inotify_epoll.c
gcc -o inotify_epoll inotify_epoll.c
mkdir tmp
./inotify_epoll tmp/ &
mkfifo tmp/1 tmp/2 tmp/3
echo aaa > tmp/1
echo bbb > tmp/2
rm tmp/3

10.1、android输入系统_必备Linux编程知识_inotify和epoll的更多相关文章

  1. 10.2、android输入系统_必备Linux编程知识_双向通信(scoketpair)

    2. 双向通信(socketpair) 输入系统肯定涉及进程通讯:进程A读取/分发输入事件,APP处理输入事件,进程A给APP发送输入事件,APP处理完事件回复信息给进程A,APP关闭的时候也要发信息 ...

  2. 10.3、android输入系统_必备Linux编程知识_任意进程双向通信(scoketpair+binder)

    3. 任意进程间通信(socketpair_binder) 进程每执行一次open打开文件,都会在内核中有一个file结构体表示它: 对每一个进程在内核中都会有一个task_struct表示进程,这个 ...

  3. 10.11 android输入系统_补充知识_activity_window_decor_view关系

    android里:1个application, 有1个或多个activity(比如支付宝有:首页.财富.口碑.朋友.我的,这些就是activity)1个activity, 有1个window(每个ac ...

  4. 10.4 android输入系统_框架、编写一个万能模拟输入驱动程序、reader/dispatcher线程启动过程源码分析

    1. 输入系统框架 android输入系统官方文档 // 需FQhttp://source.android.com/devices/input/index.html <深入理解Android 卷 ...

  5. 10.13 android输入系统_多点触摸驱动理论与框架

    1.多点触摸驱动理论 驱动程序仅上报多个触点的位置就可以,是放大还是缩小由应用程序控制 对于多点触摸驱动在linux系统中有个输入子系统,其已经实现了open/read/write等接口 我们只需要实 ...

  6. 10.14 android输入系统_多点触摸驱动测试及Reader线程、InputStage分析

    21. 多点触摸_电容屏驱动程序_实践_tiny4412 tiny4412触摸屏: 分辨率为800 x 480http://wiki.friendlyarm.com/wiki/index.php/LC ...

  7. 10.8 android输入系统_实战_使用GlobalKey一键启动程序

    11. 实战_使用GlobalKey一键启动程序参考文章:Android 两种注册(动态注册和静态注册).发送广播的区别http://www.jianshu.com/p/ea5e233d9f43 [A ...

  8. 10.9 android输入系统_APP跟输入系统建立联系和Dispatcher线程_分发dispatch

    12. 输入系统_APP跟输入系统建立联系_InputChannel和Connection核心: socketpair // 第9课第3节_输入系统_必备Linux编程知识_任意进程双向通信(scok ...

  9. 10.5 android输入系统_Reader线程_使用EventHub读取事件和核心类及配置文件_实验_分析

    4. Reader线程_使用EventHub读取事件 使用inotify监测/dev/input下文件的创建和删除 使用epoll监测有无数据上报 细节: a.fd1 = inotify_init(& ...

随机推荐

  1. js插件---JS表格组件BootstrapTable行内编辑解决方案x-editable

    js插件---JS表格组件BootstrapTable行内编辑解决方案x-editable 一.总结 一句话总结:bootstrap能够做为最火的框架,绝对不仅仅只有我看到的位置,它应该还有很多位置可 ...

  2. 阿里云Redis使用规范

    一.键值设计 1.key名设计 (1)[建议]: 可读性和可管理性 以业务名(或数据库名)为前缀(防止key冲突),用冒号分隔,比如业务名:表名:id ugc:video:1 (2)[建议]: 简洁性 ...

  3. 62.C++文件操作list实现内存检索,实现两千万数据秒查

    1 #include <iostream> #include <fstream> #include <cstdlib> #include <string> ...

  4. OpenCV —— HighGUI

    分为:硬件相关部分,文件部分以及图形用户接口部分 创建窗口 —— cvNamedWindow 若设置成 CV_WINDOW_AUTOSIZE 窗口大小会随着图像的载入而根据图像大小调整,用户没办法手动 ...

  5. ssm 框架学习-1

    理论理解 +项目阅读 SpringSpring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象.Spring的核心思想是IoC(控制反转),即 ...

  6. 【2017 Multi-University Training Contest - Team 2】Maximum Sequence

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6047 [Description] 给你一个数列a和一个数列b; 只告诉你a的前n项各是什么; 然后 ...

  7. 火狐—火狐浏览器中的“HttpWatch”

    在IE下通过HttpWatch能够查看HTTP请求的相关细节.这对我们分析程序的运行效率很有帮助,但是在火狐浏览器中的难道就没有相似的工具了吗?答案是否定的--火狐浏览器中也有.在火狐浏览器中该工具叫 ...

  8. Word Ladder II [leetcode]

    本题有几个注意点: 1. 回溯找路径时.依据路径的最大长度控制回溯深度 2. BFS时,在找到end单词后,给当前层做标记find=true,遍历完当前层后结束.不须要遍历下一层了. 3. 能够将字典 ...

  9. Google、Mozilla、Qt、LLVM 这几家的规范是明确禁用异常的

    作者:陈硕链接:https://www.zhihu.com/question/22889420/answer/22975569来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  10. 可重入锁ReentrantLock--转载

    突然被问到什么是可重入锁?脑袋里闪过了n中概念,最终没有找到,从网上学习一下. 原文地址:https://www.ibm.com/developerworks/cn/java/j-jtp10264/ ...