| 9 | #include "MaaUtils/Logger.h" |
| 10 | |
| 11 | MAA_TOOLKIT_NS_BEGIN |
| 12 | |
| 13 | std::vector<DesktopWindow> DesktopWindowLinuxFinder::find_all() const |
| 14 | { |
| 15 | std::vector<DesktopWindow> result; |
| 16 | |
| 17 | const char* xdg_runtime_dir = std::getenv("XDG_RUNTIME_DIR"); |
| 18 | if (!xdg_runtime_dir) { |
| 19 | return result; |
| 20 | } |
| 21 | |
| 22 | std::error_code ec; |
| 23 | for (const auto& entry : std::filesystem::directory_iterator(xdg_runtime_dir, ec)) { |
| 24 | if (ec) { |
| 25 | LogWarn << "Failed to iterate XDG_RUNTIME_DIR: " << ec.message(); |
| 26 | break; |
| 27 | } |
| 28 | |
| 29 | const auto& filename = entry.path().filename(); |
| 30 | if (filename.string().starts_with("wayland-") && !filename.string().ends_with(".lock")) { |
| 31 | if (!entry.is_socket(ec) || ec) { |
| 32 | LogWarn << "Cannot read status of file: " << entry << "msg: " << ec.message(); |
| 33 | ec.clear(); |
| 34 | continue; |
| 35 | } |
| 36 | |
| 37 | auto display = wl_display_connect(filename.c_str()); |
| 38 | if (display == nullptr) { |
| 39 | LogWarn << "Failed to connect socket: " << filename; |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | uint32_t id = 0; |
| 44 | if (int parsed = std::sscanf(filename.c_str(), "wayland-%u", &id); parsed != 1) { |
| 45 | LogWarn << "Failed to parse wayland socket name: " << filename; |
| 46 | wl_display_disconnect(display); |
| 47 | continue; |
| 48 | } |
| 49 | result.emplace_back( |
| 50 | DesktopWindow { .hwnd = reinterpret_cast<void*>(static_cast<uintptr_t>(id)), |
| 51 | .class_name = MAA_NS::to_u16(entry.path().string()), |
| 52 | .window_name = MAA_NS::to_u16(filename.string()) }); |
| 53 | |
| 54 | wl_display_disconnect(display); |
| 55 | } |
| 56 | } |
| 57 | #ifdef MAA_DEBUG |
| 58 | LogInfo << "Compositor list:" << result; |
| 59 | #endif |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | MAA_TOOLKIT_NS_END |
| 64 | |