~ubuntu-branches/ubuntu/oneiric/avant-window-navigator/oneiric

« back to all changes in this revision

Viewing changes to libawn/awn-settings.c

  • Committer: Bazaar Package Importer
  • Author(s): Julien Lavergne
  • Date: 2008-05-24 14:42:01 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080524144201-r3v8e4g2hv2q1i9x
Tags: 0.2.6-6
* debian/patches/04-fix-colormap.patch
 - Fix crash in awn-manager if colormap == None. Thanks Emme for the 
   patch. (Closes: #482030) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2007 Neil Jagdish Patel <njpatel@gmail.com>
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this library; if not, write to the
 
16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
 *
 
19
 *  Author : Neil Jagdish Patel <njpatel@gmail.com>
 
20
*/
 
21
 
 
22
#include <assert.h>
 
23
#include "awn-settings.h"
 
24
 
 
25
#include "config.h"
 
26
 
 
27
#include "awn-config-client.h"
 
28
 
 
29
#define FORCE_MONITOR   "force_monitor"         /*bool*/
 
30
#define MONITOR_WIDTH   "monitor_width"         /*int*/
 
31
#define MONITOR_HEIGHT  "monitor_height"        /*int*/
 
32
#define PANEL_MODE      "panel_mode"            /*bool*/
 
33
#define AUTO_HIDE       "auto_hide"             /*bool*/
 
34
#define AUTO_HIDE_DELAY "auto_hide_delay"       /*int*/
 
35
#define KEEP_BELOW      "keep_below"            /*bool*/
 
36
 
 
37
#define BAR             "bar"
 
38
#define ROUNDED_CORNERS "rounded_corners"       /*bool*/
 
39
#define CORNER_RADIUS   "corner_radius"         /*float*/
 
40
#define RENDER_PATTERN  "render_pattern"        /*bool*/
 
41
#define PATTERN_URI     "pattern_uri"           /*string*/
 
42
#define PATTERN_ALPHA   "pattern_alpha"         /*float*/
 
43
#define GLASS_STEP_1    "glass_step_1"          /*string*/
 
44
#define GLASS_STEP_2    "glass_step_2"          /*string*/
 
45
#define GLASS_HISTEP_1  "glass_histep_1"        /*string*/
 
46
#define GLASS_HISTEP_2  "glass_histep_2"        /*string*/
 
47
#define BORDER_COLOR    "border_color"          /*string*/
 
48
#define HILIGHT_COLOR   "hilight_color"         /*string*/
 
49
#define SHOW_SEPARATOR  "show_separator"        /*bool*/
 
50
#define SEP_COLOR       "sep_color"             /*string*/
 
51
#define BAR_HEIGHT      "bar_height"            /*int*/
 
52
#define BAR_ANGLE       "bar_angle"             /*int, between 0 and 90*/
 
53
#define ICON_OFFSET     "icon_offset"           /*float*/
 
54
#define BAR_POS         "bar_pos"               /*float, between 0 and 1 */
 
55
 
 
56
#define CURVES_SYMMETRY  "curves_symmetry"      /*float, between 0 and 1*/
 
57
#define CURVINESS                "curviness"            /*float, between 0 and 1*/
 
58
 
 
59
#define WINMAN          "window_manager"
 
60
#define SHOW_ALL_WINS   "show_all_windows"      /*bool*/
 
61
#define LAUNCHERS       "launchers"             /*str list*/
 
62
 
 
63
#define APP             "app"
 
64
#define ACTIVE_PNG      "active_png"            /*string*/
 
65
#define USE_PNG         "use_png"               /*bool*/
 
66
#define ARROW_COLOR     "arrow_color"           /*color*/
 
67
#define ARROW_OFFSET    "arrow_offset"          /*offset*/
 
68
#define TASKS_H_ARROWS  "tasks_have_arrows"     /*bool*/
 
69
#define NAME_CHANGE_NOTIFY      "name_change_notify"    /*bool*/
 
70
#define ALPHA_EFFECT    "alpha_effect"          /*bool*/
 
71
#define ICON_EFFECT     "icon_effect"           /*int*/
 
72
#define HOVER_BOUNCE_EFFECT     "hover_bounce_effect"   /*bool*/
 
73
 
 
74
#define TITLE           "title"
 
75
#define TEXT_COLOR      "text_color"            /*color*/
 
76
#define SHADOW_COLOR    "shadow_color"          /*color*/
 
77
#define BACKGROUND      "background"            /*color*/
 
78
#define FONT_FACE       "font_face"             /*string*/
 
79
 
 
80
/* globals */
 
81
static AwnSettings *settings            = NULL;
 
82
static AwnConfigClient *client          = NULL;
 
83
 
 
84
/* prototypes */
 
85
static void awn_load_bool        (AwnConfigClient *client, const gchar *group,
 
86
                                  const gchar* key, gboolean *data, gboolean def);
 
87
static void awn_load_string      (AwnConfigClient *client, const gchar *group,
 
88
                                  const gchar* key, gchar **data, const gchar *def);
 
89
static void awn_load_float       (AwnConfigClient *client, const gchar *group,
 
90
                                  const gchar* key, gfloat *data, gfloat def);
 
91
static void awn_load_int         (AwnConfigClient *client, const gchar *group,
 
92
                                  const gchar* key, gint *data, gint def);
 
93
static void awn_load_color       (AwnConfigClient *client, const gchar *group,
 
94
                                  const gchar* key, AwnColor *color, const gchar * def);
 
95
static void awn_load_string_list (AwnConfigClient *client, const gchar *group,
 
96
                                  const gchar* key, GSList **data, GSList *def);
 
97
 
 
98
static void awn_notify_bool   (AwnConfigClientNotifyEntry *entry, gboolean* data);
 
99
static void awn_notify_string (AwnConfigClientNotifyEntry *entry, gchar** data);
 
100
static void awn_notify_float  (AwnConfigClientNotifyEntry *entry, gfloat* data);
 
101
static void awn_notify_int    (AwnConfigClientNotifyEntry *entry, gint* data);
 
102
static void awn_notify_color  (AwnConfigClientNotifyEntry *entry, AwnColor *data);
 
103
 
 
104
static void hex2float(gchar* HexColor, gfloat* FloatColor);
 
105
 
 
106
AwnSettings* awn_get_settings(void)
 
107
{
 
108
        //assert(settings != NULL);
 
109
        if (settings) return settings;
 
110
        else return awn_settings_new();
 
111
}
 
112
 
 
113
AwnSettings* 
 
114
awn_settings_new()
 
115
{
 
116
        GdkScreen *screen;
 
117
        gint width = 1024;
 
118
        gint height = 768;
 
119
        
 
120
        screen = gdk_screen_get_default();
 
121
        if (screen) {
 
122
                width = gdk_screen_get_width(screen);
 
123
                height = gdk_screen_get_height(screen);
 
124
        }
 
125
 
 
126
        if (settings)
 
127
                return settings;
 
128
        AwnSettings *s = NULL;
 
129
        
 
130
        s = g_new(AwnSettings, 1);
 
131
        settings = s;
 
132
        client = awn_config_client_new ();
 
133
        
 
134
        s->icon_theme = gtk_icon_theme_get_default();
 
135
        /* general settings */
 
136
        awn_config_client_ensure_group (client, AWN_CONFIG_CLIENT_DEFAULT_GROUP);
 
137
        awn_load_bool(client, AWN_CONFIG_CLIENT_DEFAULT_GROUP, FORCE_MONITOR, &s->force_monitor, FALSE);
 
138
        awn_load_int(client, AWN_CONFIG_CLIENT_DEFAULT_GROUP, MONITOR_WIDTH, &s->monitor_width, width);
 
139
        awn_load_int(client, AWN_CONFIG_CLIENT_DEFAULT_GROUP, MONITOR_HEIGHT, &s->monitor_height, height);
 
140
        awn_load_bool(client, AWN_CONFIG_CLIENT_DEFAULT_GROUP, PANEL_MODE, &s->panel_mode, FALSE);
 
141
        awn_load_bool(client, AWN_CONFIG_CLIENT_DEFAULT_GROUP, AUTO_HIDE, &s->auto_hide, FALSE);
 
142
        awn_load_int(client, AWN_CONFIG_CLIENT_DEFAULT_GROUP, AUTO_HIDE_DELAY, &s->auto_hide_delay, 1000);
 
143
        awn_load_bool(client, AWN_CONFIG_CLIENT_DEFAULT_GROUP, KEEP_BELOW, &s->keep_below, FALSE);
 
144
        s->hidden = FALSE;
 
145
        
 
146
        /* Bar settings */
 
147
        awn_config_client_ensure_group (client, BAR);
 
148
 
 
149
        awn_load_int(client, BAR, BAR_HEIGHT, &s->bar_height,48);
 
150
        awn_load_float(client, BAR, BAR_POS, &s->bar_pos,0.5);
 
151
        awn_load_int(client, BAR, BAR_ANGLE, &s->bar_angle,0);
 
152
        awn_load_int(client, BAR, ICON_OFFSET, &s->icon_offset,10);
 
153
        awn_load_bool(client, BAR, ROUNDED_CORNERS, &s->rounded_corners, TRUE);
 
154
        awn_load_float(client, BAR, CORNER_RADIUS, &s->corner_radius, 10.0);    
 
155
        awn_load_bool(client, BAR, RENDER_PATTERN, &s->render_pattern, FALSE);  
 
156
        awn_load_string(client, BAR, PATTERN_URI, &s->pattern_uri, "~");
 
157
        awn_load_float(client, BAR, PATTERN_ALPHA, &s->pattern_alpha, 1.0);
 
158
        
 
159
        awn_load_color(client, BAR, GLASS_STEP_1, &s->g_step_1, "454545C8");
 
160
        awn_load_color(client, BAR, GLASS_STEP_2, &s->g_step_2, "010101BE");
 
161
        awn_load_color(client, BAR, GLASS_HISTEP_1, &s->g_histep_1, "FFFFFF0B");
 
162
        awn_load_color(client, BAR, GLASS_HISTEP_2, &s->g_histep_2, "FFFFFF0A");
 
163
        
 
164
        awn_load_color(client, BAR, BORDER_COLOR, &s->border_color, "000000CC");
 
165
        awn_load_color(client, BAR, HILIGHT_COLOR, &s->hilight_color, "FFFFFF11");
 
166
        
 
167
        awn_load_bool(client, BAR, SHOW_SEPARATOR, &s->show_separator, TRUE);
 
168
        awn_load_color(client, BAR, SEP_COLOR, &s->sep_color, "FFFFFF00");
 
169
        
 
170
        awn_load_float(client, BAR, CURVES_SYMMETRY, &s->curves_symmetry,0.7);
 
171
        awn_load_float(client, BAR, CURVINESS, &s->curviness,1.0);
 
172
        
 
173
        /* Window Manager settings */
 
174
        awn_config_client_ensure_group (client, WINMAN);
 
175
        awn_load_bool(client, WINMAN, SHOW_ALL_WINS, &s->show_all_windows, TRUE);
 
176
        awn_load_string_list(client, WINMAN, LAUNCHERS, &s->launchers, NULL);
 
177
        
 
178
        /* App settings */
 
179
        awn_config_client_ensure_group (client, APP);
 
180
        awn_load_string(client, APP, ACTIVE_PNG, &s->active_png, "~");
 
181
        awn_load_bool(client, APP, USE_PNG, &s->use_png, FALSE);
 
182
        awn_load_color(client, APP, ARROW_COLOR, &s->arrow_color, "FFFFFF66");
 
183
        awn_load_int(client, APP, ARROW_OFFSET, &s->arrow_offset, 2);
 
184
        awn_load_bool(client, APP, TASKS_H_ARROWS, &s->tasks_have_arrows, FALSE);
 
185
        awn_load_bool(client, APP, NAME_CHANGE_NOTIFY, &s->name_change_notify, FALSE);
 
186
        awn_load_bool(client, APP, ALPHA_EFFECT, &s->alpha_effect, FALSE);
 
187
        awn_load_int(client, APP, ICON_EFFECT, &s->icon_effect, 0);
 
188
 
 
189
        /* Title settings */
 
190
        awn_config_client_ensure_group (client, TITLE);
 
191
        awn_load_color(client, TITLE, TEXT_COLOR, &s->text_color, "FFFFFFFF");
 
192
        awn_load_color(client, TITLE, SHADOW_COLOR, &s->shadow_color, "1B3B12E1");
 
193
        awn_load_color(client, TITLE, BACKGROUND, &s->background, "000000AA");
 
194
        awn_load_string(client, TITLE, FONT_FACE, &s->font_face, "Sans 11");    
 
195
        
 
196
        s->task_width = settings->bar_height + 12;
 
197
        
 
198
        /* make the custom icons directory */
 
199
        gchar *path = g_build_filename (g_get_home_dir (),
 
200
                                        ".config/awn/custom-icons",
 
201
                                        NULL);
 
202
        g_mkdir_with_parents (path, 0755);
 
203
        g_free (path);
 
204
        
 
205
        return s;
 
206
}
 
207
 
 
208
static void 
 
209
awn_notify_bool (AwnConfigClientNotifyEntry *entry, gboolean* data)
 
210
{
 
211
        *data = entry->value.bool_val;
 
212
        
 
213
        //if (*data)
 
214
                //g_print("%s/%s is true\n", entry->group, entry->key);
 
215
}
 
216
 
 
217
static void 
 
218
awn_notify_string (AwnConfigClientNotifyEntry *entry, gchar** data)
 
219
{
 
220
        *data = entry->value.str_val;
 
221
        
 
222
        //g_print("%s/%s is %s\n", entry->group, entry->key, *data);
 
223
}
 
224
 
 
225
static void 
 
226
awn_notify_float (AwnConfigClientNotifyEntry *entry, gfloat* data)
 
227
{
 
228
        *data = entry->value.float_val;
 
229
        //g_print("%s/%s is %f\n", entry->group, entry->key, *data);
 
230
}
 
231
 
 
232
static void 
 
233
awn_notify_int (AwnConfigClientNotifyEntry *entry, gint* data)
 
234
{
 
235
        *data = entry->value.int_val;
 
236
        //g_print("%s/%s is %f\n", entry->group, entry->key, *data);
 
237
}
 
238
 
 
239
static void 
 
240
awn_notify_color (AwnConfigClientNotifyEntry *entry, AwnColor* color)
 
241
{
 
242
        float colors[4];
 
243
        
 
244
        hex2float (entry->value.str_val, colors);
 
245
        
 
246
        color->red = colors[0];
 
247
        color->green = colors[1];
 
248
        color->blue = colors[2];
 
249
        color->alpha = colors[3];
 
250
}
 
251
 
 
252
 
 
253
static void
 
254
awn_load_bool(AwnConfigClient *client, const gchar *group, const gchar* key, gboolean *data, gboolean def)
 
255
{
 
256
        if (awn_config_client_entry_exists (client, group, key)) {
 
257
                *data = awn_config_client_get_bool (client, group, key, NULL);
 
258
        } else {
 
259
                g_print("%s unset, setting now\n", key);
 
260
                awn_config_client_set_bool (client, group, key, def, NULL);
 
261
                *data = def;
 
262
        }
 
263
        awn_config_client_notify_add (client, group, key, (AwnConfigClientNotifyFunc)awn_notify_bool, data);
 
264
}
 
265
 
 
266
static void
 
267
awn_load_string(AwnConfigClient *client, const gchar *group, const gchar* key, gchar **data, const gchar *def)
 
268
{
 
269
        if (awn_config_client_entry_exists (client, group, key)) {
 
270
                *data = awn_config_client_get_string (client, group, key, NULL);
 
271
        } else {
 
272
                g_print("%s unset, setting now\n", key);
 
273
                awn_config_client_set_string (client, group, key, (gchar*)def, NULL);
 
274
                *data = g_strdup(def);
 
275
        }
 
276
        
 
277
        awn_config_client_notify_add (client, group, key, (AwnConfigClientNotifyFunc)awn_notify_string, data);
 
278
}
 
279
 
 
280
static void
 
281
awn_load_float(AwnConfigClient *client, const gchar *group, const gchar* key, gfloat *data, gfloat def)
 
282
{
 
283
        if (awn_config_client_entry_exists (client, group, key)) {
 
284
                *data = awn_config_client_get_float (client, group, key, NULL);
 
285
        } else {
 
286
                g_print("%s unset, setting now\n", key);
 
287
                awn_config_client_set_float (client, group, key, def, NULL);
 
288
                *data = def;
 
289
        }
 
290
        
 
291
        awn_config_client_notify_add (client, group, key, (AwnConfigClientNotifyFunc)awn_notify_float, data);
 
292
}
 
293
 
 
294
static void
 
295
awn_load_int(AwnConfigClient *client, const gchar *group, const gchar* key, gint *data, gint def)
 
296
{
 
297
        if (awn_config_client_entry_exists (client, group, key)) {
 
298
                *data = awn_config_client_get_int (client, group, key, NULL);
 
299
        } else {
 
300
                g_print("%s unset, setting now\n", key);
 
301
                awn_config_client_set_int (client, group, key, def, NULL);
 
302
                *data = def;
 
303
        }
 
304
        
 
305
        awn_config_client_notify_add (client, group, key, (AwnConfigClientNotifyFunc)awn_notify_int, data);
 
306
}
 
307
 
 
308
static void
 
309
awn_load_color(AwnConfigClient *client, const gchar *group, const gchar* key, AwnColor *color, const gchar * def)
 
310
{
 
311
        float colors[4];
 
312
        if (awn_config_client_entry_exists (client, group, key)) {
 
313
                hex2float (awn_config_client_get_string (client, group, key, NULL), colors);
 
314
        } else {
 
315
                g_print("%s unset, setting now\n", key);
 
316
                awn_config_client_set_string (client, group, key, (gchar*)def, NULL);
 
317
                hex2float ( (gchar*)def, colors);
 
318
        }
 
319
        color->red = colors[0];
 
320
        color->green = colors[1];
 
321
        color->blue = colors[2];
 
322
        color->alpha = colors[3];
 
323
                
 
324
        awn_config_client_notify_add (client, group, key, (AwnConfigClientNotifyFunc)awn_notify_color, color);
 
325
}
 
326
 
 
327
static void
 
328
awn_load_string_list(AwnConfigClient *client, const gchar *group, const gchar* key, GSList **data, GSList *def)
 
329
{
 
330
        if (awn_config_client_entry_exists (client, group, key)) {
 
331
                *data = awn_config_client_get_list (client, group, key, AWN_CONFIG_CLIENT_LIST_TYPE_STRING, NULL);
 
332
        } else {
 
333
                g_print("%s unset, setting now\n", key);
 
334
                awn_config_client_set_list (client, group, key, AWN_CONFIG_CLIENT_LIST_TYPE_STRING, def, NULL);
 
335
                *data = def;
 
336
        }
 
337
}
 
338
 
 
339
static int 
 
340
getdec(char hexchar)
 
341
{
 
342
   if ((hexchar >= '0') && (hexchar <= '9')) return hexchar - '0';
 
343
   if ((hexchar >= 'A') && (hexchar <= 'F')) return hexchar - 'A' + 10;
 
344
   if ((hexchar >= 'a') && (hexchar <= 'f')) return hexchar - 'a' + 10;
 
345
 
 
346
   return -1; // Wrong character
 
347
 
 
348
}
 
349
 
 
350
static void 
 
351
hex2float(gchar* HexColor, gfloat* FloatColor)
 
352
{
 
353
   gchar* HexColorPtr = HexColor;
 
354
 
 
355
   gint i = 0;
 
356
   for (i = 0; i < 4; i++)
 
357
   {
 
358
     gint IntColor = (getdec(HexColorPtr[0]) * 16) +
 
359
                     getdec(HexColorPtr[1]);
 
360
 
 
361
     FloatColor[i] = (gfloat) IntColor / 255.0;
 
362
     HexColorPtr += 2;
 
363
   }
 
364
 
 
365
}
 
366
 
 
367