From eda5a3d74253e5d752c5ed06e3d998ff73dc22b6 Mon Sep 17 00:00:00 2001 From: pupbrained Date: Sat, 3 May 2025 15:21:47 -0400 Subject: [PATCH] fixes --- src/os/haiku.cpp | 228 +++++---------------------------------- src/wrappers/wayland.hpp | 4 +- src/wrappers/xcb.hpp | 4 +- 3 files changed, 29 insertions(+), 207 deletions(-) diff --git a/src/os/haiku.cpp b/src/os/haiku.cpp index 30186be..5e44c42 100644 --- a/src/os/haiku.cpp +++ b/src/os/haiku.cpp @@ -1,19 +1,17 @@ #ifdef __HAIKU__ // clang-format off -#include // For BFile -#include // For BAppFileInfo and version_info -#include // For BString (optional, can use std::string -#include // Haiku specific: Defines B_OK and strerror function -#include // Haiku specific: Defines get_system_info, system_info, status_t +#include // For BFile +#include // For BAppFileInfo and version_info +#include // B_OK, strerror, status_t +#include // get_system_info, system_info #include // PATH_MAX #include // std::strlen #include // DBUS_TYPE_* #include // DBUS_BUS_SESSION -#include // Should define typedef ... BPackageKit::BPackageInfoSet; -#include // Defines BPackageKit::BPackageInfo (template argument) -#include // Defines BPackageKit::BPackageRoster -#include // For B_OK, status_t, strerror +#include // BPackageKit::BPackageInfoSet +#include // BPackageKit::BPackageInfo +#include // BPackageKit::BPackageRoster #include // ucred, getsockopt, SOL_SOCKET, SO_PEERCRED #include // statvfs #include // std::move @@ -24,8 +22,6 @@ #include "src/util/logging.hpp" #include "src/util/types.hpp" #include "src/wrappers/dbus.hpp" -#include "src/wrappers/wayland.hpp" -#include "src/wrappers/xcb.hpp" #include "os.hpp" // clang-format on @@ -34,193 +30,37 @@ using namespace util::types; using util::error::DracError, util::error::DracErrorCode; using util::helpers::GetEnv; -namespace { - fn GetX11WindowManager() -> Result { - using namespace xcb; - - const DisplayGuard conn; - - if (!conn) - if (const i32 err = connection_has_error(conn.get())) - return Err(DracError(DracErrorCode::ApiUnavailable, [&] -> String { - if (const Option connErr = getConnError(err)) { - switch (*connErr) { - case Generic: return "Stream/Socket/Pipe Error"; - case ExtNotSupported: return "Extension Not Supported"; - case MemInsufficient: return "Insufficient Memory"; - case ReqLenExceed: return "Request Length Exceeded"; - case ParseErr: return "Display String Parse Error"; - case InvalidScreen: return "Invalid Screen"; - case FdPassingFailed: return "FD Passing Failed"; - default: return std::format("Unknown Error Code ({})", err); - } - } - - return std::format("Unknown Error Code ({})", err); - }())); - - fn internAtom = [&conn](const StringView name) -> Result { - const ReplyGuard reply( - intern_atom_reply(conn.get(), intern_atom(conn.get(), 0, static_cast(name.size()), name.data()), nullptr) - ); - - if (!reply) - return Err( - DracError(DracErrorCode::PlatformSpecific, std::format("Failed to get X11 atom reply for '{}'", name)) - ); - - return reply->atom; - }; - - const Result supportingWmCheckAtom = internAtom("_NET_SUPPORTING_WM_CHECK"); - const Result wmNameAtom = internAtom("_NET_WM_NAME"); - const Result utf8StringAtom = internAtom("UTF8_STRING"); - - if (!supportingWmCheckAtom || !wmNameAtom || !utf8StringAtom) { - if (!supportingWmCheckAtom) - error_log("Failed to get _NET_SUPPORTING_WM_CHECK atom"); - - if (!wmNameAtom) - error_log("Failed to get _NET_WM_NAME atom"); - - if (!utf8StringAtom) - error_log("Failed to get UTF8_STRING atom"); - - return Err(DracError(DracErrorCode::PlatformSpecific, "Failed to get X11 atoms")); - } - - const ReplyGuard wmWindowReply(get_property_reply( - conn.get(), - get_property(conn.get(), 0, conn.rootScreen()->root, *supportingWmCheckAtom, ATOM_WINDOW, 0, 1), - nullptr - )); - - if (!wmWindowReply || wmWindowReply->type != ATOM_WINDOW || wmWindowReply->format != 32 || - get_property_value_length(wmWindowReply.get()) == 0) - return Err(DracError(DracErrorCode::NotFound, "Failed to get _NET_SUPPORTING_WM_CHECK property")); - - const window_t wmRootWindow = *static_cast(get_property_value(wmWindowReply.get())); - - const ReplyGuard wmNameReply(get_property_reply( - conn.get(), get_property(conn.get(), 0, wmRootWindow, *wmNameAtom, *utf8StringAtom, 0, 1024), nullptr - )); - - if (!wmNameReply || wmNameReply->type != *utf8StringAtom || get_property_value_length(wmNameReply.get()) == 0) - return Err(DracError(DracErrorCode::NotFound, "Failed to get _NET_WM_NAME property")); - - const char* nameData = static_cast(get_property_value(wmNameReply.get())); - const usize length = get_property_value_length(wmNameReply.get()); - - return String(nameData, length); - } - - fn GetWaylandCompositor() -> Result { - const wl::DisplayGuard display; - - if (!display) - return Err(DracError(DracErrorCode::NotFound, "Failed to connect to display (is Wayland running?)")); - - const i32 fileDescriptor = display.fd(); - if (fileDescriptor < 0) - return Err(DracError(DracErrorCode::ApiUnavailable, "Failed to get Wayland file descriptor")); - - ucred cred; - socklen_t len = sizeof(cred); - - if (getsockopt(fileDescriptor, SOL_SOCKET, SO_PEERCRED, &cred, &len) == -1) - return Err(DracError::withErrno("Failed to get socket credentials (SO_PEERCRED)")); - - Array exeLinkPathBuf {}; - - auto [out, size] = std::format_to_n(exeLinkPathBuf.data(), exeLinkPathBuf.size() - 1, "/proc/{}/exe", cred.pid); - - if (out >= exeLinkPathBuf.data() + exeLinkPathBuf.size() - 1) - return Err(DracError(DracErrorCode::InternalError, "Failed to format /proc path (PID too large?)")); - - *out = '\0'; - - const char* exeLinkPath = exeLinkPathBuf.data(); - - Array exeRealPathBuf {}; - - const isize bytesRead = readlink(exeLinkPath, exeRealPathBuf.data(), exeRealPathBuf.size() - 1); - - if (bytesRead == -1) - return Err(DracError::withErrno(std::format("Failed to read link '{}'", exeLinkPath))); - - exeRealPathBuf.at(bytesRead) = '\0'; - - StringView compositorNameView; - - const StringView pathView(exeRealPathBuf.data(), bytesRead); - - StringView filenameView; - - if (const usize lastCharPos = pathView.find_last_not_of('/'); lastCharPos != StringView::npos) { - const StringView relevantPart = pathView.substr(0, lastCharPos + 1); - - if (const usize separatorPos = relevantPart.find_last_of('/'); separatorPos == StringView::npos) - filenameView = relevantPart; - else - filenameView = relevantPart.substr(separatorPos + 1); - } - - if (!filenameView.empty()) - compositorNameView = filenameView; - - if (compositorNameView.empty() || compositorNameView == "." || compositorNameView == "/") - return Err(DracError(DracErrorCode::NotFound, "Failed to get compositor name from path")); - - if (constexpr StringView wrappedSuffix = "-wrapped"; compositorNameView.length() > 1 + wrappedSuffix.length() && - compositorNameView[0] == '.' && compositorNameView.ends_with(wrappedSuffix)) { - const StringView cleanedView = - compositorNameView.substr(1, compositorNameView.length() - 1 - wrappedSuffix.length()); - - if (cleanedView.empty()) - return Err(DracError(DracErrorCode::NotFound, "Compositor name invalid after heuristic")); - - return String(cleanedView); - } - - return String(compositorNameView); - } -} // namespace - namespace os { fn GetOSVersion() -> Result { - BFile file; + BFile file; status_t status = file.SetTo("/boot/system/lib/libbe.so", B_READ_ONLY); - if (status != B_OK) { - return Err(DracError(DracErrorCode::InternalError, "Error opening /boot/system/lib/libbe.so")); - } + if (status != B_OK) + return Err(DracError(DracErrorCode::InternalError, "Error opening /boot/system/lib/libbe.so")); BAppFileInfo appInfo; status = appInfo.SetTo(&file); - if (status != B_OK) { - return Err(DracError(DracErrorCode::InternalError, "Error initializing BAppFileInfo")); - } + if (status != B_OK) + return Err(DracError(DracErrorCode::InternalError, "Error initializing BAppFileInfo")); version_info versionInfo; status = appInfo.GetVersionInfo(&versionInfo, B_APP_VERSION_KIND); - if (status != B_OK) { - return Err(DracError(DracErrorCode::InternalError, "Error reading version info attribute")); - } + if (status != B_OK) + return Err(DracError(DracErrorCode::InternalError, "Error reading version info attribute")); - std::string versionShortString = versionInfo.short_info; + String versionShortString = versionInfo.short_info; - if (versionShortString.empty()) { - return Err(DracError(DracErrorCode::InternalError, "Version info short_info is empty")); - } + if (versionShortString.empty()) + return Err(DracError(DracErrorCode::InternalError, "Version info short_info is empty")); return std::format("Haiku {}", versionShortString); } fn GetMemInfo() -> Result { - system_info sysinfo; - const status_t status = get_system_info(&sysinfo); + system_info sysinfo; + const status_t status = get_system_info(&sysinfo); if (status != B_OK) return Err(DracError(DracErrorCode::InternalError, std::format("get_system_info failed: {}", strerror(status)))); @@ -359,26 +199,9 @@ namespace os { return MediaInfo(std::move(title), std::move(artist)); } - fn GetWindowManager() -> Result { - if (Result waylandResult = GetWaylandCompositor()) - return *waylandResult; + fn GetWindowManager() -> Result { return "app_server"; } - if (Result x11Result = GetX11WindowManager()) - return *x11Result; - - return Err(DracError(DracErrorCode::NotFound, "Could not detect window manager (Wayland/X11) or both failed")); - } - - fn GetDesktopEnvironment() -> Result { - return GetEnv("XDG_CURRENT_DESKTOP") - .transform([](String xdgDesktop) -> String { - if (const usize colon = xdgDesktop.find(':'); colon != String::npos) - xdgDesktop.resize(colon); - - return xdgDesktop; - }) - .or_else([](const DracError&) -> Result { return GetEnv("DESKTOP_SESSION"); }); - } + fn GetDesktopEnvironment() -> Result { return "Haiku Desktop Environment"; } fn GetShell() -> Result { if (const Result shellPath = GetEnv("SHELL")) { @@ -405,10 +228,9 @@ namespace os { fn GetHost() -> Result { Array hostnameBuffer {}; - if (gethostname(hostnameBuffer.data(), hostnameBuffer.size()) != 0) + if (gethostname(hostnameBuffer.data(), hostnameBuffer.size()) != 0) return Err(DracError( - DracErrorCode::ApiUnavailable, - std::format("gethostname() failed: {} (errno {})", strerror(errno), errno) + DracErrorCode::ApiUnavailable, std::format("gethostname() failed: {} (errno {})", strerror(errno), errno) )); hostnameBuffer.at(HOST_NAME_MAX) = '\0'; @@ -417,8 +239,8 @@ namespace os { } fn GetKernelVersion() -> Result { - system_info sysinfo; - const status_t status = get_system_info(&sysinfo); + system_info sysinfo; + const status_t status = get_system_info(&sysinfo); if (status != B_OK) return Err(DracError(DracErrorCode::InternalError, std::format("get_system_info failed: {}", strerror(status)))); diff --git a/src/wrappers/wayland.hpp b/src/wrappers/wayland.hpp index 2d859fe..384be68 100644 --- a/src/wrappers/wayland.hpp +++ b/src/wrappers/wayland.hpp @@ -1,6 +1,6 @@ #pragma once -#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__HAIKU__) +#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) // clang-format off #include // Wayland client library @@ -96,4 +96,4 @@ namespace wl { }; } // namespace wl -#endif // __linux__ || __FreeBSD__ || __DragonFly__ || __NetBSD__ || __HAIKU__ +#endif // __linux__ || __FreeBSD__ || __DragonFly__ || __NetBSD__ diff --git a/src/wrappers/xcb.hpp b/src/wrappers/xcb.hpp index fa1f080..c61b26d 100644 --- a/src/wrappers/xcb.hpp +++ b/src/wrappers/xcb.hpp @@ -1,6 +1,6 @@ #pragma once -#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__HAIKU__) +#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) // clang-format off #include // XCB library @@ -171,4 +171,4 @@ namespace xcb { }; } // namespace xcb -#endif // __linux__ || __FreeBSD__ || __DragonFly__ || __NetBSD__ || __HAIKU__ +#endif // __linux__ || __FreeBSD__ || __DragonFly__ || __NetBSD__