Parsing home directory from the operating system dynamically

This commit is contained in:
2025-06-11 09:55:00 +02:00
parent 256d27cf13
commit 1c3d34544e
4 changed files with 26 additions and 7 deletions

View File

@@ -45,13 +45,13 @@ void HyprlandService::toggleClientFloating(Client& c) {
void HyprlandService::setFloatingRule(bool b) {
if (b) {
FileServices::createOrOverwriteFile(FLOATING_RULE_CONF_FILE, FLOATING_RULE);
FileServices::createOrOverwriteFile(ShellService::getHomePath()+FLOATING_RULE_CONF_FILE, FLOATING_RULE);
}
else {
FileServices::emptyFile(FLOATING_RULE_CONF_FILE);
FileServices::emptyFile(ShellService::getHomePath()+FLOATING_RULE_CONF_FILE);
}
};
bool HyprlandService::isFloatingRulePresent() {
return FileServices::doesNonEmptyFileExist(FLOATING_RULE_CONF_FILE);
return FileServices::doesNonEmptyFileExist(ShellService::getHomePath()+FLOATING_RULE_CONF_FILE);
};

View File

@@ -1,4 +1,6 @@
#include "../include/ShellService.h"
#include <pwd.h>
#include <unistd.h>
std::string ShellService::exec(const std::string& command) {
char buffer[128];
@@ -19,4 +21,22 @@ std::string ShellService::exec(const std::string& command) {
}
return result;
};
};
std::string ShellService::getHomePath()
{
char* home = getenv("HOME");
if (home && *home) {
return std::string(home);
}
// Fallback: try to get home directory from passwd if HOME is not set
// This assumes that the 'htt' process is ran with the user as the process owner. Should it be run with systemd or some other wonky method, it will behave unexpectedly
struct passwd* pw = getpwuid(getuid());
if (pw && pw->pw_dir) {
return std::string(pw->pw_dir);
}
// As a last resort, exit
exit(1);
}