我正在探索使用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()和xcb_connect_to_display_with_auth_info()函数之间的深度递归(像数千帧深)中。
真正让我困惑的是,xcb_connect_to_display_with_auth_info()甚至可以调用我的connect()函数,因为它来自一个单独的库,而且我还没有传入指向函数的指针。在我看来,我的代码的行为应该是完全“线性”的,但事实并非如此。
我正在测试窗口管理器,运行Xephyr并使用X服务器名:1,并在运行程序之前将DISPLAY设置为:1。
我对XCB和C本身有点陌生,所以我可能忽略了一些显而易见的东西,但是我很感激任何指点。到目前为止,我一直在寻找hootwm的灵感。
发布于 2018-11-20 10:14:01
您正在重写C库的connect函数。XCB调用该函数以连接到X11服务器,但最终会调用您的函数。https://linux.die.net/man/2/connect
解决这个问题的一个可能的方法(除了给您的函数取另一个名称)是使它成为static。
https://stackoverflow.com/questions/53202661
复制相似问题