到目前为止,我已经获得了所有显示器。监视器是一个屏幕。所以我所做的是:
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);
Run Code Online (Sandbox Code Playgroud)
这给了我宽度和高度。但是,获得 x 和 y 至关重要。xcb_get_window_attributes_cookie_t在screen->root获取 x 和 y 的途中正在做什么?
我在这里阅读 - http://www.linuxhowtos.org/manpages/3/xcb_get_window_attributes_unchecked.htm - 但没有给出 x/y 坐标。
我假设您对显示器的几何形状感兴趣,即实际的物理设备,而不是 X 屏幕的几何形状。
在这种情况下,根窗口不是您感兴趣的。这里基本上有两件不同的事情需要考虑:
要了解如何查询各种详细信息,我的建议是查看执行此操作的程序。一个规范的建议是支持多头设置的窗口管理器,例如 i3 窗口管理器,它实际上同时支持 Xinerama 和 RandR,因此您可以查看两者的源代码。
您正在寻找的信息可以在src/randr.c和 中找到src/xinerama.c。必要的 RandR API 调用是
xcb_randr_get_screen_resources_currentxcb_randr_get_screen_resources_current_outputsxcb_randr_get_output_infoxcb_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);
Run Code Online (Sandbox Code Playgroud)
*) 免责声明:我是该工具的作者。
编辑:请注意,如果您有兴趣使信息保持最新,您还需要监听屏幕更改事件,然后再次查询输出。