Revert part of the commit for compiler warnings, to fix loading themes.
[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 g_string_free(content, TRUE);
216 g_free(file_path);
217 }
218
219 static void lxappearance_save_lxsession()
220 {
221 char* rel_path = g_strconcat("lxsession/", lxsession_name, "/desktop.conf", NULL);
222 char* user_config_file = g_build_filename(g_get_user_config_dir(), rel_path, NULL);
223 char* buf;
224 int len;
225 GKeyFile* kf = g_key_file_new();
226
227 if(!g_key_file_load_from_file(kf, user_config_file, G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL))
228 {
229 /* the user config file doesn't exist, create its parent dir */
230 len = strlen(user_config_file) - strlen("/desktop.conf");
231 user_config_file[len] = '\0';
232 g_debug("user_config_file = %s", user_config_file);
233 g_mkdir_with_parents(user_config_file, 0700);
234 user_config_file[len] = '/';
235
236 g_key_file_load_from_dirs(kf, rel_path, (const char**)g_get_system_config_dirs(), NULL,
237 G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
238 }
239
240 g_free(rel_path);
241
242 g_key_file_set_string( kf, "GTK", "sNet/ThemeName", app.widget_theme );
243 g_key_file_set_string( kf, "GTK", "sGtk/FontName", app.default_font );
244
245 g_key_file_set_string( kf, "GTK", "sGtk/ColorScheme", app.color_scheme ? app.color_scheme : "" );
246
247 g_key_file_set_string( kf, "GTK", "sNet/IconThemeName", app.icon_theme );
248
249 g_key_file_set_string( kf, "GTK", "sGtk/CursorThemeName", app.cursor_theme );
250 g_key_file_set_integer( kf, "GTK", "iGtk/CursorThemeSize", app.cursor_theme_size );
251 save_cursor_theme_name();
252
253 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", app.toolbar_style );
254 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarIconSize", app.toolbar_icon_size );
255
256 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", app.toolbar_style );
257 g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarIconSize", app.toolbar_icon_size );
258
259 g_key_file_set_integer( kf, "GTK", "iGtk/ButtonImages", app.button_images );
260 g_key_file_set_integer( kf, "GTK", "iGtk/MenuImages", app.menu_images );
261
262 #if GTK_CHECK_VERSION(2, 14, 0)
263 /* "Net/SoundThemeName\0" "gtk-sound-theme-name\0" */
264 g_key_file_set_integer( kf, "GTK", "iNet/EnableEventSounds", app.enable_event_sound);
265 g_key_file_set_integer( kf, "GTK", "iNet/EnableInputFeedbackSounds", app.enable_input_feedback);
266 #endif
267 g_key_file_set_integer( kf, "GTK", "iXft/Antialias", app.enable_antialising);
268 g_key_file_set_integer( kf, "GTK", "iXft/Hinting", app.enable_hinting);
269 g_key_file_set_string( kf, "GTK", "sXft/HintStyle", app.hinting_style);
270 g_key_file_set_string( kf, "GTK", "sXft/RGBA", app.font_rgba);
271
272 buf = g_key_file_to_data( kf, &len, NULL );
273 g_key_file_free(kf);
274
275 g_file_set_contents(user_config_file, buf, len, NULL);
276 g_free(buf);
277 g_free(user_config_file);
278 }
279
280 static void on_dlg_response(GtkDialog* dlg, int res, gpointer user_data)
281 {
282 switch(res)
283 {
284 case GTK_RESPONSE_APPLY:
285
286 if(app.use_lxsession)
287 lxappearance_save_lxsession();
288 else
289 lxappearance_save_gtkrc();
290
291 reload_all_programs();
292
293 app.changed = FALSE;
294 gtk_dialog_set_response_sensitive(GTK_DIALOG(app.dlg), GTK_RESPONSE_APPLY, FALSE);
295 break;
296 case 1: /* about dialog */
297 {
298 GtkBuilder* b = gtk_builder_new();
299 if(gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/about.ui", NULL))
300 {
301 GtkWidget* dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
302 gtk_dialog_run(GTK_DIALOG(dlg));
303 gtk_widget_destroy(dlg);
304 }
305 g_object_unref(b);
306 }
307 break;
308 default:
309 gtk_main_quit();
310 }
311 }
312
313 static void settings_init()
314 {
315 GtkSettings* settings = gtk_settings_get_default();
316 g_object_get(settings,
317 "gtk-theme-name", &app.widget_theme,
318 "gtk-font-name", &app.default_font,
319 "gtk-icon-theme-name", &app.icon_theme,
320 "gtk-cursor-theme-name", &app.cursor_theme,
321 "gtk-cursor-theme-size", &app.cursor_theme_size,
322 "gtk-toolbar-style", &app.toolbar_style,
323 "gtk-toolbar-icon-size", &app.toolbar_icon_size,
324 "gtk-button-images", &app.button_images,
325 "gtk-menu-images", &app.menu_images,
326 #if GTK_CHECK_VERSION(2, 14, 0)
327 "gtk-enable-event-sounds", &app.enable_event_sound,
328 "gtk-enable-input-feedback-sounds", &app.enable_input_feedback,
329 #endif
330 "gtk-xft-antialias", &app.enable_antialising,
331 "gtk-xft-hinting", &app.enable_hinting,
332 "gtk-xft-hintstyle", &app.hinting_style,
333 "gtk-xft-rgba", &app.font_rgba,
334 NULL);
335 /* try to figure out cursor theme used. */
336 if(!app.cursor_theme || g_strcmp0(app.cursor_theme, "default") == 0)
337 {
338 /* get the real theme name from default. */
339 GKeyFile* kf = g_key_file_new();
340 char* fpath = g_build_filename(g_get_home_dir(), ".icons/default/index.theme", NULL);
341 gboolean ret = g_key_file_load_from_file(kf, fpath, 0, NULL);
342 g_free(fpath);
343
344 if(!ret)
345 ret = g_key_file_load_from_data_dirs(kf, "icons/default/index.theme", NULL, 0, NULL);
346
347 if(ret)
348 {
349 g_free(app.cursor_theme);
350 app.cursor_theme = g_key_file_get_string(kf, "Icon Theme", "Inherits", NULL);
351 g_debug("cursor theme name: %s", app.cursor_theme);
352 }
353 g_key_file_free(kf);
354 }
355
356 app.color_scheme_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
357 /* try to load custom color scheme if available */
358 if(app.use_lxsession)
359 {
360 char* rel_path = g_strconcat("lxsession/", lxsession_name, "/desktop.conf", NULL);
361 char* user_config_file = g_build_filename(g_get_user_config_dir(), rel_path, NULL);
362 GKeyFile* kf = g_key_file_new();
363 if(g_key_file_load_from_file(kf, user_config_file, 0, NULL))
364 app.color_scheme = g_key_file_get_string(kf, "GTK", "sGtk/ColorScheme", NULL);
365 else if(g_key_file_load_from_dirs(kf, rel_path, (const char**)g_get_system_config_dirs(), NULL, 0, NULL))
366 app.color_scheme = g_key_file_get_string(kf, "GTK", "sGtk/ColorScheme", NULL);
367 g_key_file_free(kf);
368 g_free(rel_path);
369 g_free(user_config_file);
370
371 if(app.color_scheme)
372 {
373 if(*app.color_scheme)
374 color_scheme_str_to_hash(app.color_scheme_hash, app.color_scheme);
375 else
376 {
377 g_free(app.color_scheme);
378 app.color_scheme = NULL;
379 }
380 }
381 }
382 else
383 {
384 char* gtkrc_file = g_build_filename(g_get_home_dir(), ".gtkrc-2.0", NULL);
385 gtkrc_file_get_color_scheme(gtkrc_file, app.color_scheme_hash);
386 g_free(gtkrc_file);
387 if(g_hash_table_size(app.color_scheme_hash) > 0)
388 app.color_scheme = color_scheme_hash_to_str(app.color_scheme_hash);
389 }
390 }
391
392 int main(int argc, char** argv)
393 {
394 GError* err = NULL;
395 GtkBuilder* b;
396
397 /* gettext support */
398 #ifdef ENABLE_NLS
399 bindtextdomain ( GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR );
400 bind_textdomain_codeset ( GETTEXT_PACKAGE, "UTF-8" );
401 textdomain ( GETTEXT_PACKAGE );
402 #endif
403
404 g_thread_init(NULL);
405 /* initialize GTK+ and parse the command line arguments */
406 if( G_UNLIKELY( ! gtk_init_with_args( &argc, &argv, "", option_entries, GETTEXT_PACKAGE, &err ) ) )
407 {
408 g_print( "Error: %s\n", err->message );
409 return 1;
410 }
411
412 app.abi_version = LXAPPEARANCE_ABI_VERSION;
413
414 /* check if we're under LXSession */
415 check_lxsession();
416
417 /* create GUI here */
418 b = gtk_builder_new();
419 if(!gtk_builder_add_from_file(b, PACKAGE_UI_DIR "/lxappearance.ui", NULL))
420 return 1;
421
422 /* NOTE: GUI must be created prior to loading settings from GtkSettings object.
423 * Some properties of GtkSettings class are installed by some other gtk classes.
424 * For example, "gtk-menu-images" property is actually installed by initialization
425 * of GtkImageMenuItem class. If we load the GUI first, then when the menu items
426 * are created, this property gets correctly registered. So later it can be read. */
427
428 /* load config values */
429 settings_init();
430
431 app.dlg = GTK_WIDGET(gtk_builder_get_object(b, "dlg"));
432
433 widget_theme_init(b);
434 color_scheme_init(b);
435 icon_theme_init(b);
436 cursor_theme_init(b);
437 font_init(b);
438 other_init(b);
439 /* the page for window manager plugins */
440 app.wm_page = GTK_WIDGET(gtk_builder_get_object(b, "wm_page"));
441
442 plugins_init(b);
443
444 g_signal_connect(app.dlg, "response", G_CALLBACK(on_dlg_response), NULL);
445
446 gtk_window_present(GTK_WINDOW(app.dlg));
447 g_object_unref(b);
448
449 gtk_main();
450
451 plugins_finalize();
452
453 return 0;
454 }
455
456 void lxappearance_changed()
457 {
458 if(!app.changed)
459 {
460 app.changed = TRUE;
461 gtk_dialog_set_response_sensitive(GTK_DIALOG(app.dlg), GTK_RESPONSE_APPLY, TRUE);
462 }
463 }
464
465 void on_check_button_toggled(GtkToggleButton* btn, gpointer user_data)
466 {
467 gboolean* val = (gboolean*)user_data;
468 gboolean new_val = gtk_toggle_button_get_active(btn);
469 if(new_val != *val)
470 {
471 *val = new_val;
472 lxappearance_changed();
473 }
474 }