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

@@ -1,7 +1,7 @@
#ifndef MACROS_H #ifndef MACROS_H
#define MACROS_H #define MACROS_H
#define HYPRLAND_CONF_DIR "/home/typo/.config/hypr/" #define HYPRLAND_CONF_DIR ".config/hypr/"
#define FLOATING_RULE_CONF_FILE HYPRLAND_CONF_DIR "hyprland/floatbydefault.conf" #define FLOATING_RULE_CONF_FILE HYPRLAND_CONF_DIR "hyprland/floatbydefault.conf"
#define HYPRCTL_BINARY "/usr/bin/hyprctl" #define HYPRCTL_BINARY "/usr/bin/hyprctl"
@@ -10,7 +10,5 @@
#define CAT_PATH "/usr/bin/cat" #define CAT_PATH "/usr/bin/cat"
#define FLOATING_RULE "windowrule = float,class:.*" #define FLOATING_RULE "windowrule = float,class:.*"
#define SET_FLOATING_RULE ECHO_PATH " 'windowrule = float,class:.*' > " FLOATING_RULE_CONF_FILE
#define REMOVE_FLOATING_RULE CAT_PATH " " NULL_PATH " > " FLOATING_RULE_CONF_FILE
#endif #endif

View File

@@ -6,6 +6,7 @@
class ShellService { class ShellService {
public: public:
static std::string exec(const std::string& command); static std::string exec(const std::string& command);
static std::string getHomePath();
}; };
#endif #endif

View File

@@ -45,13 +45,13 @@ void HyprlandService::toggleClientFloating(Client& c) {
void HyprlandService::setFloatingRule(bool b) { void HyprlandService::setFloatingRule(bool b) {
if (b) { if (b) {
FileServices::createOrOverwriteFile(FLOATING_RULE_CONF_FILE, FLOATING_RULE); FileServices::createOrOverwriteFile(ShellService::getHomePath()+FLOATING_RULE_CONF_FILE, FLOATING_RULE);
} }
else { else {
FileServices::emptyFile(FLOATING_RULE_CONF_FILE); FileServices::emptyFile(ShellService::getHomePath()+FLOATING_RULE_CONF_FILE);
} }
}; };
bool HyprlandService::isFloatingRulePresent() { 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 "../include/ShellService.h"
#include <pwd.h>
#include <unistd.h>
std::string ShellService::exec(const std::string& command) { std::string ShellService::exec(const std::string& command) {
char buffer[128]; char buffer[128];
@@ -19,4 +21,22 @@ std::string ShellService::exec(const std::string& command) {
} }
return result; 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);
}