Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux character terminal how to move a red rectangle with the mouse

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Everything is a file! UNIX already said that. Eric Raymond said that, don't you agree?

Now that / dev/fb0 is abstracted into a display, you can draw a 32bit true color picture on the memory screen where the / dev/fb0 is mapped by the character terminal, so how to operate the mouse and keyboard?

/ dev/input/mouse0 can be used to read mouse events. When you cat it on the character terminal and move the mouse, it seems to tell you that something is happening, but you can't read it:

In order to find the correct way to interpret it, either Google, Baidu, or the most direct way is to check the read callback function of the file mouse0 in the Linux kernel source code:

The size of ps2 found in the static ssize_t mousedev_read (struct file * file, char _ user * buffer, size_t count, loff_t * ppos) {struct mousedev_client * client = file- > private_data; struct mousedev * mousedev = client- > mousedev; / / mousedev_client structure is 6 bytes. Signed char data [sizeof (client- > ps2)]; int retval = 0; spin_lock_irq (& client- > packet_lock); if (! client- > buffer & & client- > ready) {/ / this is the core, continue to follow the past mousedev_packet (client, client- > ps2); client- > buffer = client- > bufsiz;}.

Let's take a look at how mousedev_packet assembles packages:

Static void mousedev_packet (struct mousedev_client * client, signed char * ps2_data) {struct mousedev_motion * p = & client- > packets [client-> tail]; ps2_data [0] = 0x08 | ((p-> dx)

< 0) dy < 0) buttons & 0x07); ps2_data[1] = mousedev_limit_delta(p->

Dx, ps2_data [2] = mousedev_limit_delta (p-> dy, 127), p-> dx-= ps2_data [1], p-> dy-= ps2_data [2].

It is very clear that I don't care about anything else, and I have no motivation to learn. I just want to know the XMagi Y coordinates of the mouse now:

P-> dx,p- > dy can be seen from the name and from the code, this is relative to the last coordinate change!

We have all the information.

So, now, you can write code:

# include # include / / the side length of the square is 100 pixels # define LENGTH 100amp / abstract unsigned int * mem = NULL;// of the display memory saves the last screen unsigned int * old_mem = NULL;// screen information static struct fb_var_screeninfo info;int mouse_fd, fb_fd;// square is painted red int start = 0xffff0000int main (int argc, char * * argv) {signed char mouse_event [6] Char rel_x, rel_y; int old_x = 0, old_y = 0; int abs_x = 0, abs_y = 0; mouse_fd = open ("/ dev/input/mouse0", O_RDONLY); fb_fd = open ("/ dev/fb0", O_RDWR); ioctl (fb_fd, FBIOGET_VSCREENINFO, & info) Mem = (unsigned int *) mmap (NULL, info.xres*info.yres*info.bits_per_pixel/8, PROT_READ | PROT_WRITE, MAP_SHARED, fb_fd, 0); while (read (mouse_fd, & mouse_event [0], 6)) {int I, w, h; static int idx = 0; / / the relative displacement is parsed according to the definition of kernel mousedev_packet. Rel_x = (char) mouse_event [1]; rel_y = (char) mouse_event [2]; / / calculate absolute displacement abs_x + = rel_x; abs_y-= rel_y; if (abs_x = info.xres-LENGTH | | abs_y = info.yres-LENGTH) {continue } if (old_mem = = NULL) {old_mem = (unsigned int *) mmap (NULL, info.xres*info.yres*info.bits_per_pixel/8, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS,-1,0); if (old_mem = = NULL) {exit (1);} else {/ / restore the pixel for in the previous square area (w = old_x; w < old_x + LENGTH; walled +) {for (h = old_y) H < old_y + LENGTH; hackers +) {idx = h*info.xres + w; mem [idx] = old_ meme [IDX];}} old_x = abs_x; old_y = abs_y;} / / Save the current pixel so that you can restore for next time (w = abs_x; w < abs_x + LENGTH; hacks +) {for (h = abs_y; h < abs_y + LENGTH; hacks +) {idx = h*info.xres + w Old_ for [IDX] = mem [idx];}} / / smear the red rectangle for (w = abs_x; w < abs_x + LENGTH; walled +) {for (h = abs_y; h < abs_y + LENGTH; hash +) {idx = h*info.xres + w; mem [idx] = start;} return 0;}

Run it, and then move the mouse over the character terminal with the following effect:

Well, the rectangle moves with the mouse and doesn't break any characters wherever it goes.

Now, let me review what it means to do these things this weekend.

I can draw a 32-bit true color picture on the character terminal; I can detect mouse and keyboard events and react.

This means that if I have the time and energy, I can implement a GUI system.

Of course, the GUI system and the network protocol stack are separated by a wide range of lines, and they are bound to encounter a lot of trouble, not just reading and writing two files:

/ dev/fb0/dev/input/mouse0

We can take care of it.

In fact, real GUI systems never use this approach. They seem to be rebelling against UNIX's idea that everything is documented, and prove that it's better this way! Oh, by the way, the success of Windows GUI is a testament to it, and then the latest version of MacOS.

No matter what character terminal is said, the character is also drawn. It's no big deal. However, if you want to set characters in pixels, you need to know the information of the character lattice. This is a topic in another area.

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report