A circular moving mouse and a circular moving mouse
Function: The mouse pointer emits nerves and keeps moving around.
Knowledge required: Simulate the linux mouse and circle Parameter Equation
Development Environment: ubuntu 16.04LTS
1: how to simulate the mouse?
The linux kernel designs and implements an abstraction layer to process various types of input devices (such as the mouse, keyboard, joystick, and touch screen, it is the linux input subsystem.
This input subsystem allows us to easily simulate mouse and keyboard events in the user space. For example, you can write an application to the/dev/input/event3 device file of the input subsystem.
The keyboard Device File) is written to A, which is equivalent to pressing A through the keyboard, and this A is valid for any of the current active windows of the system (capture ). Similarly, we can find the device file with the mouse and
Write coordinates and other data to move the mouse pointer.
2: How to control mouse movement?
First, we need to find the device file with the mouse, open the console and enter cat/proc/bus/input/devices | grep mouse. My notebook displays the following results:
Why are there two )? Notebook, there is a touchpad, and I also have a wireless mouse (you can also view more detailed information without using the gerp mouse command ). so I can write data to/dev/input/event9 to control the mouse.
So the question is, what data can be written to the/dev/input/event9 file to control the mouse pointer movement?
In/usr/include/linux/input. h is defined. This file defines the structure of event Events, the encoding of APIs and standard buttons, etc. What we need to do is to fill the key encoding to be written into the structure, then write it to the/dev/input/event9 file.
Struct of the input event:
Struct input_event
{
Struct timeval time; // key time
_ 2010type; // key type
_ Code; // the buttons to simulate
_ S32 value; // whether to press 1 or release 0
};
If the event type code is EV_REL (moving relative coordinates), the positive and negative values of the value represent two values in different directions. for example, if the code is REL_X value is 10, it indicates the mouseRelativeIn the last coordinate, move 10 pixels to the right of the X axis.
4: program code
// file name: sim.c
# include < string. H >
# include < stdio, h >
# include < sys/types. H >
# include < sys/stat. H >
# include < an FCNTL. H >
# include < Linux/input. H >
# include < Linux/uinput. H >
# include < stdio, h >
# include < sys/time. H >
# include < sys/types. H >
# include < unistd. H >
H # include < math.h >
Void simulate_mouse(int fd,int x,int y)
{
Struct input_event event;
Memset (& event, 0, sizeof (event));
The gettimeofday (& event. Time, NULL);
Event. The type = EV_REL;
Event. Code = REL_X;
Event. The value = x;
Write (fd, & event, sizeof (event));
Event. The type = EV_REL;
Event. Code = REL_Y;
Event. The value = y;
Write (fd, & event, sizeof (event));
Event. The type = EV_SYN; / / synchronize
Event. Code = SYN_REPORT;
Event. The value = 0;
Write (fd, & event, sizeof (event));
}
Int main ()
{int rx and ry;
Int I;
Int fd_mouse;
Double theta1 = 90, pertheta theta0 = 90;
Fd_mouse = open ("/dev/input/event9 ", O_RDWR); // your computer may not be event9, see this blog post
If (fd_mouse < = 0)
{
Printf (" error open mouse \ n ");
The return - 2;
}
Pertheta = 360.0/360; // decrease by 1 degree at a time
Int r = 400;
While (1)
{
Theta1 = 90;
Theta0 = 90;
For (I = 0; I < 360; I++)
{
Rx = (int) (r * cos (theta1/180.0 * 3.141592653589792) - r * cos (theta0/180.0 * 3.1415926535897922)); // find the relative moving pixels
Ry = (int) (r * sin (theta1/180.0 * 3.141592653589792) - r * sin (theta0/180.0 * 3.141592653589792)); // find the relative moving pixels
Simulate_mouse (fd_mouse, rx and ry);
Theta0 = theta1;
Theta1 = theta1 - pertheta;
Usleep (20000);
}
}
Close (fd_mouse);
}
-Lm is required for compiling and using gcc sim. c-o sim-lm because math. h is used.
Root permission is required for running !!!!!!! So we need sudo.