How to use a Linux Virtual Input Device (uinput) to simulate the mouse and keyboard

Source: Internet
Author: User

From http://blog.csdn.net/outblue/archive/2010/02/04/5288760.aspx

Dashboard January 2007 issue
Mehul Patel
Using uinput driver in Linux-
2.6.x to send user input

Dashboard January 2007 issue
Using uinput driver in Linux-2.6.x to send user
Input
Introduction:
The Linux 2.6.x provides a "uinput" driver, which helps users to inject data to the Linux kernel.
This is very useful while writing applications to interface customized input devices like Wireless
Joystick, keyboard etc.
The driver uses/dev/uinput device to send data to kernel space which in turn send data to xwindows
Or active shell. This feature can be used to perform automated shell scripts which
Involve graphical user inputs.
Uinput is configured as a loadable module in most of the linux kernels. you can load uinput
Driver by giving the following commands.
$ Modprobe uinput
$ Lsmod
The "lsmod" command lists all the drivers loaded in the Linux system. You shoshould See "uinput"
Driver in the list.
The next step is to develop a sample application. This application will send the User Key
Sequence to kernel which is in turn sent to X-Windows or shell.
Opening an input device:
// Open the input device
Uinp_fd = open ("/dev/uinput", o_wronly | o_ndelay );
If (uinp_fd = NULL)
{
Printf ("unable to open/dev/uinput/N ");
Return-1;
}
After opening device you have to configure the uinput device parameters (mouse, keyboard,
Etc) using the ioctl () function such:
IOCTL (out_fd, ui_set_evbit, ev_key );
IOCTL (out_fd, ui_set_evbit, ev_rep );
The ev_key and ev_rep inform the uinput driver as the event is a keyboard event and
Key value contains the key repetition property.
Dashboard January 2007 issue
Sending events to kernel:
All the events coming from the user program will be carried to the kernel space through
Structure "struct input_event" defined in kernels "/usr/include/Linux/input. H ".
A keyboard can be generated using following piece of code:
Event. type = ev_key;
Event. Code = key_enter;
Event. value = 1;
Write (uinp_fd, & event, sizeof (event ));
The above example will send enter key to kernel. This key event in-turn will be sent to user
Space Application. All the key definitions are defined in "/usr/include/Linux/input. H" file.
You can use the following sample code to test Linux uinput interface.
// Uinput. c
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
/* Globals */
Static int uinp_fd =-1;
Struct uinput_user_dev uindium; // uinput device structure
Struct input_event event; // input device structure
/* Setup the uinput device */
Int setup_uinput_device ()
{
// Temporary Variable
Int I = 0;
// Open the input device
Uinp_fd = open ("/dev/uinput", o_wronly | o_ndelay );
If (uinp_fd = NULL)
{
Dashboard January 2007 issue
Printf ("unable to open/dev/uinput/N ");
Return-1;
}
Memset (& uindium, 0, sizeof (uindium); // intialize the uinput device to null
Strncpy (uindium. Name, "polyvision touch screen", uinput_max_name_size );
Uindium. Id. Version = 4;
Uindium. Id. bustype = bus_usb;
// Setup the uinput Device
IOCTL (uinp_fd, ui_set_evbit, ev_key );
IOCTL (uinp_fd, ui_set_evbit, ev_rel );
IOCTL (uinp_fd, ui_set_relbit, rel_x );
IOCTL (uinp_fd, ui_set_relbit, rel_y );
For (I = 0; I <256; I ++ ){
IOCTL (uinp_fd, ui_set_keybit, I );
}
IOCTL (uinp_fd, ui_set_keybit, btn_mouse );
IOCTL (uinp_fd, ui_set_keybit, btn_touch );
IOCTL (uinp_fd, ui_set_keybit, btn_mouse );
IOCTL (uinp_fd, ui_set_keybit, btn_left );
IOCTL (uinp_fd, ui_set_keybit, btn_middle );
IOCTL (uinp_fd, ui_set_keybit, btn_right );
IOCTL (uinp_fd, ui_set_keybit, btn_forward );
IOCTL (uinp_fd, ui_set_keybit, btn_back );
/* Create input device into input sub-system */
Write (uinp_fd, & uindium, sizeof (uindium ));
If (IOCTL (uinp_fd, ui_dev_create ))
{
Printf ("unable to create uinput device .");
Return-1;
}
Return 1;
}
Void send_click_events ()
{
// Move pointer to (0, 0) Location
Memset (& event, 0, sizeof (event ));
Gettimeofday (& event. Time, null );
Event. type = ev_rel;
Event. Code = rel_x;
Dashboard January 2007 issue
Event. value = 100;
Write (uinp_fd, & event, sizeof (event ));
Event. type = ev_rel;
Event. Code = rel_y;
Event. value = 100;
Write (uinp_fd, & event, sizeof (event ));
Event. type = ev_syn;
Event. Code = syn_report;
Event. value = 0;
Write (uinp_fd, & event, sizeof (event ));
// Report button click-press event
Memset (& event, 0, sizeof (event ));
Gettimeofday (& event. Time, null );
Event. type = ev_key;
Event. Code = btn_left;
Event. value = 1;
Write (uinp_fd, & event, sizeof (event ));
Event. type = ev_syn;
Event. Code = syn_report;
Event. value = 0;
Write (uinp_fd, & event, sizeof (event ));
// Report button click-release event
Memset (& event, 0, sizeof (event ));
Gettimeofday (& event. Time, null );
Event. type = ev_key;
Event. Code = btn_left;
Event. value = 0;
Write (uinp_fd, & event, sizeof (event ));
Event. type = ev_syn;
Event. Code = syn_report;
Event. value = 0;
Write (uinp_fd, & event, sizeof (event ));
}
Void send_a_button ()
{
// Report button click-press event
Memset (& event, 0, sizeof (event ));
Gettimeofday (& event. Time, null );
Event. type = ev_key;
Event. Code = key_a;
Dashboard January 2007 issue
Event. value = 1;
Write (uinp_fd, & event, sizeof (event ));
Event. type = ev_syn;
Event. Code = syn_report;
Event. value = 0;
Write (uinp_fd, & event, sizeof (event ));
// Report button click-release event
Memset (& event, 0, sizeof (event ));
Gettimeofday (& event. Time, null );
Event. type = ev_key;
Event. Code = key_a;
Event. value = 0;
Write (uinp_fd, & event, sizeof (event ));
Event. type = ev_syn;
Event. Code = syn_report;
Event. value = 0;
Write (uinp_fd, & event, sizeof (event ));
}
/* This function will open the uinput device. Please make
Sure that you have inserted the uinput. Ko into kernel .*/
Int main ()
{
// Return an error if device not found.
If (setup_uinput_device () <0)
{
Printf ("unable to find uinput device/N ");
Return-1;
}
Send_a_button (); // send a "A" Key
Send_click_events (); // send mouse event
/* Destroy the input device */
IOCTL (uinp_fd, ui_dev_destroy );
/* Close the uinput device */
Close (uinp_fd );
}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/outblue/archive/2010/02/04/5288760.aspx

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.