Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pkg_check_modules(
wayland-client
wayland-protocols
hyprutils>=0.2.3
hyprlang
hyprwayland-scanner>=0.4.0)

file(GLOB_RECURSE SRCFILES "src/*.cpp")
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ An application to enable a blue-light filter on Hyprland
> !IMPORTANT!
> This app requires hyprland>=0.45.0

## Usage

See `hyprsunset --help`.
## Docs / Configuration
[See the wiki](https://wiki.hyprland.org/Hypr-Ecosystem/hyprlock/)

## Support

Expand Down
27 changes: 27 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
inputs.systems.follows = "systems";
};

hyprlang = {
url = "github:hyprwm/hyprlang";
inputs.nixpkgs.follows = "nixpkgs";
inputs.systems.follows = "systems";
inputs.hyprutils.follows = "hyprutils";
};

hyprwayland-scanner = {
url = "github:hyprwm/hyprwayland-scanner";
inputs.nixpkgs.follows = "nixpkgs";
Expand Down
2 changes: 2 additions & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
pkg-config,
hyprland-protocols,
hyprutils,
hyprlang,
hyprwayland-scanner,
wayland,
wayland-protocols,
Expand Down Expand Up @@ -35,6 +36,7 @@ in
buildInputs = [
hyprland-protocols
hyprutils
hyprlang
wayland
wayland-protocols
wayland-scanner
Expand Down
1 change: 1 addition & 0 deletions nix/overlays.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ in {

hyprsunset = lib.composeManyExtensions [
inputs.hyprland-protocols.overlays.default
inputs.hyprlang.overlays.default
inputs.hyprutils.overlays.default
inputs.hyprwayland-scanner.overlays.default
(final: prev: {
Expand Down
96 changes: 96 additions & 0 deletions src/ConfigManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "ConfigManager.hpp"
#include <cstdlib>
#include <hyprlang.hpp>
#include <hyprutils/path/Path.hpp>
#include <string>
#include <sys/ucontext.h>
#include "helpers/Log.hpp"

static std::string getMainConfigPath() {
static const auto paths = Hyprutils::Path::findConfig("hyprsunset");

return paths.first.value_or("");
}

CConfigManager::CConfigManager(std::string configPath) :
m_config(configPath.empty() ? getMainConfigPath().c_str() : configPath.c_str(), Hyprlang::SConfigOptions{.throwAllErrors = true, .allowMissingConfig = true}) {
currentConfigPath = configPath.empty() ? getMainConfigPath() : configPath;
}

void CConfigManager::init() {
m_config.addConfigValue("max-gamma", Hyprlang::INT{100});

m_config.addSpecialCategory("profile", Hyprlang::SSpecialCategoryOptions{.key = nullptr, .anonymousKeyBased = true});
m_config.addSpecialConfigValue("profile", "time", Hyprlang::STRING{"00:00"});
m_config.addSpecialConfigValue("profile", "temperature", Hyprlang::INT{6000});
m_config.addSpecialConfigValue("profile", "gamma", Hyprlang::FLOAT{1.0f});
m_config.addSpecialConfigValue("profile", "identity", Hyprlang::INT{0});

m_config.commence();

auto result = m_config.parse();

if (result.error)
Debug::log(ERR, "Config has errors:\n{}\nProceeding ignoring faulty entries", result.getError());
}

std::vector<SSunsetProfile> CConfigManager::getSunsetProfiles() {
std::vector<SSunsetProfile> result;

auto keys = m_config.listKeysForSpecialCategory("profile");
result.reserve(keys.size());

for (auto& key : keys) {
std::string time;
unsigned long temperature;
float gamma;
bool identity;

try {
time = std::any_cast<Hyprlang::STRING>(m_config.getSpecialConfigValue("profile", "time", key.c_str()));
temperature = std::any_cast<Hyprlang::INT>(m_config.getSpecialConfigValue("profile", "temperature", key.c_str()));
gamma = std::any_cast<Hyprlang::FLOAT>(m_config.getSpecialConfigValue("profile", "gamma", key.c_str()));
identity = std::any_cast<Hyprlang::INT>(m_config.getSpecialConfigValue("profile", "identity", key.c_str()));
} catch (const std::bad_any_cast& e) {
RASSERT(false, "Failed to construct Profile: {}", e.what()); //
} catch (const std::out_of_range& e) {
RASSERT(false, "Missing property for Profile: {}", e.what()); //
}

size_t separator = time.find(':');

if (separator == std::string::npos)
RASSERT(false, "Invalid time format for profile {}", key);

int hour = 0, minute = 0;
try {
hour = std::stoi(time.substr(0, separator));
minute = std::stoi(time.substr(separator + 1).c_str());
} catch (const std::exception& e) {
Debug::log(ERR, "Invalid time format: {}, skipping this profile", time);
continue;
}

// clang-format off
result.push_back(SSunsetProfile{
.time = {
.hour = std::chrono::hours(hour),
.minute = std::chrono::minutes(minute),
},
.temperature = temperature,
.gamma = gamma,
.identity = identity,
});
// clang-format on
}

return result;
}

float CConfigManager::getMaxGamma() {
try {
return std::any_cast<Hyprlang::INT>(m_config.getConfigValue("max-gamma")) / 100.f;
} catch (const std::bad_any_cast& e) {
RASSERT(false, "Failed to construct max-gamma: {}", e.what()); //
}
}
22 changes: 22 additions & 0 deletions src/ConfigManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "Hyprsunset.hpp"
#include <hyprlang.hpp>
#include <vector>

class CConfigManager {
public:
CConfigManager(std::string configPath);

std::vector<SSunsetProfile> getSunsetProfiles();
float getMaxGamma();

void init();

private:
Hyprlang::CConfig m_config;

std::string currentConfigPath;
};

inline UP<CConfigManager> g_pConfigManager;
Loading