yujiri.xyz

Software

Learn programming for good instead of profit

How do 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 differently: 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 "where should this button be?" 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 (pronounced 'cute'). 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.

Proxied content from gemini://yujiri.xyz/software/guide/gui.gmi

Gemini request details:

Original URL
gemini://yujiri.xyz/software/guide/gui.gmi
Status code
Success
Meta
text/gemini; lang=en
Proxied by
kineto

Be advised that no attempt was made to verify the remote SSL certificate.

What is Gemini?