Introduction to Linux input device: APIs

Source: Internet
Author: User

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

Linux Input Device

Input. C is a Linux "input" driver that mainly supports keyboard and mouse input. input. the interesting part of the C interface is that the event method is used to process the input. The following is the input. important data structures and functions 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 *);

The Linux input mechanism can be used as a "virtual keyboard" or "virtual mouse". You only need to call input_event () to publish the input materials to the input handler.

Struct input_dev is an important data structure used to describe input events. Its prototype is declared 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;

}; Define buttons

We can set the evbit field in struct input_dev to define the input type to be accepted. There are currently eight input types:

* Ev_key: keys and buttons (buttons and buttons ).

* Ev_rel: relative axes (relative coordinates ).

* Ev_abs: absolute axes (absolute coordinate ).

* Ev_msc: MISC events (Other events ).

* Ev_led: LEDs.

* Ev_snd: Sounds (Sound Input ).

* Ev_rep: autorepeat values (auto-Override value ).

* 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 an array, and each element can index an input type. You can specify specific input materials for each input type, such as the tab key. The specified method is to use set_bit () or bit giant set to set the array of each input type. The column names of each input type are as follows:

* Keybit [nbits (key_max)]: keys and buttons (buttons and buttons ).

* Relbit [nbits (rel_max)]: relative axes (relative coordinates ).

* Absbit [nbits (abs_max)]: absolute axes (absolute coordinate ).

* Mscbit [nbits (msc_max)]: MISC events (Other events ).

* Ledbit [nbits (led_max)]: LEDs.

* Sndbit [nbits (snd_max)]: Sounds (Sound Input ).

* 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 operations in input. h are as follows:

* Nbits (x): calculate the number of array elements to record the X-bit element.

* Bit (x): return the value represented when the X-th bitwise element is 1. For example, if x = 0, the return value is 0x1, and if x = 1, the return value is 0x2, when x = 2, it is 0x4.

* Long (x): The X-th element belongs to the array elements (index value ).

We first set the driver to accept the ev_key event, and then specify the specific input value of the ev_key event as key_up and key_left. For definitions of input data for different events, see the input. h file. For example, the following is the ev_key event key 1 ~ Key 9 Definition:

/*

* 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 struct input_dev is set, call input_register_device () to register the parent layer. After "input device" is registered to the kernel, when the input device is enabled, the open method of the input device is called. When the input device is disabled, the close method of the input device is called.

When implementing the open method of input device, if the operation succeeds, 0 is returned. If the operation fails, any non-zero value is returned. If the operation is close, no value is required.

Report button

The API used by the user's buttons to return to the upstream layer is as follows:

* 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 parameters of input_event () are described as follows:

* Void input_event (struct input_dev * Dev, unsigned int type, unsigned int code, int value );

* @ Dev: Indicator pointing to the input device.

* @ Type: the input type (ev_key, ev_abs, etc ).

* @ Code: Enter the key (for example, ev_key's key_1 ).

* @ Value: key value.

Input Handler

Any key input should call input_event () to send the input event to input. C, and then input. C will dispatch the event (sent by handler) to each "input handler ". The input data can be read by drivers registered with input handler. -- 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.