Skip to content

Commit 46abee6

Browse files
committed
add background option
1 parent 4d3f7a3 commit 46abee6

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

source.c

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <assert.h>
55
#include <sys/wait.h>
66
#include <gtk/gtk.h>
7+
#include <glib/gprintf.h>
78

89
#include "auth.h"
910
#include "window.h"
@@ -23,6 +24,7 @@ static char *gtk_theme = NULL;
2324
static char *config_path = NULL;
2425
static char *style_path = NULL;
2526
static char *module_path = NULL;
27+
static char *background_path = NULL;
2628

2729
static GOptionEntry main_entries[] = {
2830
{ "daemonize", 'd', 0, G_OPTION_ARG_NONE, &should_daemonize, "Detach from the controlling terminal after locking", NULL },
@@ -34,6 +36,7 @@ static GOptionEntry config_entries[] = {
3436
{ "gtk-theme", 'g', 0, G_OPTION_ARG_STRING, &gtk_theme, "Set GTK theme", NULL },
3537
{ "style", 's', 0, G_OPTION_ARG_FILENAME, &style_path, "Load CSS style file", NULL },
3638
{ "module", 'm', 0, G_OPTION_ARG_FILENAME, &module_path, "Load gtklock module", NULL },
39+
{ "background", 'b', 0, G_OPTION_ARG_FILENAME, &background_path, "Load background", NULL },
3740
};
3841

3942
static GOptionEntry debug_entries[] = {
@@ -114,18 +117,40 @@ static void activate(GtkApplication *app, gpointer user_data) {
114117
if(parent > 0) kill(parent, SIGINT);
115118
}
116119

117-
static void attach_custom_style(const char* path) {
120+
static void attach_style(const char *format, ...) G_GNUC_PRINTF(1, 2);
121+
static void attach_style(const char *format, ...) {
118122
GtkCssProvider *provider = gtk_css_provider_new();
119123
GError *err = NULL;
124+
va_list args;
125+
va_start(args, format);
126+
char *buff = g_strdup_vprintf(format, args);
127+
va_end(args);
120128

121-
gtk_css_provider_load_from_path(provider, path, &err);
129+
gtk_css_provider_load_from_data(provider, buff, -1, &err);
122130
if(err != NULL) {
123131
g_warning("Style loading failed: %s", err->message);
124132
g_error_free(err);
125133
} else {
126134
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
127135
GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
128136
}
137+
138+
g_object_unref(provider);
139+
g_free(buff);
140+
}
141+
142+
static void attach_custom_style(const char *path) {
143+
GtkCssProvider *provider = gtk_css_provider_new();
144+
GError *err = NULL;
145+
146+
gtk_css_provider_load_from_path(provider, path, &err);
147+
if(err != NULL) {
148+
g_warning("Custom style loading failed: %s", err->message);
149+
g_error_free(err);
150+
} else {
151+
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
152+
GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION+1);
153+
}
129154
g_object_unref(provider);
130155
}
131156

@@ -191,8 +216,22 @@ int main(int argc, char **argv) {
191216
gtklock->use_layer_shell = !no_layer_shell;
192217
gtklock->use_input_inhibit = !no_input_inhibit;
193218

219+
if(background_path != NULL) {
220+
GFile *file = g_file_new_for_path(background_path);
221+
char *path = g_file_get_path(file);
222+
g_object_unref(file);
223+
attach_style(
224+
"window { "
225+
"background-color: black;"
226+
"background-image: url(\"%s\");"
227+
"background-size: 100%% 100%%;"
228+
"}",
229+
path
230+
);
231+
}
232+
194233
if(style_path == NULL) style_path = xdg_get_config_path("style.css");
195-
if(style_path) {
234+
if(style_path != NULL) {
196235
attach_custom_style(style_path);
197236
free(style_path);
198237
}

0 commit comments

Comments
 (0)