This commit is contained in:
Mars 2025-05-12 22:55:06 -04:00
parent 41ece0e405
commit 4f8379a639
Signed by: pupbrained
GPG key ID: 0FF5B8826803F895
3 changed files with 19 additions and 173 deletions

View file

@ -1,155 +0,0 @@
#ifdef __HAIKU__
// clang-format off
#include <AppFileInfo.h> // For BAppFileInfo and version_info
#include <Errors.h> // B_OK, strerror, status_t
#include <File.h> // For BFile
#include <OS.h> // get_system_info, system_info
#include <climits> // PATH_MAX
#include <cstring> // std::strlen
#include <dbus/dbus-protocol.h> // DBUS_TYPE_*
#include <dbus/dbus-shared.h> // DBUS_BUS_SESSION
#include <os/package/PackageDefs.h> // BPackageKit::BPackageInfoSet
#include <os/package/PackageInfoSet.h> // BPackageKit::BPackageInfo
#include <os/package/PackageRoster.h> // BPackageKit::BPackageRoster
#include <sys/socket.h> // ucred, getsockopt, SOL_SOCKET, SO_PEERCRED
#include <sys/statvfs.h> // statvfs
#include <utility> // std::move
#include "src/core/package.hpp"
#include "src/util/defs.hpp"
#include "src/util/error.hpp"
#include "src/util/helpers.hpp"
#include "src/util/logging.hpp"
#include "src/util/types.hpp"
#include "os.hpp"
// clang-format on
using namespace util::types;
using util::error::DracError, util::error::DracErrorCode;
using util::helpers::GetEnv;
namespace os {
fn GetOSVersion() -> Result<String> {
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"));
BAppFileInfo appInfo;
status = appInfo.SetTo(&file);
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"));
String versionShortString = versionInfo.short_info;
if (versionShortString.empty())
return Err(DracError(DracErrorCode::InternalError, "Version info short_info is empty"));
return std::format("Haiku {}", versionShortString);
}
fn GetMemInfo() -> Result<u64> {
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))));
return static_cast<u64>(sysinfo.max_pages) * B_PAGE_SIZE;
}
fn GetNowPlaying() -> Result<MediaInfo> {
return Err(DracError(DracErrorCode::NotSupported, "Now playing is not supported on Haiku"));
}
fn GetWindowManager() -> Result<String> {
return "app_server";
}
fn GetDesktopEnvironment() -> Result<String> {
return "Haiku Desktop Environment";
}
fn GetShell() -> Result<String> {
if (const Result<String> shellPath = GetEnv("SHELL")) {
// clang-format off
constexpr Array<Pair<StringView, StringView>, 5> shellMap {{
{ "bash", "Bash" },
{ "zsh", "Zsh" },
{ "fish", "Fish" },
{ "nu", "Nushell" },
{ "sh", "SH" }, // sh last because other shells contain "sh"
}};
// clang-format on
for (const auto& [exe, name] : shellMap)
if (shellPath->contains(exe))
return String(name);
return *shellPath; // fallback to the raw shell path
}
return Err(DracError(DracErrorCode::NotFound, "Could not find SHELL environment variable"));
}
fn GetHost() -> Result<String> {
Array<char, HOST_NAME_MAX + 1> hostnameBuffer {};
if (gethostname(hostnameBuffer.data(), hostnameBuffer.size()) != 0)
return Err(DracError(
DracErrorCode::ApiUnavailable, std::format("gethostname() failed: {} (errno {})", strerror(errno), errno)
));
hostnameBuffer.at(HOST_NAME_MAX) = '\0';
return String(hostnameBuffer.data(), hostnameBuffer.size());
}
fn GetKernelVersion() -> Result<String> {
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))));
return std::to_string(sysinfo.kernel_version);
}
fn GetDiskUsage() -> Result<DiskSpace> {
struct statvfs stat;
if (statvfs("/boot", &stat) == -1)
return Err(DracError::withErrno(std::format("Failed to get filesystem stats for '/boot' (statvfs call failed)")));
return DiskSpace {
.used_bytes = (stat.f_blocks * stat.f_frsize) - (stat.f_bfree * stat.f_frsize),
.total_bytes = stat.f_blocks * stat.f_frsize,
};
}
} // namespace os
namespace package {
fn GetHaikuCount() -> Result<u64> {
BPackageKit::BPackageRoster roster;
BPackageKit::BPackageInfoSet packageList;
const status_t status = roster.GetActivePackages(BPackageKit::B_PACKAGE_INSTALLATION_LOCATION_SYSTEM, packageList);
if (status != B_OK)
return Err(DracError(DracErrorCode::ApiUnavailable, "Failed to get active package list"));
return static_cast<u64>(packageList.CountInfos());
}
} // namespace package
#endif // __HAIKU__

View file

@ -3,6 +3,7 @@
#include "MetNoService.hpp"
#include <chrono> // std::chrono::{system_clock, minutes, seconds}
#include <ctime> // std::tm, std::timegm
#include <curl/curl.h> // CURL, CURLcode, CURLOPT_*, CURLE_OK
#include <curl/easy.h> // curl_easy_init, curl_easy_setopt, curl_easy_perform, curl_easy_strerror, curl_easy_cleanup
#include <format> // std::format
@ -216,9 +217,9 @@ namespace {
return 0;
#ifdef _WIN32
return static_cast<util::types::usize>(_mkgmtime(&time));
return static_cast<usize>(_mkgmtime(&time));
#else
return static_cast<util::types::usize>(timegm(&time));
return static_cast<usize>(timegm(&time));
#endif
}
} // namespace
@ -297,4 +298,4 @@ fn MetNoService::getWeatherInfo() const -> util::types::Result<WeatherReport> {
return Err(writeResult.error());
return out;
}
}

View file

@ -2,13 +2,13 @@
#include "OpenMeteoService.hpp"
#include <chrono>
#include <curl/curl.h>
#include <curl/easy.h>
#include <format>
#include <glaze/json/read.hpp>
#include <sstream>
#include <string>
#include <chrono> // std::chrono::{system_clock, minutes, seconds}
#include <ctime> // std::tm, std::timegm
#include <curl/curl.h> // CURL, CURLcode, CURLOPT_*, CURLE_OK
#include <curl/easy.h> // curl_easy_init, curl_easy_setopt, curl_easy_perform, curl_easy_strerror, curl_easy_cleanup
#include <format> // std::format
#include <glaze/json/read.hpp> // glz::read
#include <sstream> // std::istringstream
#include "Util/Caching.hpp"
#include "Util/Error.hpp"
@ -60,31 +60,31 @@ struct glz::meta<weather::OpenMeteoResponse::CurrentWeather> : weather::CurrentW
namespace {
using glz::opts;
using util::error::DracError, util::error::DracErrorCode;
using util::types::usize, util::types::Err;
using util::types::usize, util::types::Err, util::types::String;
constexpr opts glazeOpts = { .error_on_unknown_keys = false };
fn WriteCallback(void* contents, size_t size, size_t nmemb, std::string* str) -> size_t {
size_t totalSize = size * nmemb;
fn WriteCallback(void* contents, usize size, usize nmemb, String* str) -> usize {
usize totalSize = size * nmemb;
str->append(static_cast<char*>(contents), totalSize);
return totalSize;
}
fn parse_iso8601_to_epoch(const std::string& iso8601) -> usize {
fn parse_iso8601_to_epoch(const String& iso8601) -> usize {
std::tm time = {};
std::istringstream stream(iso8601);
stream >> std::get_time(&time, "%Y-%m-%dT%H:%M");
if (stream.fail())
return 0;
#ifdef _WIN32
return static_cast<util::types::usize>(_mkgmtime(&time));
return static_cast<usize>(_mkgmtime(&time));
#else
return static_cast<util::types::usize>(timegm(&time));
return static_cast<usize>(timegm(&time));
#endif
}
} // namespace
OpenMeteoService::OpenMeteoService(double lat, double lon, std::string units)
OpenMeteoService::OpenMeteoService(f64 lat, f64 lon, String units)
: m_lat(lat), m_lon(lon), m_units(std::move(units)) {}
fn OpenMeteoService::getWeatherInfo() const -> util::types::Result<WeatherReport> {
@ -153,4 +153,4 @@ fn OpenMeteoService::getWeatherInfo() const -> util::types::Result<WeatherReport
return Err(writeResult.error());
return out;
}
}