我正在探索使用 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 X Server, try %d\n
8191 times。当我查看 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
以寻找灵感。
最佳答案
您正在覆盖 C 库的 connect
函数。 XCB 调用该函数来连接到 X11 服务器,但最终却调用了您的函数。
https://linux.die.net/man/2/connect
一个可能的解决方法(除了给你的函数另一个名字)是让它成为static
。
关于c - 使用 XCB 的奇怪递归行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53202661/