[config] add missing documentation
[awesome.git] / config.c
blob6cf142c47c2a93ec35b89b69d0b9f9b97565c594
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 <errno.h>
23 #include <X11/keysym.h>
25 #include "config.h"
26 #include "statusbar.h"
27 #include "tag.h"
28 #include "rules.h"
29 #include "screen.h"
30 #include "widget.h"
31 #include "defconfig.h"
32 #include "layouts/tile.h"
33 #include "common/configopts.h"
34 #include "common/xutil.h"
36 /* Permit to use mouse with many more buttons */
37 #ifndef Button6
38 #define Button6 6
39 #endif
40 #ifndef Button7
41 #define Button7 7
42 #endif
44 extern AwesomeConf globalconf;
45 extern cfg_opt_t awesome_opts[];
47 /** Link a name to a key symbol */
48 typedef struct
50 const char *name;
51 KeyCode keycode;
52 } KeyMod;
54 /** Link a name to a mouse button symbol */
55 typedef struct
57 const char *name;
58 unsigned int button;
59 } MouseButton;
61 extern const name_func_link_t UicbList[];
62 extern const name_func_link_t WidgetList[];
63 extern const name_func_link_t LayoutList[];
64 extern const name_func_link_t FloatingPlacementList[];
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 statusbar_widgets_create(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",
269 a_strcmp(cfg_name(wptr), "taglist") ? True : False);
271 else
272 warn("ignoring unknown widget: %s.\n", cfg_name(widgets + i));
276 static void
277 config_section_titlebar_init(cfg_t *cfg_titlebar, Titlebar *tb, int screen)
279 int phys_screen = screen_virttophys(screen);
280 cfg_t *cfg_styles = cfg_getsec(cfg_titlebar, "styles");
282 tb->position = tb->dposition = cfg_getposition(cfg_titlebar, "position");
283 tb->align = cfg_getalignment(cfg_titlebar, "align");
284 tb->text_align = cfg_getalignment(cfg_titlebar, "text_align");
285 tb->width = cfg_getint(cfg_titlebar, "width");
286 tb->height = cfg_getint(cfg_titlebar, "height");
287 draw_style_init(globalconf.display, phys_screen,
288 cfg_getsec(cfg_styles, "normal"),
289 &tb->styles.normal,
290 &globalconf.screens[screen].styles.normal);
291 draw_style_init(globalconf.display, phys_screen,
292 cfg_getsec(cfg_styles, "focus"),
293 &tb->styles.focus,
294 &globalconf.screens[screen].styles.focus);
295 draw_style_init(globalconf.display, phys_screen,
296 cfg_getsec(cfg_styles, "urgent"),
297 &tb->styles.urgent,
298 &globalconf.screens[screen].styles.urgent);
301 static void
302 config_parse_screen(cfg_t *cfg, int screen)
304 char buf[2];
305 const char *tmp;
306 FloatingPlacement flpl;
307 Layout *layout = NULL;
308 Tag *tag = NULL;
309 Statusbar *statusbar = NULL;
310 cfg_t *cfg_general, *cfg_styles, *cfg_screen, *cfg_tags,
311 *cfg_layouts, *cfg_padding, *cfgsectmp, *cfg_titlebar,
312 *cfg_styles_normal, *cfg_styles_focus, *cfg_styles_urgent;
313 VirtScreen *virtscreen = &globalconf.screens[screen];
314 int i, phys_screen = screen_virttophys(screen);
316 snprintf(buf, sizeof(buf), "%d", screen);
317 cfg_screen = cfg_gettsec(cfg, "screen", buf);
318 if(!cfg_screen)
319 cfg_screen = cfg_getsec(cfg, "screen");
321 if(!cfg_screen)
323 warn("parsing configuration file failed, no screen section found\n");
324 cfg_parse_buf(cfg, AWESOME_DEFAULT_CONFIG);
325 cfg_screen = cfg_getsec(cfg, "screen");
328 /* get screen specific sections */
329 cfg_tags = cfg_getsec(cfg_screen, "tags");
330 cfg_styles = cfg_getsec(cfg_screen, "styles");
331 cfg_general = cfg_getsec(cfg_screen, "general");
332 cfg_titlebar = cfg_getsec(cfg_screen, "titlebar");
333 cfg_layouts = cfg_getsec(cfg_screen, "layouts");
334 cfg_padding = cfg_getsec(cfg_screen, "padding");
337 /* General section */
338 virtscreen->borderpx = cfg_getint(cfg_general, "border");
339 virtscreen->snap = cfg_getint(cfg_general, "snap");
340 virtscreen->resize_hints = cfg_getbool(cfg_general, "resize_hints");
341 virtscreen->sloppy_focus = cfg_getbool(cfg_general, "sloppy_focus");
342 virtscreen->sloppy_focus_raise = cfg_getbool(cfg_general, "sloppy_focus_raise");
343 virtscreen->new_become_master = cfg_getbool(cfg_general, "new_become_master");
344 virtscreen->new_get_focus = cfg_getbool(cfg_general, "new_get_focus");
345 virtscreen->opacity_unfocused = cfg_getfloat(cfg_general, "opacity_unfocused");
346 virtscreen->opacity_focused = cfg_getfloat(cfg_general, "opacity_focused");
347 virtscreen->floating_placement =
348 name_func_lookup(cfg_getstr(cfg_general, "floating_placement"),
349 FloatingPlacementList);
350 virtscreen->mwfact_lower_limit = cfg_getfloat(cfg_general, "mwfact_lower_limit");
351 virtscreen->mwfact_upper_limit = cfg_getfloat(cfg_general, "mwfact_upper_limit");
353 if(virtscreen->mwfact_upper_limit < virtscreen->mwfact_lower_limit)
355 warn("mwfact_upper_limit must be greater than mwfact_lower_limit\n");
356 virtscreen->mwfact_upper_limit = 0.9;
357 virtscreen->mwfact_lower_limit = 0.1;
360 if(!virtscreen->floating_placement)
362 warn("unknown floating placement: %s\n", cfg_getstr(cfg_general, "floating_placement"));
363 virtscreen->floating_placement = FloatingPlacementList[0].func;
366 /* Colors */
367 if(!cfg_styles)
368 eprint("no colors section found");
370 if(!(cfg_styles_normal = cfg_getsec(cfg_styles, "normal")))
371 eprint("no normal colors section found\n");
372 if(!(cfg_styles_focus = cfg_getsec(cfg_styles, "focus")))
373 eprint("no focus colors section found\n");
374 if(!(cfg_styles_urgent = cfg_getsec(cfg_styles, "urgent")))
375 eprint("no urgent colors section found\n");
377 draw_style_init(globalconf.display, phys_screen,
378 cfg_styles_normal, &virtscreen->styles.normal, NULL);
379 draw_style_init(globalconf.display, phys_screen,
380 cfg_styles_focus, &virtscreen->styles.focus, &virtscreen->styles.normal);
381 draw_style_init(globalconf.display, phys_screen,
382 cfg_styles_urgent, &virtscreen->styles.urgent, &virtscreen->styles.normal);
384 if(!virtscreen->styles.normal.font)
385 eprint("no font available\n");
387 /* Titlebar */
388 config_section_titlebar_init(cfg_titlebar, &virtscreen->titlebar_default, screen);
390 /* Statusbar */
391 statusbar_list_init(&virtscreen->statusbar);
392 for(i = cfg_size(cfg_screen, "statusbar") - 1; i >= 0; i--)
394 statusbar = p_new(Statusbar, 1);
395 cfgsectmp = cfg_getnsec(cfg_screen, "statusbar", i);
396 statusbar->position = statusbar->dposition =
397 cfg_getposition(cfgsectmp, "position");
398 statusbar->height = cfg_getint(cfgsectmp, "height");
399 statusbar->width = cfg_getint(cfgsectmp, "width");
400 statusbar->name = a_strdup(cfg_title(cfgsectmp));
401 statusbar->screen = screen;
402 statusbar_preinit(statusbar);
403 statusbar_widgets_create(cfgsectmp, statusbar);
404 statusbar_list_push(&virtscreen->statusbar, statusbar);
407 /* Layouts */
408 layout_list_init(&virtscreen->layouts);
409 if((i = cfg_size(cfg_layouts, "layout")))
410 for(--i; i >= 0; i--)
412 layout = p_new(Layout, 1);
413 cfgsectmp = cfg_getnsec(cfg_layouts, "layout", i);
414 layout->arrange = name_func_lookup(cfg_title(cfgsectmp), LayoutList);
415 if(!layout->arrange)
417 warn("unknown layout %s in configuration file\n", cfg_title(cfgsectmp));
418 layout->image = NULL;
419 continue;
421 layout->image = a_strdup(cfg_getstr(cfgsectmp, "image"));
423 layout_list_push(&virtscreen->layouts, layout);
425 else
427 warn("no default layout available\n");
428 layout = p_new(Layout, 1);
429 layout->arrange = layout_tile;
430 layout_list_push(&virtscreen->layouts, layout);
433 /* Tags */
434 tag_list_init(&virtscreen->tags);
435 if((i = cfg_size(cfg_tags, "tag")))
436 for(--i; i >= 0; i--)
438 cfgsectmp = cfg_getnsec(cfg_tags, "tag", i);
440 tmp = cfg_getstr(cfgsectmp, "layout");
441 for(layout = virtscreen->layouts;
442 layout && layout->arrange != name_func_lookup(tmp, LayoutList);
443 layout = layout->next);
444 if(!layout)
445 layout = virtscreen->layouts;
447 tag = tag_new(cfg_title(cfgsectmp),
448 layout,
449 cfg_getfloat(cfgsectmp, "mwfact"),
450 cfg_getint(cfgsectmp, "nmaster"),
451 cfg_getint(cfgsectmp, "ncol"));
453 tag_push_to_screen(tag, screen);
455 else
457 warn("fatal: no tags found in configuration file\n");
458 tag = tag_new("default", virtscreen->layouts, 0.5, 1, 1);
459 tag_push_to_screen(tag, screen);
462 /* select first tag by default */
463 virtscreen->tags[0].selected = True;
464 virtscreen->tags[0].was_selected = True;
466 /* padding */
467 virtscreen->padding.top = cfg_getint(cfg_padding, "top");
468 virtscreen->padding.bottom = cfg_getint(cfg_padding, "bottom");
469 virtscreen->padding.left = cfg_getint(cfg_padding, "left");
470 virtscreen->padding.right = cfg_getint(cfg_padding, "right");
473 /** Parse configuration file and initialize some stuff
474 * \param confpatharg Path to configuration file
476 void
477 config_parse(const char *confpatharg)
479 cfg_t *cfg, *cfg_rules, *cfg_keys, *cfg_mouse, *cfgsectmp;
480 int ret, screen, i;
481 char *confpath;
482 Rule *rule = NULL;
483 FILE *defconfig = NULL;
485 if(confpatharg)
486 confpath = a_strdup(confpatharg);
487 else
488 confpath = config_file();
490 globalconf.configpath = a_strdup(confpath);
492 cfg = cfg_new();
494 ret = cfg_parse(cfg, confpath);
496 switch(ret)
498 case CFG_FILE_ERROR:
499 warn("parsing configuration file failed: %s\n", strerror(errno));
500 if(!(defconfig = fopen(confpath, "w")))
501 warn("unable to create default configuration file: %s\n", strerror(errno));
502 break;
503 case CFG_PARSE_ERROR:
504 cfg_error(cfg, "W: awesome: parsing configuration file %s failed.\n", confpath);
505 break;
508 if(ret != CFG_SUCCESS)
510 warn("using default compile-time configuration\n");
511 cfg_free(cfg);
512 cfg = cfg_init(awesome_opts, CFGF_NONE);
513 cfg_parse_buf(cfg, AWESOME_DEFAULT_CONFIG);
516 /* get the right screen section */
517 for(screen = 0; screen < globalconf.screens_info->nscreen; screen++)
518 config_parse_screen(cfg, screen);
520 /* get general sections */
521 cfg_rules = cfg_getsec(cfg, "rules");
522 cfg_keys = cfg_getsec(cfg, "keys");
523 cfg_mouse = cfg_getsec(cfg, "mouse");
525 /* Rules */
526 rule_list_init(&globalconf.rules);
527 for(i = cfg_size(cfg_rules, "rule") - 1; i >= 0; i--)
529 rule = p_new(Rule, 1);
530 cfgsectmp = cfg_getnsec(cfg_rules, "rule", i);
531 rule->prop_r = rules_compile_regex(cfg_getstr(cfgsectmp, "name"));
532 rule->tags_r = rules_compile_regex(cfg_getstr(cfgsectmp, "tags"));
533 rule->xprop = a_strdup(cfg_getstr(cfgsectmp, "xproperty_name"));
534 rule->xpropval_r = rules_compile_regex(cfg_getstr(cfgsectmp, "xproperty_value"));
535 rule->icon = a_strdup(cfg_getstr(cfgsectmp, "icon"));
536 rule->isfloating = rules_get_fuzzy_from_str(cfg_getstr(cfgsectmp, "float"));
537 rule->screen = cfg_getint(cfgsectmp, "screen");
538 rule->ismaster = rules_get_fuzzy_from_str(cfg_getstr(cfgsectmp, "master"));
539 rule->opacity = cfg_getfloat(cfgsectmp, "opacity");
540 config_section_titlebar_init(cfg_getsec(cfgsectmp, "titlebar"), &rule->titlebar, 0);
541 if(rule->screen >= globalconf.screens_info->nscreen)
542 rule->screen = 0;
544 rule_list_push(&globalconf.rules, rule);
547 /* Mouse: root window click bindings */
548 globalconf.buttons.root = parse_mouse_bindings(cfg_mouse, "root", True);
550 /* Mouse: client windows click bindings */
551 globalconf.buttons.client = parse_mouse_bindings(cfg_mouse, "client", True);
553 /* Mouse: titlebar windows click bindings */
554 globalconf.buttons.titlebar = parse_mouse_bindings(cfg_mouse, "titlebar", True);
556 /* Keys */
557 globalconf.numlockmask = xgetnumlockmask(globalconf.display);
559 globalconf.keys = section_keys(cfg_keys);
561 if(defconfig)
563 fwrite(AWESOME_DEFAULT_CONFIG, a_strlen(AWESOME_DEFAULT_CONFIG), 1, defconfig);
564 fclose(defconfig);
567 /* Free! Like a river! */
568 cfg_free(cfg);
569 p_delete(&confpath);
572 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80