XCB 获取所有显示器及其 x, y 坐标

XCB get all monitors ands their x, y coordinates

到目前为止我已经得到了所有的显示器。监视器是屏幕。所以我所做的是:

xcb_connection_t *conn;

conn = xcb_connect(NULL, NULL);

if (xcb_connection_has_error(conn)) {
  printf("Error opening display.\n");
  exit(1);
}

const xcb_setup_t* setup;
xcb_screen_t* screen;

setup = xcb_get_setup(conn);
screen = xcb_setup_roots_iterator(setup).data;
printf("Screen dimensions: %d, %d\n", screen->width_in_pixels, screen->height_in_pixels);

这给了我宽度和高度。然而,我得到 x 和 y 是至关重要的。在 screen->root 上做 xcb_get_window_attributes_cookie_t 是获取 x 和 y 的方法吗?

我正在阅读这里 - http://www.linuxhowtos.org/manpages/3/xcb_get_window_attributes_unchecked.htm - 但没有给出 x/y 坐标。

我假设您对显示器的几何形状感兴趣,即实际的物理设备,而不是 X 屏幕。

在那种情况下,根 window 不是您感兴趣的。这里基本上有两件不同的事情需要考虑:

要了解如何查询各种详细信息,我的建议是查看执行此操作的程序。一个规范的建议是支持多头设置的 window 管理器,例如 i3 window 管理器,它实际上同时支持 Xinerama 和 RandR,因此您可以查看其源代码.

您要查找的信息可以在src/randr.csrc/xinerama.c中找到。必要的 RandR API 调用是

  • xcb_randr_get_screen_resources_current
  • xcb_randr_get_screen_resources_current_outputs
  • xcb_randr_get_output_info
  • xcb_randr_get_crtc_info

后者将为您提供输出的 CRTC 信息,其中包括输出的位置和大小。

RandR 实现的另一个来源是 xedgewarp:src/randr.c*。我将在此处包含该源代码的大幅缩短摘录:

xcb_randr_get_screen_resources_current_reply_t *reply = xcb_randr_get_screen_resources_current_reply(
        connection, xcb_randr_get_screen_resources_current(connection, root), NULL);

xcb_timestamp_t timestamp = reply->config_timestamp;
int len = xcb_randr_get_screen_resources_current_outputs_length(reply);
xcb_randr_output_t *randr_outputs = xcb_randr_get_screen_resources_current_outputs(reply);
for (int i = 0; i < len; i++) {
    xcb_randr_get_output_info_reply_t *output = xcb_randr_get_output_info_reply(
            connection, xcb_randr_get_output_info(connection, randr_outputs[i], timestamp), NULL);
    if (output == NULL)
        continue;

    if (output->crtc == XCB_NONE || output->connection == XCB_RANDR_CONNECTION_DISCONNECTED)
        continue;

    xcb_randr_get_crtc_info_reply_t *crtc = xcb_randr_get_crtc_info_reply(connection,
            xcb_randr_get_crtc_info(connection, output->crtc, timestamp), NULL);
    fprintf(stderr, "x = %d | y = %d | w = %d | h = %d\n",
            crtc->x, crtc->y, crtc->width, crtc->height);
    FREE(crtc);
    FREE(output);
}

FREE(reply);

*) 免责声明:我是该工具的作者。

编辑:请注意,如果您有兴趣使信息保持最新,您还需要监听屏幕更改事件,然后再次查询输出。