change draw_color_new() proto to fill the struct and return status
[awesome.git] / config.c
bloba695bde02c4b2b9c6a6b9d5ac9ab9a725b9e764d
1 /*
2 * config.c - configuration management
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
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.
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.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <X11/keysym.h>
24 #include "config.h"
25 #include "statusbar.h"
26 #include "tag.h"
27 #include "rules.h"
28 #include "screen.h"
29 #include "widget.h"
30 #include "xutil.h"
31 #include "ewmh.h"
32 #include "defconfig.h"
33 #include "layouts/tile.h"
34 #include "common/configopts.h"
35 #include "common/configopts.h"
37 /* Permit to use mouse with many more buttons */
38 #ifndef Button6
39 #define Button6 6
40 #endif
41 #ifndef Button7
42 #define Button7 7
43 #endif
45 extern AwesomeConf globalconf;
46 extern cfg_opt_t awesome_opts[];
48 /** Link a name to a key symbol */
49 typedef struct
51 const char *name;
52 KeyCode keycode;
53 } KeyMod;
55 /** Link a name to a mouse button symbol */
56 typedef struct
58 const char *name;
59 unsigned int button;
60 } MouseButton;
62 extern const name_func_link_t UicbList[];
63 extern const name_func_link_t WidgetList[];
64 extern const name_func_link_t LayoutList[];
66 /** Lookup for a key mask from its name
67 * \param keyname Key name
68 * \return Key mask or 0 if not found
70 static KeyCode
71 key_mask_lookup(const char *keyname)
73 /** List of keyname and corresponding X11 mask codes */
74 static const KeyMod KeyModList[] =
76 {"Shift", ShiftMask},
77 {"Lock", LockMask},
78 {"Control", ControlMask},
79 {"Mod1", Mod1Mask},
80 {"Mod2", Mod2Mask},
81 {"Mod3", Mod3Mask},
82 {"Mod4", Mod4Mask},
83 {"Mod5", Mod5Mask},
84 {NULL, NoSymbol}
86 int i;
88 if(keyname)
89 for(i = 0; KeyModList[i].name; i++)
90 if(!a_strcmp(keyname, KeyModList[i].name))
91 return KeyModList[i].keycode;
93 return 0;
96 /** Lookup for a mouse button from its name
97 * \param button Mouse button name
98 * \return Mouse button or 0 if not found
100 static unsigned int
101 mouse_button_lookup(const char *button)
103 /** List of button name and corresponding X11 mask codes */
104 static const MouseButton MouseButtonList[] =
106 {"1", Button1},
107 {"2", Button2},
108 {"3", Button3},
109 {"4", Button4},
110 {"5", Button5},
111 {"6", Button6},
112 {"7", Button7},
113 {NULL, 0}
115 int i;
117 if(button)
118 for(i = 0; MouseButtonList[i].name; i++)
119 if(!a_strcmp(button, MouseButtonList[i].name))
120 return MouseButtonList[i].button;
122 return 0;
125 static Button *
126 parse_mouse_bindings(cfg_t * cfg, const char *secname, Bool handle_arg)
128 int i, j;
129 cfg_t *cfgsectmp;
130 Button *b = NULL, *head = NULL;
132 /* Mouse: layout click bindings */
133 for(i = cfg_size(cfg, secname) - 1; i >= 0; i--)
135 b = p_new(Button, 1);
137 cfgsectmp = cfg_getnsec(cfg, secname, i);
138 for(j = cfg_size(cfgsectmp, "modkey") - 1; j >= 0; j--)
139 b->mod |= key_mask_lookup(cfg_getnstr(cfgsectmp, "modkey", j));
140 b->button = mouse_button_lookup(cfg_getstr(cfgsectmp, "button"));
141 b->func = name_func_lookup(cfg_getstr(cfgsectmp, "command"), UicbList);
142 if(!b->func)
143 warn("unknown command %s\n", cfg_getstr(cfgsectmp, "command"));
144 if(handle_arg)
145 b->arg = a_strdup(cfg_getstr(cfgsectmp, "arg"));
146 else
147 b->arg = NULL;
149 button_list_push(&head, b);
152 return head;
156 static void
157 set_key_info(Key *key, cfg_t *cfg)
159 unsigned int j;
161 for(j = 0; j < cfg_size(cfg, "modkey"); j++)
162 key->mod |= key_mask_lookup(cfg_getnstr(cfg, "modkey", j));
163 key->func = name_func_lookup(cfg_getstr(cfg, "command"), UicbList);
164 if(!key->func)
165 warn("unknown command %s\n", cfg_getstr(cfg, "command"));
168 static KeyCode
169 key_to_keycode(char *str)
171 KeyCode kc;
172 int ikc;
174 if(a_strncmp(str, "#", 1))
175 return XKeysymToKeycode(globalconf.display, XStringToKeysym(str));
177 ikc = atoi(str + 1);
178 memcpy(&kc, &ikc, sizeof(KeyCode));
179 return kc;
182 static Key *
183 section_keys(cfg_t *cfg_keys)
185 Key *key = NULL, *head = NULL;
186 int i, j, numkeys;
187 cfg_t *cfgkeytmp;
189 for(i = cfg_size(cfg_keys, "key") - 1; i >= 0; i--)
191 key = p_new(Key, 1);
192 cfgkeytmp = cfg_getnsec(cfg_keys, "key", i);
193 set_key_info(key, cfgkeytmp);
194 key->keycode = key_to_keycode(cfg_getstr(cfgkeytmp, "key"));
195 key->arg = a_strdup(cfg_getstr(cfgkeytmp, "arg"));
196 key_list_push(&head, key);
199 for(i = cfg_size(cfg_keys, "keylist") - 1; i >= 0; i--)
201 cfgkeytmp = cfg_getnsec(cfg_keys, "keylist", i);
202 numkeys = cfg_size(cfgkeytmp, "keylist");
203 if(numkeys != (int) cfg_size(cfgkeytmp, "arglist"))
205 warn("number of keys != number of args in keylist");
206 continue;
209 for(j = 0; j < numkeys; j++)
211 key = p_new(Key, 1);
212 set_key_info(key, cfgkeytmp);
213 key->keycode = key_to_keycode(cfg_getnstr(cfgkeytmp, "keylist", j));
214 key->arg = a_strdup(cfg_getnstr(cfgkeytmp, "arglist", j));
215 key_list_push(&head, key);
219 return head;
223 static int
224 cmp_widget_cfg(const void *a, const void *b)
226 if (((cfg_t*)a)->line < ((cfg_t*)b)->line)
227 return -1;
229 if (((cfg_t*)a)->line > ((cfg_t*)b)->line)
230 return 1;
232 return 0;
235 static void
236 create_widgets(cfg_t* cfg_statusbar, Statusbar *statusbar)
238 cfg_t* widgets, *wptr;
239 Widget *widget = NULL;
240 unsigned int i, j, numwidgets = 0;
241 WidgetConstructor *widget_new;
243 for(i = 0; WidgetList[i].name; i++)
244 numwidgets += cfg_size(cfg_statusbar, WidgetList[i].name);
246 widgets = p_new(cfg_t, numwidgets);
247 wptr = widgets;
249 for(i = 0; WidgetList[i].name; i++)
250 for (j = 0; j < cfg_size(cfg_statusbar, WidgetList[i].name); j++)
252 memcpy(wptr,
253 cfg_getnsec(cfg_statusbar, WidgetList[i].name, j),
254 sizeof(cfg_t));
255 wptr++;
258 qsort(widgets, numwidgets, sizeof(cfg_t), cmp_widget_cfg);
260 for(i = 0; i < numwidgets; i++)
262 wptr = widgets + i;
263 widget_new = name_func_lookup(cfg_name(wptr), WidgetList);
264 if(widget_new)
266 widget = widget_new(statusbar, wptr);
267 widget_list_append(&statusbar->widgets, widget);
268 widget->buttons = parse_mouse_bindings(wptr, "mouse", a_strcmp(cfg_name(wptr), "taglist"));
270 else
271 warn("Ignoring unknown widget: %s.\n", cfg_name(widgets + i));
275 static void
276 config_parse_screen(cfg_t *cfg, int screen)
278 char buf[2];
279 const char *tmp;
280 Layout *layout = NULL;
281 Tag *tag = NULL;
282 Statusbar *statusbar = NULL;
283 cfg_t *cfg_general, *cfg_colors, *cfg_screen, *cfg_tags,
284 *cfg_layouts, *cfg_padding, *cfgsectmp;
285 VirtScreen *virtscreen = &globalconf.screens[screen];
286 int i, phys_screen = get_phys_screen(screen);
288 snprintf(buf, sizeof(buf), "%d", screen);
289 cfg_screen = cfg_gettsec(cfg, "screen", buf);
290 if(!cfg_screen)
291 cfg_screen = cfg_getsec(cfg, "screen");
293 if(!cfg_screen)
295 warn("parsing configuration file failed, no screen section found\n");
296 cfg_parse_buf(cfg, AWESOME_DEFAULT_CONFIG);
297 cfg_screen = cfg_getsec(cfg, "screen");
300 /* get screen specific sections */
301 cfg_tags = cfg_getsec(cfg_screen, "tags");
302 cfg_colors = cfg_getsec(cfg_screen, "colors");
303 cfg_general = cfg_getsec(cfg_screen, "general");
304 cfg_layouts = cfg_getsec(cfg_screen, "layouts");
305 cfg_padding = cfg_getsec(cfg_screen, "padding");
308 /* General section */
309 virtscreen->borderpx = cfg_getint(cfg_general, "border");
310 virtscreen->snap = cfg_getint(cfg_general, "snap");
311 virtscreen->resize_hints = cfg_getbool(cfg_general, "resize_hints");
312 virtscreen->sloppy_focus = cfg_getbool(cfg_general, "sloppy_focus");
313 virtscreen->sloppy_focus_raise = cfg_getbool(cfg_general, "sloppy_focus_raise");
314 virtscreen->new_become_master = cfg_getbool(cfg_general, "new_become_master");
315 virtscreen->new_get_focus = cfg_getbool(cfg_general, "new_get_focus");
316 virtscreen->opacity_unfocused = cfg_getint(cfg_general, "opacity_unfocused");
317 virtscreen->font = XftFontOpenName(globalconf.display,
318 phys_screen,
319 cfg_getstr(cfg_general, "font"));
320 if(!virtscreen->font)
321 eprint("awesome: cannot init font\n");
323 /* Colors */
324 draw_color_new(globalconf.display, phys_screen,
325 cfg_getstr(cfg_colors, "normal_border"),
326 &virtscreen->colors_normal[ColBorder]);
327 draw_color_new(globalconf.display, phys_screen,
328 cfg_getstr(cfg_colors, "normal_bg"),
329 &virtscreen->colors_normal[ColBG]);
330 draw_color_new(globalconf.display, phys_screen,
331 cfg_getstr(cfg_colors, "normal_fg"),
332 &virtscreen->colors_normal[ColFG]);
333 draw_color_new(globalconf.display, phys_screen,
334 cfg_getstr(cfg_colors, "focus_border"),
335 &virtscreen->colors_selected[ColBorder]);
336 draw_color_new(globalconf.display, phys_screen,
337 cfg_getstr(cfg_colors, "focus_bg"),
338 &virtscreen->colors_selected[ColBG]);
339 draw_color_new(globalconf.display, phys_screen,
340 cfg_getstr(cfg_colors, "focus_fg"),
341 &virtscreen->colors_selected[ColFG]);
342 draw_color_new(globalconf.display, phys_screen,
343 cfg_getstr(cfg_colors, "urgent_bg"),
344 &virtscreen->colors_urgent[ColBG]);
345 draw_color_new(globalconf.display, phys_screen,
346 cfg_getstr(cfg_colors, "urgent_fg"),
347 &virtscreen->colors_urgent[ColFG]);
349 /* Statusbar */
351 statusbar_list_init(&virtscreen->statusbar);
352 for(i = cfg_size(cfg_screen, "statusbar") - 1; i >= 0; i--)
354 statusbar = p_new(Statusbar, 1);
355 cfgsectmp = cfg_getnsec(cfg_screen, "statusbar", i);
356 statusbar->position = statusbar->dposition =
357 statusbar_get_position_from_str(cfg_getstr(cfgsectmp, "position"));
358 statusbar->height = cfg_getint(cfgsectmp, "height");
359 statusbar->width = cfg_getint(cfgsectmp, "width");
360 statusbar->name = a_strdup(cfg_title(cfgsectmp));
361 statusbar->screen = screen;
362 statusbar_preinit(statusbar);
363 create_widgets(cfgsectmp, statusbar);
364 statusbar_list_push(&virtscreen->statusbar, statusbar);
367 /* Layouts */
368 layout_list_init(&virtscreen->layouts);
369 if((i = cfg_size(cfg_layouts, "layout")))
370 for(--i; i >= 0; i--)
372 layout = p_new(Layout, 1);
373 cfgsectmp = cfg_getnsec(cfg_layouts, "layout", i);
374 layout->arrange = name_func_lookup(cfg_title(cfgsectmp), LayoutList);
375 if(!layout->arrange)
377 warn("unknown layout %s in configuration file\n", cfg_title(cfgsectmp));
378 layout->image = NULL;
379 continue;
381 layout->image = a_strdup(cfg_getstr(cfgsectmp, "image"));
383 layout_list_push(&virtscreen->layouts, layout);
385 else
387 warn("no default layout available\n");
388 layout->arrange = layout_tile;
391 /* Tags */
392 tag_list_init(&virtscreen->tags);
393 if((i = cfg_size(cfg_tags, "tag")))
394 for(--i; i >= 0; i--)
396 cfgsectmp = cfg_getnsec(cfg_tags, "tag", i);
398 tmp = cfg_getstr(cfgsectmp, "layout");
399 for(layout = virtscreen->layouts;
400 layout && layout->arrange != name_func_lookup(tmp, LayoutList);
401 layout = layout->next);
402 if(!layout)
403 layout = virtscreen->layouts;
405 tag = tag_new(cfg_title(cfgsectmp),
406 layout,
407 cfg_getfloat(cfgsectmp, "mwfact"),
408 cfg_getint(cfgsectmp, "nmaster"),
409 cfg_getint(cfgsectmp, "ncol"));
411 tag_push_to_screen(tag, screen);
413 else
415 warn("fatal: no tags found in configuration file\n");
416 tag->name = a_strdup("default");
417 tag->layout = virtscreen->layouts;
418 tag->nmaster = 1;
419 tag->ncol = 1;
420 tag->mwfact = 0.5;
423 ewmh_update_net_numbers_of_desktop(phys_screen);
424 ewmh_update_net_current_desktop(phys_screen);
425 ewmh_update_net_desktop_names(phys_screen);
427 /* select first tag by default */
428 virtscreen->tags[0].selected = True;
429 virtscreen->tags[0].was_selected = True;
431 /* padding */
432 virtscreen->padding.top = cfg_getint(cfg_padding, "top");
433 virtscreen->padding.bottom = cfg_getint(cfg_padding, "bottom");
434 virtscreen->padding.left = cfg_getint(cfg_padding, "left");
435 virtscreen->padding.right = cfg_getint(cfg_padding, "right");
438 /** Parse configuration file and initialize some stuff
439 * \param confpatharg Path to configuration file
441 void
442 config_parse(const char *confpatharg)
444 cfg_t *cfg, *cfg_rules, *cfg_keys, *cfg_mouse, *cfgsectmp;
445 int ret, screen, i;
446 char *confpath;
447 Rule *rule = NULL;
448 FILE *defconfig = NULL;
450 if(confpatharg)
451 confpath = a_strdup(confpatharg);
452 else
453 confpath = config_file();
455 globalconf.configpath = a_strdup(confpath);
457 cfg = cfg_init(awesome_opts, CFGF_NONE);
459 ret = cfg_parse(cfg, confpath);
461 switch(ret)
463 case CFG_FILE_ERROR:
464 perror("awesome: parsing configuration file failed");
465 if(!(defconfig = fopen(confpath, "w")))
466 perror("awesome: unable to create default configuration file");
467 break;
468 case CFG_PARSE_ERROR:
469 cfg_error(cfg, "awesome: parsing configuration file %s failed.\n", confpath);
470 break;
473 if(ret != CFG_SUCCESS)
475 warn("using default compile-time configuration\n");
476 cfg_free(cfg);
477 cfg = cfg_init(awesome_opts, CFGF_NONE);
478 cfg_parse_buf(cfg, AWESOME_DEFAULT_CONFIG);
481 /* get the right screen section */
482 for(screen = 0; screen < globalconf.nscreen; screen++)
483 config_parse_screen(cfg, screen);
485 /* get general sections */
486 cfg_rules = cfg_getsec(cfg, "rules");
487 cfg_keys = cfg_getsec(cfg, "keys");
488 cfg_mouse = cfg_getsec(cfg, "mouse");
490 /* Rules */
491 rule_list_init(&globalconf.rules);
492 for(i = cfg_size(cfg_rules, "rule") - 1; i >= 0; i--)
494 rule = p_new(Rule, 1);
495 cfgsectmp = cfg_getnsec(cfg_rules, "rule", i);
496 rule->prop_r = rules_compile_regex(cfg_getstr(cfgsectmp, "name"));
497 rule->tags_r = rules_compile_regex(cfg_getstr(cfgsectmp, "tags"));
498 rule->xprop = a_strdup(cfg_getstr(cfgsectmp, "xproperty_name"));
499 rule->xpropval_r = rules_compile_regex(cfg_getstr(cfgsectmp, "xproperty_value"));
500 rule->icon = a_strdup(cfg_getstr(cfgsectmp, "icon"));
501 rule->isfloating = rules_get_fuzzy_from_str(cfg_getstr(cfgsectmp, "float"));
502 rule->screen = cfg_getint(cfgsectmp, "screen");
503 rule->ismaster = rules_get_fuzzy_from_str(cfg_getstr(cfgsectmp, "master"));
504 rule->opacity = cfg_getfloat(cfgsectmp, "opacity");
505 if(rule->screen >= globalconf.nscreen)
506 rule->screen = 0;
508 rule_list_push(&globalconf.rules, rule);
511 /* Mouse: root window click bindings */
512 globalconf.buttons.root = parse_mouse_bindings(cfg_mouse, "root", True);
514 /* Mouse: client windows click bindings */
515 globalconf.buttons.client = parse_mouse_bindings(cfg_mouse, "client", True);
517 /* Keys */
518 globalconf.numlockmask = get_numlockmask(globalconf.display);
520 globalconf.keys = section_keys(cfg_keys);
522 if(defconfig)
524 cfg_print(cfg, defconfig);
525 fclose(defconfig);
528 /* Free! Like a river! */
529 cfg_free(cfg);
530 p_delete(&confpath);
533 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80