rename opts to awesome_opts
[awesome.git] / config.c
blobe9db2feab044033a3a7604cf3990aff27bbf6275
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 /**
23 * @defgroup ui_callback User Interface Callbacks
26 #include <X11/keysym.h>
28 #include "config.h"
29 #include "statusbar.h"
30 #include "tag.h"
31 #include "rules.h"
32 #include "screen.h"
33 #include "widget.h"
34 #include "xutil.h"
35 #include "ewmh.h"
36 #include "defconfig.h"
37 #include "layouts/tile.h"
38 #include "common/configopts.h"
40 /* Permit to use mouse with many more buttons */
41 #ifndef Button6
42 #define Button6 6
43 #endif
44 #ifndef Button7
45 #define Button7 7
46 #endif
48 extern AwesomeConf globalconf;
50 /** Link a name to a key symbol */
51 typedef struct
53 const char *name;
54 KeySym keysym;
55 } KeyMod;
57 /** Link a name to a mouse button symbol */
58 typedef struct
60 const char *name;
61 unsigned int button;
62 } MouseButton;
64 extern const name_func_link_t UicbList[];
65 extern const name_func_link_t WidgetList[];
66 extern const name_func_link_t LayoutList[];
68 /** Lookup for a key mask from its name
69 * \param keyname Key name
70 * \return Key mask or 0 if not found
72 static KeySym
73 key_mask_lookup(const char *keyname)
75 /** List of keyname and corresponding X11 mask codes */
76 static const KeyMod KeyModList[] =
78 {"Shift", ShiftMask},
79 {"Lock", LockMask},
80 {"Control", ControlMask},
81 {"Mod1", Mod1Mask},
82 {"Mod2", Mod2Mask},
83 {"Mod3", Mod3Mask},
84 {"Mod4", Mod4Mask},
85 {"Mod5", Mod5Mask},
86 {NULL, NoSymbol}
88 int i;
90 if(keyname)
91 for(i = 0; KeyModList[i].name; i++)
92 if(!a_strcmp(keyname, KeyModList[i].name))
93 return KeyModList[i].keysym;
95 return NoSymbol;
98 /** Lookup for a mouse button from its name
99 * \param button Mouse button name
100 * \return Mouse button or 0 if not found
102 static unsigned int
103 mouse_button_lookup(const char *button)
105 /** List of button name and corresponding X11 mask codes */
106 static const MouseButton MouseButtonList[] =
108 {"1", Button1},
109 {"2", Button2},
110 {"3", Button3},
111 {"4", Button4},
112 {"5", Button5},
113 {"6", Button6},
114 {"7", Button7},
115 {NULL, 0}
117 int i;
119 if(button)
120 for(i = 0; MouseButtonList[i].name; i++)
121 if(!a_strcmp(button, MouseButtonList[i].name))
122 return MouseButtonList[i].button;
124 return 0;
127 static Button *
128 parse_mouse_bindings(cfg_t * cfg, const char *secname, Bool handle_arg)
130 int i, j;
131 cfg_t *cfgsectmp;
132 Button *b = NULL, *head = NULL;
134 /* Mouse: layout click bindings */
135 for(i = cfg_size(cfg, secname) - 1; i >= 0; i--)
137 b = p_new(Button, 1);
139 cfgsectmp = cfg_getnsec(cfg, secname, i);
140 for(j = cfg_size(cfgsectmp, "modkey") - 1; j >= 0; j--)
141 b->mod |= key_mask_lookup(cfg_getnstr(cfgsectmp, "modkey", j));
142 b->button = mouse_button_lookup(cfg_getstr(cfgsectmp, "button"));
143 b->func = name_func_lookup(cfg_getstr(cfgsectmp, "command"), UicbList);
144 if(!b->func)
145 warn("unknown command %s\n", cfg_getstr(cfgsectmp, "command"));
146 if(handle_arg)
147 b->arg = a_strdup(cfg_getstr(cfgsectmp, "arg"));
148 else
149 b->arg = NULL;
151 button_list_push(&head, b);
154 return head;
158 static void
159 set_key_info(Key *key, cfg_t *cfg)
161 unsigned int j;
163 for(j = 0; j < cfg_size(cfg, "modkey"); j++)
164 key->mod |= key_mask_lookup(cfg_getnstr(cfg, "modkey", j));
165 key->func = name_func_lookup(cfg_getstr(cfg, "command"), UicbList);
166 if(!key->func)
167 warn("unknown command %s\n", cfg_getstr(cfg, "command"));
170 static KeySym
171 key_to_keysym(char *str)
173 KeyCode kc;
174 int ikc;
176 if(a_strncmp(str, "#", 1))
177 return XStringToKeysym(str);
179 ikc = atoi(str + 1);
180 memcpy(&kc, &ikc, sizeof(KeyCode));
181 return XKeycodeToKeysym(globalconf.display, kc, 0);
184 static Key *
185 section_keys(cfg_t *cfg_keys)
187 Key *key = NULL, *head = NULL;
188 int i, j, numkeys;
189 cfg_t *cfgkeytmp;
191 for(i = cfg_size(cfg_keys, "key") - 1; i >= 0; i--)
193 key = p_new(Key, 1);
194 cfgkeytmp = cfg_getnsec(cfg_keys, "key", i);
195 set_key_info(key, cfgkeytmp);
196 key->keysym = key_to_keysym(cfg_getstr(cfgkeytmp, "key"));
197 key->arg = a_strdup(cfg_getstr(cfgkeytmp, "arg"));
198 key_list_push(&head, key);
201 for(i = cfg_size(cfg_keys, "keylist") - 1; i >= 0; i--)
203 cfgkeytmp = cfg_getnsec(cfg_keys, "keylist", i);
204 numkeys = cfg_size(cfgkeytmp, "keylist");
205 if(numkeys != (int) cfg_size(cfgkeytmp, "arglist"))
207 warn("number of keys != number of args in keylist");
208 continue;
211 for(j = 0; j < numkeys; j++)
213 key = p_new(Key, 1);
214 set_key_info(key, cfgkeytmp);
215 key->keysym = key_to_keysym(cfg_getnstr(cfgkeytmp, "keylist", j));
216 key->arg = a_strdup(cfg_getnstr(cfgkeytmp, "arglist", j));
217 key_list_push(&head, key);
221 return head;
225 static int
226 cmp_widget_cfg(const void *a, const void *b)
228 if (((cfg_t*)a)->line < ((cfg_t*)b)->line)
229 return -1;
231 if (((cfg_t*)a)->line > ((cfg_t*)b)->line)
232 return 1;
234 return 0;
237 static void
238 create_widgets(cfg_t* cfg_statusbar, Statusbar *statusbar)
240 cfg_t* widgets, *wptr;
241 Widget *widget = NULL;
242 unsigned int i, j, numwidgets = 0;
243 WidgetConstructor *widget_new;
245 for(i = 0; WidgetList[i].name; i++)
246 numwidgets += cfg_size(cfg_statusbar, WidgetList[i].name);
248 widgets = p_new(cfg_t, numwidgets);
249 wptr = widgets;
251 for(i = 0; WidgetList[i].name; i++)
252 for (j = 0; j < cfg_size(cfg_statusbar, WidgetList[i].name); j++)
254 memcpy(wptr,
255 cfg_getnsec(cfg_statusbar, WidgetList[i].name, j),
256 sizeof(cfg_t));
257 wptr++;
260 qsort(widgets, numwidgets, sizeof(cfg_t), cmp_widget_cfg);
262 for(i = 0; i < numwidgets; i++)
264 wptr = widgets + i;
265 widget_new = name_func_lookup(cfg_name(wptr), WidgetList);
266 if(widget_new)
268 widget = widget_new(statusbar, wptr);
269 widget_list_append(&statusbar->widgets, widget);
270 widget->buttons = parse_mouse_bindings(wptr, "mouse", a_strcmp(cfg_name(wptr), "taglist"));
272 else
273 warn("Ignoring unknown widget: %s.\n", cfg_name(widgets + i));
277 static void
278 config_parse_screen(cfg_t *cfg, int screen)
280 char buf[2];
281 const char *tmp;
282 Layout *layout = NULL;
283 Tag *tag = NULL;
284 Statusbar *statusbar = NULL;
285 cfg_t *cfg_general, *cfg_colors, *cfg_screen, *cfg_tags,
286 *cfg_layouts, *cfg_padding, *cfgsectmp;
287 VirtScreen *virtscreen = &globalconf.screens[screen];
288 int i, phys_screen = get_phys_screen(screen);
290 snprintf(buf, sizeof(buf), "%d", screen);
291 cfg_screen = cfg_gettsec(cfg, "screen", buf);
292 if(!cfg_screen)
293 cfg_screen = cfg_getsec(cfg, "screen");
295 if(!cfg_screen)
297 warn("parsing configuration file failed, no screen section found\n");
298 cfg_parse_buf(cfg, AWESOME_DEFAULT_CONFIG);
299 cfg_screen = cfg_getsec(cfg, "screen");
302 /* get screen specific sections */
303 cfg_tags = cfg_getsec(cfg_screen, "tags");
304 cfg_colors = cfg_getsec(cfg_screen, "colors");
305 cfg_general = cfg_getsec(cfg_screen, "general");
306 cfg_layouts = cfg_getsec(cfg_screen, "layouts");
307 cfg_padding = cfg_getsec(cfg_screen, "padding");
310 /* General section */
311 virtscreen->borderpx = cfg_getint(cfg_general, "border");
312 virtscreen->snap = cfg_getint(cfg_general, "snap");
313 virtscreen->resize_hints = cfg_getbool(cfg_general, "resize_hints");
314 virtscreen->sloppy_focus = cfg_getbool(cfg_general, "sloppy_focus");
315 virtscreen->sloppy_focus_raise = cfg_getbool(cfg_general, "sloppy_focus_raise");
316 virtscreen->new_become_master = cfg_getbool(cfg_general, "new_become_master");
317 virtscreen->new_get_focus = cfg_getbool(cfg_general, "new_get_focus");
318 virtscreen->font = XftFontOpenName(globalconf.display,
319 phys_screen,
320 cfg_getstr(cfg_general, "font"));
321 if(!virtscreen->font)
322 eprint("awesome: cannot init font\n");
324 /* Colors */
325 virtscreen->colors_normal[ColBorder] = draw_color_new(globalconf.display, phys_screen,
326 cfg_getstr(cfg_colors, "normal_border"));
327 virtscreen->colors_normal[ColBG] = draw_color_new(globalconf.display, phys_screen,
328 cfg_getstr(cfg_colors, "normal_bg"));
329 virtscreen->colors_normal[ColFG] = draw_color_new(globalconf.display, phys_screen,
330 cfg_getstr(cfg_colors, "normal_fg"));
331 virtscreen->colors_selected[ColBorder] = draw_color_new(globalconf.display, phys_screen,
332 cfg_getstr(cfg_colors, "focus_border"));
333 virtscreen->colors_selected[ColBG] = draw_color_new(globalconf.display, phys_screen,
334 cfg_getstr(cfg_colors, "focus_bg"));
335 virtscreen->colors_selected[ColFG] = draw_color_new(globalconf.display, phys_screen,
336 cfg_getstr(cfg_colors, "focus_fg"));
337 virtscreen->colors_urgent[ColBG] = draw_color_new(globalconf.display, phys_screen,
338 cfg_getstr(cfg_colors, "urgent_bg"));
339 virtscreen->colors_urgent[ColFG] = draw_color_new(globalconf.display, phys_screen,
340 cfg_getstr(cfg_colors, "urgent_fg"));
342 /* Statusbar */
344 statusbar_list_init(&virtscreen->statusbar);
345 for(i = cfg_size(cfg_screen, "statusbar") - 1; i >= 0; i--)
347 statusbar = p_new(Statusbar, 1);
348 cfgsectmp = cfg_getnsec(cfg_screen, "statusbar", i);
349 statusbar->position = statusbar->dposition =
350 statusbar_get_position_from_str(cfg_getstr(cfgsectmp, "position"));
351 statusbar->height = cfg_getint(cfgsectmp, "height");
352 statusbar->width = cfg_getint(cfgsectmp, "width");
353 statusbar->name = a_strdup(cfg_title(cfgsectmp));
354 statusbar->screen = screen;
355 statusbar_preinit(statusbar);
356 create_widgets(cfgsectmp, statusbar);
357 statusbar_list_push(&virtscreen->statusbar, statusbar);
360 /* Layouts */
361 layout_list_init(&virtscreen->layouts);
362 if((i = cfg_size(cfg_layouts, "layout")))
363 for(--i; i >= 0; i--)
365 layout = p_new(Layout, 1);
366 cfgsectmp = cfg_getnsec(cfg_layouts, "layout", i);
367 layout->arrange = name_func_lookup(cfg_title(cfgsectmp), LayoutList);
368 if(!layout->arrange)
370 warn("unknown layout %s in configuration file\n", cfg_title(cfgsectmp));
371 layout->image = NULL;
372 continue;
374 layout->image = a_strdup(cfg_getstr(cfgsectmp, "image"));
376 layout_list_push(&virtscreen->layouts, layout);
378 else
380 warn("no default layout available\n");
381 layout->arrange = layout_tile;
384 /* Tags */
385 tag_list_init(&virtscreen->tags);
386 if((i = cfg_size(cfg_tags, "tag")))
387 for(--i; i >= 0; i--)
389 cfgsectmp = cfg_getnsec(cfg_tags, "tag", i);
391 tmp = cfg_getstr(cfgsectmp, "layout");
392 for(layout = virtscreen->layouts;
393 layout && layout->arrange != name_func_lookup(tmp, LayoutList);
394 layout = layout->next);
395 if(!layout)
396 layout = virtscreen->layouts;
398 tag = tag_new(cfg_title(cfgsectmp),
399 layout,
400 cfg_getfloat(cfgsectmp, "mwfact"),
401 cfg_getint(cfgsectmp, "nmaster"),
402 cfg_getint(cfgsectmp, "ncol"));
404 tag_push_to_screen(tag, screen);
406 else
408 warn("fatal: no tags found in configuration file\n");
409 tag->name = a_strdup("default");
410 tag->layout = virtscreen->layouts;
411 tag->nmaster = 1;
412 tag->ncol = 1;
413 tag->mwfact = 0.5;
416 ewmh_update_net_numbers_of_desktop(phys_screen);
417 ewmh_update_net_current_desktop(phys_screen);
418 ewmh_update_net_desktop_names(phys_screen);
420 /* select first tag by default */
421 virtscreen->tags[0].selected = True;
422 virtscreen->tags[0].was_selected = True;
424 /* padding */
425 virtscreen->padding.top = cfg_getint(cfg_padding, "top");
426 virtscreen->padding.bottom = cfg_getint(cfg_padding, "bottom");
427 virtscreen->padding.left = cfg_getint(cfg_padding, "left");
428 virtscreen->padding.right = cfg_getint(cfg_padding, "right");
431 static char *
432 config_file(void)
434 const char *homedir;
435 char * confpath;
436 ssize_t confpath_len;
438 homedir = getenv("HOME");
439 confpath_len = a_strlen(homedir) + a_strlen(AWESOME_CONFIG_FILE) + 2;
440 confpath = p_new(char, confpath_len);
441 a_strcpy(confpath, confpath_len, homedir);
442 a_strcat(confpath, confpath_len, "/");
443 a_strcat(confpath, confpath_len, AWESOME_CONFIG_FILE);
445 return confpath;
449 config_check(const char *confpatharg)
451 cfg_t *cfg;
452 int ret;
453 char *confpath;
455 cfg = cfg_init(awesome_opts, CFGF_NONE);
457 if(confpatharg)
458 confpath = a_strdup(confpatharg);
459 else
460 confpath = config_file();
462 switch((ret = cfg_parse(cfg, confpath)))
464 case CFG_FILE_ERROR:
465 perror("awesome: parsing configuration file failed");
466 break;
467 case CFG_PARSE_ERROR:
468 cfg_error(cfg, "awesome: parsing configuration file %s failed.\n", confpath);
469 break;
470 case CFG_SUCCESS:
471 printf("Configuration file OK.\n");
472 break;
475 p_delete(&confpath);
477 return ret;
480 /** Parse configuration file and initialize some stuff
481 * \param confpatharg Path to configuration file
483 void
484 config_parse(const char *confpatharg)
486 cfg_t *cfg, *cfg_rules, *cfg_keys, *cfg_mouse, *cfgsectmp;
487 int ret, screen, i;
488 char *confpath;
489 Rule *rule = NULL;
490 FILE *defconfig = NULL;
492 if(confpatharg)
493 confpath = a_strdup(confpatharg);
494 else
495 confpath = config_file();
497 globalconf.configpath = a_strdup(confpath);
499 cfg = cfg_init(awesome_opts, CFGF_NONE);
501 ret = cfg_parse(cfg, confpath);
503 switch(ret)
505 case CFG_FILE_ERROR:
506 perror("awesome: parsing configuration file failed");
507 if(!(defconfig = fopen(confpath, "w")))
508 perror("awesome: unable to create default configuration file");
509 break;
510 case CFG_PARSE_ERROR:
511 cfg_error(cfg, "awesome: parsing configuration file %s failed.\n", confpath);
512 break;
515 if(ret != CFG_SUCCESS)
517 warn("using default compile-time configuration\n");
518 cfg_free(cfg);
519 cfg = cfg_init(awesome_opts, CFGF_NONE);
520 cfg_parse_buf(cfg, AWESOME_DEFAULT_CONFIG);
523 /* get the right screen section */
524 for(screen = 0; screen < globalconf.nscreen; screen++)
525 config_parse_screen(cfg, screen);
527 /* get general sections */
528 cfg_rules = cfg_getsec(cfg, "rules");
529 cfg_keys = cfg_getsec(cfg, "keys");
530 cfg_mouse = cfg_getsec(cfg, "mouse");
532 /* Rules */
533 rule_list_init(&globalconf.rules);
534 for(i = cfg_size(cfg_rules, "rule") - 1; i >= 0; i--)
536 rule = p_new(Rule, 1);
537 cfgsectmp = cfg_getnsec(cfg_rules, "rule", i);
538 rule->prop_r = rules_compile_regex(cfg_getstr(cfgsectmp, "name"));
539 rule->tags_r = rules_compile_regex(cfg_getstr(cfgsectmp, "tags"));
540 rule->xprop = a_strdup(cfg_getstr(cfgsectmp, "xproperty_name"));
541 rule->xpropval_r = rules_compile_regex(cfg_getstr(cfgsectmp, "xproperty_value"));
542 rule->icon = a_strdup(cfg_getstr(cfgsectmp, "icon"));
543 rule->isfloating = rules_get_float_from_str(cfg_getstr(cfgsectmp, "float"));
544 rule->screen = cfg_getint(cfgsectmp, "screen");
545 rule->not_master = cfg_getbool(cfgsectmp, "not_master");
546 rule->opacity = cfg_getfloat(cfgsectmp, "opacity");
547 if(rule->screen >= globalconf.nscreen)
548 rule->screen = 0;
550 rule_list_push(&globalconf.rules, rule);
553 /* Mouse: root window click bindings */
554 globalconf.buttons.root = parse_mouse_bindings(cfg_mouse, "root", True);
556 /* Mouse: client windows click bindings */
557 globalconf.buttons.client = parse_mouse_bindings(cfg_mouse, "client", True);
559 /* Keys */
560 globalconf.numlockmask = get_numlockmask(globalconf.display);
562 globalconf.keys = section_keys(cfg_keys);
564 if(defconfig)
566 cfg_print(cfg, defconfig);
567 fclose(defconfig);
570 /* Free! Like a river! */
571 cfg_free(cfg);
572 p_delete(&confpath);
575 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80