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>
25 #include "statusbar.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 */
44 extern AwesomeConf globalconf
;
45 extern cfg_opt_t awesome_opts
[];
47 /** Link a name to a key symbol */
54 /** Link a name to a mouse button symbol */
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
71 key_mask_lookup(const char *keyname
)
73 /** List of keyname and corresponding X11 mask codes */
74 static const KeyMod KeyModList
[] =
78 {"Control", ControlMask
},
89 for(i
= 0; KeyModList
[i
].name
; i
++)
90 if(!a_strcmp(keyname
, KeyModList
[i
].name
))
91 return KeyModList
[i
].keycode
;
96 /** Lookup for a mouse button from its name
97 * \param button Mouse button name
98 * \return Mouse button or 0 if not found
101 mouse_button_lookup(const char *button
)
103 /** List of button name and corresponding X11 mask codes */
104 static const MouseButton MouseButtonList
[] =
118 for(i
= 0; MouseButtonList
[i
].name
; i
++)
119 if(!a_strcmp(button
, MouseButtonList
[i
].name
))
120 return MouseButtonList
[i
].button
;
126 parse_mouse_bindings(cfg_t
* cfg
, const char *secname
, Bool handle_arg
)
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
);
143 warn("unknown command %s\n", cfg_getstr(cfgsectmp
, "command"));
145 b
->arg
= a_strdup(cfg_getstr(cfgsectmp
, "arg"));
149 button_list_push(&head
, b
);
157 set_key_info(Key
*key
, cfg_t
*cfg
)
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
);
165 warn("unknown command %s\n", cfg_getstr(cfg
, "command"));
169 key_to_keycode(char *str
)
174 if(a_strncmp(str
, "#", 1))
175 return XKeysymToKeycode(globalconf
.display
, XStringToKeysym(str
));
178 memcpy(&kc
, &ikc
, sizeof(KeyCode
));
183 section_keys(cfg_t
*cfg_keys
)
185 Key
*key
= NULL
, *head
= NULL
;
189 for(i
= cfg_size(cfg_keys
, "key") - 1; i
>= 0; i
--)
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");
209 for(j
= 0; j
< numkeys
; j
++)
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
);
224 cmp_widget_cfg(const void *a
, const void *b
)
226 if (((cfg_t
*)a
)->line
< ((cfg_t
*)b
)->line
)
229 if (((cfg_t
*)a
)->line
> ((cfg_t
*)b
)->line
)
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
);
249 for(i
= 0; WidgetList
[i
].name
; i
++)
250 for (j
= 0; j
< cfg_size(cfg_statusbar
, WidgetList
[i
].name
); j
++)
253 cfg_getnsec(cfg_statusbar
, WidgetList
[i
].name
, j
),
258 qsort(widgets
, numwidgets
, sizeof(cfg_t
), cmp_widget_cfg
);
260 for(i
= 0; i
< numwidgets
; i
++)
263 widget_new
= name_func_lookup(cfg_name(wptr
), WidgetList
);
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"));
271 warn("Ignoring unknown widget: %s.\n", cfg_name(widgets
+ i
));
276 config_parse_screen(cfg_t
*cfg
, int screen
)
280 FloatingPlacement flpl
;
281 Layout
*layout
= NULL
;
283 Statusbar
*statusbar
= NULL
;
284 cfg_t
*cfg_general
, *cfg_colors
, *cfg_screen
, *cfg_tags
,
285 *cfg_layouts
, *cfg_padding
, *cfgsectmp
;
286 VirtScreen
*virtscreen
= &globalconf
.screens
[screen
];
287 int i
, phys_screen
= get_phys_screen(screen
);
289 snprintf(buf
, sizeof(buf
), "%d", screen
);
290 cfg_screen
= cfg_gettsec(cfg
, "screen", buf
);
292 cfg_screen
= cfg_getsec(cfg
, "screen");
296 warn("parsing configuration file failed, no screen section found\n");
297 cfg_parse_buf(cfg
, AWESOME_DEFAULT_CONFIG
);
298 cfg_screen
= cfg_getsec(cfg
, "screen");
301 /* get screen specific sections */
302 cfg_tags
= cfg_getsec(cfg_screen
, "tags");
303 cfg_colors
= cfg_getsec(cfg_screen
, "colors");
304 cfg_general
= cfg_getsec(cfg_screen
, "general");
305 cfg_layouts
= cfg_getsec(cfg_screen
, "layouts");
306 cfg_padding
= cfg_getsec(cfg_screen
, "padding");
309 /* General section */
310 virtscreen
->borderpx
= cfg_getint(cfg_general
, "border");
311 virtscreen
->snap
= cfg_getint(cfg_general
, "snap");
312 virtscreen
->resize_hints
= cfg_getbool(cfg_general
, "resize_hints");
313 virtscreen
->sloppy_focus
= cfg_getbool(cfg_general
, "sloppy_focus");
314 virtscreen
->sloppy_focus_raise
= cfg_getbool(cfg_general
, "sloppy_focus_raise");
315 virtscreen
->new_become_master
= cfg_getbool(cfg_general
, "new_become_master");
316 virtscreen
->new_get_focus
= cfg_getbool(cfg_general
, "new_get_focus");
317 virtscreen
->opacity_unfocused
= cfg_getint(cfg_general
, "opacity_unfocused");
318 virtscreen
->font
= XftFontOpenName(globalconf
.display
,
320 cfg_getstr(cfg_general
, "font"));
321 virtscreen
->shadow_offset
= cfg_getint(cfg_general
, "text_shadow_offset");
322 virtscreen
->floating_placement
=
323 name_func_lookup(cfg_getstr(cfg_general
, "floating_placement"),
324 FloatingPlacementList
);
326 virtscreen
->mwfact_lower_limit
= cfg_getfloat(cfg_general
, "mwfact_lower_limit");
327 virtscreen
->mwfact_upper_limit
= cfg_getfloat(cfg_general
, "mwfact_upper_limit");
329 if(virtscreen
->mwfact_lower_limit
< 0 || virtscreen
->mwfact_lower_limit
> 1)
331 warn("incorrect value %f for mwfact_lower_limit, must be between 0 and 1\n",
332 virtscreen
->mwfact_lower_limit
);
333 virtscreen
->mwfact_lower_limit
= 0.1;
336 if(virtscreen
->mwfact_upper_limit
< 0 || virtscreen
->mwfact_upper_limit
> 1)
338 warn("incorrect value %f for mwfact_upper_limit, must be between 0 and 1\n",
339 virtscreen
->mwfact_lower_limit
);
340 virtscreen
->mwfact_upper_limit
= 0.9;
343 if(virtscreen
->mwfact_upper_limit
< virtscreen
->mwfact_lower_limit
)
345 warn("mwfact_upper_limit must be greater than mwfact_lower_limit\n");
346 virtscreen
->mwfact_upper_limit
= 0.9;
347 virtscreen
->mwfact_lower_limit
= 0.1;
350 if(!virtscreen
->floating_placement
)
352 warn("unknown floating placement: %s\n", cfg_getstr(cfg_general
, "floating_placement"));
353 virtscreen
->floating_placement
= FloatingPlacementList
[0].func
;
356 if(!virtscreen
->font
)
357 eprint("awesome: cannot init font\n");
360 draw_color_new(globalconf
.display
, phys_screen
,
361 cfg_getstr(cfg_colors
, "normal_border"),
362 &virtscreen
->colors_normal
[ColBorder
]);
363 draw_color_new(globalconf
.display
, phys_screen
,
364 cfg_getstr(cfg_colors
, "normal_bg"),
365 &virtscreen
->colors_normal
[ColBG
]);
366 draw_color_new(globalconf
.display
, phys_screen
,
367 cfg_getstr(cfg_colors
, "normal_fg"),
368 &virtscreen
->colors_normal
[ColFG
]);
369 draw_color_new(globalconf
.display
, phys_screen
,
370 cfg_getstr(cfg_colors
, "focus_border"),
371 &virtscreen
->colors_selected
[ColBorder
]);
372 draw_color_new(globalconf
.display
, phys_screen
,
373 cfg_getstr(cfg_colors
, "focus_bg"),
374 &virtscreen
->colors_selected
[ColBG
]);
375 draw_color_new(globalconf
.display
, phys_screen
,
376 cfg_getstr(cfg_colors
, "focus_fg"),
377 &virtscreen
->colors_selected
[ColFG
]);
378 draw_color_new(globalconf
.display
, phys_screen
,
379 cfg_getstr(cfg_colors
, "urgent_bg"),
380 &virtscreen
->colors_urgent
[ColBG
]);
381 draw_color_new(globalconf
.display
, phys_screen
,
382 cfg_getstr(cfg_colors
, "urgent_fg"),
383 &virtscreen
->colors_urgent
[ColFG
]);
387 statusbar_list_init(&virtscreen
->statusbar
);
388 for(i
= cfg_size(cfg_screen
, "statusbar") - 1; i
>= 0; i
--)
390 statusbar
= p_new(Statusbar
, 1);
391 cfgsectmp
= cfg_getnsec(cfg_screen
, "statusbar", i
);
392 statusbar
->position
= statusbar
->dposition
=
393 statusbar_get_position_from_str(cfg_getstr(cfgsectmp
, "position"));
394 statusbar
->height
= cfg_getint(cfgsectmp
, "height");
395 statusbar
->width
= cfg_getint(cfgsectmp
, "width");
396 statusbar
->name
= a_strdup(cfg_title(cfgsectmp
));
397 statusbar
->screen
= screen
;
398 statusbar_preinit(statusbar
);
399 create_widgets(cfgsectmp
, statusbar
);
400 statusbar_list_push(&virtscreen
->statusbar
, statusbar
);
404 layout_list_init(&virtscreen
->layouts
);
405 if((i
= cfg_size(cfg_layouts
, "layout")))
406 for(--i
; i
>= 0; i
--)
408 layout
= p_new(Layout
, 1);
409 cfgsectmp
= cfg_getnsec(cfg_layouts
, "layout", i
);
410 layout
->arrange
= name_func_lookup(cfg_title(cfgsectmp
), LayoutList
);
413 warn("unknown layout %s in configuration file\n", cfg_title(cfgsectmp
));
414 layout
->image
= NULL
;
417 layout
->image
= a_strdup(cfg_getstr(cfgsectmp
, "image"));
419 layout_list_push(&virtscreen
->layouts
, layout
);
423 warn("no default layout available\n");
424 layout
= p_new(Layout
, 1);
425 layout
->arrange
= layout_tile
;
426 layout_list_push(&virtscreen
->layouts
, layout
);
430 tag_list_init(&virtscreen
->tags
);
431 if((i
= cfg_size(cfg_tags
, "tag")))
432 for(--i
; i
>= 0; i
--)
434 cfgsectmp
= cfg_getnsec(cfg_tags
, "tag", i
);
436 tmp
= cfg_getstr(cfgsectmp
, "layout");
437 for(layout
= virtscreen
->layouts
;
438 layout
&& layout
->arrange
!= name_func_lookup(tmp
, LayoutList
);
439 layout
= layout
->next
);
441 layout
= virtscreen
->layouts
;
443 tag
= tag_new(cfg_title(cfgsectmp
),
445 cfg_getfloat(cfgsectmp
, "mwfact"),
446 cfg_getint(cfgsectmp
, "nmaster"),
447 cfg_getint(cfgsectmp
, "ncol"));
449 tag_push_to_screen(tag
, screen
);
453 warn("fatal: no tags found in configuration file\n");
454 tag
= tag_new("default", virtscreen
->layouts
, 0.5, 1, 1);
455 tag_push_to_screen(tag
, screen
);
458 ewmh_update_net_numbers_of_desktop(phys_screen
);
459 ewmh_update_net_current_desktop(phys_screen
);
460 ewmh_update_net_desktop_names(phys_screen
);
462 /* select first tag by default */
463 virtscreen
->tags
[0].selected
= True
;
464 virtscreen
->tags
[0].was_selected
= True
;
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
477 config_parse(const char *confpatharg
)
479 cfg_t
*cfg
, *cfg_rules
, *cfg_keys
, *cfg_mouse
, *cfgsectmp
;
483 FILE *defconfig
= NULL
;
486 confpath
= a_strdup(confpatharg
);
488 confpath
= config_file();
490 globalconf
.configpath
= a_strdup(confpath
);
492 cfg
= cfg_init(awesome_opts
, CFGF_NONE
);
494 ret
= cfg_parse(cfg
, confpath
);
499 perror("awesome: parsing configuration file failed");
500 if(!(defconfig
= fopen(confpath
, "w")))
501 perror("awesome: unable to create default configuration file");
503 case CFG_PARSE_ERROR
:
504 cfg_error(cfg
, "awesome: parsing configuration file %s failed.\n", confpath
);
508 if(ret
!= CFG_SUCCESS
)
510 warn("using default compile-time configuration\n");
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");
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 if(rule
->screen
>= globalconf
.screens_info
->nscreen
)
543 rule_list_push(&globalconf
.rules
, rule
);
546 /* Mouse: root window click bindings */
547 globalconf
.buttons
.root
= parse_mouse_bindings(cfg_mouse
, "root", True
);
549 /* Mouse: client windows click bindings */
550 globalconf
.buttons
.client
= parse_mouse_bindings(cfg_mouse
, "client", True
);
553 globalconf
.numlockmask
= get_numlockmask(globalconf
.display
);
555 globalconf
.keys
= section_keys(cfg_keys
);
559 fwrite(AWESOME_DEFAULT_CONFIG
, a_strlen(AWESOME_DEFAULT_CONFIG
), 1, defconfig
);
563 /* Free! Like a river! */
568 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80