2

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

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 是至关重要的。是在做得到x和yxcb_get_window_attributes_cookie_tscreen->root路上吗?

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

4

1 回答 1

6

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

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

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

您正在寻找的信息可以在src/randr.c和中找到src/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);

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

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

于 2016-05-03T17:50:28.840 回答