FreeBSD works now

This commit is contained in:
Mars 2024-08-29 17:58:42 -04:00
parent 08f0637584
commit b1bbc19d62
4 changed files with 127 additions and 3 deletions

2
.clangd Normal file
View file

@ -0,0 +1,2 @@
Diagnostics:
Suppress: -Wmissing-template-arg-list-after-template-kw

View file

@ -63,6 +63,8 @@ source_file_names = [
if host_machine.system() == 'linux'
source_file_names += ['src/os/linux.cpp']
elif host_machine.system() == 'freebsd'
source_file_names += ['src/os/freebsd.cpp']
elif host_machine.system() == 'darwin'
source_file_names += [
'src/os/macos.cpp',
@ -90,7 +92,7 @@ if host_machine.system() == 'darwin'
deps += dependency('MediaPlayer')
deps += dependency('SystemConfiguration')
deps += dependency('iconv')
elif host_machine.system() == 'linux'
elif host_machine.system() == 'linux' or host_machine.system() == 'freebsd'
deps += dependency('sdbus-c++')
deps += dependency('x11')
endif

View file

@ -80,8 +80,12 @@ fn main() -> i32 {
if (weather.enabled)
fmt::println("It is {}°F in {}", std::lround(weatherInfo.main.temp), weatherInfo.name);
if (nowPlayingEnabled)
fmt::println("{}", GetNowPlaying());
if (nowPlayingEnabled) {
const string nowPlaying = GetNowPlaying();
if (!nowPlaying.empty())
fmt::println("{}", nowPlaying);
else fmt::println("No song playing");
}
return 0;
}

116
src/os/freebsd.cpp Normal file
View file

@ -0,0 +1,116 @@
#ifdef __FreeBSD__
#include <fmt/format.h>
#include <fstream>
#include <iostream>
#include <sys/sysctl.h>
#include <sdbus-c++/sdbus-c++.h>
#include "os.h"
fn GetMemInfo() -> u64 {
u64 mem = 0;
usize size = sizeof(mem);
sysctlbyname("hw.physmem", &mem, &size, nullptr, 0);
return mem;
}
fn GetOSVersion() -> string {
std::ifstream file("/etc/os-release");
if (!file.is_open()) {
std::cerr << "Failed to open /etc/os-release" << std::endl;
return ""; // Return empty string indicating failure
}
string line;
const string prefix = "PRETTY_NAME=";
while (std::getline(file, line)) {
if (line.find(prefix) == 0) {
string prettyName = line.substr(prefix.size());
// Remove surrounding quotes if present
if (!prettyName.empty() && prettyName.front() == '"' && prettyName.back() == '"')
prettyName = prettyName.substr(1, prettyName.size() - 2);
return prettyName;
}
}
return ""; // Return empty string if PRETTY_NAME= line not found
}
fn GetMprisPlayers(sdbus::IConnection& connection) -> std::vector<string> {
const sdbus::ServiceName dbusInterface = sdbus::ServiceName("org.freedesktop.DBus");
const sdbus::ObjectPath dbusObjectPath = sdbus::ObjectPath("/org/freedesktop/DBus");
const char* dbusMethodListNames = "ListNames";
const std::unique_ptr<sdbus::IProxy> dbusProxy =
createProxy(connection, dbusInterface, dbusObjectPath);
std::vector<string> names;
dbusProxy->callMethod(dbusMethodListNames).onInterface(dbusInterface).storeResultsTo(names);
std::vector<string> mprisPlayers;
for (const std::basic_string<char>& name : names)
if (const char* mprisInterfaceName = "org.mpris.MediaPlayer2";
name.find(mprisInterfaceName) != std::string::npos)
mprisPlayers.push_back(name);
return mprisPlayers;
}
fn GetActivePlayer(const std::vector<string>& mprisPlayers) -> string {
if (!mprisPlayers.empty())
return mprisPlayers.front();
return "";
}
fn GetNowPlaying() -> string {
try {
const char *playerObjectPath = "/org/mpris/MediaPlayer2",
*playerInterfaceName = "org.mpris.MediaPlayer2.Player";
std::unique_ptr<sdbus::IConnection> connection = sdbus::createSessionBusConnection();
std::vector<string> mprisPlayers = GetMprisPlayers(*connection);
if (mprisPlayers.empty())
return "";
string activePlayer = GetActivePlayer(mprisPlayers);
if (activePlayer.empty())
return "";
auto playerProxy = sdbus::createProxy(
*connection, sdbus::ServiceName(activePlayer), sdbus::ObjectPath(playerObjectPath)
);
sdbus::Variant metadataVariant =
playerProxy->getProperty("Metadata").onInterface(playerInterfaceName);
if (metadataVariant.containsValueOfType<std::map<std::string, sdbus::Variant>>()) {
const auto& metadata = metadataVariant.get<std::map<std::string, sdbus::Variant>>();
auto iter = metadata.find("xesam:title");
if (iter != metadata.end() && iter->second.containsValueOfType<std::string>())
return iter->second.get<std::string>();
}
} catch (const sdbus::Error& e) {
if (e.getName() != "com.github.altdesktop.playerctld.NoActivePlayer")
return fmt::format("Error: {}", e.what());
return "No active player";
}
return "";
}
#endif