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"
40 static void initfont(const char *, Display
*, DC
*);
41 static unsigned long initcolor(const char *, Display
*, int);
42 static unsigned int get_numlockmask(Display
*);
44 /** Link a name to a function */
51 /** Link a name to a key symbol */
58 /** List of keyname and corresponding X11 mask codes */
59 static const KeyMod KeyModList
[] =
63 {"Control", ControlMask
},
72 /** List of available layouts and link between name and functions */
73 static const NameFuncLink LayoutsList
[] =
76 {"tileleft", tileleft
},
77 {"floating", floating
},
83 /** List of available UI bindable callbacks and functions */
84 static const NameFuncLink KeyfuncList
[] = {
86 {"spawn", uicb_spawn
},
88 {"killclient", uicb_killclient
},
89 {"moveresize", uicb_moveresize
},
90 {"settrans", uicb_settrans
},
93 {"togglefloating", uicb_togglefloating
},
94 {"toggleview", uicb_toggleview
},
95 {"toggletag", uicb_toggletag
},
97 {"viewprevtags", uicb_viewprevtags
},
99 {"setlayout", uicb_setlayout
},
100 {"focusnext", uicb_focusnext
},
101 {"focusprev", uicb_focusprev
},
102 {"togglemax", uicb_togglemax
},
103 {"toggleverticalmax", uicb_toggleverticalmax
},
104 {"togglehorizontalmax", uicb_togglehorizontalmax
},
107 {"setmwfact", uicb_setmwfact
},
108 {"setnmaster", uicb_setnmaster
},
109 {"setncols", uicb_setncols
},
113 {"togglebar", uicb_togglebar
},
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 /* Main configuration object for parsing*/
176 config_t awesomelibconf
;
177 config_setting_t
*conftags
;
178 config_setting_t
*conflayouts
, *confsublayouts
;
179 config_setting_t
*confrules
, *confsubrules
;
180 config_setting_t
*confkeys
, *confsubkeys
, *confkeysmasks
, *confkeymaskelem
;
183 const char *tmp
, *homedir
;
187 set_default_config(awesomeconf
);
189 homedir
= getenv("HOME");
190 confpath
= p_new(char, strlen(homedir
) + strlen(AWESOME_CONFIG_FILE
) + 2);
191 strcpy(confpath
, homedir
);
192 strcat(confpath
, "/");
193 strcat(confpath
, AWESOME_CONFIG_FILE
);
195 config_init(&awesomelibconf
);
198 awesomeconf
->screen
= scr
;
200 if(config_read_file(&awesomelibconf
, confpath
) == CONFIG_FALSE
)
201 eprint("awesome: error parsing configuration file at line %d: %s\n",
202 config_error_line(&awesomelibconf
), config_error_text(&awesomelibconf
));
205 tmp
= config_lookup_string(&awesomelibconf
, "awesome.font");
206 initfont(tmp
? tmp
: "-*-fixed-medium-r-normal-*-13-*-*-*-*-*-*-*", disp
, drawcontext
);
209 conflayouts
= config_lookup(&awesomelibconf
, "awesome.layouts");
212 fprintf(stderr
, "layouts not found in configuration file\n");
215 awesomeconf
->nlayouts
= config_setting_length(conflayouts
);
216 awesomeconf
->layouts
= p_new(Layout
, awesomeconf
->nlayouts
+ 1);
217 for(i
= 0; (confsublayouts
= config_setting_get_elem(conflayouts
, i
)); i
++)
219 awesomeconf
->layouts
[i
].arrange
=
220 name_func_lookup(config_setting_get_string_elem(confsublayouts
, 1), LayoutsList
);
221 if(!awesomeconf
->layouts
[i
].arrange
)
223 fprintf(stderr
, "awesome: unknown layout #%d in configuration file\n", i
);
224 awesomeconf
->layouts
[i
].symbol
= NULL
;
227 awesomeconf
->layouts
[i
].symbol
= a_strdup(config_setting_get_string_elem(confsublayouts
, 0));
229 j
= textw(drawcontext
->font
.set
, drawcontext
->font
.xfont
, awesomeconf
->layouts
[i
].symbol
, drawcontext
->font
.height
);
230 if(j
> awesomeconf
->statusbar
.width
)
231 awesomeconf
->statusbar
.width
= j
;
233 awesomeconf
->layouts
[i
].symbol
= NULL
;
234 awesomeconf
->layouts
[i
].arrange
= NULL
;
238 if(!awesomeconf
->layouts
[0].arrange
)
239 eprint("awesome: fatal: no default layout available\n");
240 /** \todo put this in set_default_layout */
241 awesomeconf
->current_layout
= awesomeconf
->layouts
;
244 conftags
= config_lookup(&awesomelibconf
, "awesome.tags");
247 eprint("awesome: fatal: no tags found in configuration file\n");
248 awesomeconf
->ntags
= config_setting_length(conftags
);
249 awesomeconf
->tags
= p_new(char *, awesomeconf
->ntags
);
250 awesomeconf
->selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
251 awesomeconf
->prev_selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
252 awesomeconf
->tag_layouts
= p_new(Layout
*, awesomeconf
->ntags
);
254 for(i
= 0; (tmp
= config_setting_get_string_elem(conftags
, i
)); i
++)
256 awesomeconf
->tags
[i
] = a_strdup(tmp
);
257 awesomeconf
->selected_tags
[i
] = False
;
258 awesomeconf
->prev_selected_tags
[i
] = False
;
259 /** \todo add support for default tag/layout in configuration file */
260 awesomeconf
->tag_layouts
[i
] = awesomeconf
->layouts
;
263 /* select first tag by default */
264 awesomeconf
->selected_tags
[0] = True
;
265 awesomeconf
->prev_selected_tags
[0] = True
;
268 confrules
= config_lookup(&awesomelibconf
, "awesome.rules");
271 fprintf(stderr
, "awesome: no rules found in configuration file\n");
274 awesomeconf
->nrules
= config_setting_length(confrules
);
275 awesomeconf
->rules
= p_new(Rule
, awesomeconf
->nrules
);
276 for(i
= 0; (confsubrules
= config_setting_get_elem(confrules
, i
)); i
++)
278 awesomeconf
->rules
[i
].prop
= a_strdup(config_setting_get_string(config_setting_get_member(confsubrules
, "name")));
279 awesomeconf
->rules
[i
].tags
= a_strdup(config_setting_get_string(config_setting_get_member(confsubrules
, "tags")));
280 if(awesomeconf
->rules
[i
].tags
&& !strlen(awesomeconf
->rules
[i
].tags
))
281 awesomeconf
->rules
[i
].tags
= NULL
;
282 awesomeconf
->rules
[i
].isfloating
=
283 config_setting_get_bool(config_setting_get_member(confsubrules
, "float"));
288 tmp_key
= key_mask_lookup(config_lookup_string(&awesomelibconf
, "awesome.modkey"));
289 awesomeconf
->modkey
= tmp_key
? tmp_key
: Mod1Mask
;
291 /* find numlock mask */
292 awesomeconf
->numlockmask
= get_numlockmask(disp
);
295 confkeys
= config_lookup(&awesomelibconf
, "awesome.keys");
298 fprintf(stderr
, "awesome: no keys found in configuration file\n");
301 awesomeconf
->nkeys
= config_setting_length(confkeys
);
302 awesomeconf
->keys
= p_new(Key
, awesomeconf
->nkeys
);
304 for(i
= 0; (confsubkeys
= config_setting_get_elem(confkeys
, i
)); i
++)
306 confkeysmasks
= config_setting_get_elem(confsubkeys
, 0);
307 for(j
= 0; (confkeymaskelem
= config_setting_get_elem(confkeysmasks
, j
)); j
++)
308 awesomeconf
->keys
[i
].mod
|= key_mask_lookup(config_setting_get_string(confkeymaskelem
));
309 awesomeconf
->keys
[i
].keysym
= XStringToKeysym(config_setting_get_string_elem(confsubkeys
, 1));
310 awesomeconf
->keys
[i
].func
=
311 name_func_lookup(config_setting_get_string_elem(confsubkeys
, 2), KeyfuncList
);
312 awesomeconf
->keys
[i
].arg
= a_strdup(config_setting_get_string_elem(confsubkeys
, 3));
317 tmp
= config_lookup_string(&awesomelibconf
, "awesome.barpos");
319 if(tmp
&& !strncmp(tmp
, "off", 6))
320 awesomeconf
->statusbar_default_position
= BarOff
;
321 else if(tmp
&& !strncmp(tmp
, "bottom", 6))
322 awesomeconf
->statusbar_default_position
= BarBot
;
324 awesomeconf
->statusbar_default_position
= BarTop
;
326 awesomeconf
->statusbar
.position
= awesomeconf
->statusbar_default_position
;
329 awesomeconf
->borderpx
= config_lookup_int(&awesomelibconf
, "awesome.borderpx");
332 awesomeconf
->opacity_unfocused
= config_lookup_int(&awesomelibconf
, "awesome.opacity_unfocused");
333 if(awesomeconf
->opacity_unfocused
>= 100)
334 awesomeconf
->opacity_unfocused
= -1;
337 i
= config_lookup_int(&awesomelibconf
, "awesome.snap");
338 awesomeconf
->snap
= i
? i
: 8;
341 i
= config_lookup_int(&awesomelibconf
, "awesome.nmaster");
342 awesomeconf
->nmaster
= i
? i
: 1;
345 i
= config_lookup_int(&awesomelibconf
, "awesome.ncols");
346 awesomeconf
->ncols
= i
? i
: 1;
349 f
= config_lookup_float(&awesomelibconf
, "awesome.mwfact");
350 awesomeconf
->mwfact
= f
? f
: 0.6;
353 awesomeconf
->resize_hints
= config_lookup_float(&awesomelibconf
, "awesome.resize_hints");
356 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_border_color");
357 drawcontext
->norm
[ColBorder
] = initcolor(tmp
? tmp
: "#dddddd", disp
, scr
);
359 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_bg_color");
360 drawcontext
->norm
[ColBG
] = initcolor(tmp
? tmp
: "#000000", disp
, scr
);
362 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_fg_color");
363 drawcontext
->norm
[ColFG
] = initcolor(tmp
? tmp
: "#ffffff", disp
, scr
);
365 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_border_color");
366 drawcontext
->sel
[ColBorder
] = initcolor(tmp
? tmp
: "#008b8b", disp
, scr
);
368 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_bg_color");
369 drawcontext
->sel
[ColBG
] = initcolor(tmp
? tmp
: "#008b8b", disp
, scr
);
371 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_fg_color");
372 drawcontext
->sel
[ColFG
] = initcolor(tmp
? tmp
: "#ffffff", disp
, scr
);
374 config_destroy(&awesomelibconf
);
378 /** Initialize font from X side and store in draw context
379 * \param fontstr Font name
380 * \param disp Display ref
381 * \param drawcontext Draw context
384 initfont(const char *fontstr
, Display
* disp
, DC
* drawcontext
)
386 char *def
, **missing
;
390 if(drawcontext
->font
.set
)
391 XFreeFontSet(disp
, drawcontext
->font
.set
);
392 drawcontext
->font
.set
= XCreateFontSet(disp
, fontstr
, &missing
, &n
, &def
);
396 fprintf(stderr
, "awesome: missing fontset: %s\n", missing
[n
]);
397 XFreeStringList(missing
);
399 if(drawcontext
->font
.set
)
401 XFontSetExtents
*font_extents
;
402 XFontStruct
**xfonts
;
404 drawcontext
->font
.ascent
= drawcontext
->font
.descent
= 0;
405 font_extents
= XExtentsOfFontSet(drawcontext
->font
.set
);
406 n
= XFontsOfFontSet(drawcontext
->font
.set
, &xfonts
, &font_names
);
407 for(i
= 0, drawcontext
->font
.ascent
= 0, drawcontext
->font
.descent
= 0; i
< n
; i
++)
409 if(drawcontext
->font
.ascent
< (*xfonts
)->ascent
)
410 drawcontext
->font
.ascent
= (*xfonts
)->ascent
;
411 if(drawcontext
->font
.descent
< (*xfonts
)->descent
)
412 drawcontext
->font
.descent
= (*xfonts
)->descent
;
418 if(drawcontext
->font
.xfont
)
419 XFreeFont(disp
, drawcontext
->font
.xfont
);
420 drawcontext
->font
.xfont
= NULL
;
421 if(!(drawcontext
->font
.xfont
= XLoadQueryFont(disp
, fontstr
))
422 || !(drawcontext
->font
.xfont
= XLoadQueryFont(disp
, "fixed")))
423 eprint("error, cannot load font: '%s'\n", fontstr
);
424 drawcontext
->font
.ascent
= drawcontext
->font
.xfont
->ascent
;
425 drawcontext
->font
.descent
= drawcontext
->font
.xfont
->descent
;
427 drawcontext
->font
.height
= drawcontext
->font
.ascent
+ drawcontext
->font
.descent
;
431 get_numlockmask(Display
*disp
)
433 XModifierKeymap
*modmap
;
434 unsigned int mask
= 0;
437 modmap
= XGetModifierMapping(disp
);
438 for(i
= 0; i
< 8; i
++)
439 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
441 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
442 == XKeysymToKeycode(disp
, XK_Num_Lock
))
446 XFreeModifiermap(modmap
);
451 /** Initialize color from X side
452 * \param colorstr Color code
453 * \param disp Display ref
454 * \param scr Screen number
455 * \return XColor pixel
458 initcolor(const char *colstr
, Display
* disp
, int scr
)
460 Colormap cmap
= DefaultColormap(disp
, scr
);
462 if(!XAllocNamedColor(disp
, cmap
, colstr
, &color
, &color
))
463 eprint("error, cannot allocate color '%s'\n", colstr
);