diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..b02cc59 --- /dev/null +++ b/.vscode/launch.json @@ -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 + } + ] + } + ] +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 69a3c14..148818a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) \ No newline at end of file diff --git a/motif_app.cpp b/motif_app.cpp index c74fb7f..01e0429 100644 --- a/motif_app.cpp +++ b/motif_app.cpp @@ -1,12 +1,16 @@ #include #include +#include #include "qt_dialog.h" +#include + // Qt 头文件必须在 Motif/X11 之前 #include #include #include #include // for usleep +#include // 全局 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; }