Write gtkrc file in gtk3 directory
[lxde/lxappearance.git] / src / lxappearance.c
1 /*
2 * lxappearance.c
3 *
4 * Copyright 2010 PCMan <pcman.tw@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "lxappearance.h"
27
28 #include <gtk/gtk.h>
29 #include <glib/gi18n.h>
30
31 #include <X11/X.h>
32 #include <X11/Xatom.h>
33 #include <X11/Xlib.h>
34 #include <gdk/gdkx.h>
35 #include <string.h>
36
37 #include "widget-theme.h"
38 #include "color-scheme.h"
39 #include "icon-theme.h"
40 #include "cursor-theme.h"
41 #include "font.h"
42 #include "other.h"
43 #include "plugin.h"
44
45 LXAppearance app = {0};
46
47 Atom lxsession_atom = 0;
48 static const char* lxsession_name = NULL;
49
50 static void check_lxsession()
51 {
52 lxsession_atom = XInternAtom( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), "_LXSESSION", True );
53 if( lxsession_atom != None )
54 {
55 XGrabServer( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()) );
56 if( XGetSelectionOwner( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), lxsession_atom ) )
57 {
58 app.use_lxsession = TRUE;
59 lxsession_name = g_getenv("DESKTOP_SESSION");
60 }
61 XUngrabServer( GDK_DISPLAY_XDISPLAY(gdk_display_get_default()) );
62 }
63 }
64
65 static GOptionEntry option_entries[] =
66 {
67 { NULL }
68 };
69
70 static void save_cursor_theme_name()
71 {
72 char* dir_path;
73 if(!app.cursor_theme || !g_strcmp0(app.cursor_theme, "default"))
74 return;
75
76 dir_path = g_build_filename(g_get_home_dir(), ".icons/default", NULL);
77 if(0 == g_mkdir_with_parents(dir_path, 0700))
78 {
79 char* index_theme = g_build_filename(dir_path, "index.theme", NULL);
80 char* content = g_strdup_printf(
81 "# This file is written by LXAppearance. Do not edit."
82 "[Icon Theme]\n"
83 "Name=Default\n"
84 "Comment=Default Cursor Theme\n"
85 "Inherits=%s\n", app.cursor_theme ? app.cursor_theme : "");
86 g_file_set_contents(index_theme, content, -1, NULL);
87 g_free(content);
88 g_free(index_theme);
89 }
90 g_free(dir_path);
91
92 /*
93 dir_path = g_build_filename(g_get_home_dir(), ".Xdefaults", NULL);
94 Xcursor.theme: name
95 Xcursor.size: [size]
96 g_file_set_contents(dir_path, "", -1, NULL);
97 g_free(dir_path);
98 */
99 }
100
101 static void reload_all_programs()
102 {
103 GdkEventClient event;
104 event.type = GDK_CLIENT_EVENT;
105 event.send_event = TRUE;
106 event.window = NULL;
107
108 if( app.use_lxsession )
109 {
110 event.message_type = gdk_atom_intern_static_string("_LXSESSION");
111 event.data.b[0] = 0; /* LXS_RELOAD */
112 }
113 else
114 {
115 /* if( icon_only )
116 event.message_type = gdk_atom_intern("_GTK_LOAD_ICONTHEMES", FALSE);
117 */
118 event.message_type = gdk_atom_intern("_GTK_READ_RCFILES", FALSE);
119 }
120 event.data_format = 8;
121 gdk_event_send_clientmessage_toall((GdkEvent *)&event);
122 }
123
124 static const char* bool2str(gboolean val)
125 {
126 return val ? "TRUE" : "FALSE";
127 }
128
129
130 static void lxappearance_save_gtkrc()
131 {
132 static const char* tb_styles[]={
133 "GTK_TOOLBAR_ICONS",
134 "GTK_TOOLBAR_TEXT",
135 "GTK_TOOLBAR_BOTH",
136 "GTK_TOOLBAR_BOTH_HORIZ"
137 };
138 static const char* tb_icon_sizes[]={
139 "GTK_ICON_SIZE_INVALID",
140 "GTK_ICON_SIZE_MENU",
141 "GTK_ICON_SIZE_SMALL_TOOLBAR",
142 "GTK_ICON_SIZE_LARGE_TOOLBAR",
143 "GTK_ICON_SIZE_BUTTON",
144 "GTK_ICON_SIZE_DND",
145 "GTK_ICON_SIZE_DIALOG"
146 };
147
148 char* file_path = g_build_filename(g_get_home_dir(), ".gtkrc-2.0", NULL);
149 GString* content = g_string_sized_new(512);
150 g_string_append(content,
151 "# DO NOT EDIT! This file will be overwritten by LXAppearance.\n"
152 "# Any customization should be done in ~/.gtkrc-2.0.mine instead.\n\n");
153 if(app.widget_theme)
154 g_string_append_printf(content,
155 "gtk-theme-name=\"%s\"\n", app.widget_theme);
156 if(app.icon_theme)
157 g_string_append_printf(content,
158 "gtk-icon-theme-name=\"%s\"\n", app.icon_theme);
159 if(app.default_font)
160 g_string_append_printf(content,
161 "gtk-font-name=\"%s\"\n", app.default_font);
162 if(app.cursor_theme)
163 g_string_append_printf(content,
164 "gtk-cursor-theme-name=\"%s\"\n", app.cursor_theme);
165 save_cursor_theme_name();
166
167 g_string_append_printf(content,
168 "gtk-cursor-theme-size=%d\n"
169 "gtk-toolbar-style=%s\n"
170 "gtk-toolbar-icon-size=%s\n"
171 "gtk-button-images=%d\n"
172 "gtk-menu-images=%d\n"
173 #if GTK_CHECK_VERSION(2, 14, 0)
174 "gtk-enable-event-sounds=%d\n"
175 "gtk-enable-input-feedback-sounds=%d\n"
176 #endif
177 "gtk-xft-antialias=%d\n"
178 "gtk-xft-hinting=%d\n"
179
180 , app.cursor_theme_size,
181 tb_styles[app.toolbar_style],
182 tb_icon_sizes[app.toolbar_icon_size],
183 app.button_images ? 1 : 0,
184 app.menu_images ? 1 : 0,
185 #if GTK_CHECK_VERSION(2, 14, 0)
186 app.enable_event_sound ? 1 : 0,
187 app.enable_input_feedback ? 1 : 0,
188 #endif
189 app.enable_antialising ? 1 : 0,
190 app.enable_hinting ? 1 : 0
191 );
192
193 if(app.hinting_style)
194 g_string_append_printf(content,
195 "gtk-xft-hintstyle=\"%s\"\n", app.hinting_style);
196
197 if(app.font_rgba)
198 g_string_append_printf(content,
199 "gtk-xft-rgba=\"%s\"\n", app.font_rgba);
200
201 if(app.color_scheme)
202 {
203 char* escaped = g_strescape(app.color_scheme, NULL);
204 g_string_append_printf(content,
205 "gtk-color-scheme=\"%s\"\n",
206 escaped);
207 g_free(escaped);
208 }
209
210 g_string_append_printf(content,
211 "include \"%s/.gtkrc-2.0.mine\"\n",
212 g_get_home_dir());
213
214 g_file_set_contents(file_path, content->str, content->len, NULL);
215
216 /* Save also for GTK3 */
217 g_string_prepend(content, "[Settings] \n");
218 char* file_path_gtk3 = g_build_filename(g_get_home_dir(), "gtk-3.0", NULL);
219 char* file_path_settings = g_build_filename(g_get_home_dir(), "gtk-3.0", ".settings.ini", NULL);
220
221 if (!g_file_test(file_path_gtk3, G_FILE_TEST_IS_DIR))
222 {
223 g_mkdir_with_parents(file_path_gtk3, 0755);
224 }
225
226 g_file_set_contents(file_path_settings, content->str, content->len, NULL);
227
228 g_string_free(content, TRUE);
229 g_free(file_path);
230 }
231
232 static void lxappearance_save_lxsession()
233 {
234 char* rel_path = g_strconcat("lxsession/", lxsession_name, "/desktop.conf", NULL);
235 char* user_config_file = g_build_filename(g_get_user_config_dir(), rel_path, NULL);
236 char* buf;
237 int len;
238 GKeyFile* kf = g_key_file_new();
239
240 if(!g_key_file_load_from_file(kf, user_config_file, G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL))
241 {
242 /* the user config file doesn't exist, create its parent dir */
243 len = strlen(user_config_file) - strlen("/desktop.conf");
244 user_config_file[len] = '\0';
245 g_debug("user_config_file = %s", user_config_file);
246 g_mkdir_with_parents(user_config_file, 0700);
247 user_config_file[len] = '/';
248
249 g_key_file_load_from_dirs(kf, rel_path, (const char**)g_get_system_config_dirs(), NULL,
250 G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
251 }
252
253 g_free(rel_path);
254
255 g_key_file_set_string( kf, "GTK", "sNet/ThemeName", app.widget_theme );
256 g_key_file_set_string( kf, "GTK", "sGtk/FontName", app.default_font );
257
258 g_key_file_set_string( kf, "GTK", "sGtk/ColorScheme", app.color_scheme ? app.color_scheme : "" );
259
260 g_key_file_set_string( kf, "GTK", "sNet/IconThemeName", app.icon_theme );
261
262 g_key_file_set_string( kf, "GTK", "sGtk/CursorThemeName", app.cursor_theme );
263 g_key_file_set_integer( kf, "GTK", "iGtk/CursorThemeSize", app.cursor_theme_size );
264 save_cursor_theme_name();
265
266 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", app.toolbar_style );
267 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarIconSize", app.toolbar_icon_size );
268
269 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", app.toolbar_style );
270 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarIconSize", app.toolbar_icon_size );
271
272 g_key_file_set_integer( kf, "GTK", "iGtk/ButtonImages", app.button_images );
273 g_key_file_set_integer( kf, "GTK", "iGtk/MenuImages", app.menu_images );
274
275 #if GTK_CHECK_VERSION(2, 14, 0)
276 /* "Net/SoundThemeName\0" "gtk-sound-theme-name\0" */
277 g_key_file_set_integer( kf, "GTK", "iNet/EnableEventSounds", app.enable_event_sound);
278 g_key_file_set_integer( kf, "GTK", "iNet/EnableInputFeedbackSounds", app.enable_input_feedback);
279 #endif
280 g_key_file_set_integer( kf, "GTK", "iXft/Antialias", app.enable_antialising);
281 g_key_file_set_integer( kf, "GTK", "iXft/Hinting", app.enable_hinting);
282 g_key_file_set_string( kf, "GTK", "sXft/HintStyle", app.hinting_style);
283 g_key_file_set_string( kf, "GTK", "sXft/RGBA", app.font_rgba);
284
285 buf = g_key_file_to_data( kf, &len, NULL );
286 g_key_file_free(kf);
287
288 g_file_set_contents(user_config_file, buf, len, NULL);
289 g_free(buf);
290 g_free(user_config_file);
291 }
292
293 static void on_dlg_response(GtkDialog* dlg, int res, gpointer user_data)
294 {
295 switch(res)
296 {
297 case GTK_RESPONSE_APPLY:
298
299 if(app.use_lxsession)
300 lxappearance_save_lxsession();
301 else
302 lxappearance_save_gtkrc();
303
304 reload_all_programs();
305
306 app.changed = FALSE;
307 gtk_dialog_set_response_sensitive(GTK_DIALOG(app.dlg), GTK_RESPONSE_APPLY, FALSE);
308 break;
309 case 1: /* about dialog */
310 {
311 GtkBuilder* b = gtk_builder_new();
312 if(gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/about.ui", NULL))
313 {
314 GtkWidget* dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
315 gtk_dialog_run(GTK_DIALOG(dlg));
316 gtk_widget_destroy(dlg);
317 }
318 g_object_unref(b);
319 }
320 break;
321 default:
322 gtk_main_quit();
323 }
324 }
325
326 static void settings_init()
327 {
328 GtkSettings* settings = gtk_settings_get_default();
329 g_object_get(settings,
330 "gtk-theme-name", &app.widget_theme,
331 "gtk-font-name", &app.default_font,
332 "gtk-icon-theme-name", &app.icon_theme,
333 "gtk-cursor-theme-name", &app.cursor_theme,
334 "gtk-cursor-theme-size", &app.cursor_theme_size,
335 "gtk-toolbar-style", &app.toolbar_style,
336 "gtk-toolbar-icon-size", &app.toolbar_icon_size,
337 "gtk-button-images", &app.button_images,
338 "gtk-menu-images", &app.menu_images,
339 #if GTK_CHECK_VERSION(2, 14, 0)
340 "gtk-enable-event-sounds", &app.enable_event_sound,
341 "gtk-enable-input-feedback-sounds", &app.enable_input_feedback,
342 #endif
343 "gtk-xft-antialias", &app.enable_antialising,
344 "gtk-xft-hinting", &app.enable_hinting,
345 "gtk-xft-hintstyle", &app.hinting_style,
346 "gtk-xft-rgba", &app.font_rgba,
347 NULL);
348 /* try to figure out cursor theme used. */
349 if(!app.cursor_theme || g_strcmp0(app.cursor_theme, "default") == 0)
350 {
351 /* get the real theme name from default. */
352 GKeyFile* kf = g_key_file_new();
353 char* fpath = g_build_filename(g_get_home_dir(), ".icons/default/index.theme", NULL);
354 gboolean ret = g_key_file_load_from_file(kf, fpath, 0, NULL);
355 g_free(fpath);
356
357 if(!ret)
358 ret = g_key_file_load_from_data_dirs(kf, "icons/default/index.theme", NULL, 0, NULL);
359
360 if(ret)
361 {
362 g_free(app.cursor_theme);
363 app.cursor_theme = g_key_file_get_string(kf, "Icon Theme", "Inherits", NULL);
364 g_debug("cursor theme name: %s", app.cursor_theme);
365 }
366 g_key_file_free(kf);
367 }
368
369 app.color_scheme_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
370 /* try to load custom color scheme if available */
371 if(app.use_lxsession)
372 {
373 char* rel_path = g_strconcat("lxsession/", lxsession_name, "/desktop.conf", NULL);
374 char* user_config_file = g_build_filename(g_get_user_config_dir(), rel_path, NULL);
375 GKeyFile* kf = g_key_file_new();
376 if(g_key_file_load_from_file(kf, user_config_file, 0, NULL))
377 app.color_scheme = g_key_file_get_string(kf, "GTK", "sGtk/ColorScheme", NULL);
378 else if(g_key_file_load_from_dirs(kf, rel_path, (const char**)g_get_system_config_dirs(), NULL, 0, NULL))
379 app.color_scheme = g_key_file_get_string(kf, "GTK", "sGtk/ColorScheme", NULL);
380 g_key_file_free(kf);
381 g_free(rel_path);
382 g_free(user_config_file);
383
384 if(app.color_scheme)
385 {
386 if(*app.color_scheme)
387 color_scheme_str_to_hash(app.color_scheme_hash, app.color_scheme);
388 else
389 {
390 g_free(app.color_scheme);
391 app.color_scheme = NULL;
392 }
393 }
394 }
395 else
396 {
397 char* gtkrc_file = g_build_filename(g_get_home_dir(), ".gtkrc-2.0", NULL);
398 gtkrc_file_get_color_scheme(gtkrc_file, app.color_scheme_hash);
399 g_free(gtkrc_file);
400 if(g_hash_table_size(app.color_scheme_hash) > 0)
401 app.color_scheme = color_scheme_hash_to_str(app.color_scheme_hash);
402 }
403 }
404
405 int main(int argc, char** argv)
406 {
407 GError* err = NULL;
408 GtkBuilder* b;
409
410 /* gettext support */
411 #ifdef ENABLE_NLS
412 bindtextdomain ( GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR );
413 bind_textdomain_codeset ( GETTEXT_PACKAGE, "UTF-8" );
414 textdomain ( GETTEXT_PACKAGE );
415 #endif
416
417 g_thread_init(NULL);
418 /* initialize GTK+ and parse the command line arguments */
419 if( G_UNLIKELY( ! gtk_init_with_args( &argc, &argv, "", option_entries, GETTEXT_PACKAGE, &err ) ) )
420 {
421 g_print( "Error: %s\n", err->message );
422 return 1;
423 }
424
425 app.abi_version = LXAPPEARANCE_ABI_VERSION;
426
427 /* check if we're under LXSession */
428 check_lxsession();
429
430 /* create GUI here */
431 b = gtk_builder_new();
432 if(!gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/lxappearance.ui", NULL))
433 return 1;
434
435 /* NOTE: GUI must be created prior to loading settings from GtkSettings object.
436 * Some properties of GtkSettings class are installed by some other gtk classes.
437 * For example, "gtk-menu-images" property is actually installed by initialization
438 * of GtkImageMenuItem class. If we load the GUI first, then when the menu items
439 * are created, this property gets correctly registered. So later it can be read. */
440
441 /* load config values */
442 settings_init();
443
444 app.dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
445
446 widget_theme_init(b);
447 color_scheme_init(b);
448 icon_theme_init(b);
449 cursor_theme_init(b);
450 font_init(b);
451 other_init(b);
452 /* the page for window manager plugins */
453 app.wm_page = GTK_WIDGET(gtk_builder_get_object(b, "wm_page"));
454
455 plugins_init(b);
456
457 g_signal_connect(app.dlg, "response", G_CALLBACK(on_dlg_response), NULL);
458
459 gtk_window_present(GTK_WINDOW(app.dlg));
460 g_object_unref(b);
461
462 gtk_main();
463
464 plugins_finalize();
465
466 return 0;
467 }
468
469 void lxappearance_changed()
470 {
471 if(!app.changed)
472 {
473 app.changed = TRUE;
474 gtk_dialog_set_response_sensitive(GTK_DIALOG(app.dlg), GTK_RESPONSE_APPLY, TRUE);
475 }
476 }
477
478 void on_check_button_toggled(GtkToggleButton* btn, gpointer user_data)
479 {
480 gboolean* val = (gboolean*)user_data;
481 gboolean new_val = gtk_toggle_button_get_active(btn);
482 if(new_val != *val)
483 {
484 *val = new_val;
485 lxappearance_changed();
486 }
487 }