Skip to content

Commit 851c400

Browse files
committed
add config file support
1 parent 7092605 commit 851c400

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

config.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// gtklock
2+
// Copyright (c) 2022 Jovan Lanik
3+
4+
// Configuration
5+
6+
#include "config.h"
7+
8+
void config_load(char *path, GOptionEntry entries[]) {
9+
GKeyFile *keyfile = g_key_file_new();
10+
g_key_file_load_from_file(keyfile, path, G_KEY_FILE_NONE, NULL);
11+
for(int i = 0; entries[i].long_name != NULL; ++i) {
12+
GError *error = NULL;
13+
switch(entries[i].arg) {
14+
case G_OPTION_ARG_NONE:
15+
*(gboolean *)entries[i].arg_data =
16+
g_key_file_get_boolean(keyfile, "main", entries[i].long_name, NULL);
17+
break;
18+
case G_OPTION_ARG_STRING:
19+
*(gchar **)entries[i].arg_data =
20+
g_key_file_get_string(keyfile, "main", entries[i].long_name, NULL);
21+
break;
22+
case G_OPTION_ARG_FILENAME:
23+
*(gchar **)entries[i].arg_data =
24+
g_key_file_get_string(keyfile, "main", entries[i].long_name, &error);
25+
if(error == NULL && !g_file_test(entries[i].arg_data, G_FILE_TEST_EXISTS))
26+
g_free(entries[i].arg_data);
27+
break;
28+
default:
29+
g_error("Unknown entry argument type");
30+
}
31+
}
32+
}
33+

include/config.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// gtklock
2+
// Copyright (c) 2022 Jovan Lanik
3+
4+
// Configuration
5+
6+
#pragma once
7+
8+
#include "glib.h"
9+
10+
void config_load(char *path, GOptionEntry entries[]);
11+

source.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "auth.h"
99
#include "window.h"
1010
#include "gtklock.h"
11+
#include "config.h"
1112
#include "module.h"
1213
#include "xdg.h"
1314

@@ -19,16 +20,25 @@ static gboolean no_input_inhibit = FALSE;
1920

2021
static char *gtk_theme = NULL;
2122

23+
static char *config_path = NULL;
2224
static char *style_path = NULL;
2325
static char *module_path = NULL;
2426

25-
static GOptionEntry entries[] = {
27+
static GOptionEntry main_entries[] = {
2628
{ "daemonize", 'd', 0, G_OPTION_ARG_NONE, &should_daemonize, "Detach from the controlling terminal after locking", NULL },
27-
{ "no-layer-shell", 'l', 0, G_OPTION_ARG_NONE, &no_layer_shell, "Don't use wlr-layer-shell", NULL },
28-
{ "no-input-inhibit", 'i', 0, G_OPTION_ARG_NONE, &no_input_inhibit, "Don't use wlr-input-inhibitor", NULL },
29+
{ "config", 'c', 0, G_OPTION_ARG_FILENAME, &config_path, "Load config file", NULL },
30+
{ NULL },
31+
};
32+
33+
static GOptionEntry config_entries[] = {
2934
{ "gtk-theme", 'g', 0, G_OPTION_ARG_STRING, &gtk_theme, "Set GTK theme", NULL },
3035
{ "style", 's', 0, G_OPTION_ARG_FILENAME, &style_path, "Load CSS style file", NULL },
3136
{ "module", 'm', 0, G_OPTION_ARG_FILENAME, &module_path, "Load gtklock module", NULL },
37+
};
38+
39+
static GOptionEntry debug_entries[] = {
40+
{ "no-layer-shell", 'l', 0, G_OPTION_ARG_NONE, &no_layer_shell, "Don't use wlr-layer-shell", NULL },
41+
{ "no-input-inhibit", 'i', 0, G_OPTION_ARG_NONE, &no_input_inhibit, "Don't use wlr-input-inhibitor", NULL },
3242
{ NULL },
3343
};
3444

@@ -146,13 +156,26 @@ static void daemonize(void) {
146156
int main(int argc, char **argv) {
147157
GError *error = NULL;
148158
GOptionContext *option_context = g_option_context_new("- GTK-based lockscreen for sway");
149-
g_option_context_add_main_entries(option_context, entries, NULL);
159+
g_option_context_add_main_entries(option_context, main_entries, NULL);
150160
g_option_context_set_help_enabled(option_context, FALSE);
151161
g_option_context_set_ignore_unknown_options(option_context, TRUE);
152162
g_option_context_parse(option_context, &argc, &argv, &error);
153163

164+
if(config_path == NULL) config_path = xdg_get_config_path("config.ini");
165+
if(config_path) config_load(config_path, config_entries);
166+
154167
if(should_daemonize) daemonize();
155168

169+
GOptionGroup *config_group =
170+
g_option_group_new("config", "Config options", "Show options available in the config", NULL, NULL);
171+
GOptionGroup *debug_group =
172+
g_option_group_new("debug", "Debug options", "Show options for debugging and styling", NULL, NULL);
173+
174+
g_option_group_add_entries(config_group, config_entries);
175+
g_option_group_add_entries(debug_group, debug_entries);
176+
g_option_context_add_group(option_context, config_group);
177+
g_option_context_add_group(option_context, debug_group);
178+
156179
g_option_context_add_group(option_context, gtk_get_option_group(TRUE));
157180
g_option_context_set_help_enabled(option_context, TRUE);
158181
g_option_context_set_ignore_unknown_options(option_context, FALSE);

0 commit comments

Comments
 (0)