Linux Input Device

Source: Internet
Author: User

Linux input device is an important subsystem. Before introducing the example, let's take a look at the related APIs.

Linux Input Device

Input. c is the "input" notebook program of Linux, which mainly supports loading and mouse loading. input. the interesting part of the C interface is that the handler uses the event Method to handle incoming operations. The following is the input. important information structures and data of the C interface:

* Struct input_dev
* Void input_event (struct input_dev * Dev, unsigned int type, unsigned int code, int value)
* Void input_register_device (struct input_dev *);
* Void input_unregister_device (struct input_dev *);
* Void input_register_handler (struct input_handler *);
* Void input_unregister_handler (struct input_handler *);

In Linux, the input machine can be used as the "Dynamic Route upload mouse" or "Dynamic Route mouse". You only need to call input_event () to send the incoming data to the input handler.

Struct input_dev is an important component used to describe an incoming event. Its prototype is as follows:

Struct input_dev {

Void * private;

Int number;
Char * Name;
Unsigned short idbus;
Unsigned short idvendor;
Unsigned short idproduct;
Unsigned short idversion;

Unsigned long evbit [nbits (ev_max)];
Unsigned long keybit [nbits (key_max)];
Unsigned long relbit [nbits (rel_max)];
Unsigned long absbit [nbits (abs_max)];
Unsigned long mscbit [nbits (msc_max)];
Unsigned long ledbit [nbits (led_max)];
Unsigned long sndbit [nbits (snd_max)];
Unsigned long ffbit [nbits (ff_max)];
Int ff_effects_max;

Unsigned int keycodemax;
Unsigned int keycodesize;
Void * keycode;

Unsigned int repeat_key;
Struct timer_list timer;

Int ABS [abs_max + 1];
Int rep [rep_max + 1];

Unsigned long key [nbits (key_max)];
Unsigned long led [nbits (led_max)];
Unsigned long snd [nbits (snd_max)];

Int absmax [abs_max + 1];
Int absmin [abs_max + 1];
Int absfuzz [abs_max + 1];
Int absflat [abs_max + 1];

INT (* open) (struct input_dev * Dev );
Void (* close) (struct input_dev * Dev );
INT (* event) (struct input_dev * Dev, unsigned int type, unsigned int code, int value );
INT (* upload_effect) (struct input_dev * Dev, struct ff_effect * effect );
INT (* erase_effect) (struct input_dev * Dev, int effect_id );

Struct input_handle * handle;
Struct input_dev * next;
};

 

Definition by definition

We can set the evbit bit in struct input_dev to define the acceptable sequence type. Currently, there are eight sequence type types as follows:

* Ev_key: keys and buttons (by region and by region ).
* Ev_rel: relative axes (relative base mark ).
* Ev_abs: absolute axes (opposite seat tag ).
* Ev_msc: MISC events (Other events ).
* Ev_led: LEDs.
* Ev_snd: Sounds (audio transfer ).
* Ev_rep: autorepeat values ).
* Ev_ff: Force Feedback event.

The following is an example. We specify Dev to accept the ev_key event:

Dev. evbit [0] = bit (ev_key );

Evbit is a primary column. Each element can be indexed to a category. Each category type can specify a specific category, for example, Tab material. The specified method is to use set_bit () or the bit huge set to set the sequence columns of each sequence type. The following table lists the sequence names of each sequence type:

* Keybit [nbits (key_max)]: keys and buttons (by hour and by hour ).
* Relbit [nbits (rel_max)]: relative axes (opposite base ).
* Absbit [nbits (abs_max)]: absolute axes (seat pair ).
* Mscbit [nbits (msc_max)]: MISC events (Other events ).
* Ledbit [nbits (led_max)]: LEDs.
* Sndbit [nbits (snd_max)]: Sounds (audio transfer ).
* Ffbit [nbits (ff_max)]: Force Feedback event.

The following example uses set_bit:

* Set_bit (key_up, Dev. keybit );
* Set_bit (key_left, Dev. keybit );

Or you can use the bit collection:

* Keybit [0] = bit (keyup) | bit (key_left );

The three major sets for bitwise calculation in input. h are as follows:

* Nbits (x): calculates the number of X-bit elements that need to be specified only when there are several primary column elements.
* Bit (x): indicates the number of the X-th bitwise dollar representing 1 in a ticket. For example, if x = 0, the value is 0x1, X = 1 is 0x2, and X = 2 is 0x4.
* Long (x): The X-th digit is the element (index value) corresponding to the first vertex column ).

We first set the event handlers to accept the ev_key event, and then specify the specific values of the ev_key event as key_up and key_left. For more information about different events, see input. h later ~ According to question 9:

/*
* Keys and buttons
*/

# Define key_1 2
# Define key_2 3
# Define key_3 4
# Define key_4 5
# Define key_5 6
# Define key_6 7
# Define key_7 8
# Define key_8 9
# Define key_9 10

After the struct input_dev is set, call input_register_device () again until it reaches the upper limit. When "input device" is switched to the kernel, when the input device is enabled, the open method of the input device is called. When the input device is disabled, call the close method of the input device.
When the open method of the input device is implemented, if 0 is successfully returned, any non-zero value is returned if the value is lost. If close method is used, no callback value is required.

Report by region

The following APIs are used by the user to roll back the token by token:

* Input_report_key (struct input_dev * Dev, unsigned int code, int value );
* Input_report_rel (struct input_dev * Dev, unsigned int code, int value );
* Input_report_abs (struct input_dev * Dev, unsigned int code, int value );
* Input_report_key and input_report_rel both use the huge set of input_event (). The prototype and parameter description of input_event () are as follows:
* Void input_event (struct input_dev * Dev, unsigned int type, unsigned int code, int value );

* @ Dev: indicates the input device.
* @ Type: type (ev_key, ev_abs, etc ).
* @ Code: Press keys (for example, key_1 of ev_key ).
* @ Value: the value by region.

Input Handler

Call input_event () to send the input event to the input. c, and then input. c sends an event to each input handler ". When there is an upload handler input handler, you can retrieve the upload data.

-- Jollen

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.