TranslateProject/sources/tech/20191203 Solutions to the tiny window manager challenge.md
DarkSun 06dda66830 选题: 20191203 Solutions to the tiny window manager challenge
sources/tech/20191203 Solutions to the tiny window manager challenge.md
2019-12-05 01:00:04 +08:00

6.1 KiB
Raw Blame History

Solutions to the tiny window manager challenge

Hello! Last week I posted a small programming challenge to write a tiny window manager that bounces windows around the screen.

Ill write a bit about my experience of solving the challenge, or you can just skip to the end to see the solutions.

whats a window manager?

An X window manager is a program that sends messages to the X server (which is in charge of drawing your windows) to tell it which windows to display and where.

I found out that you can trace those events with xtrace. Heres some example output from xtrace (for the toy window manager which is just moving windows about)

000:<:02d8: 20: Request(12): ConfigureWindow window=0x004158e5 values={x=560 y=8}
000:<:02da: 20: Request(12): ConfigureWindow window=0x004158e5 values={x=554 y=12}
000:<:02dc: 20: Request(12): ConfigureWindow window=0x004158e5 values={x=548 y=16}
000:<:02de: 20: Request(12): ConfigureWindow window=0x004158e5 values={x=542 y=20}
000:<:02e0: 20: Request(12): ConfigureWindow window=0x004158e5 values={x=536 y=24}
000:<:02e2: 20: Request(12): ConfigureWindow window=0x004158e5 values={x=530 y=28}
000:<:02e4: 20: Request(12): ConfigureWindow window=0x004158e5 values={x=524 y=32}

you can run programs without a window manager

You technically dont need a window manager to run graphical programs if you want to start an xterm in a window-manager-less X session you can just run

xterm -display :1

and itll start the xterm. Heres a screenshot of an X session with no window manager open. I even have 2 windows open! (chrome and an xterm). It has some major usability problems, for example I dont think you can resize or move or switch between windows. Which is where the window manager comes in!

https://jvns.ca/images/no-wm.png

move a window with XMoveWindow

The challenge was to make the window bounce around the screen.

In the tinywm source they use XMoveResizeWindow to move and resize windows, but I found in the docs that theres also a function called XMoveWindow. Perfect!

Heres what it looks like. What could be simpler, right? And it works just the way Id expect!

XMoveWindow(display, windowID, x, y)

Except…

problem: multiple XMoveWindows dont work

I ran into a problem (which I got stuck on for a couple of hours) where when I ran XMoveWindow twice, it would only apply the last move.

XMoveWindow(display, windowID, 100, 200)
usleep(2000 * 1000); # sleep for 2 seconds
XMoveWindow(display, windowID, 300, 400)

Id expect this to move the window once, wait 2 seconds, and them move it again. But that was not what happened! Instead, it would pause for 2 seconds and then move the window once (to the second location).

use xtrace to trace window manager events

I used xtrace to trace the events and found out that my ConfigureWindow events that XMoveWindow was sending were all being sent at the same time. So it seemed like X was batching the events. But why?

XSync forces X to process events

I didnt know why this was happening, but I emailed Julian about it and he pointed me in the direction of XSync, which forces X to process all the events youve sent it. Sure enough, I used XSync and everything worked beautifully.

solutions

I asked people to email me if they completed the challenge, and 4 people did! Here are their solutions. All the solutions I got implemented more features than I did, so Id encourage you to look at all the solutions if youre interested in how to solve this problem!

Heres a gif of Alexseys solution. Apparently XQuartz on a Mac performs better than Xephyr!

And Aldrins solution, with a great use of xeyes:

thanks!

Thanks to everyone who emailed me a solution, and if you write your own implementation Id love to post it here too, especially if you write one that isnt in C or Ruby! Im [email protected]


via: https://jvns.ca/blog/2019/12/03/solutions-to-the-tiny-window-manager-challenge/

作者:Julia Evans 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出