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"
37 #include "layouts/tile.h"
38 #include "layouts/max.h"
39 #include "layouts/floating.h"
41 static XColor
initxcolor(Display
*, int, const char *);
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
},
88 {"killclient", uicb_killclient
},
89 {"moveresize", uicb_moveresize
},
90 {"settrans", uicb_settrans
},
91 {"setborder", uicb_setborder
},
92 {"swapnext", uicb_swapnext
},
93 {"swapprev", uicb_swapprev
},
96 {"togglefloating", uicb_togglefloating
},
97 {"toggleview", uicb_toggleview
},
98 {"toggletag", uicb_toggletag
},
100 {"view_tag_prev_selected", uicb_tag_prev_selected
},
101 {"view_tag_previous", uicb_tag_viewprev
},
102 {"view_tag_next", uicb_tag_viewnext
},
104 {"setlayout", uicb_setlayout
},
105 {"focusnext", uicb_focusnext
},
106 {"focusprev", uicb_focusprev
},
107 {"togglemax", uicb_togglemax
},
108 {"toggleverticalmax", uicb_toggleverticalmax
},
109 {"togglehorizontalmax", uicb_togglehorizontalmax
},
112 {"setmwfact", uicb_setmwfact
},
113 {"setnmaster", uicb_setnmaster
},
114 {"setncols", uicb_setncols
},
116 {"focusnextscreen", uicb_focusnextscreen
},
117 {"focusprevscreen", uicb_focusprevscreen
},
118 {"movetoscreen", uicb_movetoscreen
},
122 {"togglebar", uicb_togglebar
},
126 /** Lookup for a key mask from its name
127 * \param keyname Key name
128 * \return Key mask or 0 if not found
131 key_mask_lookup(const char *keyname
)
136 for(i
= 0; KeyModList
[i
].name
; i
++)
137 if(!a_strcmp(keyname
, KeyModList
[i
].name
))
138 return KeyModList
[i
].keysym
;
143 /** Lookup for a function pointer from its name
144 * in the given NameFuncLink list
145 * \param funcname Function name
146 * \param list Function and name link list
147 * \return function pointer
150 name_func_lookup(const char *funcname
, const NameFuncLink
* list
)
155 for(i
= 0; list
[i
].name
; i
++)
156 if(!a_strcmp(funcname
, list
[i
].name
))
162 /** Parse configuration file and initialize some stuff
163 * \param disp Display ref
164 * \param scr Screen number
165 * \param drawcontext Draw context
168 parse_config(Display
* disp
, int scr
, DC
* drawcontext
, const char *confpatharg
, awesome_config
*awesomeconf
)
170 /* Main configuration object for parsing*/
171 config_t awesomelibconf
;
172 config_setting_t
*conftags
;
173 config_setting_t
*conflayouts
, *confsublayouts
;
174 config_setting_t
*confrules
, *confsubrules
;
175 config_setting_t
*confkeys
, *confsubkeys
, *confkeysmasks
, *confkeymaskelem
;
178 const char *tmp
, *homedir
;
181 ssize_t confpath_len
;
185 confpath
= a_strdup(confpatharg
);
188 homedir
= getenv("HOME");
189 confpath_len
= a_strlen(homedir
) + a_strlen(AWESOME_CONFIG_FILE
) + 2;
190 confpath
= p_new(char, confpath_len
);
191 a_strcpy(confpath
, confpath_len
, homedir
);
192 a_strcat(confpath
, confpath_len
, "/");
193 a_strcat(confpath
, confpath_len
, AWESOME_CONFIG_FILE
);
196 config_init(&awesomelibconf
);
198 a_strcpy(awesomeconf
->statustext
, sizeof(awesomeconf
->statustext
), "awesome-" VERSION
);
201 awesomeconf
->display
= disp
;
204 awesomeconf
->screen
= scr
;
205 awesomeconf
->phys_screen
= get_phys_screen(disp
, scr
);
207 if(config_read_file(&awesomelibconf
, confpath
) == CONFIG_FALSE
)
208 fprintf(stderr
, "awesome: error parsing configuration file at line %d: %s\n",
209 config_error_line(&awesomelibconf
), config_error_text(&awesomelibconf
));
213 tmp
= config_lookup_string(&awesomelibconf
, "awesome.font");
214 drawcontext
->font
= XftFontOpenName(disp
, awesomeconf
->phys_screen
, tmp
? tmp
: "sans-12");
216 if(!drawcontext
->font
)
217 eprint("awesome: cannot init font\n");
220 conflayouts
= config_lookup(&awesomelibconf
, "awesome.layouts");
224 fprintf(stderr
, "awesome: layouts not found in configuration file, setting default\n");
225 awesomeconf
->nlayouts
= 2;
226 awesomeconf
->layouts
= p_new(Layout
, awesomeconf
->nlayouts
+ 1);
227 awesomeconf
->layouts
[0].symbol
= a_strdup("[]=");
228 awesomeconf
->layouts
[0].arrange
= layout_tile
;
229 awesomeconf
->layouts
[1].symbol
= a_strdup("<><");
230 awesomeconf
->layouts
[1].arrange
= layout_floating
;
231 awesomeconf
->layouts
[2].symbol
= NULL
;
232 awesomeconf
->layouts
[2].arrange
= NULL
;
236 awesomeconf
->nlayouts
= config_setting_length(conflayouts
);
237 awesomeconf
->layouts
= p_new(Layout
, awesomeconf
->nlayouts
+ 1);
238 for(i
= 0; (confsublayouts
= config_setting_get_elem(conflayouts
, i
)); i
++)
240 awesomeconf
->layouts
[i
].arrange
=
241 name_func_lookup(config_setting_get_string_elem(confsublayouts
, 1), LayoutsList
);
242 if(!awesomeconf
->layouts
[i
].arrange
)
244 fprintf(stderr
, "awesome: unknown layout #%d in configuration file\n", i
);
245 awesomeconf
->layouts
[i
].symbol
= NULL
;
248 awesomeconf
->layouts
[i
].symbol
= a_strdup(config_setting_get_string_elem(confsublayouts
, 0));
250 awesomeconf
->layouts
[i
].symbol
= NULL
;
251 awesomeconf
->layouts
[i
].arrange
= NULL
;
254 if(!awesomeconf
->nlayouts
)
255 eprint("awesome: fatal: no default layout available\n");
257 for(i
= 0; i
< awesomeconf
->nlayouts
; i
++)
259 j
= drawcontext
->font
->height
+
260 textwidth(disp
, drawcontext
->font
,
261 awesomeconf
->layouts
[i
].symbol
, a_strlen(awesomeconf
->layouts
[i
].symbol
));
262 if(j
> awesomeconf
->statusbar
.width
)
263 awesomeconf
->statusbar
.width
= j
;
266 awesomeconf
->current_layout
= awesomeconf
->layouts
;
269 conftags
= config_lookup(&awesomelibconf
, "awesome.tags");
273 fprintf(stderr
, "awesome: tags not found in configuration file, setting default\n");
274 awesomeconf
->ntags
= 3;
275 awesomeconf
->tags
= p_new(Tag
, awesomeconf
->ntags
);
276 awesomeconf
->tags
[0].name
= a_strdup("this");
277 awesomeconf
->tags
[1].name
= a_strdup("is");
278 awesomeconf
->tags
[2].name
= a_strdup("awesome");
279 awesomeconf
->tags
[0].selected
= True
;
280 awesomeconf
->tags
[1].selected
= False
;
281 awesomeconf
->tags
[2].selected
= False
;
282 awesomeconf
->tags
[0].was_selected
= False
;
283 awesomeconf
->tags
[1].was_selected
= False
;
284 awesomeconf
->tags
[2].was_selected
= False
;
285 awesomeconf
->tags
[0].layout
= awesomeconf
->layouts
;
286 awesomeconf
->tags
[1].layout
= awesomeconf
->layouts
;
287 awesomeconf
->tags
[2].layout
= awesomeconf
->layouts
;
291 awesomeconf
->ntags
= config_setting_length(conftags
);
292 awesomeconf
->tags
= p_new(Tag
, awesomeconf
->ntags
);
294 for(i
= 0; (tmp
= config_setting_get_string_elem(conftags
, i
)); i
++)
296 awesomeconf
->tags
[i
].name
= a_strdup(tmp
);
297 awesomeconf
->tags
[i
].selected
= False
;
298 awesomeconf
->tags
[i
].was_selected
= False
;
299 /** \todo add support for default tag/layout in configuration file */
300 awesomeconf
->tags
[i
].layout
= awesomeconf
->layouts
;
304 if(!awesomeconf
->ntags
)
305 eprint("awesome: fatal: no tags found in configuration file\n");
307 /* select first tag by default */
308 awesomeconf
->tags
[0].selected
= True
;
309 awesomeconf
->tags
[0].was_selected
= True
;
312 confrules
= config_lookup(&awesomelibconf
, "awesome.rules");
316 awesomeconf
->nrules
= 0;
317 fprintf(stderr
, "awesome: no rules found in configuration file\n");
321 awesomeconf
->nrules
= config_setting_length(confrules
);
322 awesomeconf
->rules
= p_new(Rule
, awesomeconf
->nrules
);
323 for(i
= 0; (confsubrules
= config_setting_get_elem(confrules
, i
)); i
++)
325 awesomeconf
->rules
[i
].prop
= a_strdup(config_setting_get_string(config_setting_get_member(confsubrules
, "name")));
326 awesomeconf
->rules
[i
].tags
= a_strdup(config_setting_get_string(config_setting_get_member(confsubrules
, "tags")));
327 if(awesomeconf
->rules
[i
].tags
&& !a_strlen(awesomeconf
->rules
[i
].tags
))
328 awesomeconf
->rules
[i
].tags
= NULL
;
329 awesomeconf
->rules
[i
].isfloating
=
330 config_setting_get_bool(config_setting_get_member(confsubrules
, "float"));
335 tmp_key
= key_mask_lookup(config_lookup_string(&awesomelibconf
, "awesome.modkey"));
336 awesomeconf
->modkey
= tmp_key
? tmp_key
: Mod1Mask
;
338 /* find numlock mask */
339 awesomeconf
->numlockmask
= get_numlockmask(disp
);
342 confkeys
= config_lookup(&awesomelibconf
, "awesome.keys");
346 awesomeconf
->nkeys
= 0;
347 fprintf(stderr
, "awesome: no keys found in configuration file\n");
351 awesomeconf
->nkeys
= config_setting_length(confkeys
);
352 awesomeconf
->keys
= p_new(Key
, awesomeconf
->nkeys
);
354 for(i
= 0; (confsubkeys
= config_setting_get_elem(confkeys
, i
)); i
++)
356 confkeysmasks
= config_setting_get_elem(confsubkeys
, 0);
357 for(j
= 0; (confkeymaskelem
= config_setting_get_elem(confkeysmasks
, j
)); j
++)
358 awesomeconf
->keys
[i
].mod
|= key_mask_lookup(config_setting_get_string(confkeymaskelem
));
359 awesomeconf
->keys
[i
].keysym
= XStringToKeysym(config_setting_get_string_elem(confsubkeys
, 1));
360 awesomeconf
->keys
[i
].func
=
361 name_func_lookup(config_setting_get_string_elem(confsubkeys
, 2), KeyfuncList
);
362 awesomeconf
->keys
[i
].arg
= a_strdup(config_setting_get_string_elem(confsubkeys
, 3));
367 tmp
= config_lookup_string(&awesomelibconf
, "awesome.barpos");
369 if(tmp
&& !a_strncmp(tmp
, "off", 6))
370 awesomeconf
->statusbar_default_position
= BarOff
;
371 else if(tmp
&& !a_strncmp(tmp
, "bottom", 6))
372 awesomeconf
->statusbar_default_position
= BarBot
;
374 awesomeconf
->statusbar_default_position
= BarTop
;
376 awesomeconf
->statusbar
.position
= awesomeconf
->statusbar_default_position
;
379 awesomeconf
->borderpx
= config_lookup_int(&awesomelibconf
, "awesome.borderpx");
382 awesomeconf
->opacity_unfocused
= config_lookup_int(&awesomelibconf
, "awesome.opacity_unfocused");
383 if(awesomeconf
->opacity_unfocused
>= 100 || awesomeconf
->opacity_unfocused
== 0)
384 awesomeconf
->opacity_unfocused
= -1;
387 i
= config_lookup_int(&awesomelibconf
, "awesome.snap");
388 awesomeconf
->snap
= i
? i
: 8;
391 i
= config_lookup_int(&awesomelibconf
, "awesome.nmaster");
392 awesomeconf
->nmaster
= i
? i
: 1;
395 i
= config_lookup_int(&awesomelibconf
, "awesome.ncols");
396 awesomeconf
->ncols
= i
? i
: 1;
399 f
= config_lookup_float(&awesomelibconf
, "awesome.mwfact");
400 awesomeconf
->mwfact
= f
? f
: 0.6;
403 awesomeconf
->resize_hints
= config_lookup_float(&awesomelibconf
, "awesome.resize_hints");
406 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_border_color");
407 colorbuf
= initxcolor(disp
, awesomeconf
->phys_screen
, tmp
? tmp
: "#dddddd");
408 drawcontext
->norm
[ColBorder
] = colorbuf
.pixel
;
410 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_bg_color");
411 colorbuf
= initxcolor(disp
, awesomeconf
->phys_screen
, tmp
? tmp
: "#000000");
412 drawcontext
->norm
[ColBG
] = colorbuf
.pixel
;
414 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_fg_color");
415 colorbuf
= initxcolor(disp
, awesomeconf
->phys_screen
, tmp
? tmp
: "#ffffff");
416 drawcontext
->norm
[ColFG
] = colorbuf
.pixel
;
417 drawcontext
->text_normal
= colorbuf
;
419 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_border_color");
420 colorbuf
= initxcolor(disp
, awesomeconf
->phys_screen
, tmp
? tmp
: "#008b8b");
421 drawcontext
->sel
[ColBorder
] = colorbuf
.pixel
;
423 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_bg_color");
424 colorbuf
= initxcolor(disp
, awesomeconf
->phys_screen
, tmp
? tmp
: "#008b8b");
425 drawcontext
->sel
[ColBG
] = colorbuf
.pixel
;
427 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_fg_color");
428 colorbuf
= initxcolor(disp
, awesomeconf
->phys_screen
, tmp
? tmp
: "#ffffff");
429 drawcontext
->sel
[ColFG
] = colorbuf
.pixel
;
430 drawcontext
->text_selected
= colorbuf
;
432 config_destroy(&awesomelibconf
);
437 get_numlockmask(Display
*disp
)
439 XModifierKeymap
*modmap
;
440 unsigned int mask
= 0;
443 modmap
= XGetModifierMapping(disp
);
444 for(i
= 0; i
< 8; i
++)
445 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
447 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
448 == XKeysymToKeycode(disp
, XK_Num_Lock
))
452 XFreeModifiermap(modmap
);
457 /** Initialize color from X side
458 * \param colorstr Color code
459 * \param disp Display ref
460 * \param scr Screen number
461 * \return XColor pixel
464 initxcolor(Display
*disp
, int scr
, const char *colstr
)
467 if(!XAllocNamedColor(disp
, DefaultColormap(disp
, scr
), colstr
, &color
, &color
))
468 die("awesome: error, cannot allocate color '%s'\n", colstr
);