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

32
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,32 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/MotifQtApp",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}

View File

@@ -10,10 +10,12 @@ set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt5 COMPONENTS Widgets X11Extras REQUIRED)
# Motif
find_package(X11 REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(XCB REQUIRED xcb)
find_library(MOTIF_LIB Xm)
message(STATUS
@@ -33,6 +35,9 @@ set(SOURCES
add_executable(MotifQtApp ${SOURCES})
# 链接库
target_link_libraries(MotifQtApp PRIVATE Qt5::Widgets ${MOTIF_LIB} X11::X11 ${X11_Xt_LIB})
target_link_libraries(MotifQtApp PRIVATE Qt5::Widgets ${MOTIF_LIB} X11::X11 ${X11_Xt_LIB} ${XCB_LIBRARIES} Qt5::X11Extras)
target_include_directories(MotifQtApp PRIVATE ${X11_INCLUDE_DIR})
target_include_directories(MotifQtApp PRIVATE ${X11_INCLUDE_DIR} ${XCB_INCLUDE_DIRS} )
# 处理可能的编译器参数
target_compile_options(MotifQtApp PRIVATE ${XCB_CFLAGS_OTHER})

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;
}