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>
33 #include "layouts/tile.h"
34 #include "layouts/spiral.h"
35 #include "layouts/floating.h"
38 static void initfont(const char *, Display
*, DC
*);
39 static unsigned long initcolor(const char *colstr
, Display
*, int);
40 static unsigned int get_numlockmask(Display
*);
42 /** Main configuration object for parsing*/
43 config_t awesomelibconf
;
45 /** Link a name to a function */
52 /** Link a name to a key symbol */
59 /** List of keyname and corresponding X11 mask codes */
60 static const KeyMod KeyModList
[] =
64 {"Control", ControlMask
},
73 /** List of available layouts and link between name and functions */
74 static const NameFuncLink LayoutsList
[] =
77 {"tileleft", tileleft
},
78 {"floating", floating
},
84 /** List of available UI bindable callbacks and functions */
85 static const NameFuncLink KeyfuncList
[] = {
87 {"spawn", uicb_spawn
},
89 {"killclient", uicb_killclient
},
90 {"moveresize", uicb_moveresize
},
91 {"settrans", uicb_settrans
},
94 {"togglefloating", uicb_togglefloating
},
95 {"toggleview", uicb_toggleview
},
96 {"toggletag", uicb_toggletag
},
98 {"viewprevtags", uicb_viewprevtags
},
100 {"setlayout", uicb_setlayout
},
101 {"togglebar", uicb_togglebar
},
102 {"focusnext", uicb_focusnext
},
103 {"focusprev", uicb_focusprev
},
104 {"togglemax", uicb_togglemax
},
105 {"toggleverticalmax", uicb_toggleverticalmax
},
106 {"togglehorizontalmax", uicb_togglehorizontalmax
},
109 {"setmwfact", uicb_setmwfact
},
110 {"setnmaster", uicb_setnmaster
},
111 {"setncols", uicb_setncols
},
117 /** Lookup for a key mask from its name
118 * \param keyname Key name
119 * \return Key mask or 0 if not found
122 key_mask_lookup(const char *keyname
)
127 for(i
= 0; KeyModList
[i
].name
; i
++)
128 if(!strcmp(keyname
, KeyModList
[i
].name
))
129 return KeyModList
[i
].keysym
;
134 /** Lookup for a function pointer from its name
135 * in the given NameFuncLink list
136 * \param funcname Function name
137 * \param list Function and name link list
138 * \return function pointer
141 name_func_lookup(const char *funcname
, const NameFuncLink
* list
)
146 for(i
= 0; list
[i
].name
; i
++)
147 if(!strcmp(funcname
, list
[i
].name
))
153 /** Set default configuration
154 * \param awesomeconf awesome config ref
157 set_default_config(awesome_config
*awesomeconf
)
159 strcpy(awesomeconf
->statustext
, "awesome-" VERSION
);
160 awesomeconf
->statusbar
.width
= 0;
161 awesomeconf
->statusbar
.height
= 0;
162 awesomeconf
->opacity_unfocused
= -1;
163 awesomeconf
->nkeys
= 0;
164 awesomeconf
->nrules
= 0;
167 /** Parse configuration file and initialize some stuff
168 * \param disp Display ref
169 * \param scr Screen number
170 * \param drawcontext Draw context
173 parse_config(Display
* disp
, int scr
, DC
* drawcontext
, awesome_config
*awesomeconf
)
175 config_setting_t
*conftags
;
176 config_setting_t
*conflayouts
, *confsublayouts
;
177 config_setting_t
*confrules
, *confsubrules
;
178 config_setting_t
*confkeys
, *confsubkeys
, *confkeysmasks
, *confkeymaskelem
;
181 const char *tmp
, *homedir
;
185 set_default_config(awesomeconf
);
187 homedir
= getenv("HOME");
188 confpath
= p_new(char, strlen(homedir
) + strlen(AWESOME_CONFIG_FILE
) + 2);
189 strcpy(confpath
, homedir
);
190 strcat(confpath
, "/");
191 strcat(confpath
, AWESOME_CONFIG_FILE
);
193 config_init(&awesomelibconf
);
195 if(config_read_file(&awesomelibconf
, confpath
) == CONFIG_FALSE
)
196 eprint("awesome: error parsing configuration file at line %d: %s\n",
197 config_error_line(&awesomelibconf
), config_error_text(&awesomelibconf
));
200 tmp
= config_lookup_string(&awesomelibconf
, "awesome.font");
201 initfont(tmp
? tmp
: "-*-fixed-medium-r-normal-*-13-*-*-*-*-*-*-*", disp
, drawcontext
);
204 conflayouts
= config_lookup(&awesomelibconf
, "awesome.layouts");
207 fprintf(stderr
, "layouts not found in configuration file\n");
210 awesomeconf
->nlayouts
= config_setting_length(conflayouts
);
211 awesomeconf
->layouts
= p_new(Layout
, awesomeconf
->nlayouts
+ 1);
212 for(i
= 0; (confsublayouts
= config_setting_get_elem(conflayouts
, i
)); i
++)
214 awesomeconf
->layouts
[i
].arrange
=
215 name_func_lookup(config_setting_get_string_elem(confsublayouts
, 1), LayoutsList
);
216 if(!awesomeconf
->layouts
[i
].arrange
)
218 fprintf(stderr
, "awesome: unknown layout #%d in configuration file\n", i
);
219 awesomeconf
->layouts
[i
].symbol
= NULL
;
222 awesomeconf
->layouts
[i
].symbol
= config_setting_get_string_elem(confsublayouts
, 0);
224 j
= textw(drawcontext
->font
.set
, drawcontext
->font
.xfont
, awesomeconf
->layouts
[i
].symbol
, drawcontext
->font
.height
);
225 if(j
> awesomeconf
->statusbar
.width
)
226 awesomeconf
->statusbar
.width
= j
;
228 awesomeconf
->layouts
[i
].symbol
= NULL
;
229 awesomeconf
->layouts
[i
].arrange
= NULL
;
233 if(!awesomeconf
->layouts
[0].arrange
)
234 eprint("awesome: fatal: no default layout available\n");
235 /** \todo put this in set_default_layout */
236 awesomeconf
->current_layout
= awesomeconf
->layouts
;
239 conftags
= config_lookup(&awesomelibconf
, "awesome.tags");
242 eprint("awesome: fatal: no tags found in configuration file\n");
243 awesomeconf
->ntags
= config_setting_length(conftags
);
244 awesomeconf
->tags
= p_new(const char *, awesomeconf
->ntags
);
245 awesomeconf
->selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
246 awesomeconf
->prev_selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
247 awesomeconf
->tag_layouts
= p_new(Layout
*, awesomeconf
->ntags
);
249 for(i
= 0; (tmp
= config_setting_get_string_elem(conftags
, i
)); i
++)
251 awesomeconf
->tags
[i
] = tmp
;
252 awesomeconf
->selected_tags
[i
] = False
;
253 awesomeconf
->prev_selected_tags
[i
] = False
;
254 /** \todo add support for default tag/layout in configuration file */
255 awesomeconf
->tag_layouts
[i
] = awesomeconf
->layouts
;
258 /* select first tag by default */
259 awesomeconf
->selected_tags
[0] = True
;
260 awesomeconf
->prev_selected_tags
[0] = True
;
263 confrules
= config_lookup(&awesomelibconf
, "awesome.rules");
266 fprintf(stderr
, "awesome: no rules found in configuration file\n");
269 awesomeconf
->nrules
= config_setting_length(confrules
);
270 awesomeconf
->rules
= p_new(Rule
, awesomeconf
->nrules
);
271 for(i
= 0; (confsubrules
= config_setting_get_elem(confrules
, i
)); i
++)
273 awesomeconf
->rules
[i
].prop
= config_setting_get_string(config_setting_get_member(confsubrules
, "name"));
274 awesomeconf
->rules
[i
].tags
= config_setting_get_string(config_setting_get_member(confsubrules
, "tags"));
275 if(awesomeconf
->rules
[i
].tags
&& !strlen(awesomeconf
->rules
[i
].tags
))
276 awesomeconf
->rules
[i
].tags
= NULL
;
277 awesomeconf
->rules
[i
].isfloating
=
278 config_setting_get_bool(config_setting_get_member(confsubrules
, "float"));
283 tmp_key
= key_mask_lookup(config_lookup_string(&awesomelibconf
, "awesome.modkey"));
284 awesomeconf
->modkey
= tmp_key
? tmp_key
: Mod1Mask
;
286 /* find numlock mask */
287 awesomeconf
->numlockmask
= get_numlockmask(disp
);
290 confkeys
= config_lookup(&awesomelibconf
, "awesome.keys");
293 fprintf(stderr
, "awesome: no keys found in configuration file\n");
296 awesomeconf
->nkeys
= config_setting_length(confkeys
);
297 awesomeconf
->keys
= p_new(Key
, awesomeconf
->nkeys
);
299 for(i
= 0; (confsubkeys
= config_setting_get_elem(confkeys
, i
)); i
++)
301 confkeysmasks
= config_setting_get_elem(confsubkeys
, 0);
302 for(j
= 0; (confkeymaskelem
= config_setting_get_elem(confkeysmasks
, j
)); j
++)
303 awesomeconf
->keys
[i
].mod
|= key_mask_lookup(config_setting_get_string(confkeymaskelem
));
304 awesomeconf
->keys
[i
].keysym
= XStringToKeysym(config_setting_get_string_elem(confsubkeys
, 1));
305 awesomeconf
->keys
[i
].func
=
306 name_func_lookup(config_setting_get_string_elem(confsubkeys
, 2), KeyfuncList
);
307 awesomeconf
->keys
[i
].arg
= config_setting_get_string_elem(confsubkeys
, 3);
312 tmp
= config_lookup_string(&awesomelibconf
, "awesome.barpos");
314 if(tmp
&& !strncmp(tmp
, "BarOff", 6))
315 awesomeconf
->statusbar_default_position
= BarOff
;
316 else if(tmp
&& !strncmp(tmp
, "BarBot", 6))
317 awesomeconf
->statusbar_default_position
= BarBot
;
319 awesomeconf
->statusbar_default_position
= BarTop
;
321 awesomeconf
->statusbar
.position
= awesomeconf
->statusbar_default_position
;
324 awesomeconf
->borderpx
= config_lookup_int(&awesomelibconf
, "awesome.borderpx");
327 awesomeconf
->opacity_unfocused
= config_lookup_int(&awesomelibconf
, "awesome.opacity_unfocused");
328 if(awesomeconf
->opacity_unfocused
>= 100)
329 awesomeconf
->opacity_unfocused
= -1;
332 i
= config_lookup_int(&awesomelibconf
, "awesome.snap");
333 awesomeconf
->snap
= i
? i
: 8;
336 i
= config_lookup_int(&awesomelibconf
, "awesome.nmaster");
337 awesomeconf
->nmaster
= i
? i
: 1;
340 i
= config_lookup_int(&awesomelibconf
, "awesome.ncols");
341 awesomeconf
->ncols
= i
? i
: 1;
344 f
= config_lookup_float(&awesomelibconf
, "awesome.mwfact");
345 awesomeconf
->mwfact
= f
? f
: 0.6;
348 awesomeconf
->resize_hints
= config_lookup_float(&awesomelibconf
, "awesome.resize_hints");
351 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_border_color");
352 drawcontext
->norm
[ColBorder
] = initcolor(tmp
? tmp
: "#dddddd", disp
, scr
);
354 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_bg_color");
355 drawcontext
->norm
[ColBG
] = initcolor(tmp
? tmp
: "#000000", disp
, scr
);
357 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_fg_color");
358 drawcontext
->norm
[ColFG
] = initcolor(tmp
? tmp
: "#ffffff", disp
, scr
);
360 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_border_color");
361 drawcontext
->sel
[ColBorder
] = initcolor(tmp
? tmp
: "#008b8b", disp
, scr
);
363 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_bg_color");
364 drawcontext
->sel
[ColBG
] = initcolor(tmp
? tmp
: "#008b8b", disp
, scr
);
366 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_fg_color");
367 drawcontext
->sel
[ColFG
] = initcolor(tmp
? tmp
: "#ffffff", disp
, scr
);
372 /** Initialize font from X side and store in draw context
373 * \param fontstr Font name
374 * \param disp Display ref
375 * \param drawcontext Draw context
378 initfont(const char *fontstr
, Display
* disp
, DC
* drawcontext
)
380 char *def
, **missing
;
384 if(drawcontext
->font
.set
)
385 XFreeFontSet(disp
, drawcontext
->font
.set
);
386 drawcontext
->font
.set
= XCreateFontSet(disp
, fontstr
, &missing
, &n
, &def
);
390 fprintf(stderr
, "awesome: missing fontset: %s\n", missing
[n
]);
391 XFreeStringList(missing
);
393 if(drawcontext
->font
.set
)
395 XFontSetExtents
*font_extents
;
396 XFontStruct
**xfonts
;
398 drawcontext
->font
.ascent
= drawcontext
->font
.descent
= 0;
399 font_extents
= XExtentsOfFontSet(drawcontext
->font
.set
);
400 n
= XFontsOfFontSet(drawcontext
->font
.set
, &xfonts
, &font_names
);
401 for(i
= 0, drawcontext
->font
.ascent
= 0, drawcontext
->font
.descent
= 0; i
< n
; i
++)
403 if(drawcontext
->font
.ascent
< (*xfonts
)->ascent
)
404 drawcontext
->font
.ascent
= (*xfonts
)->ascent
;
405 if(drawcontext
->font
.descent
< (*xfonts
)->descent
)
406 drawcontext
->font
.descent
= (*xfonts
)->descent
;
412 if(drawcontext
->font
.xfont
)
413 XFreeFont(disp
, drawcontext
->font
.xfont
);
414 drawcontext
->font
.xfont
= NULL
;
415 if(!(drawcontext
->font
.xfont
= XLoadQueryFont(disp
, fontstr
)))
416 eprint("error, cannot load font: '%s'\n", fontstr
);
417 drawcontext
->font
.ascent
= drawcontext
->font
.xfont
->ascent
;
418 drawcontext
->font
.descent
= drawcontext
->font
.xfont
->descent
;
420 drawcontext
->font
.height
= drawcontext
->font
.ascent
+ drawcontext
->font
.descent
;
424 get_numlockmask(Display
*disp
)
426 XModifierKeymap
*modmap
;
427 unsigned int mask
= 0;
430 modmap
= XGetModifierMapping(disp
);
431 for(i
= 0; i
< 8; i
++)
432 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
434 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
435 == XKeysymToKeycode(disp
, XK_Num_Lock
))
439 XFreeModifiermap(modmap
);
444 /** Initialize color from X side
445 * \param colorstr Color code
446 * \param disp Display ref
447 * \param scr Screen number
448 * \return XColor pixel
451 initcolor(const char *colstr
, Display
* disp
, int scr
)
453 Colormap cmap
= DefaultColormap(disp
, scr
);
455 if(!XAllocNamedColor(disp
, cmap
, colstr
, &color
, &color
))
456 eprint("error, cannot allocate color '%s'\n", colstr
);