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/floating.h"
39 static void initfont(const char *, Display
*, DC
*);
40 static unsigned long initcolor(const char *, Display
*, int);
41 static unsigned int get_numlockmask(Display
*);
43 /** Link a name to a function */
50 /** Link a name to a key symbol */
57 /** List of keyname and corresponding X11 mask codes */
58 static const KeyMod KeyModList
[] =
62 {"Control", ControlMask
},
71 /** List of available layouts and link between name and functions */
72 static const NameFuncLink LayoutsList
[] =
75 {"tileleft", tileleft
},
76 {"floating", floating
},
80 /** List of available UI bindable callbacks and functions */
81 static const NameFuncLink KeyfuncList
[] = {
83 {"spawn", uicb_spawn
},
85 {"killclient", uicb_killclient
},
86 {"moveresize", uicb_moveresize
},
87 {"settrans", uicb_settrans
},
88 {"setborder", uicb_setborder
},
91 {"togglefloating", uicb_togglefloating
},
92 {"toggleview", uicb_toggleview
},
93 {"toggletag", uicb_toggletag
},
95 {"viewprevtags", uicb_viewprevtags
},
97 {"setlayout", uicb_setlayout
},
98 {"focusnext", uicb_focusnext
},
99 {"focusprev", uicb_focusprev
},
100 {"togglemax", uicb_togglemax
},
101 {"toggleverticalmax", uicb_toggleverticalmax
},
102 {"togglehorizontalmax", uicb_togglehorizontalmax
},
105 {"setmwfact", uicb_setmwfact
},
106 {"setnmaster", uicb_setnmaster
},
107 {"setncols", uicb_setncols
},
111 {"togglebar", uicb_togglebar
},
115 /** Lookup for a key mask from its name
116 * \param keyname Key name
117 * \return Key mask or 0 if not found
120 key_mask_lookup(const char *keyname
)
125 for(i
= 0; KeyModList
[i
].name
; i
++)
126 if(!strcmp(keyname
, KeyModList
[i
].name
))
127 return KeyModList
[i
].keysym
;
132 /** Lookup for a function pointer from its name
133 * in the given NameFuncLink list
134 * \param funcname Function name
135 * \param list Function and name link list
136 * \return function pointer
139 name_func_lookup(const char *funcname
, const NameFuncLink
* list
)
144 for(i
= 0; list
[i
].name
; i
++)
145 if(!strcmp(funcname
, list
[i
].name
))
151 /** Set default configuration
152 * \param awesomeconf awesome config ref
155 set_default_config(awesome_config
*awesomeconf
)
157 /** \todo most of this stuff aren't freed when we initialize
158 * the real configuration, we should add a clean conf function */
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;
166 awesomeconf
->nlayouts
= 2;
167 awesomeconf
->layouts
= p_new(Layout
, awesomeconf
->nlayouts
+ 1);
168 awesomeconf
->layouts
[0].symbol
= a_strdup("[]=");
169 awesomeconf
->layouts
[0].arrange
= tile
;
170 awesomeconf
->layouts
[1].symbol
= a_strdup("<><");
171 awesomeconf
->layouts
[1].arrange
= floating
;
172 awesomeconf
->layouts
[2].symbol
= NULL
;
173 awesomeconf
->layouts
[2].arrange
= NULL
;
175 awesomeconf
->ntags
= 3;
176 awesomeconf
->tags
= p_new(char *, awesomeconf
->ntags
);
177 awesomeconf
->selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
178 awesomeconf
->prev_selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
179 awesomeconf
->tag_layouts
= p_new(Layout
*, awesomeconf
->ntags
);
180 awesomeconf
->tags
[0] = a_strdup("this");
181 awesomeconf
->tags
[1] = a_strdup("is");
182 awesomeconf
->tags
[2] = a_strdup("awesome");
183 awesomeconf
->selected_tags
[0] = True
;
184 awesomeconf
->selected_tags
[1] = False
;
185 awesomeconf
->selected_tags
[2] = False
;
186 awesomeconf
->prev_selected_tags
[0] = False
;
187 awesomeconf
->prev_selected_tags
[1] = False
;
188 awesomeconf
->prev_selected_tags
[2] = False
;
189 awesomeconf
->tag_layouts
[0] = awesomeconf
->layouts
;
190 awesomeconf
->tag_layouts
[1] = awesomeconf
->layouts
;
191 awesomeconf
->tag_layouts
[2] = awesomeconf
->layouts
;
194 /** Parse configuration file and initialize some stuff
195 * \param disp Display ref
196 * \param scr Screen number
197 * \param drawcontext Draw context
200 parse_config(Display
* disp
, int scr
, DC
* drawcontext
, awesome_config
*awesomeconf
)
202 /* Main configuration object for parsing*/
203 config_t awesomelibconf
;
204 config_setting_t
*conftags
;
205 config_setting_t
*conflayouts
, *confsublayouts
;
206 config_setting_t
*confrules
, *confsubrules
;
207 config_setting_t
*confkeys
, *confsubkeys
, *confkeysmasks
, *confkeymaskelem
;
210 const char *tmp
, *homedir
;
214 set_default_config(awesomeconf
);
216 homedir
= getenv("HOME");
217 confpath
= p_new(char, strlen(homedir
) + strlen(AWESOME_CONFIG_FILE
) + 2);
218 strcpy(confpath
, homedir
);
219 strcat(confpath
, "/");
220 strcat(confpath
, AWESOME_CONFIG_FILE
);
222 config_init(&awesomelibconf
);
225 awesomeconf
->screen
= scr
;
227 if(config_read_file(&awesomelibconf
, confpath
) == CONFIG_FALSE
)
228 fprintf(stderr
, "awesome: error parsing configuration file at line %d: %s\n",
229 config_error_line(&awesomelibconf
), config_error_text(&awesomelibconf
));
233 tmp
= config_lookup_string(&awesomelibconf
, "awesome.font");
234 initfont(tmp
? tmp
: "-*-fixed-medium-r-normal-*-13-*-*-*-*-*-*-*", disp
, drawcontext
);
237 conflayouts
= config_lookup(&awesomelibconf
, "awesome.layouts");
240 fprintf(stderr
, "awesome: layouts not found in configuration file\n");
243 awesomeconf
->nlayouts
= config_setting_length(conflayouts
);
244 awesomeconf
->layouts
= p_new(Layout
, awesomeconf
->nlayouts
+ 1);
245 for(i
= 0; (confsublayouts
= config_setting_get_elem(conflayouts
, i
)); i
++)
247 awesomeconf
->layouts
[i
].arrange
=
248 name_func_lookup(config_setting_get_string_elem(confsublayouts
, 1), LayoutsList
);
249 if(!awesomeconf
->layouts
[i
].arrange
)
251 fprintf(stderr
, "awesome: unknown layout #%d in configuration file\n", i
);
252 awesomeconf
->layouts
[i
].symbol
= NULL
;
255 awesomeconf
->layouts
[i
].symbol
= a_strdup(config_setting_get_string_elem(confsublayouts
, 0));
257 awesomeconf
->layouts
[i
].symbol
= NULL
;
258 awesomeconf
->layouts
[i
].arrange
= NULL
;
261 if(!awesomeconf
->nlayouts
)
262 eprint("awesome: fatal: no default layout available\n");
264 for(i
= 0; i
< awesomeconf
->nlayouts
; i
++)
266 j
= textw(drawcontext
->font
.set
, drawcontext
->font
.xfont
, awesomeconf
->layouts
[i
].symbol
, drawcontext
->font
.height
);
267 if(j
> awesomeconf
->statusbar
.width
)
268 awesomeconf
->statusbar
.width
= j
;
271 awesomeconf
->current_layout
= awesomeconf
->layouts
;
274 conftags
= config_lookup(&awesomelibconf
, "awesome.tags");
277 fprintf(stderr
, "awesome: tags not found in configuration file\n");
280 awesomeconf
->ntags
= config_setting_length(conftags
);
281 awesomeconf
->tags
= p_new(char *, awesomeconf
->ntags
);
282 awesomeconf
->selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
283 awesomeconf
->prev_selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
284 /** \todo move this in tags or layouts */
285 awesomeconf
->tag_layouts
= p_new(Layout
*, awesomeconf
->ntags
);
287 for(i
= 0; (tmp
= config_setting_get_string_elem(conftags
, i
)); i
++)
289 awesomeconf
->tags
[i
] = a_strdup(tmp
);
290 awesomeconf
->selected_tags
[i
] = False
;
291 awesomeconf
->prev_selected_tags
[i
] = False
;
292 /** \todo add support for default tag/layout in configuration file */
293 awesomeconf
->tag_layouts
[i
] = awesomeconf
->layouts
;
297 if(!awesomeconf
->ntags
)
298 eprint("awesome: fatal: no tags found in configuration file\n");
300 /* select first tag by default */
301 awesomeconf
->selected_tags
[0] = True
;
302 awesomeconf
->prev_selected_tags
[0] = True
;
305 confrules
= config_lookup(&awesomelibconf
, "awesome.rules");
308 fprintf(stderr
, "awesome: no rules found in configuration file\n");
311 awesomeconf
->nrules
= config_setting_length(confrules
);
312 awesomeconf
->rules
= p_new(Rule
, awesomeconf
->nrules
);
313 for(i
= 0; (confsubrules
= config_setting_get_elem(confrules
, i
)); i
++)
315 awesomeconf
->rules
[i
].prop
= a_strdup(config_setting_get_string(config_setting_get_member(confsubrules
, "name")));
316 awesomeconf
->rules
[i
].tags
= a_strdup(config_setting_get_string(config_setting_get_member(confsubrules
, "tags")));
317 if(awesomeconf
->rules
[i
].tags
&& !strlen(awesomeconf
->rules
[i
].tags
))
318 awesomeconf
->rules
[i
].tags
= NULL
;
319 awesomeconf
->rules
[i
].isfloating
=
320 config_setting_get_bool(config_setting_get_member(confsubrules
, "float"));
325 tmp_key
= key_mask_lookup(config_lookup_string(&awesomelibconf
, "awesome.modkey"));
326 awesomeconf
->modkey
= tmp_key
? tmp_key
: Mod1Mask
;
328 /* find numlock mask */
329 awesomeconf
->numlockmask
= get_numlockmask(disp
);
332 confkeys
= config_lookup(&awesomelibconf
, "awesome.keys");
335 fprintf(stderr
, "awesome: no keys found in configuration file\n");
338 awesomeconf
->nkeys
= config_setting_length(confkeys
);
339 awesomeconf
->keys
= p_new(Key
, awesomeconf
->nkeys
);
341 for(i
= 0; (confsubkeys
= config_setting_get_elem(confkeys
, i
)); i
++)
343 confkeysmasks
= config_setting_get_elem(confsubkeys
, 0);
344 for(j
= 0; (confkeymaskelem
= config_setting_get_elem(confkeysmasks
, j
)); j
++)
345 awesomeconf
->keys
[i
].mod
|= key_mask_lookup(config_setting_get_string(confkeymaskelem
));
346 awesomeconf
->keys
[i
].keysym
= XStringToKeysym(config_setting_get_string_elem(confsubkeys
, 1));
347 awesomeconf
->keys
[i
].func
=
348 name_func_lookup(config_setting_get_string_elem(confsubkeys
, 2), KeyfuncList
);
349 awesomeconf
->keys
[i
].arg
= a_strdup(config_setting_get_string_elem(confsubkeys
, 3));
354 tmp
= config_lookup_string(&awesomelibconf
, "awesome.barpos");
356 if(tmp
&& !strncmp(tmp
, "off", 6))
357 awesomeconf
->statusbar_default_position
= BarOff
;
358 else if(tmp
&& !strncmp(tmp
, "bottom", 6))
359 awesomeconf
->statusbar_default_position
= BarBot
;
361 awesomeconf
->statusbar_default_position
= BarTop
;
363 awesomeconf
->statusbar
.position
= awesomeconf
->statusbar_default_position
;
366 awesomeconf
->borderpx
= config_lookup_int(&awesomelibconf
, "awesome.borderpx");
369 awesomeconf
->opacity_unfocused
= config_lookup_int(&awesomelibconf
, "awesome.opacity_unfocused");
370 if(awesomeconf
->opacity_unfocused
>= 100)
371 awesomeconf
->opacity_unfocused
= -1;
374 i
= config_lookup_int(&awesomelibconf
, "awesome.snap");
375 awesomeconf
->snap
= i
? i
: 8;
378 i
= config_lookup_int(&awesomelibconf
, "awesome.nmaster");
379 awesomeconf
->nmaster
= i
? i
: 1;
382 i
= config_lookup_int(&awesomelibconf
, "awesome.ncols");
383 awesomeconf
->ncols
= i
? i
: 1;
386 f
= config_lookup_float(&awesomelibconf
, "awesome.mwfact");
387 awesomeconf
->mwfact
= f
? f
: 0.6;
390 awesomeconf
->resize_hints
= config_lookup_float(&awesomelibconf
, "awesome.resize_hints");
393 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_border_color");
394 drawcontext
->norm
[ColBorder
] = initcolor(tmp
? tmp
: "#dddddd", disp
, scr
);
396 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_bg_color");
397 drawcontext
->norm
[ColBG
] = initcolor(tmp
? tmp
: "#000000", disp
, scr
);
399 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_fg_color");
400 drawcontext
->norm
[ColFG
] = initcolor(tmp
? tmp
: "#ffffff", disp
, scr
);
402 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_border_color");
403 drawcontext
->sel
[ColBorder
] = initcolor(tmp
? tmp
: "#008b8b", disp
, scr
);
405 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_bg_color");
406 drawcontext
->sel
[ColBG
] = initcolor(tmp
? tmp
: "#008b8b", disp
, scr
);
408 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_fg_color");
409 drawcontext
->sel
[ColFG
] = initcolor(tmp
? tmp
: "#ffffff", disp
, scr
);
411 config_destroy(&awesomelibconf
);
415 /** Initialize font from X side and store in draw context
416 * \param fontstr Font name
417 * \param disp Display ref
418 * \param drawcontext Draw context
421 initfont(const char *fontstr
, Display
* disp
, DC
* drawcontext
)
423 char *def
, **missing
;
427 if(drawcontext
->font
.set
)
428 XFreeFontSet(disp
, drawcontext
->font
.set
);
429 drawcontext
->font
.set
= XCreateFontSet(disp
, fontstr
, &missing
, &n
, &def
);
433 fprintf(stderr
, "awesome: missing fontset: %s\n", missing
[n
]);
434 XFreeStringList(missing
);
436 if(drawcontext
->font
.set
)
438 XFontSetExtents
*font_extents
;
439 XFontStruct
**xfonts
;
441 drawcontext
->font
.ascent
= drawcontext
->font
.descent
= 0;
442 font_extents
= XExtentsOfFontSet(drawcontext
->font
.set
);
443 n
= XFontsOfFontSet(drawcontext
->font
.set
, &xfonts
, &font_names
);
444 for(i
= 0, drawcontext
->font
.ascent
= 0, drawcontext
->font
.descent
= 0; i
< n
; i
++)
446 if(drawcontext
->font
.ascent
< (*xfonts
)->ascent
)
447 drawcontext
->font
.ascent
= (*xfonts
)->ascent
;
448 if(drawcontext
->font
.descent
< (*xfonts
)->descent
)
449 drawcontext
->font
.descent
= (*xfonts
)->descent
;
455 if(drawcontext
->font
.xfont
)
456 XFreeFont(disp
, drawcontext
->font
.xfont
);
457 drawcontext
->font
.xfont
= NULL
;
458 if(!(drawcontext
->font
.xfont
= XLoadQueryFont(disp
, fontstr
))
459 || !(drawcontext
->font
.xfont
= XLoadQueryFont(disp
, "fixed")))
460 die("awesome: error, cannot load font: '%s'\n", fontstr
);
461 drawcontext
->font
.ascent
= drawcontext
->font
.xfont
->ascent
;
462 drawcontext
->font
.descent
= drawcontext
->font
.xfont
->descent
;
464 drawcontext
->font
.height
= drawcontext
->font
.ascent
+ drawcontext
->font
.descent
;
468 get_numlockmask(Display
*disp
)
470 XModifierKeymap
*modmap
;
471 unsigned int mask
= 0;
474 modmap
= XGetModifierMapping(disp
);
475 for(i
= 0; i
< 8; i
++)
476 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
478 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
479 == XKeysymToKeycode(disp
, XK_Num_Lock
))
483 XFreeModifiermap(modmap
);
488 /** Initialize color from X side
489 * \param colorstr Color code
490 * \param disp Display ref
491 * \param scr Screen number
492 * \return XColor pixel
495 initcolor(const char *colstr
, Display
* disp
, int scr
)
497 Colormap cmap
= DefaultColormap(disp
, scr
);
499 if(!XAllocNamedColor(disp
, cmap
, colstr
, &color
, &color
))
500 die("awesome: error, cannot allocate color '%s'\n", colstr
);