gpui-cc Course handbook
CHAPTER 01 / STAGE 03COURSE

Handle close without guessing

Read an event, distinguish a close request, and exit cleanly.

window-close
On this page

Learn: a close request is a message

A window manager normally asks a cooperative client to close instead of destroying its window on the spot. The request is an X11 ClientMessage. Think of it as an envelope addressed to a window: the event kind says it is a ClientMessage, type identifies the protocol, and data identifies a particular operation inside that protocol.

Run it: decode an event without a display

inspect-message.cpp constructs two event headers in memory. It doesn’t contact a server. The second header has the synthetic-event flag set, as can happen when an event is delivered using X11 SendEvent.

C++ · inspect-message.cpp
#include <iostream>
#include <xcb/xcb.h>

void inspect(const xcb_client_message_event_t& message) {
    constexpr unsigned synthetic_bit = 0x80;
    const unsigned base_type = message.response_type & 0x7f;
    if (base_type != XCB_CLIENT_MESSAGE) {
        std::cout << "Not a ClientMessage\n";
        return;
    }
    std::cout << "ClientMessage, " << ((message.response_type & synthetic_bit) ? "synthetic" : "native")
              << ", format " << unsigned(message.format);
    if (message.format == 32) std::cout << ", first word " << message.data.data32[0];
    std::cout << '\n';
}

int main() {
    // This is an in-memory example, not an event sent to another program.
    xcb_client_message_event_t message{};
    message.response_type = XCB_CLIENT_MESSAGE;
    message.format = 32;
    message.data.data32[0] = 42;
    inspect(message);

    message.response_type |= 0x80;
    inspect(message);
}
Terminal
mkdir -p build/examples
c++ -std=c++20 course/examples/inspect-message.cpp -o build/examples/inspect-message \
  $(pkg-config --cflags --libs xcb)
./build/examples/inspect-message
Text
ClientMessage, native, format 32, first word 42
ClientMessage, synthetic, format 32, first word 42

The high bit marks how the event arrived. Mask it off before comparing the event kind. The payload format tells you which view of the data to read: the sample only reads data32[0] for format 32. Try it: copy the sample to build/examples/, change the word to 7, and rebuild the copy. The same two event kinds now carry a different operation. Change the format to 8 and the word is no longer printed. The sample illustrates decoding, not which operations the window should act on.

Build it: register and handle the close protocol

Your challenge: Advertise support for WM_DELETE_WINDOW. When that exact close request arrives, close and exit 0. Ignore an unrelated protocol message. If the display server disappears while waiting, exit 1 without hanging. Keep the window and connection-probe stages green.

An atom is the server’s numeric ID for a name such as WM_PROTOCOLS. A name string and its atom ID cannot be used interchangeably. Use xcb_intern_atom to request an ID, xcb_intern_atom_reply to read the reply’s atom field, and std::free on the reply afterwards. If no reply arrives or the atom is XCB_ATOM_NONE, you do not have an ID to register. The same request/reply shape appeared in the map-state sample.

Before mapping, register a property named by the WM_PROTOCOLS atom. Think back to the title call: this time the property type is XCB_ATOM_ATOM, the format is 32, and the data is a list of one atom, WM_DELETE_WINDOW. The item count is 1, not the number of bytes in that atom.

In src/main.cpp, only consider a ClientMessage after testing the base kind on the event returned by xcb_wait_for_event. Then interpret it as a xcb_client_message_event_t. Match the window ID, the WM_PROTOCOLS type, the format, and the first payload word. Don’t free the event until you’ve read what you need. Release it once even when it’s the event that ends the loop. A null event is the separate failure path; the server is gone, so don’t keep waiting or try to send more requests.

Check it

What the checker does: Confirms the actual property contains WM_DELETE_WINDOW. On the bare X server it then sends an unrelated WM_PROTOCOLS message followed by the matching close message. The first must leave the process alive; the second must result in exit 0 within three seconds. In a fresh run the checker shuts down only its own X server; the app must notice the loss and return 1. This is a protocol test, not an automated click on your desktop’s close button.

Terminal
python3 course/check.py window-close --previous

Hints and troubleshooting

Hint 1: why is a synthetic ClientMessage not matching my switch?

An event delivered by SendEvent can have the high bit set in response_type. Compare the base event code, then check the client-message payload.

Hint 2: it closes on the wrong message, or never exits on server loss

Both messages deliberately share the same outer type. Compare the operation in data32[0], not just the type of the event. Separately, see what xcb_wait_for_event returns on connection failure; a loop that blindly waits again can spin after the server is gone.

If the checker can’t see registration, inspect the property’s type, item format, and item count before debugging the event loop. If the app stays running after the server is lost, trace the null return separately from the event dispatch case. Optional upstream note: GPUI-CE registers the same close protocol.

Pilot finish line: window-close --previous passes, and you can point to where the connection, window, and event lifetimes end. Report the exact check output for the active stage. Later stages will make this window display a CPU-produced frame before introducing drawing primitives.