summarylogtreecommitdiffstats
path: root/lxappearance-wayland-sync.patch
blob: c24f0be5e9d57537a1ae084db34a400271df493d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
diff --git a/src/lxappearance.c b/src/lxappearance.c
index f705f61..339977c 100644
--- a/src/lxappearance.c
+++ b/src/lxappearance.c
@@ -385,6 +385,8 @@ static void lxappearance_save_gtkrc()
     GKeyFile *content = g_key_file_new();
     char* file_path = g_build_filename(g_get_user_config_dir(), "gtk-3.0", NULL);
     char* file_path_settings = g_build_filename(file_path, "settings.ini", NULL);
+    char* file_path4 = g_build_filename(g_get_user_config_dir(), "gtk-4.0", NULL);
+    char* file_path_settings4 = g_build_filename(file_path4, "settings.ini", NULL);
 
     if (!g_file_test(file_path, G_FILE_TEST_IS_DIR))
     {
@@ -456,16 +458,30 @@ static void lxappearance_save_gtkrc()
 
 #if GLIB_CHECK_VERSION(2, 40, 0)
     g_key_file_save_to_file(content, file_path_settings, NULL);
+    g_message("Saved GTK settings: %s", file_path_settings);
+    if (!g_file_test(file_path4, G_FILE_TEST_IS_DIR))
+        g_mkdir_with_parents(file_path4, 0755);
+    g_key_file_save_to_file(content, file_path_settings4, NULL);
+    g_message("Saved GTK settings: %s", file_path_settings4);
 #else
     char *contents;
     gsize s;
 
     contents = g_key_file_to_data(content, &s, NULL);
     if (contents)
+    {
         g_file_set_contents(file_path_settings, contents, s, NULL);
+        g_message("Saved GTK settings: %s", file_path_settings);
+        if (!g_file_test(file_path4, G_FILE_TEST_IS_DIR))
+            g_mkdir_with_parents(file_path4, 0755);
+        g_file_set_contents(file_path_settings4, contents, s, NULL);
+        g_message("Saved GTK settings: %s", file_path_settings4);
+    }
     g_free(contents);
 #endif
 
+    g_free(file_path_settings4);
+    g_free(file_path4);
     g_free(file_path_settings);
     g_key_file_free(content);
 
@@ -535,6 +551,195 @@ static void lxappearance_save_lxsession()
     g_free(user_config_file);
 }
 
+
+static void sync_gsettings_interface()
+{
+    char *quoted, *command;
+    gboolean ok = FALSE;
+
+    if (!g_find_program_in_path("gsettings"))
+    {
+        g_message("Skipping gsettings sync: gsettings not found");
+        return;
+    }
+
+    if (app.widget_theme && *app.widget_theme)
+    {
+        quoted = g_shell_quote(app.widget_theme);
+        command = g_strdup_printf("gsettings set org.gnome.desktop.interface gtk-theme %s", quoted);
+        g_spawn_command_line_sync(command, NULL, NULL, NULL, NULL);
+        ok = TRUE;
+        g_free(command);
+        g_free(quoted);
+    }
+
+    if (app.icon_theme && *app.icon_theme)
+    {
+        quoted = g_shell_quote(app.icon_theme);
+        command = g_strdup_printf("gsettings set org.gnome.desktop.interface icon-theme %s", quoted);
+        g_spawn_command_line_sync(command, NULL, NULL, NULL, NULL);
+        ok = TRUE;
+        g_free(command);
+        g_free(quoted);
+    }
+
+    if (app.cursor_theme && *app.cursor_theme)
+    {
+        quoted = g_shell_quote(app.cursor_theme);
+        command = g_strdup_printf("gsettings set org.gnome.desktop.interface cursor-theme %s", quoted);
+        g_spawn_command_line_sync(command, NULL, NULL, NULL, NULL);
+        ok = TRUE;
+        g_free(command);
+        g_free(quoted);
+    }
+
+    if (app.default_font && *app.default_font)
+    {
+        quoted = g_shell_quote(app.default_font);
+        command = g_strdup_printf("gsettings set org.gnome.desktop.interface font-name %s", quoted);
+        g_spawn_command_line_sync(command, NULL, NULL, NULL, NULL);
+        ok = TRUE;
+        g_free(command);
+        g_free(quoted);
+    }
+
+    command = g_strdup_printf("gsettings set org.gnome.desktop.interface cursor-size %d", app.cursor_theme_size);
+    g_spawn_command_line_sync(command, NULL, NULL, NULL, NULL);
+    g_free(command);
+    ok = TRUE;
+
+    if (ok)
+        g_message("Applied appearance values to gsettings");
+}
+
+static gboolean labwc_environment_line_matches_key(const char *line, const char *key)
+{
+    gsize key_len;
+
+    while (*line == ' ' || *line == '\t')
+        ++line;
+
+    if (*line == '#')
+        return FALSE;
+
+    key_len = strlen(key);
+    if (strncmp(line, key, key_len) != 0)
+        return FALSE;
+
+    line += key_len;
+    while (*line == ' ' || *line == '\t')
+        ++line;
+
+    return *line == '=';
+}
+
+static void append_labwc_environment_line(GString *out, const char *line,
+                                          const char *theme, const char *size,
+                                          gboolean *theme_written,
+                                          gboolean *size_written)
+{
+    if (labwc_environment_line_matches_key(line, "XCURSOR_THEME"))
+    {
+        if (!*theme_written)
+            g_string_append_printf(out, "XCURSOR_THEME=%s\n", theme);
+        *theme_written = TRUE;
+        return;
+    }
+
+    if (labwc_environment_line_matches_key(line, "XCURSOR_SIZE"))
+    {
+        if (!*size_written)
+            g_string_append_printf(out, "XCURSOR_SIZE=%s\n", size);
+        *size_written = TRUE;
+        return;
+    }
+
+    g_string_append(out, line);
+    g_string_append_c(out, '\n');
+}
+
+static void sync_labwc_environment()
+{
+    char *labwc_dir;
+    char *environment_path;
+    char *contents = NULL;
+    char **lines;
+    char *size;
+    const char *theme;
+    GString *out;
+    gboolean theme_written = FALSE;
+    gboolean size_written = FALSE;
+    int i;
+
+    theme = (app.cursor_theme && *app.cursor_theme) ? app.cursor_theme : "default";
+    size = g_strdup_printf("%d", app.cursor_theme_size);
+
+    labwc_dir = g_build_filename(g_get_user_config_dir(), "labwc", NULL);
+    g_mkdir_with_parents(labwc_dir, 0755);
+    environment_path = g_build_filename(labwc_dir, "environment", NULL);
+
+    if (!g_file_get_contents(environment_path, &contents, NULL, NULL))
+        contents = g_strdup("");
+
+    lines = g_strsplit(contents, "\n", -1);
+    out = g_string_new(NULL);
+
+    for (i = 0; lines[i] != NULL; ++i)
+    {
+        if (lines[i + 1] == NULL && lines[i][0] == '\0')
+            break;
+        append_labwc_environment_line(out, lines[i], theme, size,
+                                      &theme_written, &size_written);
+    }
+
+    if (!theme_written)
+        g_string_append_printf(out, "XCURSOR_THEME=%s\n", theme);
+    if (!size_written)
+        g_string_append_printf(out, "XCURSOR_SIZE=%s\n", size);
+
+    if (g_file_set_contents(environment_path, out->str, -1, NULL))
+        g_message("Applied cursor values to labwc environment: %s", environment_path);
+
+    /* If we run under labwc, ask it to reload XCURSOR_THEME/XCURSOR_SIZE now.
+     * Other wlroots compositors do not provide this command; there the values
+     * above still apply to newly started clients and after the next login. */
+    if (g_getenv("LABWC_PID") && g_find_program_in_path("labwc"))
+        g_spawn_command_line_async("labwc --reconfigure", NULL);
+
+    g_string_free(out, TRUE);
+    g_strfreev(lines);
+    g_free(contents);
+    g_free(environment_path);
+    g_free(labwc_dir);
+    g_free(size);
+}
+
+static void ensure_user_autostart_sync()
+{
+    char *autostart_dir;
+    char *desktop_path;
+    const char *desktop_content =
+        "[Desktop Entry]\n"
+        "Type=Application\n"
+        "Name=Synchronize GTK Appearance\n"
+        "Comment=Sync GTK appearance values to gsettings\n"
+        "Exec=/usr/bin/lxappearance-wlr-sync-gsettings\n"
+        "OnlyShowIn=labwc;\n"
+        "NoDisplay=true\n"
+        "X-GNOME-Autostart-enabled=true\n";
+
+    autostart_dir = g_build_filename(g_get_user_config_dir(), "autostart", NULL);
+    if (!g_file_test(autostart_dir, G_FILE_TEST_IS_DIR))
+        g_mkdir_with_parents(autostart_dir, 0755);
+
+    desktop_path = g_build_filename(autostart_dir, "lxappearance-wlr-sync-gsettings.desktop", NULL);
+    g_file_set_contents(desktop_path, desktop_content, -1, NULL);
+    g_message("Ensured autostart entry: %s", desktop_path);
+
+    g_free(desktop_path);
+    g_free(autostart_dir);
+}
+
 static void on_dlg_response(GtkDialog* dlg, int res, gpointer user_data)
 {
     switch(res)
@@ -544,6 +749,9 @@ static void on_dlg_response(GtkDialog* dlg, int res, gpointer user_data)
         if(app.use_lxsession)
             lxappearance_save_lxsession();
         lxappearance_save_gtkrc();
+        ensure_user_autostart_sync();
+        sync_gsettings_interface();
+        sync_labwc_environment();
 
         reload_all_programs();