【发布时间】:2019-04-11 15:52:59
【问题描述】:
我正在探索使用 XCB 创建一个窗口管理器,但我很早就遇到了一些麻烦。我的代码甚至无法使用xcb_connect 连接到 XCB。我认为这很简单,但我得到了一些非常奇怪的行为。我的代码如下所示:
#include <stdio.h>
#include <xcb/xcb.h>
int i = 0;
int connect(xcb_connection_t** conn) {
xcb_connection_t* try_conn = xcb_connect(NULL, NULL);
int status = 0;
int conn_status = xcb_connection_has_error(try_conn);
if (conn_status != 0) {
i = i + 1;
switch (conn_status) {
case XCB_CONN_ERROR:
printf("Error connecting to the X Server, try %d\n", i);
break;
case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
printf("Connection closed, extension not supported\n");
break;
case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
printf("Connection closed, memory insufficient\n");
break;
case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
printf("Connection closed, required length exceeded\n");
break;
case XCB_CONN_CLOSED_PARSE_ERR:
printf("Connection closed, parse error\n");
break;
case XCB_CONN_CLOSED_INVALID_SCREEN:
printf("Connection closed, invalid screen\n");
break;
default:
printf("Connection failed with unknown cause\n");
break;
}
status = 1;
} else {
*conn = try_conn;
status = 0;
}
return status;
}
int main() {
xcb_connection_t* conn = NULL;
if (connect(&conn) != 0) {
printf("Error connecting to the X Server\n");
return -1;
}
return 0;
}
每次我运行程序时,它都会打印出Error connecting the the X Server, try %d\n 8191 次。当我查看 gdb 的情况时,似乎每次我调用 xcb_connect 时,我的代码都会在 xcb_connect_to_display_with_auth_info() 和我的 connect() 函数之间进行深度递归(比如数千帧深度)。
真正让我困惑的部分是xcb_connect_to_display_with_auth_info() 甚至可以调用我的connect() 函数,因为它来自一个单独的库并且我没有传递指向我的函数的指针。我的代码在我看来它的行为应该是完全“线性的”,但事实并非如此。
我正在测试窗口管理器,方法是使用 X 服务器名称 :1 运行 Xephyr,并在运行程序之前将 DISPLAY 设置为 :1。
我对 XCB 和 C 本身有点陌生,所以我可能遗漏了一些明显的东西,但我会很感激任何指针。到目前为止,我一直在寻找hootwm 的灵感。
【问题讨论】:
-
会不会是你在运行 Wayland?
-
@perreal 不,我在 X 上运行 i3。不过我已经取得了一些不错的进展。我将所有内容移至主函数并注释掉其他函数,并且连接正常。我猜可能存在名称冲突或其他问题,尽管我正在使用 -Wall 进行编译,并且我认为会出现类似的情况。
-
哦,是的,我改了函数名,它也可以了。
标签: c recursion xorg window-managers xcb