diff --git a/docs/XCB_Cheatsheet.md b/docs/XCB_Cheatsheet.md new file mode 100644 index 0000000..31e07ce --- /dev/null +++ b/docs/XCB_Cheatsheet.md @@ -0,0 +1,53 @@ +# XCB + +XCB is a library for communicating with the X-Windows system used on +Linux, FreeBSD, and other Unix-like operating systems. XCB's interface +is written in C. + +- Connecting, Verifying Connection, and Disconnecting the server. +- + +## Connecting. + +X-Windows is a server. It listens for events (keyboard events, mouse +events, timer events from connected programs, etc.) and "stores" the +results on a *display*, which is intended to be seen with the human +eye. A display is made up of one or more *screens*. Screens can be +literal (one of the physical devices in a multi-monitor setup) or +virtual (a virtualized desktop where the window manager supports +different "pages" on the same monitor), or even just parts of the same +physical screen space broken up by some logic. + +To connect to X via XCB, you use the `xcb_connect` function. It takes two +arguments, a string with the name of the display, and a +pointer-to-int to the preferred screen. It returns an opaque data +structure, 'xcb_connection_t'. + +``` +xcb_connection_t* xcbConnection = xcb_connect(const char* display, int* screen); +``` + +There are variants for connection-with-authorization, and +connection-with-file-descriptor. + +This function always returns an allocated structure, even on failure. +You _must_ test for failure with: + +``` +int error = xcb_connection_has_error(xcb_connection_t* xcbConnection); +``` + +The error is an number defined in `xcb.h`. See that file for the list +of possible failure modes. + +## Disconnecting + +You _must_ close the connection when you are finished. In the event +of a connection failure, you _must_ still call this function to +free the memory XCB used to report the connection failure: + +``` +void xcb_disconnect(xcb_connection_t* xcbConnection); +``` + + diff --git a/src/xrandr.cpp b/src/xrandr.cpp index d01b2e5..5aacce9 100644 --- a/src/xrandr.cpp +++ b/src/xrandr.cpp @@ -2,8 +2,13 @@ #include int main(int argc, const char *argv[]) { - xcb_connection_t *sXRandR11XCBConnection = xcb_connect(nullptr, nullptr); - std::cout << sXRandR11XCBConnection << std::endl; - xcb_disconnect(sXRandR11XCBConnection); + xcb_connection_t *xConnection = xcb_connect(nullptr, nullptr); + for (auto iter = xcb_setup_roots_iterator(xcb_get_setup(xConnection)); + iter.rem; xcb_screen_next(&iter)) { + xcb_screen_t *screen = iter.data; + std::cout << "Screen " << iter.index << " (" << screen->width_in_pixels + << ", " << screen->height_in_pixels << ")" << std::endl; + } + xcb_disconnect(xConnection); return 0; }