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
},
90 {"togglefloating", uicb_togglefloating
},
91 {"toggleview", uicb_toggleview
},
92 {"toggletag", uicb_toggletag
},
94 {"viewprevtags", uicb_viewprevtags
},
96 {"setlayout", uicb_setlayout
},
97 {"focusnext", uicb_focusnext
},
98 {"focusprev", uicb_focusprev
},
99 {"togglemax", uicb_togglemax
},
100 {"toggleverticalmax", uicb_toggleverticalmax
},
101 {"togglehorizontalmax", uicb_togglehorizontalmax
},
104 {"setmwfact", uicb_setmwfact
},
105 {"setnmaster", uicb_setnmaster
},
106 {"setncols", uicb_setncols
},
110 {"togglebar", uicb_togglebar
},
114 /** Lookup for a key mask from its name
115 * \param keyname Key name
116 * \return Key mask or 0 if not found
119 key_mask_lookup(const char *keyname
)
124 for(i
= 0; KeyModList
[i
].name
; i
++)
125 if(!strcmp(keyname
, KeyModList
[i
].name
))
126 return KeyModList
[i
].keysym
;
131 /** Lookup for a function pointer from its name
132 * in the given NameFuncLink list
133 * \param funcname Function name
134 * \param list Function and name link list
135 * \return function pointer
138 name_func_lookup(const char *funcname
, const NameFuncLink
* list
)
143 for(i
= 0; list
[i
].name
; i
++)
144 if(!strcmp(funcname
, list
[i
].name
))
150 /** Set default configuration
151 * \param awesomeconf awesome config ref
154 set_default_config(awesome_config
*awesomeconf
)
156 /** \todo most of this stuff aren't freed when we initialize
157 * the real configuration, we should add a clean conf function */
158 strcpy(awesomeconf
->statustext
, "awesome-" VERSION
);
159 awesomeconf
->statusbar
.width
= 0;
160 awesomeconf
->statusbar
.height
= 0;
161 awesomeconf
->opacity_unfocused
= -1;
162 awesomeconf
->nkeys
= 0;
163 awesomeconf
->nrules
= 0;
165 awesomeconf
->nlayouts
= 2;
166 awesomeconf
->layouts
= p_new(Layout
, awesomeconf
->nlayouts
+ 1);
167 awesomeconf
->layouts
[0].symbol
= a_strdup("[]=");
168 awesomeconf
->layouts
[0].arrange
= tile
;
169 awesomeconf
->layouts
[1].symbol
= a_strdup("<><");
170 awesomeconf
->layouts
[1].arrange
= floating
;
171 awesomeconf
->layouts
[2].symbol
= NULL
;
172 awesomeconf
->layouts
[2].arrange
= NULL
;
174 awesomeconf
->ntags
= 3;
175 awesomeconf
->tags
= p_new(char *, awesomeconf
->ntags
);
176 awesomeconf
->selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
177 awesomeconf
->prev_selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
178 awesomeconf
->tag_layouts
= p_new(Layout
*, awesomeconf
->ntags
);
179 awesomeconf
->tags
[0] = a_strdup("this");
180 awesomeconf
->tags
[1] = a_strdup("is");
181 awesomeconf
->tags
[2] = a_strdup("awesome");
182 awesomeconf
->selected_tags
[0] = True
;
183 awesomeconf
->selected_tags
[1] = False
;
184 awesomeconf
->selected_tags
[2] = False
;
185 awesomeconf
->prev_selected_tags
[0] = False
;
186 awesomeconf
->prev_selected_tags
[1] = False
;
187 awesomeconf
->prev_selected_tags
[2] = False
;
188 awesomeconf
->tag_layouts
[0] = awesomeconf
->layouts
;
189 awesomeconf
->tag_layouts
[1] = awesomeconf
->layouts
;
190 awesomeconf
->tag_layouts
[2] = awesomeconf
->layouts
;
193 /** Parse configuration file and initialize some stuff
194 * \param disp Display ref
195 * \param scr Screen number
196 * \param drawcontext Draw context
199 parse_config(Display
* disp
, int scr
, DC
* drawcontext
, awesome_config
*awesomeconf
)
201 /* Main configuration object for parsing*/
202 config_t awesomelibconf
;
203 config_setting_t
*conftags
;
204 config_setting_t
*conflayouts
, *confsublayouts
;
205 config_setting_t
*confrules
, *confsubrules
;
206 config_setting_t
*confkeys
, *confsubkeys
, *confkeysmasks
, *confkeymaskelem
;
209 const char *tmp
, *homedir
;
213 set_default_config(awesomeconf
);
215 homedir
= getenv("HOME");
216 confpath
= p_new(char, strlen(homedir
) + strlen(AWESOME_CONFIG_FILE
) + 2);
217 strcpy(confpath
, homedir
);
218 strcat(confpath
, "/");
219 strcat(confpath
, AWESOME_CONFIG_FILE
);
221 config_init(&awesomelibconf
);
224 awesomeconf
->screen
= scr
;
226 if(config_read_file(&awesomelibconf
, confpath
) == CONFIG_FALSE
)
227 fprintf(stderr
, "awesome: error parsing configuration file at line %d: %s\n",
228 config_error_line(&awesomelibconf
), config_error_text(&awesomelibconf
));
232 tmp
= config_lookup_string(&awesomelibconf
, "awesome.font");
233 initfont(tmp
? tmp
: "-*-fixed-medium-r-normal-*-13-*-*-*-*-*-*-*", disp
, drawcontext
);
236 conflayouts
= config_lookup(&awesomelibconf
, "awesome.layouts");
239 fprintf(stderr
, "awesome: layouts not found in configuration file\n");
242 awesomeconf
->nlayouts
= config_setting_length(conflayouts
);
243 awesomeconf
->layouts
= p_new(Layout
, awesomeconf
->nlayouts
+ 1);
244 for(i
= 0; (confsublayouts
= config_setting_get_elem(conflayouts
, i
)); i
++)
246 awesomeconf
->layouts
[i
].arrange
=
247 name_func_lookup(config_setting_get_string_elem(confsublayouts
, 1), LayoutsList
);
248 if(!awesomeconf
->layouts
[i
].arrange
)
250 fprintf(stderr
, "awesome: unknown layout #%d in configuration file\n", i
);
251 awesomeconf
->layouts
[i
].symbol
= NULL
;
254 awesomeconf
->layouts
[i
].symbol
= a_strdup(config_setting_get_string_elem(confsublayouts
, 0));
256 awesomeconf
->layouts
[i
].symbol
= NULL
;
257 awesomeconf
->layouts
[i
].arrange
= NULL
;
260 if(!awesomeconf
->nlayouts
)
261 eprint("awesome: fatal: no default layout available\n");
263 for(i
= 0; i
< awesomeconf
->nlayouts
; i
++)
265 j
= textw(drawcontext
->font
.set
, drawcontext
->font
.xfont
, awesomeconf
->layouts
[i
].symbol
, drawcontext
->font
.height
);
266 if(j
> awesomeconf
->statusbar
.width
)
267 awesomeconf
->statusbar
.width
= j
;
270 awesomeconf
->current_layout
= awesomeconf
->layouts
;
273 conftags
= config_lookup(&awesomelibconf
, "awesome.tags");
276 fprintf(stderr
, "awesome: tags not found in configuration file\n");
279 awesomeconf
->ntags
= config_setting_length(conftags
);
280 awesomeconf
->tags
= p_new(char *, awesomeconf
->ntags
);
281 awesomeconf
->selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
282 awesomeconf
->prev_selected_tags
= p_new(Bool
, awesomeconf
->ntags
);
283 /** \todo move this in tags or layouts */
284 awesomeconf
->tag_layouts
= p_new(Layout
*, awesomeconf
->ntags
);
286 for(i
= 0; (tmp
= config_setting_get_string_elem(conftags
, i
)); i
++)
288 awesomeconf
->tags
[i
] = a_strdup(tmp
);
289 awesomeconf
->selected_tags
[i
] = False
;
290 awesomeconf
->prev_selected_tags
[i
] = False
;
291 /** \todo add support for default tag/layout in configuration file */
292 awesomeconf
->tag_layouts
[i
] = awesomeconf
->layouts
;
296 if(!awesomeconf
->ntags
)
297 eprint("awesome: fatal: no tags found in configuration file\n");
299 /* select first tag by default */
300 awesomeconf
->selected_tags
[0] = True
;
301 awesomeconf
->prev_selected_tags
[0] = True
;
304 confrules
= config_lookup(&awesomelibconf
, "awesome.rules");
307 fprintf(stderr
, "awesome: no rules found in configuration file\n");
310 awesomeconf
->nrules
= config_setting_length(confrules
);
311 awesomeconf
->rules
= p_new(Rule
, awesomeconf
->nrules
);
312 for(i
= 0; (confsubrules
= config_setting_get_elem(confrules
, i
)); i
++)
314 awesomeconf
->rules
[i
].prop
= a_strdup(config_setting_get_string(config_setting_get_member(confsubrules
, "name")));
315 awesomeconf
->rules
[i
].tags
= a_strdup(config_setting_get_string(config_setting_get_member(confsubrules
, "tags")));
316 if(awesomeconf
->rules
[i
].tags
&& !strlen(awesomeconf
->rules
[i
].tags
))
317 awesomeconf
->rules
[i
].tags
= NULL
;
318 awesomeconf
->rules
[i
].isfloating
=
319 config_setting_get_bool(config_setting_get_member(confsubrules
, "float"));
324 tmp_key
= key_mask_lookup(config_lookup_string(&awesomelibconf
, "awesome.modkey"));
325 awesomeconf
->modkey
= tmp_key
? tmp_key
: Mod1Mask
;
327 /* find numlock mask */
328 awesomeconf
->numlockmask
= get_numlockmask(disp
);
331 confkeys
= config_lookup(&awesomelibconf
, "awesome.keys");
334 fprintf(stderr
, "awesome: no keys found in configuration file\n");
337 awesomeconf
->nkeys
= config_setting_length(confkeys
);
338 awesomeconf
->keys
= p_new(Key
, awesomeconf
->nkeys
);
340 for(i
= 0; (confsubkeys
= config_setting_get_elem(confkeys
, i
)); i
++)
342 confkeysmasks
= config_setting_get_elem(confsubkeys
, 0);
343 for(j
= 0; (confkeymaskelem
= config_setting_get_elem(confkeysmasks
, j
)); j
++)
344 awesomeconf
->keys
[i
].mod
|= key_mask_lookup(config_setting_get_string(confkeymaskelem
));
345 awesomeconf
->keys
[i
].keysym
= XStringToKeysym(config_setting_get_string_elem(confsubkeys
, 1));
346 awesomeconf
->keys
[i
].func
=
347 name_func_lookup(config_setting_get_string_elem(confsubkeys
, 2), KeyfuncList
);
348 awesomeconf
->keys
[i
].arg
= a_strdup(config_setting_get_string_elem(confsubkeys
, 3));
353 tmp
= config_lookup_string(&awesomelibconf
, "awesome.barpos");
355 if(tmp
&& !strncmp(tmp
, "off", 6))
356 awesomeconf
->statusbar_default_position
= BarOff
;
357 else if(tmp
&& !strncmp(tmp
, "bottom", 6))
358 awesomeconf
->statusbar_default_position
= BarBot
;
360 awesomeconf
->statusbar_default_position
= BarTop
;
362 awesomeconf
->statusbar
.position
= awesomeconf
->statusbar_default_position
;
365 awesomeconf
->borderpx
= config_lookup_int(&awesomelibconf
, "awesome.borderpx");
368 awesomeconf
->opacity_unfocused
= config_lookup_int(&awesomelibconf
, "awesome.opacity_unfocused");
369 if(awesomeconf
->opacity_unfocused
>= 100)
370 awesomeconf
->opacity_unfocused
= -1;
373 i
= config_lookup_int(&awesomelibconf
, "awesome.snap");
374 awesomeconf
->snap
= i
? i
: 8;
377 i
= config_lookup_int(&awesomelibconf
, "awesome.nmaster");
378 awesomeconf
->nmaster
= i
? i
: 1;
381 i
= config_lookup_int(&awesomelibconf
, "awesome.ncols");
382 awesomeconf
->ncols
= i
? i
: 1;
385 f
= config_lookup_float(&awesomelibconf
, "awesome.mwfact");
386 awesomeconf
->mwfact
= f
? f
: 0.6;
389 awesomeconf
->resize_hints
= config_lookup_float(&awesomelibconf
, "awesome.resize_hints");
392 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_border_color");
393 drawcontext
->norm
[ColBorder
] = initcolor(tmp
? tmp
: "#dddddd", disp
, scr
);
395 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_bg_color");
396 drawcontext
->norm
[ColBG
] = initcolor(tmp
? tmp
: "#000000", disp
, scr
);
398 tmp
= config_lookup_string(&awesomelibconf
, "awesome.normal_fg_color");
399 drawcontext
->norm
[ColFG
] = initcolor(tmp
? tmp
: "#ffffff", disp
, scr
);
401 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_border_color");
402 drawcontext
->sel
[ColBorder
] = initcolor(tmp
? tmp
: "#008b8b", disp
, scr
);
404 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_bg_color");
405 drawcontext
->sel
[ColBG
] = initcolor(tmp
? tmp
: "#008b8b", disp
, scr
);
407 tmp
= config_lookup_string(&awesomelibconf
, "awesome.focus_fg_color");
408 drawcontext
->sel
[ColFG
] = initcolor(tmp
? tmp
: "#ffffff", disp
, scr
);
410 config_destroy(&awesomelibconf
);
414 /** Initialize font from X side and store in draw context
415 * \param fontstr Font name
416 * \param disp Display ref
417 * \param drawcontext Draw context
420 initfont(const char *fontstr
, Display
* disp
, DC
* drawcontext
)
422 char *def
, **missing
;
426 if(drawcontext
->font
.set
)
427 XFreeFontSet(disp
, drawcontext
->font
.set
);
428 drawcontext
->font
.set
= XCreateFontSet(disp
, fontstr
, &missing
, &n
, &def
);
432 fprintf(stderr
, "awesome: missing fontset: %s\n", missing
[n
]);
433 XFreeStringList(missing
);
435 if(drawcontext
->font
.set
)
437 XFontSetExtents
*font_extents
;
438 XFontStruct
**xfonts
;
440 drawcontext
->font
.ascent
= drawcontext
->font
.descent
= 0;
441 font_extents
= XExtentsOfFontSet(drawcontext
->font
.set
);
442 n
= XFontsOfFontSet(drawcontext
->font
.set
, &xfonts
, &font_names
);
443 for(i
= 0, drawcontext
->font
.ascent
= 0, drawcontext
->font
.descent
= 0; i
< n
; i
++)
445 if(drawcontext
->font
.ascent
< (*xfonts
)->ascent
)
446 drawcontext
->font
.ascent
= (*xfonts
)->ascent
;
447 if(drawcontext
->font
.descent
< (*xfonts
)->descent
)
448 drawcontext
->font
.descent
= (*xfonts
)->descent
;
454 if(drawcontext
->font
.xfont
)
455 XFreeFont(disp
, drawcontext
->font
.xfont
);
456 drawcontext
->font
.xfont
= NULL
;
457 if(!(drawcontext
->font
.xfont
= XLoadQueryFont(disp
, fontstr
))
458 || !(drawcontext
->font
.xfont
= XLoadQueryFont(disp
, "fixed")))
459 die("awesome: error, cannot load font: '%s'\n", fontstr
);
460 drawcontext
->font
.ascent
= drawcontext
->font
.xfont
->ascent
;
461 drawcontext
->font
.descent
= drawcontext
->font
.xfont
->descent
;
463 drawcontext
->font
.height
= drawcontext
->font
.ascent
+ drawcontext
->font
.descent
;
467 get_numlockmask(Display
*disp
)
469 XModifierKeymap
*modmap
;
470 unsigned int mask
= 0;
473 modmap
= XGetModifierMapping(disp
);
474 for(i
= 0; i
< 8; i
++)
475 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
477 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
478 == XKeysymToKeycode(disp
, XK_Num_Lock
))
482 XFreeModifiermap(modmap
);
487 /** Initialize color from X side
488 * \param colorstr Color code
489 * \param disp Display ref
490 * \param scr Screen number
491 * \return XColor pixel
494 initcolor(const char *colstr
, Display
* disp
, int scr
)
496 Colormap cmap
= DefaultColormap(disp
, scr
);
498 if(!XAllocNamedColor(disp
, cmap
, colstr
, &color
, &color
))
499 die("awesome: error, cannot allocate color '%s'\n", colstr
);