This commit is contained in:
Mars 2025-05-03 15:21:47 -04:00
parent 311484b724
commit eda5a3d742
Signed by: pupbrained
GPG key ID: 0FF5B8826803F895
3 changed files with 29 additions and 207 deletions

View file

@ -3,17 +3,15 @@
// clang-format off // clang-format off
#include <File.h> // For BFile #include <File.h> // For BFile
#include <AppFileInfo.h> // For BAppFileInfo and version_info #include <AppFileInfo.h> // For BAppFileInfo and version_info
#include <String.h> // For BString (optional, can use std::string #include <Errors.h> // B_OK, strerror, status_t
#include <Errors.h> // Haiku specific: Defines B_OK and strerror function #include <OS.h> // get_system_info, system_info
#include <OS.h> // Haiku specific: Defines get_system_info, system_info, status_t
#include <climits> // PATH_MAX #include <climits> // PATH_MAX
#include <cstring> // std::strlen #include <cstring> // std::strlen
#include <dbus/dbus-protocol.h> // DBUS_TYPE_* #include <dbus/dbus-protocol.h> // DBUS_TYPE_*
#include <dbus/dbus-shared.h> // DBUS_BUS_SESSION #include <dbus/dbus-shared.h> // DBUS_BUS_SESSION
#include <os/package/PackageDefs.h> // Should define typedef ... BPackageKit::BPackageInfoSet; #include <os/package/PackageDefs.h> // BPackageKit::BPackageInfoSet
#include <os/package/PackageInfoSet.h> // Defines BPackageKit::BPackageInfo (template argument) #include <os/package/PackageInfoSet.h> // BPackageKit::BPackageInfo
#include <os/package/PackageRoster.h> // Defines BPackageKit::BPackageRoster #include <os/package/PackageRoster.h> // BPackageKit::BPackageRoster
#include <support/Errors.h> // For B_OK, status_t, strerror
#include <sys/socket.h> // ucred, getsockopt, SOL_SOCKET, SO_PEERCRED #include <sys/socket.h> // ucred, getsockopt, SOL_SOCKET, SO_PEERCRED
#include <sys/statvfs.h> // statvfs #include <sys/statvfs.h> // statvfs
#include <utility> // std::move #include <utility> // std::move
@ -24,8 +22,6 @@
#include "src/util/logging.hpp" #include "src/util/logging.hpp"
#include "src/util/types.hpp" #include "src/util/types.hpp"
#include "src/wrappers/dbus.hpp" #include "src/wrappers/dbus.hpp"
#include "src/wrappers/wayland.hpp"
#include "src/wrappers/xcb.hpp"
#include "os.hpp" #include "os.hpp"
// clang-format on // clang-format on
@ -34,186 +30,30 @@ using namespace util::types;
using util::error::DracError, util::error::DracErrorCode; using util::error::DracError, util::error::DracErrorCode;
using util::helpers::GetEnv; using util::helpers::GetEnv;
namespace {
fn GetX11WindowManager() -> Result<String, DracError> {
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<ConnError> 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<atom_t, DracError> {
const ReplyGuard<intern_atom_reply_t> reply(
intern_atom_reply(conn.get(), intern_atom(conn.get(), 0, static_cast<u16>(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<atom_t, DracError> supportingWmCheckAtom = internAtom("_NET_SUPPORTING_WM_CHECK");
const Result<atom_t, DracError> wmNameAtom = internAtom("_NET_WM_NAME");
const Result<atom_t, DracError> 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<get_property_reply_t> 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<window_t*>(get_property_value(wmWindowReply.get()));
const ReplyGuard<get_property_reply_t> 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<const char*>(get_property_value(wmNameReply.get()));
const usize length = get_property_value_length(wmNameReply.get());
return String(nameData, length);
}
fn GetWaylandCompositor() -> Result<String, DracError> {
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<char, 128> 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<char, PATH_MAX> 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 { namespace os {
fn GetOSVersion() -> Result<String, DracError> { fn GetOSVersion() -> Result<String, DracError> {
BFile file; BFile file;
status_t status = file.SetTo("/boot/system/lib/libbe.so", B_READ_ONLY); status_t status = file.SetTo("/boot/system/lib/libbe.so", B_READ_ONLY);
if (status != B_OK) { if (status != B_OK)
return Err(DracError(DracErrorCode::InternalError, "Error opening /boot/system/lib/libbe.so")); return Err(DracError(DracErrorCode::InternalError, "Error opening /boot/system/lib/libbe.so"));
}
BAppFileInfo appInfo; BAppFileInfo appInfo;
status = appInfo.SetTo(&file); status = appInfo.SetTo(&file);
if (status != B_OK) { if (status != B_OK)
return Err(DracError(DracErrorCode::InternalError, "Error initializing BAppFileInfo")); return Err(DracError(DracErrorCode::InternalError, "Error initializing BAppFileInfo"));
}
version_info versionInfo; version_info versionInfo;
status = appInfo.GetVersionInfo(&versionInfo, B_APP_VERSION_KIND); status = appInfo.GetVersionInfo(&versionInfo, B_APP_VERSION_KIND);
if (status != B_OK) { if (status != B_OK)
return Err(DracError(DracErrorCode::InternalError, "Error reading version info attribute")); return Err(DracError(DracErrorCode::InternalError, "Error reading version info attribute"));
}
std::string versionShortString = versionInfo.short_info; String versionShortString = versionInfo.short_info;
if (versionShortString.empty()) { if (versionShortString.empty())
return Err(DracError(DracErrorCode::InternalError, "Version info short_info is empty")); return Err(DracError(DracErrorCode::InternalError, "Version info short_info is empty"));
}
return std::format("Haiku {}", versionShortString); return std::format("Haiku {}", versionShortString);
} }
@ -359,26 +199,9 @@ namespace os {
return MediaInfo(std::move(title), std::move(artist)); return MediaInfo(std::move(title), std::move(artist));
} }
fn GetWindowManager() -> Result<String, DracError> { fn GetWindowManager() -> Result<String, DracError> { return "app_server"; }
if (Result<String, DracError> waylandResult = GetWaylandCompositor())
return *waylandResult;
if (Result<String, DracError> x11Result = GetX11WindowManager()) fn GetDesktopEnvironment() -> Result<String, DracError> { return "Haiku Desktop Environment"; }
return *x11Result;
return Err(DracError(DracErrorCode::NotFound, "Could not detect window manager (Wayland/X11) or both failed"));
}
fn GetDesktopEnvironment() -> Result<String, DracError> {
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<String, DracError> { return GetEnv("DESKTOP_SESSION"); });
}
fn GetShell() -> Result<String, DracError> { fn GetShell() -> Result<String, DracError> {
if (const Result<String, DracError> shellPath = GetEnv("SHELL")) { if (const Result<String, DracError> shellPath = GetEnv("SHELL")) {
@ -407,8 +230,7 @@ namespace os {
if (gethostname(hostnameBuffer.data(), hostnameBuffer.size()) != 0) if (gethostname(hostnameBuffer.data(), hostnameBuffer.size()) != 0)
return Err(DracError( return Err(DracError(
DracErrorCode::ApiUnavailable, DracErrorCode::ApiUnavailable, std::format("gethostname() failed: {} (errno {})", strerror(errno), errno)
std::format("gethostname() failed: {} (errno {})", strerror(errno), errno)
)); ));
hostnameBuffer.at(HOST_NAME_MAX) = '\0'; hostnameBuffer.at(HOST_NAME_MAX) = '\0';

View file

@ -1,6 +1,6 @@
#pragma once #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 // clang-format off
#include <wayland-client.h> // Wayland client library #include <wayland-client.h> // Wayland client library
@ -96,4 +96,4 @@ namespace wl {
}; };
} // namespace wl } // namespace wl
#endif // __linux__ || __FreeBSD__ || __DragonFly__ || __NetBSD__ || __HAIKU__ #endif // __linux__ || __FreeBSD__ || __DragonFly__ || __NetBSD__

View file

@ -1,6 +1,6 @@
#pragma once #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 // clang-format off
#include <xcb/xcb.h> // XCB library #include <xcb/xcb.h> // XCB library
@ -171,4 +171,4 @@ namespace xcb {
}; };
} // namespace xcb } // namespace xcb
#endif // __linux__ || __FreeBSD__ || __DragonFly__ || __NetBSD__ || __HAIKU__ #endif // __linux__ || __FreeBSD__ || __DragonFly__ || __NetBSD__