gpui-cc Course handbook
CHAPTER 01 / STAGE 02COURSE

Show one real window

Create a native window and prove it is actually visible.

window-map
On this page

Learn: what does the server own?

You already have a connection. Ask the server about the selected screen: its root window is the existing parent at the top of that screen’s window tree. A normal top-level app window is created as a child of that root. You don’t create or destroy the root.

Text
display server
└── selected screen's root  (already exists, already mapped)
    └── our window ID       (create gives it geometry; map asks to show it)

xcb_generate_id reserves a name for a resource; it does not create it. After creation the window exists but is unmapped. After mapping, it is viewable when its ancestors are mapped too. “Viewable” is a window state, not proof it painted pixels or isn’t covered by another window.

Run it: watch create and map change server state

map-state.cpp requests a tiny untitled test window, asks for its map state twice, then disconnects. It deliberately bypasses the window manager (XCB_CW_OVERRIDE_REDIRECT) so this experiment measures X11 mapping alone. Don’t copy that setting for the application window.

Terminal
mkdir -p build/examples
c++ -std=c++20 course/examples/map-state.cpp -o build/examples/map-state \
  $(pkg-config --cflags --libs xcb)
./build/examples/map-state
./build/examples/map-state skip-map

Expected from a usable desktop. A tiny window may flash briefly; the printed state is the observation that matters. If you have xvfb-run instead of a desktop, prefix each example invocation with xvfb-run -a.

Text
after create: unmapped
after map: viewable
after create: unmapped
without map: unmapped

Follow the code in the order the server sees it:

Pick the parent and reserve an ID

C++
auto screen = xcb_setup_roots_iterator(xcb_get_setup(connection));
for (int i = 0; i < screen_number && screen.rem > 0; ++i) {
    xcb_screen_next(&screen);
}

xcb_get_setup gives connection-owned server setup data. The iterator starts on screen 0, and rem is the number of records left; the sample checks it before reading screen.data. That record’s root is the parent ID. xcb_generate_id(connection) gives you the new ID. Mixing them up means asking the server to create a resource using an ID it already owns.

Read the create request as groups

C++
const auto create = xcb_create_window_checked(
    connection, XCB_COPY_FROM_PARENT, window, screen.data->root,
    0, 0, 112, 72, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
    screen.data->root_visual, XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT, values);
  • XCB_COPY_FROM_PARENT here is the depth argument. The new window inherits the root’s depth.
  • 0, 0, 112, 72, 0 means x, y, width, height, border width. It makes a 112 × 72 client area at the parent’s origin.
  • INPUT_OUTPUT means the window can be drawn to and receive input. The root’s visual specifies how pixel values are interpreted; we use the root’s choice in this example.
  • The final mask tells XCB which attributes are present. The values array supplies one value per set bit, in increasing bit order: background pixel, then override-redirect.

Ask what actually happened

The checked call returns a cookie, not a verdict. xcb_request_check(connection, create) uses it to learn about a server-side error. xcb_get_window_attributes_reply waits for the reply describing the actual window state. The sample frees allocated errors and replies with std::free. The setup record is borrowed from the connection, so it is not freed separately.

XCB can buffer requests. The sample’s checks and replies require a conversation with the server. In the app, explicitly xcb_flush before waiting so queued setup is sent. A flush sends requests; it is not a success reply. On a regular desktop, the window manager may make mapping asynchronous.

Read the complete worked example
C++ · map-state.cpp
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <xcb/xcb.h>

bool checked(xcb_connection_t* connection, xcb_void_cookie_t cookie,
             const char* operation) {
    auto* error = xcb_request_check(connection, cookie);
    if (!error) return xcb_connection_has_error(connection) == 0;
    std::cerr << operation << " failed: X11 error " << unsigned(error->error_code) << '\n';
    std::free(error);
    return false;
}

bool print_state(xcb_connection_t* connection, xcb_window_t window, const char* label) {
    auto cookie = xcb_get_window_attributes(connection, window);
    auto* reply = xcb_get_window_attributes_reply(connection, cookie, nullptr);
    if (!reply) {
        std::cerr << "Could not read window attributes\n";
        return false;
    }
    const char* state = "unviewable";
    if (reply->map_state == XCB_MAP_STATE_UNMAPPED) state = "unmapped";
    if (reply->map_state == XCB_MAP_STATE_VIEWABLE) state = "viewable";
    std::cout << label << ": " << state << '\n';
    std::free(reply);
    return true;
}

bool inspect_mapping(xcb_connection_t* connection, int screen_number, bool skip_map) {
    auto screen = xcb_setup_roots_iterator(xcb_get_setup(connection));
    for (int i = 0; i < screen_number && screen.rem > 0; ++i) xcb_screen_next(&screen);
    if (screen.rem == 0) {
        std::cerr << "Requested screen was not in the server setup\n";
        return false;
    }

    // The root is already mapped. We bypass any window manager for this short experiment.
    const xcb_window_t window = xcb_generate_id(connection);
    const std::uint32_t values[] = {screen.data->black_pixel, 1};
    const auto create = xcb_create_window_checked(
        connection, XCB_COPY_FROM_PARENT, window, screen.data->root,
        0, 0, 112, 72, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
        screen.data->root_visual, XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT, values);

    if (!checked(connection, create, "create") || !print_state(connection, window, "after create")) {
        return false;
    }
    if (!skip_map && !checked(connection, xcb_map_window_checked(connection, window), "map")) {
        return false;
    }
    return print_state(connection, window, skip_map ? "without map" : "after map");
}

int main(int argc, char**) {
    int screen_number = 0;
    auto* connection = xcb_connect(nullptr, &screen_number);
    bool ok = false;
    if (xcb_connection_has_error(connection) == 0) {
        ok = inspect_mapping(connection, screen_number, argc > 1);
    } else {
        std::cerr << "Could not connect to the default X11 display\n";
    }
    // X11's default close-down behavior also releases our short-lived test window.
    xcb_disconnect(connection);
    return ok ? 0 : 1;
}

Try it: remove or skip only the map request. The resource can still answer the attributes query, which proves “created” and “shown” are not the same operation.

Build it: make the sample idea into a real app window

Your challenge: Run gpui_cc without arguments. A real viewable X11 top-level window titled gpui-cc, with a 640 × 480 client area, must appear, and your process must stay alive. Keep the probe working. No drawing or click-to-close handling is required yet.

In src/main.cpp, keep the probe path separate. For the ordinary path, use what you observed to create a normal window on the selected screen. Use a title property before mapping. Here is the shape of a title assignment, with a sample name rather than the challenge value:

C++
constexpr char title[] = "Practice";
xcb_change_property(connection, XCB_PROP_MODE_REPLACE, window,
    XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, sizeof(title) - 1, title);

Read the last four arguments as property type, bits per item, number of items, and data. An ASCII title is a sequence of 8-bit items; sizeof(title) - 1 leaves out C++’s trailing null. A title printed to stdout never reaches the server.

The sample disconnected and removed its short-lived window. The application must keep the connection alive. xcb_wait_for_event(connection) blocks until an event or a connection failure. In your loop, free each non-null event with std::free and don’t wait again if the result is null. No action needs handling yet. The checker ends the process after observing the window. For a manual run, use Ctrl+C in the launching terminal until Stage 3.

Check it

What the checker does: Starts the executable on its private server, asks the server for top-level children and their titles, and checks window attributes and geometry. It looks for one viewable match with the documented client dimensions while the app is still running. A title printed in the terminal is not evidence. A found, unmapped window reports the server’s actual map state in the failure.

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

Hints and troubleshooting

Hint 1: find the parent and the new resource ID

xcb_get_setup and xcb_setup_roots_iterator give you the screens. Select the index reported by xcb_connect. Use xcb_generate_id for the new window, not screen->root; that ID belongs to the existing root.

Hint 2: debug creation separately from visibility

Look at xcb_create_window_checked, xcb_request_check, xcb_change_property, xcb_map_window, and xcb_flush. The observer accepts an exact title in WM_NAME or _NET_WM_NAME. Free returned error/event allocations when you are done with them.

If the program exits before the observer finds a window, check your connection lifetime and whether you reached the event wait. If the checker sees map state 0, check the map call and flush rather than changing the title. A passed stage gives us a real destination for future frames. Optional upstream note: GPUI-CE creates and maps separately.