10.1, Android Input System _ Linux programming Knowledge _inotify and Epoll

Source: Internet
Author: User
Tags epoll inotify
<span id="Label3"></p><p><p>1. INotify and Epoll</p></p><p><p>How to monitor keyboard access and unplug?</p></p><p><p>(1) hotplug mechanism: Kernel discovery keyboard Access/unplug ==> start hotplug process ==> Send message to input system</p></p><p><p>(2) inotify mechanism: The input system uses inotify to monitor the Directory/dev/input</p></p><p><p>Android using inofity mechanism</p></p><p><p></p></p><p><p>When inserting multiple keyboards, How does the system know which keyboard is being pressed?</p></p><p><p>Using Epoll under android, You can monitor multiple files at the same time, and when a file changes, it will know who changed</p></p><p><p></p></p><p><p>Reference code:<br>Frameworks\native\services\inputflinger\eventhub.cpp</p></p><p><p>Reference article:<br>Deep understanding of Android Volume III fifth in-depth understanding of the Android input system<br>http://blog.csdn.net/innost/article/details/47660387</p></p><p><p></p></p><p><p>Use of inotify (monitoring of changes in directories or files)</p></p><p><p>(1) FD = Inotify_init ()</p></p><p><p>(2) inotify_add_watch (directory name/file name, Create/delete)</p></p><p><p>(3) read (fd), usually the directory and files are not created or deleted, will hibernate, changed after read return multiple inotify_event structure</p></p><p><p>Inotify_event.name Saves the name, Inotify_event.len indicates the length of the name, inotify_event.mask indicates that a change in the description has occurred (create or Delete)</p></p><p><p></p></p><p><p>INOTIFY.C writing (usage:inotify <dir> changes in this Directory)</p></p><p><p>#include <unistd.h></p></p><p><p>#include <stdio.h></p></p><p><p>#include <sys/inotify.h></p></p><p><p>#include <string.h></p></p><p><p>#include <errno.h></p></p><p><p>int read_process_inotify_fd (int Fd) {</p></p><p><p>int res;</p></p><p><p>Char event_buf[512];</p></p><p><p>int event_size;</p></p><p><p>int event_pos = 0;</p></p><p><p>struct Inotify_event *event;</p></p><p><p>res = Read (fd,event_buf,sizeof (event_buf));</p></p><p><p>If (res < (int) sizeof (*event)) {</p></p><p><p>if (errno = = Eintr)</p></p><p><p>Return 0;</p></p><p><p>printf ("could not get event,%s\n", strerror (errno));</p></p><p><p>return-1;</p></p><p><p>}</p></p><p><p>Process the data, read the data is one or more inotify_event, they len different, processing individually</p></p><p><p>While (res >= (int) sizeof (*event)) {</p></p><p><p>event = (struct inotify_event *) (event_buf+event_pos);</p></p><p><p>If (event->len) {</p></p><p><p>If (event->mask & In_create) {</p></p><p><p>printf ("create file:%s\n", event->name);</p></p><p><p>}else{</p></p><p><p>printf ("delete file:%s\n", event->name);</p></p><p><p>}</p></p><p><p>}</p></p><p><p>event_size = sizeof (*event) +event->len;</p></p><p><p>res-= event_size;</p></p><p><p>Event_pos + = event_size;</p></p><p><p>}</p></p><p><p>Return 0;</p></p><p><p>}</p></p><p><p></p></p><p><p>int main (int Argc,char **argv)</p></p><p><p>{</p></p><p><p>int minotifyfd;</p></p><p><p>int result;</p></p><p><p>If (argc! = 2)</p></p><p><p>{</p></p><p><p>printf ("usage:%s <dir>\n", argv[0]);</p></p><p><p>return-1;</p></p><p><p>}</p></p><p><p>MINOTIFYFD = Inotify_init ();</p></p><p><p>result = Inotify_add_watch (minotifyfd,argv[1],in_delete | in_create);</p></p><p><p>While (1)</p></p><p><p>{</p></p><p><p>READ_PROCESS_INOTIFY_FD (minotifyfd);</p></p><p><p>}</p></p><p><p>Return 0;</p></p><p><p></p></p><p><p>}<br>Gcc-o inotify inotify.c<br>mkdir tmp<br>./inotify TMP &</p></p><p><p>echo > TMP/1<br>echo > TMP/2<br>RM TMP/1 TMP/2</p></p><p><p></p></p><p><p>Epoll is used to detect multiple files with or without data for reading, there is no space for writing</p></p><p><p>(1) epoll_create//create FD</p></p><p><p>(2) for each file execution Epoll_ctl (..., epoll_ctl_add,) means to monitor it</p></p><p><p>(3) epoll_wait//wait for a file to be available</p></p><p><p>(4) do not want to monitor a file available execution Epoll_ctl (..., epoll_ctl_del,)</p></p><p><p><br>epoll, fifo:<br>Http://stackoverflow.com/questions/15055065/o-rdwr-on-named-pipes-with-poll</p></p><p><p>Using FIFO yes, our Epoll program is reader<br>echo AA > TMP/1 is writer<br>A.<br>If reader takes o_rdonly| O_nonblock Open a FIFO file,<br>When writer writes data, Epoll_wait will return immediately;<br>When writer closes the fifo, reader calls epoll_wait again and it returns immediately (reason is eppllhup, the descriptor is hung Up)<br>B.<br>If reader opens a FIFO file with O_rdwr<br>When writer writes data, Epoll_wait will return immediately;<br>When writer closes the fifo, reader calls epoll_wait again, it does not return immediately, but continues to wait for the data</p></p><p><p>Epoll.c</p></p><p><p>/*usage:epoll <file1> [file2] [file3]*/</p></p><p><p>#include <sys/epoll.h></p></p><p><p>#include <unistd.h></p></p><p><p>#include <stdio.h></p></p><p><p>#include <sys/types.h></p></p><p><p>#include <sys/stat.h></p></p><p><p>#include <fcntl.h></p></p><p><p>#include <string.h></p></p><p><p>#define Data_max_len 500</p></p><p><p>int Add_to_epoll (int Fd,int epollfd)</p></p><p><p>{</p></p><p><p>int result;</p></p><p><p>struct Epoll_event eventitem;</p></p><p><p>Memset (&eventitem,0,sizeof (eventitem));</p></p><p><p>Eventitem.events = Epollin;//means monitoring its data</p></p><p><p>eventitem.data.fd=fd;</p></p><p><p>result = Epoll_ctl (epollfd,epoll_ctl_add,fd,&eventitem);</p></p><p><p>Return result;</p></p><p><p>}</p></p><p><p>void Rm_from_epoll (int Fd,int epollfd)</p></p><p><p>{</p></p><p><p>result = Epoll_ctl (epollfd,epoll_ctl_del,fd,null);</p></p><p><p>}</p></p><p><p>int main (int Argc,char **argv)</p></p><p><p>{</p></p><p><p>int mepollfd;</p></p><p><p>int i;</p></p><p><p>Char buf[data_max_len];</p></p><p><p>static const int epoll_max_events = 16;//epoll_wait maximum number of monitored events</p></p><p><p>struct Epoll_event mpendingeventitems[epoll_max_events];</p></p><p><p>If (ARGC < 2)</p></p><p><p>{</p></p><p><p>printf ("usage:%s<file1> [file2] [file3] \ n", argv[0]);</p></p><p><p>return-1;</p></p><p><p>}</p></p><p><p>MEPOLLFD = Epoll_create (8);</p></p><p><p>/*for each file:open it/add it to epoll*/</p></p><p><p>For (i = 1;i < Argc;i++)</p></p><p><p>{</p></p><p><p>int TMPFD = Open (argv[i],o_rdwr);</p></p><p><p>Add_to_epoll (tmpfd,mepollfd);</p></p><p><p>}</p></p><p><p>/*epoll_wait*/</p></p><p><p>While (1) {</p></p><p><p>int pollresult = epoll_wait (mepollfd, mpendingeventitems,epoll_max_events,-1);//-1 means forever monitoring does not exit</p></p><p><p>For (i=0;i<pollresult;i++)</p></p><p><p>{</p></p><p><p>int Len =read (mpendingeventitems[i].data.fd,buf,data_max_len);</p></p><p><p>buf[len] = ' + ';</p></p><p><p>printf ("get data:%s\n", buf);</p></p><p><p>}</p></p><p><p>}</p></p><p><p>Return 0;</p></p><p><p>}</p></p><p><p><br>Gcc-o Epoll EPOLL.C<br>mkdir tmp<br>Mkfifo TMP/1 TMP/2 TMP/3<br>./epoll tmp/1 TMP/2 TMP/3 &<br>echo AAA > TMP/1<br>echo BBB > TMP/2</p></p><p><p></p></p><p><p>After-school assignments:<br>Write inotify_epoll.c, use it to monitor the tmp/directory: there are files created/deleted, there are files to read the data<br>A. When a file is created under tmp/, it is immediately monitored and used epoll to monitor the file<br>B. When the file has data, read the data<br>C. When the tmp/file is deleted, it is immediately monitored and removed from the epoll no longer monitored</p></p><p><p>Inotify_epoll.c<br>Gcc-o Inotify_epoll INOTIFY_EPOLL.C<br>mkdir tmp<br>./inotify_epoll tmp/&<br>Mkfifo TMP/1 TMP/2 TMP/3<br>echo AAA > TMP/1<br>echo BBB > TMP/2<br>RM TMP/3</p></p><p><p>10.1, Android Input System _ Linux programming Knowledge _inotify and Epoll</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.