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