2 * config.c - configuration management
4 * Copyright © 2007 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.
24 * \defgroup ui_callback
27 #include <libconfig.h>
28 #include <X11/keysym.h>
35 #include "statusbar.h"
36 #include "layouts/tile.h"
37 #include "layouts/spiral.h"
38 #include "layouts/floating.h"
41 static void initfont(const char *, Display
*, DC
*);
42 static unsigned long initcolor(const char *colstr
, Display
*, int);
43 static unsigned int get_numlockmask(Display
*);
45 /** Main configuration object for parsing*/
46 config_t awesomelibconf
;
48 /** Link a name to a function */
55 /** Link a name to a key symbol */
62 /** List of keyname and corresponding X11 mask codes */
63 static const KeyMod KeyModList
[] =
67 {"Control", ControlMask
},
76 /** List of available layouts and link between name and functions */
77 static const NameFuncLink LayoutsList
[] =
80 {"tileleft", tileleft
},
81 {"floating", floating
},
87 /** List of available UI bindable callbacks and functions */
88 static const NameFuncLink KeyfuncList
[] = {
90 {"spawn", uicb_spawn
},
92 {"killclient", uicb_killclient
},
93 {"moveresize", uicb_moveresize
},
94 {"settrans", uicb_settrans
},
97 {"togglefloating", uicb_togglefloating
},
98 {"toggleview", uicb_toggleview
},
99 {"toggletag", uicb_toggletag
},
101 {"viewprevtags", uicb_viewprevtags
},
103 {"setlayout", uicb_setlayout
},
104 {"focusnext", uicb_focusnext
},
105 {"focusprev", uicb_focusprev
},
106 {"togglemax", uicb_togglemax
},
107 {"toggleverticalmax", uicb_toggleverticalmax
},
108 {"togglehorizontalmax", uicb_togglehorizontalmax
},
111 {"setmwfact", uicb_setmwfact
},
112 {"setnmaster", uicb_setnmaster
},
113 {"setncols", uicb_setncols
},
117 {"togglebar", uicb_togglebar
},
121 /** Lookup for a key mask from its name
122 * \param keyname Key name
123 * \return Key mask or 0 if not found
126 key_mask_lookup(const char *keyname
)
131 for(i
= 0; KeyModList
[i
].name
; i
++)
132 if(!strcmp(keyname
, KeyModList
[i
].name
))
133 return KeyModList
[i
].keysym
;
138 /** Lookup for a function pointer from its name
139 * in the given NameFuncLink list
140 * \param funcname Function name
141 * \param list Function and name link list
142 * \return function pointer
145 name_func_lookup(const char *funcname
, const NameFuncLink
* list
)
150 for(i
= 0; list
[i
].name
; i
++)
151 if(!strcmp(funcname
, list
[i
].name
))
157 /** Set default configuration
158 * \param awesomeconf awesome config ref
161 set_default_config(awesome_config
*awesomeconf
)
163 strcpy(awesomeconf
->statustext
, "awesome-" VERSION
);
164 awesomeconf
->statusbar
.width
= 0;
165 awesomeconf
->statusbar
.height
= 0;
166 awesomeconf
->opacity_unfocused
= -1;
167 awesomeconf
->nkeys
= 0;
168 awesomeconf
->nrules
= 0;
171 /** Parse configuration file and initialize some stuff
172 * \param disp Display ref
173 * \param scr Screen number
174 * \param drawcontext Draw context
177 parse_config(Display
* disp
, int scr
, DC
* drawcontext
, awesome_config
*awesomeconf
)
179 config_setting_t
*conftags
;
180 config_setting_t
*conflayouts
, *confsublayouts
;
181 config_setting_t
*confrules
, *confsubrules
;
182 config_setting_t
*confkeys
, *confsubkeys
, *confkeysmasks
, *confkeymaskelem
;
185 const char *tmp
, *homedir
;
189 set_default_config(awesomeconf
);
191 homedir
= getenv("HOME");
192 confpath
= p_new(char, strlen(homedir
) + strlen(AWESOME_CONFIG_FILE
) + 2);
193 strcpy(confpath
, homedir
);
194 strcat(confpath
, "/");
195 strcat(confpath
, AWESOME_CONFIG_FILE
);
197 config_init(&awesomelibconf
);
199 if(config_read_file(&awesomelibconf
, confpath
) == CONFIG_FALSE
)
200 eprint("awesome: error parsing configuration file at line %d: %s\n",
201 config_error_line(&awesomelibconf
), config_error_text(&awesomelibconf
));
204 tmp
= config_lookup_string(&awesomelibconf
, "awesome.font");
205 initfont(tmp
? tmp
: "-*-fixed-medium-r-normal-*-13-*-*-*-*-*-*-*", disp
, drawcontext
);
208 conflayouts
= config_lookup(&awesomelibconf
, "awesome.layouts");
211 fprintf(stderr
, "layouts not found in configuration file\n");
214 awesomeconf
->nlayouts
= config_setting_length(conflayouts
);
215 awesomeconf
->layouts
= p_new(Layout
, awesomeconf
->nlayouts
+ 1);
216 for(i
= 0; (confsublayouts
= config_setting_get_elem(conflayouts
, i
)); i
++)
218 awesomeconf
->layouts
[i
].arrange
=
219 name_func_lookup(config_setting_get_string_elem(confsublayouts
, 1), LayoutsList
);
220 if(!awesomeconf
->layouts
[i
].arrange
)
222 fprintf(stderr
, "awesome: unknown layout #%d in configuration file\n", i
);
223 awesomeconf
->layouts
[i
].symbol
= NULL
;
226 awesomeconf
->layouts
[i
].symbol
= config_setting_get_string_elem(confsublayouts
, 0);
228 j
= textw(drawcontext
->font
.set
, drawcontext
->font
.xfont
, awesomeconf
->layouts
[i
].symbol
, drawcontext
->font
.height
);
229 if(j
> awesomeconf
->statusbar
.width
)
230 awesomeconf
->statusbar
.width
= j
;
232 awesomeconf
->layouts
[i
].symbol
= NULL
;
233 awesomeconf
->layouts
[i
].arrange
= NULL
;
237 if(!awesomeconf
->layouts
[0].arrange
)
238 eprint("awesome: fatal: no default layout available\n");
239 /** \todo put this in set_default_layout */
240 awesomeconf
->current_layout
= awesomeconf
->layouts
;
243 conftags
= config_lookup(&awesomelibconf
, "awesome.tags");
246 eprint("awesome: fatal: no tags found in configuration file\n");
247 awesomeconf
->ntags
= config_setting_length(conftags
);
248 awesomeconf
->tags
= p_new(const char *, awesomeconf
->ntags
);
249 awesomeconf
->selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
250 awesomeconf
->prev_selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
251 awesomeconf
->tag_layouts
= p_new(Layout
*, awesomeconf
->ntags
);
253 for(i
= 0; (tmp
= config_setting_get_string_elem(conftags
, i
)); i
++)
255 awesomeconf
->tags
[i
] = tmp
;
256 awesomeconf
->selected_tags
[i
] = False
;
257 awesomeconf
->prev_selected_tags
[i
] = False
;
258 /** \todo add support for default tag/layout in configuration file */
259 awesomeconf
->tag_layouts
[i
] = awesomeconf
->layouts
;
262 /* select first tag by default */
263 awesomeconf
->selected_tags
[0] = True
;
264 awesomeconf
->prev_selected_tags
[0] = True
;
267 confrules
= config_lookup(&awesomelibconf
, "awesome.rules");
270 fprintf(stderr
, "awesome: no rules found in configuration file\n");
273 awesomeconf
->nrules
= config_setting_length(confrules
);
274 awesomeconf
->rules
= p_new(Rule
, awesomeconf
->nrules
);
275 for(i
= 0; (confsubrules
= config_setting_get_elem(confrules
, i
)); i
++)
277 awesomeconf
->rules
[i
].prop
= config_setting_get_string(config_setting_get_member(confsubrules
, "name"));
278 awesomeconf
->rules
[i
].tags
= config_setting_get_string(config_setting_get_member(confsubrules
, "tags"));
279 if(awesomeconf
->rules
[i
].tags
&& !strlen(awesomeconf
->rules
[i
].tags
))
280 awesomeconf
->rules
[i
].tags
= NULL
;
281 awesomeconf
->rules
[i
].isfloating
=
282 config_setting_get_bool(config_setting_get_member(confsubrules
, "float"));
287 tmp_key
= key_mask_lookup(config_lookup_string(&awesomelibconf
, "awesome.modkey"));
288 awesomeconf
->modkey
= tmp_key
? tmp_key
: Mod1Mask
;
290 /* find numlock mask */
291 awesomeconf
->numlockmask
= get_numlockmask(disp
);
294 confkeys
= config_lookup(&awesomelibconf
, "awesome.keys");
297 fprintf(stderr
, "awesome: no keys found in configuration file\n");
300 awesomeconf
->nkeys
= config_setting_length(confkeys
);
301 awesomeconf
->keys
= p_new(Key
, awesomeconf
->nkeys
);
303 for(i
= 0; (confsubkeys
= config_setting_get_elem(confkeys
, i
)); i
++)
305 confkeysmasks
= config_setting_get_elem(confsubkeys
, 0);
306 for(j
= 0; (confkeymaskelem
= config_setting_get_elem(confkeysmasks
, j
)); j
++)
307 awesomeconf
->keys
[i
].mod
|= key_mask_lookup(config_setting_get_string(confkeymaskelem
));
308 awesomeconf
->keys
[i
].keysym
= XStringToKeysym(config_setting_get_string_elem(confsubkeys
, 1));
309 awesomeconf
->keys
[i
].func
=
310 name_func_lookup(config_setting_get_string_elem(confsubkeys
, 2), KeyfuncList
);
311 awesomeconf
->keys
[i
].arg
= config_setting_get_string_elem(confsubkeys
, 3);
316 tmp
= config_lookup_string(&awesomelibconf
, "awesome.barpos");
318 if(tmp
&& !strncmp(tmp
, "off", 6))
319 awesomeconf
->statusbar_default_position
= BarOff
;
320 else if(tmp
&& !strncmp(tmp
, "bottom", 6))
321 awesomeconf
->statusbar_default_position
= BarBot
;
323 awesomeconf
->statusbar_default_position
= BarTop
;
325 awesomeconf
->statusbar
.position
= awesomeconf
->statusbar_default_position
;
328 awesomeconf
->borderpx
= config_lookup_int(&awesomelibconf
, "awesome.borderpx");
331 awesomeconf
->opacity_unfocused
= config_lookup_int(&awesomelibconf
, "awesome.opacity_unfocused");
332 if(awesomeconf
->opacity_unfocused
>= 100)
333 awesomeconf
->opacity_unfocused
= -1;
336 i
= config_lookup_int(&awesomelibconf
, "awesome.snap");
337 awesomeconf
->snap
= i
? i
: 8;
340 i
= config_lookup_int(&awesomelibconf
, "awesome.nmaster");
341 awesomeconf
->nmaster
= i
? i
: 1;
344 i
= config_lookup_int(&awesomelibconf
, "awesome.ncols");
345 awesomeconf
->ncols
= i
? i
: 1;
348 f
= config_lookup_float(&awesomelibconf
, "awesome.mwfact");
349 awesomeconf
->mwfact
= f
? f
: 0.6;
352 awesomeconf
->resize_hints
= config_lookup_float(&awesomelibconf
, "awesome.resize_hints");
355 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_border_color");
356 drawcontext
->norm
[ColBorder
] = initcolor(tmp
? tmp
: "#dddddd", disp
, scr
);
358 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_bg_color");
359 drawcontext
->norm
[ColBG
] = initcolor(tmp
? tmp
: "#000000", disp
, scr
);
361 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_fg_color");
362 drawcontext
->norm
[ColFG
] = initcolor(tmp
? tmp
: "#ffffff", disp
, scr
);
364 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_border_color");
365 drawcontext
->sel
[ColBorder
] = initcolor(tmp
? tmp
: "#008b8b", disp
, scr
);
367 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_bg_color");
368 drawcontext
->sel
[ColBG
] = initcolor(tmp
? tmp
: "#008b8b", disp
, scr
);
370 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_fg_color");
371 drawcontext
->sel
[ColFG
] = initcolor(tmp
? tmp
: "#ffffff", disp
, scr
);
376 /** Initialize font from X side and store in draw context
377 * \param fontstr Font name
378 * \param disp Display ref
379 * \param drawcontext Draw context
382 initfont(const char *fontstr
, Display
* disp
, DC
* drawcontext
)
384 char *def
, **missing
;
388 if(drawcontext
->font
.set
)
389 XFreeFontSet(disp
, drawcontext
->font
.set
);
390 drawcontext
->font
.set
= XCreateFontSet(disp
, fontstr
, &missing
, &n
, &def
);
394 fprintf(stderr
, "awesome: missing fontset: %s\n", missing
[n
]);
395 XFreeStringList(missing
);
397 if(drawcontext
->font
.set
)
399 XFontSetExtents
*font_extents
;
400 XFontStruct
**xfonts
;
402 drawcontext
->font
.ascent
= drawcontext
->font
.descent
= 0;
403 font_extents
= XExtentsOfFontSet(drawcontext
->font
.set
);
404 n
= XFontsOfFontSet(drawcontext
->font
.set
, &xfonts
, &font_names
);
405 for(i
= 0, drawcontext
->font
.ascent
= 0, drawcontext
->font
.descent
= 0; i
< n
; i
++)
407 if(drawcontext
->font
.ascent
< (*xfonts
)->ascent
)
408 drawcontext
->font
.ascent
= (*xfonts
)->ascent
;
409 if(drawcontext
->font
.descent
< (*xfonts
)->descent
)
410 drawcontext
->font
.descent
= (*xfonts
)->descent
;
416 if(drawcontext
->font
.xfont
)
417 XFreeFont(disp
, drawcontext
->font
.xfont
);
418 drawcontext
->font
.xfont
= NULL
;
419 if(!(drawcontext
->font
.xfont
= XLoadQueryFont(disp
, fontstr
))
420 || !(drawcontext
->font
.xfont
= XLoadQueryFont(disp
, "fixed")))
421 eprint("error, cannot load font: '%s'\n", fontstr
);
422 drawcontext
->font
.ascent
= drawcontext
->font
.xfont
->ascent
;
423 drawcontext
->font
.descent
= drawcontext
->font
.xfont
->descent
;
425 drawcontext
->font
.height
= drawcontext
->font
.ascent
+ drawcontext
->font
.descent
;
429 get_numlockmask(Display
*disp
)
431 XModifierKeymap
*modmap
;
432 unsigned int mask
= 0;
435 modmap
= XGetModifierMapping(disp
);
436 for(i
= 0; i
< 8; i
++)
437 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
439 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
440 == XKeysymToKeycode(disp
, XK_Num_Lock
))
444 XFreeModifiermap(modmap
);
449 /** Initialize color from X side
450 * \param colorstr Color code
451 * \param disp Display ref
452 * \param scr Screen number
453 * \return XColor pixel
456 initcolor(const char *colstr
, Display
* disp
, int scr
)
458 Colormap cmap
= DefaultColormap(disp
, scr
);
460 if(!XAllocNamedColor(disp
, cmap
, colstr
, &color
, &color
))
461 eprint("error, cannot allocate color '%s'\n", colstr
);