From 067b91c28a0eda516060cb4e72cfd5d9ce36dce7 Mon Sep 17 00:00:00 2001 From: Cameron Reed Date: Mon, 17 Jun 2024 12:45:42 -0600 Subject: [PATCH] Config file --- README.md | 13 ++++- inc/config.h | 16 ++++++ src/config.cpp | 150 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 13 +++-- 4 files changed, 184 insertions(+), 8 deletions(-) create mode 100644 inc/config.h create mode 100644 src/config.cpp diff --git a/README.md b/README.md index 798e7ef..500852a 100644 --- a/README.md +++ b/README.md @@ -43,9 +43,16 @@ but that is left up to you # Configuring -In the future, there will be a configuration file to configure the position, size, and font. However, when running under Wayland, -the requested position of the window will not be respected, so to set the position, as well as removing window decorations, and -preventing the window from stealing focus when it is opened, you will need to set window rules. You can see the rules I use below: +You can configure the position the window requests and the font that is used using a config file at $XDG_CONFIG_HOME/timer_overlay/config.txt or +~/.config/timer_overlay/config.txt. The syntax looks like: +``` +POSX=300 +POSY=30 +FONT=/usr/share/fonts/noto/NotoSans-Bold.ttf +``` +However, when running under Wayland, the requested position of the window will not be respected, so to set the position, as well +as removing window decorations, and preventing the window from stealing focus when it is opened, you will need to set window rules. +You can see the rules I use below: ![window rules](https://cam123.dev/files/hidden/images/window_rules.png) diff --git a/inc/config.h b/inc/config.h new file mode 100644 index 0000000..b83027e --- /dev/null +++ b/inc/config.h @@ -0,0 +1,16 @@ +#include +#include +#include + + +struct Config +{ + uint32_t pos_x = 10; + uint32_t pos_y = 25; + std::string font = "/usr/share/fonts/noto/NotoSans-Regular.ttf"; +}; + + +std::filesystem::path getConfigPath(const char* const configFolderName); +Config readConfig(std::filesystem::path config_path); + diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 0000000..0dbc519 --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,150 @@ +#include +#include +#include +#include +#include +#include + + +#include "config.h" + + +std::filesystem::path getConfigPath(const char* const configFolderName) +{ + std::filesystem::path config_dir = getenv("XDG_CONFIG_HOME"); + if (config_dir.empty()) { + config_dir = getenv("HOME"); + if (config_dir.empty()) { + return config_dir; + } + + config_dir = config_dir / ".config"; + } + + + return config_dir / configFolderName / "config.txt"; +} + +Config readConfig(std::filesystem::path config_path) +{ + Config config{}; + std::ifstream file(config_path); + if (!file.is_open()) { + return config; + } + + int line_num = 0; + std::string line; + while (std::getline(file, line)) { + line_num++; + + if (line.empty()) { + continue; + } + + size_t i = 0; + while (line.at(i) == ' ' || line.at(i) == '\t') { + i++; + if (i == line.length()) { + break; + } + } + if (i == line.length()) { + continue; + } + + if (line.at(i) == '#') { + continue; + } + + size_t name_start = i; + while (line.at(i) != ' ' && line.at(i) != '=') { + i++; + if (i == line.length()) { + std::cerr << "Warning: Invalid line in config file" << std::endl; + std::cerr << line_num << " | " << line << std::endl; + break; + } + } + if (i == line.length()) { + continue; + } + std::string_view name = std::string_view(line).substr(name_start, i - name_start); + + while (line.at(i) == ' ' || line.at(i) == '\t') { + i++; + if (i == line.length()) { + std::cerr << "Warning: Invalid line in config file" << std::endl; + std::cerr << line_num << " | " << line << std::endl; + break; + } + } + if (i == line.length()) { + continue; + } + + if (line.at(i) != '=') { + std::cerr << "Warning: Invalid line in config file" << std::endl; + std::cerr << line_num << " | " << line << std::endl; + continue; + } + i++; + + while (line.at(i) == ' ' || line.at(i) == '\t') { + i++; + if (i == line.length()) { + std::cerr << "Warning: Invalid line in config file" << std::endl; + std::cerr << line_num << " | " << line << std::endl; + break; + } + } + if (i == line.length()) { + continue; + } + + size_t value_start = i; + while (line.at(i) != ' ' && line.at(i) != '\t' && line.at(i) != '#') { + i++; + if (i == line.length()) { + break; + } + } + std::string value = line.substr(value_start, i - value_start); + + if (i != line.length()) { + while (line.at(i) == ' ' || line.at(i) == '\t') { + i++; + } + } + + if (i != line.length()) { + if (line.at(i) != '#') { + std::cerr << "Warning: Invalid line in config file" << std::endl; + std::cerr << line_num << " | " << line << std::endl; + continue; + } + } + + if (name == "POSX") { + config.pos_x = std::stoi(value); + } else if (name == "POSY") { + config.pos_y = std::stoi(value); + } else if (name == "FONT") { + config.font = value; + } else { + std::cerr << "Warning: Invalid line in config file" << std::endl; + std::cerr << line_num << " | " << line << std::endl; + } + } + file.close(); + + return config; +} + + +// int main() +// { +// auto path = getConfigPath("timer_overlay"); +// readConfig(path); +// } + diff --git a/src/main.cpp b/src/main.cpp index 1e4437e..2867d6c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,7 @@ #include "character_utils.h" #include "shortcuts.h" #include "timer.h" +#include "config.h" void error_callback(int error, const char* description) @@ -27,6 +28,8 @@ void error_callback(int error, const char* description) int main() { + Config cfg = readConfig(getConfigPath("timer_overlay")); + std::cout << glfwGetVersionString() << std::endl; glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND); @@ -46,8 +49,8 @@ int main() glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_TRUE); glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE); glfwWindowHint(GLFW_MOUSE_PASSTHROUGH, GLFW_TRUE); - glfwWindowHint(GLFW_POSITION_X, 200); - glfwWindowHint(GLFW_POSITION_Y, 10); + glfwWindowHint(GLFW_POSITION_X, cfg.pos_x); + glfwWindowHint(GLFW_POSITION_Y, cfg.pos_y); glfwWindowHintString(GLFW_WAYLAND_APP_ID, "timer-overlay"); glfwWindowHintString(GLFW_X11_CLASS_NAME, "timer-overlay"); glfwWindowHintString(GLFW_X11_INSTANCE_NAME, "timer-overlay"); @@ -83,8 +86,8 @@ int main() glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); - Font NotoSans("/usr/share/fonts/noto/NotoSans-Regular.ttf"); - if (NotoSans.LoadError) { + Font font(cfg.font.c_str()); + if (font.LoadError) { return -1; } @@ -164,7 +167,7 @@ int main() } if (!time_diff.negative || (time_diff.seconds_absolute < 3 && time_diff.milliseconds > 500)) { - NotoSans.RenderText(VAO, VBO, shaderProgram, buf, 0.0f, 10.0f, 1.0f); + font.RenderText(VAO, VBO, shaderProgram, buf, 0.0f, 10.0f, 1.0f); } else { usleep(500); }