Config file

This commit is contained in:
Cameron Reed 2024-06-17 12:45:42 -06:00
parent a27f89e5f4
commit 067b91c28a
4 changed files with 184 additions and 8 deletions

View File

@ -43,9 +43,16 @@ but that is left up to you
# Configuring # Configuring
In the future, there will be a configuration file to configure the position, size, and font. However, when running under Wayland, 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
the requested position of the window will not be respected, so to set the position, as well as removing window decorations, and ~/.config/timer_overlay/config.txt. The syntax looks like:
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: ```
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) ![window rules](https://cam123.dev/files/hidden/images/window_rules.png)

16
inc/config.h Normal file
View File

@ -0,0 +1,16 @@
#include <filesystem>
#include <cstdint>
#include <string>
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);

150
src/config.cpp Normal file
View File

@ -0,0 +1,150 @@
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <string>
#include <iostream>
#include <string_view>
#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);
// }

View File

@ -18,6 +18,7 @@
#include "character_utils.h" #include "character_utils.h"
#include "shortcuts.h" #include "shortcuts.h"
#include "timer.h" #include "timer.h"
#include "config.h"
void error_callback(int error, const char* description) void error_callback(int error, const char* description)
@ -27,6 +28,8 @@ void error_callback(int error, const char* description)
int main() int main()
{ {
Config cfg = readConfig(getConfigPath("timer_overlay"));
std::cout << glfwGetVersionString() << std::endl; std::cout << glfwGetVersionString() << std::endl;
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND); glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND);
@ -46,8 +49,8 @@ int main()
glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_TRUE); glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE); glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_MOUSE_PASSTHROUGH, GLFW_TRUE); glfwWindowHint(GLFW_MOUSE_PASSTHROUGH, GLFW_TRUE);
glfwWindowHint(GLFW_POSITION_X, 200); glfwWindowHint(GLFW_POSITION_X, cfg.pos_x);
glfwWindowHint(GLFW_POSITION_Y, 10); glfwWindowHint(GLFW_POSITION_Y, cfg.pos_y);
glfwWindowHintString(GLFW_WAYLAND_APP_ID, "timer-overlay"); glfwWindowHintString(GLFW_WAYLAND_APP_ID, "timer-overlay");
glfwWindowHintString(GLFW_X11_CLASS_NAME, "timer-overlay"); glfwWindowHintString(GLFW_X11_CLASS_NAME, "timer-overlay");
glfwWindowHintString(GLFW_X11_INSTANCE_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)); glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
Font NotoSans("/usr/share/fonts/noto/NotoSans-Regular.ttf"); Font font(cfg.font.c_str());
if (NotoSans.LoadError) { if (font.LoadError) {
return -1; return -1;
} }
@ -164,7 +167,7 @@ int main()
} }
if (!time_diff.negative || (time_diff.seconds_absolute < 3 && time_diff.milliseconds > 500)) { 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 { } else {
usleep(500); usleep(500);
} }