Learn: a connection is not a window
X11 is a protocol. XCB is the C library our C++ program calls to speak it. The server manages display resources; the client is our program. A display address such as :0 tells the client which server to contact. It doesn’t describe an image, an existing window, or a screen’s dimensions.
Run it: inspect an explicit display address
The standalone inspect-display.cpp uses an address supplied on the command line and prints the selected screen number. It does not open a window or add any mode to the framework.
#include <iostream>
#include <xcb/xcb.h>
int main(int argc, char** argv) {
if (argc != 2) {
std::cerr << "usage: inspect-display ADDRESS\n";
return 2;
}
int screen_number = 0;
xcb_connection_t* connection = xcb_connect(argv[1], &screen_number);
const int error = xcb_connection_has_error(connection);
if (error == 0) {
std::cout << "Connected to " << argv[1] << "; screen " << screen_number << '\n';
} else {
std::cerr << "Could not connect to " << argv[1] << "; XCB error " << error << '\n';
}
xcb_disconnect(connection);
return error == 0 ? 0 : 1;
}
mkdir -p build/examples
c++ -std=c++20 course/examples/inspect-display.cpp -o build/examples/inspect-display \
$(pkg-config --cflags --libs xcb)
./build/examples/inspect-display "$DISPLAY"
echo $?
On a usable desktop session, expect a line such as Connected to :0; screen 0, then 0. Your address and screen number may differ. If the address is empty or inaccessible, use an installed xvfb-run -a sh -c './build/examples/inspect-display "$DISPLAY"' or run in a desktop terminal with access. Do not invent an address or change the server’s access controls.
xcb_connect(argv[1], &screen_number)uses an explicit address. The&lets XCB write back the selected screen number; that result is meaningful when the connection succeeds. The framework task will request the default address instead.xcb_connection_has_errorreturns zero on success. The pointer can be non-null on both paths; testing it would confuse “object allocated” with “connection usable.”xcb_disconnectreleases that object. Both paths reach the same cleanup call.pkg-config --cflags --libs xcbsupplies flags for the compiler and linker. If it fails, check the XCB development package before editing anything else.
Try it: change only the address. ./build/examples/inspect-display not-an-x11-address should print a diagnostic with a nonzero XCB error. echo $? right afterward should print 1; that is the program’s exit status, not necessarily the XCB error number.
Build it: retain a default-display probe
Your challenge: Add a --probe-display mode to gpui_cc. With a usable X11 display, it exits 0. With no default display it writes a diagnostic to stderr and exits 1. This mode must remain available after you add windows.
Open CMakeLists.txt. After the gpui_cc target declaration, use CMake’s package-discovery integration:
find_package(PkgConfig REQUIRED)
pkg_check_modules(XCB REQUIRED IMPORTED_TARGET xcb)
target_link_libraries(gpui_cc PRIVATE PkgConfig::XCB)
In src/main.cpp, recognize the single --probe-display argument and dispatch to a probe path. Unlike the sample, the user does not supply an address: a null first argument to xcb_connect tells XCB to use DISPLAY. A null second argument means you do not need the screen number yet. Preserve the ordinary invocation; it can still print the Stage 0 greeting.
Check it
What the checker does:
- Invokes
gpui_cc --probe-displaywithDISPLAYunset. Expects exit1and nonempty stderr, not a verbatim diagnostic. - Starts and authenticates to a new private X server. Reinvokes the probe there and expects exit
0.
python3 course/check.py display-connect
On the starter the meaningful failure is that the program printed the greeting and exited 0 even without a display. If the result is instead BLOCKED, fix the named prerequisite. If you’re using the temporary Xvfb executable from this workspace, see the verification notes for the exact --xvfb command. For a manual failure probe after a successful local build:
env -u DISPLAY ./target/debug/gpui_cc --probe-display
echo $?
Hints and troubleshooting
Hint 1: which signal tells you the connection succeeded?
Look up xcb_connection_has_error. Zero means success; the handle itself is not a success signal.
Hint 2: who frees a failed connection object?
The caller does. Arrange for xcb_disconnect to be called once before returning the status in either case. An early return before cleanup is the edge to inspect.
If #include <xcb/xcb.h> compiles but the linker reports undefined reference to xcb_connect, inspect the CMake target’s link dependencies. If the example succeeded with a named address but the default probe fails, check DISPLAY in the shell launching the program. Once both checks pass, the same connection can ask the server to create resources. Optional upstream note: X11Client::new connects to the default display.