Chatting with the input device in Linux (12) read her mind and focus on her inner world

Source: Internet
Author: User

Insert a message: the official rocket website has just announced that Yao has never said goodbye to this season. It seems that retiring is also a matter of time. In fact, I have been paying attention to the rockets since I went to college. One of the main reasons is Yao Ming. Who made us Chinese? The Chinese are very patriotic, of course, except for some espionage. Because Yao Ming liked McGrady ...... Now both of them are in this situation, and it is chilling to think about it. Fireworks are cold and personnel are easy to score. Okay, don't mention these sad things. Let's continue to study our evdev device.

She opened her heart. A brainless brother asked, "What can I do after I open it? Of course it is read. Well, let's analyze the second function in the operation set: evdev_read () for further tracking:

Static ssize_t evdev_read (struct file * file, char _ User * buffer,

Size_t count, loff_t * PPOs)

{

1 struct evdev_client * client = file-> private_data;

2 struct evdev * evdev = client-> evdev;

3 struct input_event event;

4 int retval;

 

5 If (count <input_event_size ())

6 Return-einval;

 

7 if (client-> head = client-> tail & evdev-> exist &&

8 (file-> f_flags & o_nonblock ))

9 return-eagain;

 

10 retval = wait_event_interruptible (evdev-> wait,

11 client-> head! = Client-> tail |! Evdev-> exist );

12 if (retval)

13 return retval;

 

14 if (! Evdev-> exist)

15 return-enodev;

 

16 while (retval + input_event_size () <= count &&

17 evdev_fetch_next_event (client, & event )){

 

18 if (Input_event_to_user (buffer + retval, & event))

Return-efault;

 

19 retval + = input_event_size ();

}

 

20 return retval;

}

I believe that you will not have any difficulties in analyzing this function.

Line 1: Put the private data in evdev_open into the struct evdev_client * client.

Rows 5-6. If the number of bytes to copy is smaller than the size of an event, sorry. Let's end. Incomplete events are useless for us. Don't waste your expressions.

Line 10-11: wait_event_interruptible (evdev-> wait, client-> head! = Client-> tail |! Evdev-> exist );

Remember her? Yes, she has been sleeping here for a long time, waiting for her good luck to wake her up. Later client-> head! = Client-> tail |! Evdev-> exist is a condition that must be met before she wakes up.

Line 16-19: each time an input event data is retrieved from the client buffer and put into our event, it is then transferred to the buffer at the application layer, retval records the total number of returned bytes. In-depth input_event_to_user (buffer + retval, & event ):

Int input_event_to_user (char _ User * buffer,

Const struct input_event * event)

{

If (copy_to_user (buffer, event, sizeof (struct input_event )))

Return-efault;

 

Return 0;

}

The copy_to_user function puts a kernel space buffer into our user space buffer. His brother copy_from_user completes the opposite action.

All right, the data is in our user space. Now we can do what we want ......

As for functions such as evdev_write and evdev_ioctl in the function set, I will not analyze them one by one. How can I write something in her mind and operate on her? Does it sound a little yellow, I am sorry to discuss it with you again. You need to explore these things slowly, as the saying goes: "Do it yourself, enrich your clothes ".

By now, the entire series has been analyzed and found that it is one thing to understand and analyze the code. Writing it out is another thing. Let's talk about it again: we need to practice more. Whether it's for work or for the girl, you don't want to practice it. If it's just YY, the effect is still quite different. Thank you for your attention. If you have any questions, contact me privately.

Insert a message: the official rocket website has just announced that Yao has never said goodbye to this season. It seems that retiring is also a matter of time. In fact, I have been paying attention to the rockets since I went to college. One of the main reasons is Yao Ming. Who made us Chinese? The Chinese are very patriotic, of course, except for some espionage. Because Yao Ming liked McGrady ...... Now both of them are in this situation, and it is chilling to think about it. Fireworks are cold and personnel are easy to score. Okay, don't mention these sad things. Let's continue to study our evdev device.

She opened her heart. A brainless brother asked, "What can I do after I open it? Of course it is read. Well, let's analyze the second function in the operation set: evdev_read () for further tracking:

Static ssize_t evdev_read (struct file * file, char _ User * buffer,

Size_t count, loff_t * PPOs)

{

1 struct evdev_client * client = file-> private_data;

2 struct evdev * evdev = client-> evdev;

3 struct input_event event;

4 int retval;

 

5 If (count <input_event_size ())

6 Return-einval;

 

7 if (client-> head = client-> tail & evdev-> exist &&

8 (file-> f_flags & o_nonblock ))

9 return-eagain;

 

10 retval = wait_event_interruptible (evdev-> wait,

11 client-> head! = Client-> tail |! Evdev-> exist );

12 if (retval)

13 return retval;

 

14 if (! Evdev-> exist)

15 return-enodev;

 

16 while (retval + input_event_size () <= count &&

17 evdev_fetch_next_event (client, & event )){

 

18 if (Input_event_to_user (buffer + retval, & event))

Return-efault;

 

19 retval + = input_event_size ();

}

 

20 return retval;

}

I believe that you will not have any difficulties in analyzing this function.

Line 1: Put the private data in evdev_open into the struct evdev_client * client.

Rows 5-6. If the number of bytes to copy is smaller than the size of an event, sorry. Let's end. Incomplete events are useless for us. Don't waste your expressions.

Line 10-11: wait_event_interruptible (evdev-> wait, client-> head! = Client-> tail |! Evdev-> exist );

Remember her? Yes, she has been sleeping here for a long time, waiting for her good luck to wake her up. Later client-> head! = Client-> tail |! Evdev-> exist is a condition that must be met before she wakes up.

Line 16-19: each time an input event data is retrieved from the client buffer and put into our event, it is then transferred to the buffer at the application layer, retval records the total number of returned bytes. In-depth input_event_to_user (buffer + retval, & event ):

Int input_event_to_user (char _ User * buffer,

Const struct input_event * event)

{

If (copy_to_user (buffer, event, sizeof (struct input_event )))

Return-efault;

 

Return 0;

}

The copy_to_user function puts a kernel space buffer into our user space buffer. His brother copy_from_user completes the opposite action.

All right, the data is in our user space. Now we can do what we want ......

As for functions such as evdev_write and evdev_ioctl in the function set, I will not analyze them one by one. How can I write something in her mind and operate on her? Does it sound a little yellow, I am sorry to discuss it with you again. You need to explore these things slowly, as the saying goes: "Do it yourself, enrich your clothes ".

By now, the entire series has been analyzed and found that it is one thing to understand and analyze the code. Writing it out is another thing. Let's talk about it again: we need to practice more. Whether it's for work or for the girl, you don't want to practice it. If it's just YY, the effect is still quite different. Thank you for your attention. If you have any questions, contact me privately.

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.