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/max.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
[] =
75 {"tile", layout_tile
},
76 {"tileleft", layout_tileleft
},
78 {"floating", layout_floating
},
82 /** List of available UI bindable callbacks and functions */
83 static const NameFuncLink KeyfuncList
[] = {
85 {"spawn", uicb_spawn
},
87 {"killclient", uicb_killclient
},
88 {"moveresize", uicb_moveresize
},
89 {"settrans", uicb_settrans
},
90 {"setborder", uicb_setborder
},
93 {"togglefloating", uicb_togglefloating
},
94 {"toggleview", uicb_toggleview
},
95 {"toggletag", uicb_toggletag
},
97 {"view_tag_prev_selected", uicb_tag_prev_selected
},
98 {"view_tag_previous", uicb_tag_viewprev
},
99 {"view_tag_next", uicb_tag_viewnext
},
101 {"setlayout", uicb_setlayout
},
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
},
115 {"togglebar", uicb_togglebar
},
119 /** Lookup for a key mask from its name
120 * \param keyname Key name
121 * \return Key mask or 0 if not found
124 key_mask_lookup(const char *keyname
)
129 for(i
= 0; KeyModList
[i
].name
; i
++)
130 if(!a_strcmp(keyname
, KeyModList
[i
].name
))
131 return KeyModList
[i
].keysym
;
136 /** Lookup for a function pointer from its name
137 * in the given NameFuncLink list
138 * \param funcname Function name
139 * \param list Function and name link list
140 * \return function pointer
143 name_func_lookup(const char *funcname
, const NameFuncLink
* list
)
148 for(i
= 0; list
[i
].name
; i
++)
149 if(!a_strcmp(funcname
, list
[i
].name
))
155 /** Set default configuration
156 * \param awesomeconf awesome config ref
159 set_default_config(awesome_config
*awesomeconf
)
161 /** \todo most of this stuff aren't freed when we initialize
162 * the real configuration, we should add a clean conf function */
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;
170 awesomeconf
->nlayouts
= 2;
171 awesomeconf
->layouts
= p_new(Layout
, awesomeconf
->nlayouts
+ 1);
172 awesomeconf
->layouts
[0].symbol
= a_strdup("[]=");
173 awesomeconf
->layouts
[0].arrange
= layout_tile
;
174 awesomeconf
->layouts
[1].symbol
= a_strdup("<><");
175 awesomeconf
->layouts
[1].arrange
= layout_floating
;
176 awesomeconf
->layouts
[2].symbol
= NULL
;
177 awesomeconf
->layouts
[2].arrange
= NULL
;
179 awesomeconf
->ntags
= 3;
180 awesomeconf
->tags
= p_new(char *, awesomeconf
->ntags
);
181 awesomeconf
->selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
182 awesomeconf
->prev_selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
183 awesomeconf
->tag_layouts
= p_new(Layout
*, awesomeconf
->ntags
);
184 awesomeconf
->tags
[0] = a_strdup("this");
185 awesomeconf
->tags
[1] = a_strdup("is");
186 awesomeconf
->tags
[2] = a_strdup("awesome");
187 awesomeconf
->selected_tags
[0] = True
;
188 awesomeconf
->selected_tags
[1] = False
;
189 awesomeconf
->selected_tags
[2] = False
;
190 awesomeconf
->prev_selected_tags
[0] = False
;
191 awesomeconf
->prev_selected_tags
[1] = False
;
192 awesomeconf
->prev_selected_tags
[2] = False
;
193 awesomeconf
->tag_layouts
[0] = awesomeconf
->layouts
;
194 awesomeconf
->tag_layouts
[1] = awesomeconf
->layouts
;
195 awesomeconf
->tag_layouts
[2] = awesomeconf
->layouts
;
198 /** Parse configuration file and initialize some stuff
199 * \param disp Display ref
200 * \param scr Screen number
201 * \param drawcontext Draw context
204 parse_config(Display
* disp
, int scr
, DC
* drawcontext
, awesome_config
*awesomeconf
)
206 /* Main configuration object for parsing*/
207 config_t awesomelibconf
;
208 config_setting_t
*conftags
;
209 config_setting_t
*conflayouts
, *confsublayouts
;
210 config_setting_t
*confrules
, *confsubrules
;
211 config_setting_t
*confkeys
, *confsubkeys
, *confkeysmasks
, *confkeymaskelem
;
214 const char *tmp
, *homedir
;
218 set_default_config(awesomeconf
);
220 homedir
= getenv("HOME");
221 confpath
= p_new(char, strlen(homedir
) + strlen(AWESOME_CONFIG_FILE
) + 2);
222 strcpy(confpath
, homedir
);
223 strcat(confpath
, "/");
224 strcat(confpath
, AWESOME_CONFIG_FILE
);
226 config_init(&awesomelibconf
);
229 awesomeconf
->screen
= scr
;
231 if(config_read_file(&awesomelibconf
, confpath
) == CONFIG_FALSE
)
232 fprintf(stderr
, "awesome: error parsing configuration file at line %d: %s\n",
233 config_error_line(&awesomelibconf
), config_error_text(&awesomelibconf
));
237 tmp
= config_lookup_string(&awesomelibconf
, "awesome.font");
238 initfont(tmp
? tmp
: "-*-fixed-medium-r-normal-*-13-*-*-*-*-*-*-*", disp
, drawcontext
);
241 conflayouts
= config_lookup(&awesomelibconf
, "awesome.layouts");
244 fprintf(stderr
, "awesome: layouts not found in configuration file\n");
247 awesomeconf
->nlayouts
= config_setting_length(conflayouts
);
248 awesomeconf
->layouts
= p_new(Layout
, awesomeconf
->nlayouts
+ 1);
249 for(i
= 0; (confsublayouts
= config_setting_get_elem(conflayouts
, i
)); i
++)
251 awesomeconf
->layouts
[i
].arrange
=
252 name_func_lookup(config_setting_get_string_elem(confsublayouts
, 1), LayoutsList
);
253 if(!awesomeconf
->layouts
[i
].arrange
)
255 fprintf(stderr
, "awesome: unknown layout #%d in configuration file\n", i
);
256 awesomeconf
->layouts
[i
].symbol
= NULL
;
259 awesomeconf
->layouts
[i
].symbol
= a_strdup(config_setting_get_string_elem(confsublayouts
, 0));
261 awesomeconf
->layouts
[i
].symbol
= NULL
;
262 awesomeconf
->layouts
[i
].arrange
= NULL
;
265 if(!awesomeconf
->nlayouts
)
266 eprint("awesome: fatal: no default layout available\n");
268 for(i
= 0; i
< awesomeconf
->nlayouts
; i
++)
270 j
= textw(drawcontext
->font
.set
, drawcontext
->font
.xfont
, awesomeconf
->layouts
[i
].symbol
, drawcontext
->font
.height
);
271 if(j
> awesomeconf
->statusbar
.width
)
272 awesomeconf
->statusbar
.width
= j
;
275 awesomeconf
->current_layout
= awesomeconf
->layouts
;
278 conftags
= config_lookup(&awesomelibconf
, "awesome.tags");
281 fprintf(stderr
, "awesome: tags not found in configuration file\n");
284 awesomeconf
->ntags
= config_setting_length(conftags
);
285 awesomeconf
->tags
= p_new(char *, awesomeconf
->ntags
);
286 awesomeconf
->selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
287 awesomeconf
->prev_selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
288 /** \todo move this in tags or layouts */
289 awesomeconf
->tag_layouts
= p_new(Layout
*, awesomeconf
->ntags
);
291 for(i
= 0; (tmp
= config_setting_get_string_elem(conftags
, i
)); i
++)
293 awesomeconf
->tags
[i
] = a_strdup(tmp
);
294 awesomeconf
->selected_tags
[i
] = False
;
295 awesomeconf
->prev_selected_tags
[i
] = False
;
296 /** \todo add support for default tag/layout in configuration file */
297 awesomeconf
->tag_layouts
[i
] = awesomeconf
->layouts
;
301 if(!awesomeconf
->ntags
)
302 eprint("awesome: fatal: no tags found in configuration file\n");
304 /* select first tag by default */
305 awesomeconf
->selected_tags
[0] = True
;
306 awesomeconf
->prev_selected_tags
[0] = True
;
309 confrules
= config_lookup(&awesomelibconf
, "awesome.rules");
312 fprintf(stderr
, "awesome: no rules found in configuration file\n");
315 awesomeconf
->nrules
= config_setting_length(confrules
);
316 awesomeconf
->rules
= p_new(Rule
, awesomeconf
->nrules
);
317 for(i
= 0; (confsubrules
= config_setting_get_elem(confrules
, i
)); i
++)
319 awesomeconf
->rules
[i
].prop
= a_strdup(config_setting_get_string(config_setting_get_member(confsubrules
, "name")));
320 awesomeconf
->rules
[i
].tags
= a_strdup(config_setting_get_string(config_setting_get_member(confsubrules
, "tags")));
321 if(awesomeconf
->rules
[i
].tags
&& !strlen(awesomeconf
->rules
[i
].tags
))
322 awesomeconf
->rules
[i
].tags
= NULL
;
323 awesomeconf
->rules
[i
].isfloating
=
324 config_setting_get_bool(config_setting_get_member(confsubrules
, "float"));
329 tmp_key
= key_mask_lookup(config_lookup_string(&awesomelibconf
, "awesome.modkey"));
330 awesomeconf
->modkey
= tmp_key
? tmp_key
: Mod1Mask
;
332 /* find numlock mask */
333 awesomeconf
->numlockmask
= get_numlockmask(disp
);
336 confkeys
= config_lookup(&awesomelibconf
, "awesome.keys");
339 fprintf(stderr
, "awesome: no keys found in configuration file\n");
342 awesomeconf
->nkeys
= config_setting_length(confkeys
);
343 awesomeconf
->keys
= p_new(Key
, awesomeconf
->nkeys
);
345 for(i
= 0; (confsubkeys
= config_setting_get_elem(confkeys
, i
)); i
++)
347 confkeysmasks
= config_setting_get_elem(confsubkeys
, 0);
348 for(j
= 0; (confkeymaskelem
= config_setting_get_elem(confkeysmasks
, j
)); j
++)
349 awesomeconf
->keys
[i
].mod
|= key_mask_lookup(config_setting_get_string(confkeymaskelem
));
350 awesomeconf
->keys
[i
].keysym
= XStringToKeysym(config_setting_get_string_elem(confsubkeys
, 1));
351 awesomeconf
->keys
[i
].func
=
352 name_func_lookup(config_setting_get_string_elem(confsubkeys
, 2), KeyfuncList
);
353 awesomeconf
->keys
[i
].arg
= a_strdup(config_setting_get_string_elem(confsubkeys
, 3));
358 tmp
= config_lookup_string(&awesomelibconf
, "awesome.barpos");
360 if(tmp
&& !strncmp(tmp
, "off", 6))
361 awesomeconf
->statusbar_default_position
= BarOff
;
362 else if(tmp
&& !strncmp(tmp
, "bottom", 6))
363 awesomeconf
->statusbar_default_position
= BarBot
;
365 awesomeconf
->statusbar_default_position
= BarTop
;
367 awesomeconf
->statusbar
.position
= awesomeconf
->statusbar_default_position
;
370 awesomeconf
->borderpx
= config_lookup_int(&awesomelibconf
, "awesome.borderpx");
373 awesomeconf
->opacity_unfocused
= config_lookup_int(&awesomelibconf
, "awesome.opacity_unfocused");
374 if(awesomeconf
->opacity_unfocused
>= 100)
375 awesomeconf
->opacity_unfocused
= -1;
378 i
= config_lookup_int(&awesomelibconf
, "awesome.snap");
379 awesomeconf
->snap
= i
? i
: 8;
382 i
= config_lookup_int(&awesomelibconf
, "awesome.nmaster");
383 awesomeconf
->nmaster
= i
? i
: 1;
386 i
= config_lookup_int(&awesomelibconf
, "awesome.ncols");
387 awesomeconf
->ncols
= i
? i
: 1;
390 f
= config_lookup_float(&awesomelibconf
, "awesome.mwfact");
391 awesomeconf
->mwfact
= f
? f
: 0.6;
394 awesomeconf
->resize_hints
= config_lookup_float(&awesomelibconf
, "awesome.resize_hints");
397 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_border_color");
398 drawcontext
->norm
[ColBorder
] = initcolor(tmp
? tmp
: "#dddddd", disp
, scr
);
400 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_bg_color");
401 drawcontext
->norm
[ColBG
] = initcolor(tmp
? tmp
: "#000000", disp
, scr
);
403 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_fg_color");
404 drawcontext
->norm
[ColFG
] = initcolor(tmp
? tmp
: "#ffffff", disp
, scr
);
406 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_border_color");
407 drawcontext
->sel
[ColBorder
] = initcolor(tmp
? tmp
: "#008b8b", disp
, scr
);
409 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_bg_color");
410 drawcontext
->sel
[ColBG
] = initcolor(tmp
? tmp
: "#008b8b", disp
, scr
);
412 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_fg_color");
413 drawcontext
->sel
[ColFG
] = initcolor(tmp
? tmp
: "#ffffff", disp
, scr
);
415 config_destroy(&awesomelibconf
);
419 /** Initialize font from X side and store in draw context
420 * \param fontstr Font name
421 * \param disp Display ref
422 * \param drawcontext Draw context
425 initfont(const char *fontstr
, Display
* disp
, DC
* drawcontext
)
427 char *def
, **missing
;
431 if(drawcontext
->font
.set
)
432 XFreeFontSet(disp
, drawcontext
->font
.set
);
433 drawcontext
->font
.set
= XCreateFontSet(disp
, fontstr
, &missing
, &n
, &def
);
437 fprintf(stderr
, "awesome: missing fontset: %s\n", missing
[n
]);
438 XFreeStringList(missing
);
440 if(drawcontext
->font
.set
)
442 XFontSetExtents
*font_extents
;
443 XFontStruct
**xfonts
;
445 drawcontext
->font
.ascent
= drawcontext
->font
.descent
= 0;
446 font_extents
= XExtentsOfFontSet(drawcontext
->font
.set
);
447 n
= XFontsOfFontSet(drawcontext
->font
.set
, &xfonts
, &font_names
);
448 for(i
= 0, drawcontext
->font
.ascent
= 0, drawcontext
->font
.descent
= 0; i
< n
; i
++)
450 if(drawcontext
->font
.ascent
< (*xfonts
)->ascent
)
451 drawcontext
->font
.ascent
= (*xfonts
)->ascent
;
452 if(drawcontext
->font
.descent
< (*xfonts
)->descent
)
453 drawcontext
->font
.descent
= (*xfonts
)->descent
;
459 if(drawcontext
->font
.xfont
)
460 XFreeFont(disp
, drawcontext
->font
.xfont
);
461 drawcontext
->font
.xfont
= NULL
;
462 if(!(drawcontext
->font
.xfont
= XLoadQueryFont(disp
, fontstr
))
463 && !(drawcontext
->font
.xfont
= XLoadQueryFont(disp
, "fixed")))
464 die("awesome: error, cannot load font: '%s'\n", fontstr
);
465 drawcontext
->font
.ascent
= drawcontext
->font
.xfont
->ascent
;
466 drawcontext
->font
.descent
= drawcontext
->font
.xfont
->descent
;
468 drawcontext
->font
.height
= drawcontext
->font
.ascent
+ drawcontext
->font
.descent
;
472 get_numlockmask(Display
*disp
)
474 XModifierKeymap
*modmap
;
475 unsigned int mask
= 0;
478 modmap
= XGetModifierMapping(disp
);
479 for(i
= 0; i
< 8; i
++)
480 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
482 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
483 == XKeysymToKeycode(disp
, XK_Num_Lock
))
487 XFreeModifiermap(modmap
);
492 /** Initialize color from X side
493 * \param colorstr Color code
494 * \param disp Display ref
495 * \param scr Screen number
496 * \return XColor pixel
499 initcolor(const char *colstr
, Display
* disp
, int scr
)
501 Colormap cmap
= DefaultColormap(disp
, scr
);
503 if(!XAllocNamedColor(disp
, cmap
, colstr
, &color
, &color
))
504 die("awesome: error, cannot allocate color '%s'\n", colstr
);