Add xcb test code

This commit is contained in:
2025-08-31 15:16:25 +08:00
parent 055116147e
commit 0573dea159
3 changed files with 74 additions and 4 deletions

View File

@@ -1,12 +1,16 @@
#include <QApplication>
#include <QTimer>
#include <QX11Info>
#include "qt_dialog.h"
#include <xcb/xcb.h>
// Qt 头文件必须在 Motif/X11 之前
#include <X11/Intrinsic.h>
#include <Xm/Xm.h>
#include <Xm/PushB.h>
#include <unistd.h> // for usleep
#include <iostream>
// 全局 Qt 应用和对话框指针
QApplication *g_app = nullptr;
@@ -16,7 +20,7 @@ void openQtDialog(Widget w, XtPointer client_data, XtPointer call_data)
{
auto dialog = new QtDialog();
dialog->setAttribute(Qt::WA_DeleteOnClose); // 关闭时自动删除
dialog->show(); // 非阻塞显示
dialog->show(); // 非阻塞显示
}
// Motif 主循环中处理 Qt 事件
@@ -36,6 +40,34 @@ int main(int argc, char **argv)
// 创建 QApplication必须在 Qt 头文件之后)
g_app = new QApplication(argc, nullptr);
// int screen_num;
// xcb_connection_t *connection = xcb_connect(NULL, &screen_num);
xcb_connection_t *connection = QX11Info::connection();
// Qt 内部管理的 screen_num
int screen_num = QX11Info::appScreen();
if (xcb_connection_has_error(connection))
{
fprintf(stderr, "无法连接到 X 服务器\n");
return -1;
}
const xcb_setup_t *setup = xcb_get_setup(connection);
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
for (int i = 0; i <= screen_num; i++)
{
xcb_screen_t *screen = iter.data;
xcb_screen_next(&iter);
printf("屏幕根窗口: %u, 尺寸: %dx%d\n",
screen->root, screen->width_in_pixels, screen->height_in_pixels);
std::cout << "screen_num: " << screen_num << std::endl;
std::cout << "screen root window: " << screen->root << std::endl;
std::cout << "screen size: " << screen->width_in_pixels
<< "x" << screen->height_in_pixels << std::endl;
}
// 创建 Motif 主窗口
toplevel = XtVaAppInitialize(&app_context, "MotifQtApp", NULL, 0,
&argc, argv, NULL, NULL);
@@ -65,6 +97,7 @@ int main(int argc, char **argv)
// 清理(一般不会执行到这里)
delete g_app;
// xcb_disconnect(connection);
return 0;
}