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 void initfont(const char *, Display
*, DC
*);
42 static unsigned long initcolor(const char *, Display
*, int);
43 static unsigned int get_numlockmask(Display
*);
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
[] =
76 {"tile", layout_tile
},
77 {"tileleft", layout_tileleft
},
79 {"floating", layout_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
},
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
},
121 {"togglebar", uicb_togglebar
},
125 /** Lookup for a key mask from its name
126 * \param keyname Key name
127 * \return Key mask or 0 if not found
130 key_mask_lookup(const char *keyname
)
135 for(i
= 0; KeyModList
[i
].name
; i
++)
136 if(!a_strcmp(keyname
, KeyModList
[i
].name
))
137 return KeyModList
[i
].keysym
;
142 /** Lookup for a function pointer from its name
143 * in the given NameFuncLink list
144 * \param funcname Function name
145 * \param list Function and name link list
146 * \return function pointer
149 name_func_lookup(const char *funcname
, const NameFuncLink
* list
)
154 for(i
= 0; list
[i
].name
; i
++)
155 if(!a_strcmp(funcname
, list
[i
].name
))
161 /** Set default configuration
162 * \param awesomeconf awesome config ref
165 set_default_config(awesome_config
*awesomeconf
)
167 /** \todo most of this stuff aren't freed when we initialize
168 * the real configuration, we should add a clean conf function */
169 a_strcpy(awesomeconf
->statustext
, sizeof(awesomeconf
->statustext
), "awesome-" VERSION
);
170 awesomeconf
->statusbar
.width
= 0;
171 awesomeconf
->statusbar
.height
= 0;
172 awesomeconf
->opacity_unfocused
= -1;
173 awesomeconf
->nkeys
= 0;
174 awesomeconf
->nrules
= 0;
176 awesomeconf
->nlayouts
= 2;
177 awesomeconf
->layouts
= p_new(Layout
, awesomeconf
->nlayouts
+ 1);
178 awesomeconf
->layouts
[0].symbol
= a_strdup("[]=");
179 awesomeconf
->layouts
[0].arrange
= layout_tile
;
180 awesomeconf
->layouts
[1].symbol
= a_strdup("<><");
181 awesomeconf
->layouts
[1].arrange
= layout_floating
;
182 awesomeconf
->layouts
[2].symbol
= NULL
;
183 awesomeconf
->layouts
[2].arrange
= NULL
;
185 awesomeconf
->ntags
= 3;
186 awesomeconf
->tags
= p_new(Tag
, awesomeconf
->ntags
);
187 awesomeconf
->tags
[0].name
= a_strdup("this");
188 awesomeconf
->tags
[1].name
= a_strdup("is");
189 awesomeconf
->tags
[2].name
= a_strdup("awesome");
190 awesomeconf
->tags
[0].selected
= True
;
191 awesomeconf
->tags
[1].selected
= False
;
192 awesomeconf
->tags
[2].selected
= False
;
193 awesomeconf
->tags
[0].was_selected
= False
;
194 awesomeconf
->tags
[1].was_selected
= False
;
195 awesomeconf
->tags
[2].was_selected
= False
;
196 awesomeconf
->tags
[0].layout
= awesomeconf
->layouts
;
197 awesomeconf
->tags
[1].layout
= awesomeconf
->layouts
;
198 awesomeconf
->tags
[2].layout
= awesomeconf
->layouts
;
201 /** Parse configuration file and initialize some stuff
202 * \param disp Display ref
203 * \param scr Screen number
204 * \param drawcontext Draw context
207 parse_config(Display
* disp
, int scr
, DC
* drawcontext
, awesome_config
*awesomeconf
)
209 /* Main configuration object for parsing*/
210 config_t awesomelibconf
;
211 config_setting_t
*conftags
;
212 config_setting_t
*conflayouts
, *confsublayouts
;
213 config_setting_t
*confrules
, *confsubrules
;
214 config_setting_t
*confkeys
, *confsubkeys
, *confkeysmasks
, *confkeymaskelem
;
217 const char *tmp
, *homedir
;
220 ssize_t confpath_len
;
222 set_default_config(awesomeconf
);
224 homedir
= getenv("HOME");
225 confpath_len
= a_strlen(homedir
) + a_strlen(AWESOME_CONFIG_FILE
) + 2;
226 confpath
= p_new(char, confpath_len
);
227 a_strcpy(confpath
, confpath_len
, homedir
);
228 a_strcat(confpath
, confpath_len
, "/");
229 a_strcat(confpath
, confpath_len
, AWESOME_CONFIG_FILE
);
231 config_init(&awesomelibconf
);
234 awesomeconf
->screen
= scr
;
236 if(config_read_file(&awesomelibconf
, confpath
) == CONFIG_FALSE
)
237 fprintf(stderr
, "awesome: error parsing configuration file at line %d: %s\n",
238 config_error_line(&awesomelibconf
), config_error_text(&awesomelibconf
));
242 tmp
= config_lookup_string(&awesomelibconf
, "awesome.font");
243 initfont(tmp
? tmp
: "-*-fixed-medium-r-normal-*-13-*-*-*-*-*-*-*", disp
, drawcontext
);
246 conflayouts
= config_lookup(&awesomelibconf
, "awesome.layouts");
249 fprintf(stderr
, "awesome: layouts not found in configuration file\n");
252 awesomeconf
->nlayouts
= config_setting_length(conflayouts
);
253 awesomeconf
->layouts
= p_new(Layout
, awesomeconf
->nlayouts
+ 1);
254 for(i
= 0; (confsublayouts
= config_setting_get_elem(conflayouts
, i
)); i
++)
256 awesomeconf
->layouts
[i
].arrange
=
257 name_func_lookup(config_setting_get_string_elem(confsublayouts
, 1), LayoutsList
);
258 if(!awesomeconf
->layouts
[i
].arrange
)
260 fprintf(stderr
, "awesome: unknown layout #%d in configuration file\n", i
);
261 awesomeconf
->layouts
[i
].symbol
= NULL
;
264 awesomeconf
->layouts
[i
].symbol
= a_strdup(config_setting_get_string_elem(confsublayouts
, 0));
266 awesomeconf
->layouts
[i
].symbol
= NULL
;
267 awesomeconf
->layouts
[i
].arrange
= NULL
;
270 if(!awesomeconf
->nlayouts
)
271 eprint("awesome: fatal: no default layout available\n");
273 for(i
= 0; i
< awesomeconf
->nlayouts
; i
++)
275 j
= textw(drawcontext
->font
.set
, drawcontext
->font
.xfont
, awesomeconf
->layouts
[i
].symbol
, drawcontext
->font
.height
);
276 if(j
> awesomeconf
->statusbar
.width
)
277 awesomeconf
->statusbar
.width
= j
;
280 awesomeconf
->current_layout
= awesomeconf
->layouts
;
283 conftags
= config_lookup(&awesomelibconf
, "awesome.tags");
286 fprintf(stderr
, "awesome: tags not found in configuration file\n");
289 awesomeconf
->ntags
= config_setting_length(conftags
);
290 awesomeconf
->tags
= p_new(Tag
, awesomeconf
->ntags
);
292 for(i
= 0; (tmp
= config_setting_get_string_elem(conftags
, i
)); i
++)
294 awesomeconf
->tags
[i
].name
= a_strdup(tmp
);
295 awesomeconf
->tags
[i
].selected
= False
;
296 awesomeconf
->tags
[i
].was_selected
= False
;
297 /** \todo add support for default tag/layout in configuration file */
298 awesomeconf
->tags
[i
].layout
= awesomeconf
->layouts
;
302 if(!awesomeconf
->ntags
)
303 eprint("awesome: fatal: no tags found in configuration file\n");
305 /* select first tag by default */
306 awesomeconf
->tags
[0].selected
= True
;
307 awesomeconf
->tags
[0].was_selected
= True
;
310 confrules
= config_lookup(&awesomelibconf
, "awesome.rules");
313 fprintf(stderr
, "awesome: no rules found in configuration file\n");
316 awesomeconf
->nrules
= config_setting_length(confrules
);
317 awesomeconf
->rules
= p_new(Rule
, awesomeconf
->nrules
);
318 for(i
= 0; (confsubrules
= config_setting_get_elem(confrules
, i
)); i
++)
320 awesomeconf
->rules
[i
].prop
= a_strdup(config_setting_get_string(config_setting_get_member(confsubrules
, "name")));
321 awesomeconf
->rules
[i
].tags
= a_strdup(config_setting_get_string(config_setting_get_member(confsubrules
, "tags")));
322 if(awesomeconf
->rules
[i
].tags
&& !a_strlen(awesomeconf
->rules
[i
].tags
))
323 awesomeconf
->rules
[i
].tags
= NULL
;
324 awesomeconf
->rules
[i
].isfloating
=
325 config_setting_get_bool(config_setting_get_member(confsubrules
, "float"));
330 tmp_key
= key_mask_lookup(config_lookup_string(&awesomelibconf
, "awesome.modkey"));
331 awesomeconf
->modkey
= tmp_key
? tmp_key
: Mod1Mask
;
333 /* find numlock mask */
334 awesomeconf
->numlockmask
= get_numlockmask(disp
);
337 confkeys
= config_lookup(&awesomelibconf
, "awesome.keys");
340 fprintf(stderr
, "awesome: no keys found in configuration file\n");
343 awesomeconf
->nkeys
= config_setting_length(confkeys
);
344 awesomeconf
->keys
= p_new(Key
, awesomeconf
->nkeys
);
346 for(i
= 0; (confsubkeys
= config_setting_get_elem(confkeys
, i
)); i
++)
348 confkeysmasks
= config_setting_get_elem(confsubkeys
, 0);
349 for(j
= 0; (confkeymaskelem
= config_setting_get_elem(confkeysmasks
, j
)); j
++)
350 awesomeconf
->keys
[i
].mod
|= key_mask_lookup(config_setting_get_string(confkeymaskelem
));
351 awesomeconf
->keys
[i
].keysym
= XStringToKeysym(config_setting_get_string_elem(confsubkeys
, 1));
352 awesomeconf
->keys
[i
].func
=
353 name_func_lookup(config_setting_get_string_elem(confsubkeys
, 2), KeyfuncList
);
354 awesomeconf
->keys
[i
].arg
= a_strdup(config_setting_get_string_elem(confsubkeys
, 3));
359 tmp
= config_lookup_string(&awesomelibconf
, "awesome.barpos");
361 if(tmp
&& !a_strncmp(tmp
, "off", 6))
362 awesomeconf
->statusbar_default_position
= BarOff
;
363 else if(tmp
&& !a_strncmp(tmp
, "bottom", 6))
364 awesomeconf
->statusbar_default_position
= BarBot
;
366 awesomeconf
->statusbar_default_position
= BarTop
;
368 awesomeconf
->statusbar
.position
= awesomeconf
->statusbar_default_position
;
371 awesomeconf
->borderpx
= config_lookup_int(&awesomelibconf
, "awesome.borderpx");
374 awesomeconf
->opacity_unfocused
= config_lookup_int(&awesomelibconf
, "awesome.opacity_unfocused");
375 if(awesomeconf
->opacity_unfocused
>= 100)
376 awesomeconf
->opacity_unfocused
= -1;
379 i
= config_lookup_int(&awesomelibconf
, "awesome.snap");
380 awesomeconf
->snap
= i
? i
: 8;
383 i
= config_lookup_int(&awesomelibconf
, "awesome.nmaster");
384 awesomeconf
->nmaster
= i
? i
: 1;
387 i
= config_lookup_int(&awesomelibconf
, "awesome.ncols");
388 awesomeconf
->ncols
= i
? i
: 1;
391 f
= config_lookup_float(&awesomelibconf
, "awesome.mwfact");
392 awesomeconf
->mwfact
= f
? f
: 0.6;
395 awesomeconf
->resize_hints
= config_lookup_float(&awesomelibconf
, "awesome.resize_hints");
398 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_border_color");
399 drawcontext
->norm
[ColBorder
] = initcolor(tmp
? tmp
: "#dddddd", disp
, scr
);
401 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_bg_color");
402 drawcontext
->norm
[ColBG
] = initcolor(tmp
? tmp
: "#000000", disp
, scr
);
404 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_fg_color");
405 drawcontext
->norm
[ColFG
] = initcolor(tmp
? tmp
: "#ffffff", disp
, scr
);
407 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_border_color");
408 drawcontext
->sel
[ColBorder
] = initcolor(tmp
? tmp
: "#008b8b", disp
, scr
);
410 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_bg_color");
411 drawcontext
->sel
[ColBG
] = initcolor(tmp
? tmp
: "#008b8b", disp
, scr
);
413 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_fg_color");
414 drawcontext
->sel
[ColFG
] = initcolor(tmp
? tmp
: "#ffffff", disp
, scr
);
416 config_destroy(&awesomelibconf
);
420 /** Initialize font from X side and store in draw context
421 * \param fontstr Font name
422 * \param disp Display ref
423 * \param drawcontext Draw context
426 initfont(const char *fontstr
, Display
* disp
, DC
* drawcontext
)
428 char *def
, **missing
;
432 if(drawcontext
->font
.set
)
433 XFreeFontSet(disp
, drawcontext
->font
.set
);
434 drawcontext
->font
.set
= XCreateFontSet(disp
, fontstr
, &missing
, &n
, &def
);
438 fprintf(stderr
, "awesome: missing fontset: %s\n", missing
[n
]);
439 XFreeStringList(missing
);
441 if(drawcontext
->font
.set
)
443 XFontSetExtents
*font_extents
;
444 XFontStruct
**xfonts
;
446 drawcontext
->font
.ascent
= drawcontext
->font
.descent
= 0;
447 font_extents
= XExtentsOfFontSet(drawcontext
->font
.set
);
448 n
= XFontsOfFontSet(drawcontext
->font
.set
, &xfonts
, &font_names
);
449 for(i
= 0, drawcontext
->font
.ascent
= 0, drawcontext
->font
.descent
= 0; i
< n
; i
++)
451 if(drawcontext
->font
.ascent
< (*xfonts
)->ascent
)
452 drawcontext
->font
.ascent
= (*xfonts
)->ascent
;
453 if(drawcontext
->font
.descent
< (*xfonts
)->descent
)
454 drawcontext
->font
.descent
= (*xfonts
)->descent
;
460 if(drawcontext
->font
.xfont
)
461 XFreeFont(disp
, drawcontext
->font
.xfont
);
462 drawcontext
->font
.xfont
= NULL
;
463 if(!(drawcontext
->font
.xfont
= XLoadQueryFont(disp
, fontstr
))
464 && !(drawcontext
->font
.xfont
= XLoadQueryFont(disp
, "fixed")))
465 die("awesome: error, cannot load font: '%s'\n", fontstr
);
466 drawcontext
->font
.ascent
= drawcontext
->font
.xfont
->ascent
;
467 drawcontext
->font
.descent
= drawcontext
->font
.xfont
->descent
;
469 drawcontext
->font
.height
= drawcontext
->font
.ascent
+ drawcontext
->font
.descent
;
473 get_numlockmask(Display
*disp
)
475 XModifierKeymap
*modmap
;
476 unsigned int mask
= 0;
479 modmap
= XGetModifierMapping(disp
);
480 for(i
= 0; i
< 8; i
++)
481 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
483 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
484 == XKeysymToKeycode(disp
, XK_Num_Lock
))
488 XFreeModifiermap(modmap
);
493 /** Initialize color from X side
494 * \param colorstr Color code
495 * \param disp Display ref
496 * \param scr Screen number
497 * \return XColor pixel
500 initcolor(const char *colstr
, Display
* disp
, int scr
)
502 Colormap cmap
= DefaultColormap(disp
, scr
);
504 if(!XAllocNamedColor(disp
, cmap
, colstr
, &color
, &color
))
505 die("awesome: error, cannot allocate color '%s'\n", colstr
);