💾 Archived View for yujiri.xyz › software › guide › gui.gmi captured on 2023-09-08 at 16:35:30. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-03-20)

➡️ Next capture (2023-12-28)

🚧 View Differences

-=-=-=-=-=-=-

yujiri.xyz

Software

Learn programming for good instead of profit

How graphical and windowed programs work

The reason I put this off for so long is because the story is really complex. Writing any GUI (Graphical User Interface) or windowed program will almost certainly involve several libraries, which is why I don't recommend it to a beginner.

The Wayland story

On Linux, windows are provided by a program called a window manager or compositor (because it composits many windowed programs onto one screen). Programs that run in windows start by connecting to your window manager over a Unix socket and asking it to create a window for them. The window manager also establishes a block of shared memory that represents the contents of the window. After that, the program can draw to the window by writing to this region of memory.

What's a Unix socket? How does shared memory work?

The protocol that programs use to communicate with the window manager is called Wayland.

The X11 story

Wayland's predecessor, which is still used in some Linux environments, is called X11, or just X. X works a little differentlly: in X, the role of a Wayland compositor is split into two different programs: the X server, which controls the graphics hardware, and the window manager, which controls how windows are arranged. Both the window manager and windowed programs communicate directly with the X server.

The widget toolkit story

It doesn't end there. Wayland and X are both very low-level things, and the stuff most windowed prorgams do - like rendering text, buttons, and other user interface elements - is actually incredibly complicated, much more so than you would think*. As an app developer, you'd be crazy to try to implement those things on your own. You would want to use a library that handles the hard stuff, so you can think about questions like "what should happen when the user clicks this button?" instead of "what arrangement of shaded pixels do I need to draw to make this square look like a button, or to make it look pressed in when the user clicks on it?" and "how to line-break text?" and "how to support screenreaders?".

There are two main libraries for this: GTK and Qt. Almost every GUI app on Linux uses one of the two.

Text Rendering Hates You

Text Editing Hates You Too

Widget, by the way, is short for window gadget, and refers to the elements of a GUI like buttons and text input boxes.