simplify manage(), remove screen arg
[awesome.git] / config.c
blobc36545cbae7f619e61dcd27d662e2526f1717ca3
1 /*
2 * config.c - configuration management
3 *
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
5 *
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.
23 /**
24 * \defgroup ui_callback
27 #include <libconfig.h>
28 #include <X11/keysym.h>
30 #include "awesome.h"
31 #include "layout.h"
32 #include "tag.h"
33 #include "draw.h"
34 #include "util.h"
35 #include "statusbar.h"
36 #include "layouts/tile.h"
37 #include "layouts/spiral.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 */
45 typedef struct
47 const char *name;
48 void *func;
49 } NameFuncLink;
51 /** Link a name to a key symbol */
52 typedef struct
54 const char *name;
55 KeySym keysym;
56 } KeyMod;
58 /** List of keyname and corresponding X11 mask codes */
59 static const KeyMod KeyModList[] =
61 {"Shift", ShiftMask},
62 {"Lock", LockMask},
63 {"Control", ControlMask},
64 {"Mod1", Mod1Mask},
65 {"Mod2", Mod2Mask},
66 {"Mod3", Mod3Mask},
67 {"Mod4", Mod4Mask},
68 {"Mod5", Mod5Mask},
69 {"None", 0}
72 /** List of available layouts and link between name and functions */
73 static const NameFuncLink LayoutsList[] =
75 {"tile", tile},
76 {"tileleft", tileleft},
77 {"floating", floating},
78 {"spiral", spiral},
79 {"dwindle", dwindle},
80 {NULL, NULL}
83 /** List of available UI bindable callbacks and functions */
84 static const NameFuncLink KeyfuncList[] = {
85 /* util.c */
86 {"spawn", uicb_spawn},
87 /* client.c */
88 {"killclient", uicb_killclient},
89 {"moveresize", uicb_moveresize},
90 {"settrans", uicb_settrans},
91 /* tag.c */
92 {"tag", uicb_tag},
93 {"togglefloating", uicb_togglefloating},
94 {"toggleview", uicb_toggleview},
95 {"toggletag", uicb_toggletag},
96 {"view", uicb_view},
97 {"viewprevtags", uicb_viewprevtags},
98 /* layout.c */
99 {"setlayout", uicb_setlayout},
100 {"focusnext", uicb_focusnext},
101 {"focusprev", uicb_focusprev},
102 {"togglemax", uicb_togglemax},
103 {"toggleverticalmax", uicb_toggleverticalmax},
104 {"togglehorizontalmax", uicb_togglehorizontalmax},
105 {"zoom", uicb_zoom},
106 /* layouts/tile.c */
107 {"setmwfact", uicb_setmwfact},
108 {"setnmaster", uicb_setnmaster},
109 {"setncols", uicb_setncols},
110 /* awesome.c */
111 {"quit", uicb_quit},
112 /* statusbar.c */
113 {"togglebar", uicb_togglebar},
114 {NULL, NULL}
117 /** Lookup for a key mask from its name
118 * \param keyname Key name
119 * \return Key mask or 0 if not found
121 static KeySym
122 key_mask_lookup(const char *keyname)
124 int i;
126 if(keyname)
127 for(i = 0; KeyModList[i].name; i++)
128 if(!strcmp(keyname, KeyModList[i].name))
129 return KeyModList[i].keysym;
131 return 0;
134 /** Lookup for a function pointer from its name
135 * in the given NameFuncLink list
136 * \param funcname Function name
137 * \param list Function and name link list
138 * \return function pointer
140 static void *
141 name_func_lookup(const char *funcname, const NameFuncLink * list)
143 int i;
145 if(funcname && list)
146 for(i = 0; list[i].name; i++)
147 if(!strcmp(funcname, list[i].name))
148 return list[i].func;
150 return NULL;
153 /** Set default configuration
154 * \param awesomeconf awesome config ref
156 static void
157 set_default_config(awesome_config *awesomeconf)
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;
167 /** Parse configuration file and initialize some stuff
168 * \param disp Display ref
169 * \param scr Screen number
170 * \param drawcontext Draw context
172 void
173 parse_config(Display * disp, int scr, DC * drawcontext, awesome_config *awesomeconf)
175 /* Main configuration object for parsing*/
176 config_t awesomelibconf;
177 config_setting_t *conftags;
178 config_setting_t *conflayouts, *confsublayouts;
179 config_setting_t *confrules, *confsubrules;
180 config_setting_t *confkeys, *confsubkeys, *confkeysmasks, *confkeymaskelem;
181 int i = 0, j = 0;
182 double f = 0.0;
183 const char *tmp, *homedir;
184 char *confpath;
185 KeySym tmp_key;
187 set_default_config(awesomeconf);
189 homedir = getenv("HOME");
190 confpath = p_new(char, strlen(homedir) + strlen(AWESOME_CONFIG_FILE) + 2);
191 strcpy(confpath, homedir);
192 strcat(confpath, "/");
193 strcat(confpath, AWESOME_CONFIG_FILE);
195 config_init(&awesomelibconf);
197 /* set screen */
198 awesomeconf->screen = scr;
200 if(config_read_file(&awesomelibconf, confpath) == CONFIG_FALSE)
201 eprint("awesome: error parsing configuration file at line %d: %s\n",
202 config_error_line(&awesomelibconf), config_error_text(&awesomelibconf));
204 /* font */
205 tmp = config_lookup_string(&awesomelibconf, "awesome.font");
206 initfont(tmp ? tmp : "-*-fixed-medium-r-normal-*-13-*-*-*-*-*-*-*", disp, drawcontext);
208 /* layouts */
209 conflayouts = config_lookup(&awesomelibconf, "awesome.layouts");
211 if(!conflayouts)
212 fprintf(stderr, "layouts not found in configuration file\n");
213 else
215 awesomeconf->nlayouts = config_setting_length(conflayouts);
216 awesomeconf->layouts = p_new(Layout, awesomeconf->nlayouts + 1);
217 for(i = 0; (confsublayouts = config_setting_get_elem(conflayouts, i)); i++)
219 awesomeconf->layouts[i].arrange =
220 name_func_lookup(config_setting_get_string_elem(confsublayouts, 1), LayoutsList);
221 if(!awesomeconf->layouts[i].arrange)
223 fprintf(stderr, "awesome: unknown layout #%d in configuration file\n", i);
224 awesomeconf->layouts[i].symbol = NULL;
225 continue;
227 awesomeconf->layouts[i].symbol = a_strdup(config_setting_get_string_elem(confsublayouts, 0));
229 j = textw(drawcontext->font.set, drawcontext->font.xfont, awesomeconf->layouts[i].symbol, drawcontext->font.height);
230 if(j > awesomeconf->statusbar.width)
231 awesomeconf->statusbar.width = j;
233 awesomeconf->layouts[i].symbol = NULL;
234 awesomeconf->layouts[i].arrange = NULL;
238 if(!awesomeconf->layouts[0].arrange)
239 eprint("awesome: fatal: no default layout available\n");
240 /** \todo put this in set_default_layout */
241 awesomeconf->current_layout = awesomeconf->layouts;
243 /* tags */
244 conftags = config_lookup(&awesomelibconf, "awesome.tags");
246 if(!conftags)
247 eprint("awesome: fatal: no tags found in configuration file\n");
248 awesomeconf->ntags = config_setting_length(conftags);
249 awesomeconf->tags = p_new(char *, awesomeconf->ntags);
250 awesomeconf->selected_tags = p_new(Bool, awesomeconf->ntags);
251 awesomeconf->prev_selected_tags = p_new(Bool, awesomeconf->ntags);
252 awesomeconf->tag_layouts = p_new(Layout *, awesomeconf->ntags);
254 for(i = 0; (tmp = config_setting_get_string_elem(conftags, i)); i++)
256 awesomeconf->tags[i] = a_strdup(tmp);
257 awesomeconf->selected_tags[i] = False;
258 awesomeconf->prev_selected_tags[i] = False;
259 /** \todo add support for default tag/layout in configuration file */
260 awesomeconf->tag_layouts[i] = awesomeconf->layouts;
263 /* select first tag by default */
264 awesomeconf->selected_tags[0] = True;
265 awesomeconf->prev_selected_tags[0] = True;
267 /* rules */
268 confrules = config_lookup(&awesomelibconf, "awesome.rules");
270 if(!confrules)
271 fprintf(stderr, "awesome: no rules found in configuration file\n");
272 else
274 awesomeconf->nrules = config_setting_length(confrules);
275 awesomeconf->rules = p_new(Rule, awesomeconf->nrules);
276 for(i = 0; (confsubrules = config_setting_get_elem(confrules, i)); i++)
278 awesomeconf->rules[i].prop = a_strdup(config_setting_get_string(config_setting_get_member(confsubrules, "name")));
279 awesomeconf->rules[i].tags = a_strdup(config_setting_get_string(config_setting_get_member(confsubrules, "tags")));
280 if(awesomeconf->rules[i].tags && !strlen(awesomeconf->rules[i].tags))
281 awesomeconf->rules[i].tags = NULL;
282 awesomeconf->rules[i].isfloating =
283 config_setting_get_bool(config_setting_get_member(confsubrules, "float"));
287 /* modkey */
288 tmp_key = key_mask_lookup(config_lookup_string(&awesomelibconf, "awesome.modkey"));
289 awesomeconf->modkey = tmp_key ? tmp_key : Mod1Mask;
291 /* find numlock mask */
292 awesomeconf->numlockmask = get_numlockmask(disp);
294 /* keys */
295 confkeys = config_lookup(&awesomelibconf, "awesome.keys");
297 if(!confkeys)
298 fprintf(stderr, "awesome: no keys found in configuration file\n");
299 else
301 awesomeconf->nkeys = config_setting_length(confkeys);
302 awesomeconf->keys = p_new(Key, awesomeconf->nkeys);
304 for(i = 0; (confsubkeys = config_setting_get_elem(confkeys, i)); i++)
306 confkeysmasks = config_setting_get_elem(confsubkeys, 0);
307 for(j = 0; (confkeymaskelem = config_setting_get_elem(confkeysmasks, j)); j++)
308 awesomeconf->keys[i].mod |= key_mask_lookup(config_setting_get_string(confkeymaskelem));
309 awesomeconf->keys[i].keysym = XStringToKeysym(config_setting_get_string_elem(confsubkeys, 1));
310 awesomeconf->keys[i].func =
311 name_func_lookup(config_setting_get_string_elem(confsubkeys, 2), KeyfuncList);
312 awesomeconf->keys[i].arg = a_strdup(config_setting_get_string_elem(confsubkeys, 3));
316 /* barpos */
317 tmp = config_lookup_string(&awesomelibconf, "awesome.barpos");
319 if(tmp && !strncmp(tmp, "off", 6))
320 awesomeconf->statusbar_default_position = BarOff;
321 else if(tmp && !strncmp(tmp, "bottom", 6))
322 awesomeconf->statusbar_default_position = BarBot;
323 else
324 awesomeconf->statusbar_default_position = BarTop;
326 awesomeconf->statusbar.position = awesomeconf->statusbar_default_position;
328 /* borderpx */
329 awesomeconf->borderpx = config_lookup_int(&awesomelibconf, "awesome.borderpx");
331 /* opacity */
332 awesomeconf->opacity_unfocused = config_lookup_int(&awesomelibconf, "awesome.opacity_unfocused");
333 if(awesomeconf->opacity_unfocused >= 100)
334 awesomeconf->opacity_unfocused = -1;
336 /* snap */
337 i = config_lookup_int(&awesomelibconf, "awesome.snap");
338 awesomeconf->snap = i ? i : 8;
340 /* nmaster */
341 i = config_lookup_int(&awesomelibconf, "awesome.nmaster");
342 awesomeconf->nmaster = i ? i : 1;
344 /* ncols */
345 i = config_lookup_int(&awesomelibconf, "awesome.ncols");
346 awesomeconf->ncols = i ? i : 1;
348 /* mwfact */
349 f = config_lookup_float(&awesomelibconf, "awesome.mwfact");
350 awesomeconf->mwfact = f ? f : 0.6;
352 /* resize_hints */
353 awesomeconf->resize_hints = config_lookup_float(&awesomelibconf, "awesome.resize_hints");
355 /* colors */
356 tmp = config_lookup_string(&awesomelibconf, "awesome.normal_border_color");
357 drawcontext->norm[ColBorder] = initcolor(tmp ? tmp : "#dddddd", disp, scr);
359 tmp = config_lookup_string(&awesomelibconf, "awesome.normal_bg_color");
360 drawcontext->norm[ColBG] = initcolor(tmp ? tmp : "#000000", disp, scr);
362 tmp = config_lookup_string(&awesomelibconf, "awesome.normal_fg_color");
363 drawcontext->norm[ColFG] = initcolor(tmp ? tmp : "#ffffff", disp, scr);
365 tmp = config_lookup_string(&awesomelibconf, "awesome.focus_border_color");
366 drawcontext->sel[ColBorder] = initcolor(tmp ? tmp : "#008b8b", disp, scr);
368 tmp = config_lookup_string(&awesomelibconf, "awesome.focus_bg_color");
369 drawcontext->sel[ColBG] = initcolor(tmp ? tmp : "#008b8b", disp, scr);
371 tmp = config_lookup_string(&awesomelibconf, "awesome.focus_fg_color");
372 drawcontext->sel[ColFG] = initcolor(tmp ? tmp : "#ffffff", disp, scr);
374 config_destroy(&awesomelibconf);
375 p_delete(&confpath);
378 /** Initialize font from X side and store in draw context
379 * \param fontstr Font name
380 * \param disp Display ref
381 * \param drawcontext Draw context
383 static void
384 initfont(const char *fontstr, Display * disp, DC * drawcontext)
386 char *def, **missing;
387 int i, n;
389 missing = NULL;
390 if(drawcontext->font.set)
391 XFreeFontSet(disp, drawcontext->font.set);
392 drawcontext->font.set = XCreateFontSet(disp, fontstr, &missing, &n, &def);
393 if(missing)
395 while(n--)
396 fprintf(stderr, "awesome: missing fontset: %s\n", missing[n]);
397 XFreeStringList(missing);
399 if(drawcontext->font.set)
401 XFontSetExtents *font_extents;
402 XFontStruct **xfonts;
403 char **font_names;
404 drawcontext->font.ascent = drawcontext->font.descent = 0;
405 font_extents = XExtentsOfFontSet(drawcontext->font.set);
406 n = XFontsOfFontSet(drawcontext->font.set, &xfonts, &font_names);
407 for(i = 0, drawcontext->font.ascent = 0, drawcontext->font.descent = 0; i < n; i++)
409 if(drawcontext->font.ascent < (*xfonts)->ascent)
410 drawcontext->font.ascent = (*xfonts)->ascent;
411 if(drawcontext->font.descent < (*xfonts)->descent)
412 drawcontext->font.descent = (*xfonts)->descent;
413 xfonts++;
416 else
418 if(drawcontext->font.xfont)
419 XFreeFont(disp, drawcontext->font.xfont);
420 drawcontext->font.xfont = NULL;
421 if(!(drawcontext->font.xfont = XLoadQueryFont(disp, fontstr))
422 || !(drawcontext->font.xfont = XLoadQueryFont(disp, "fixed")))
423 eprint("error, cannot load font: '%s'\n", fontstr);
424 drawcontext->font.ascent = drawcontext->font.xfont->ascent;
425 drawcontext->font.descent = drawcontext->font.xfont->descent;
427 drawcontext->font.height = drawcontext->font.ascent + drawcontext->font.descent;
430 static unsigned int
431 get_numlockmask(Display *disp)
433 XModifierKeymap *modmap;
434 unsigned int mask = 0;
435 int i, j;
437 modmap = XGetModifierMapping(disp);
438 for(i = 0; i < 8; i++)
439 for(j = 0; j < modmap->max_keypermod; j++)
441 if(modmap->modifiermap[i * modmap->max_keypermod + j]
442 == XKeysymToKeycode(disp, XK_Num_Lock))
443 mask = (1 << i);
446 XFreeModifiermap(modmap);
448 return mask;
451 /** Initialize color from X side
452 * \param colorstr Color code
453 * \param disp Display ref
454 * \param scr Screen number
455 * \return XColor pixel
457 static unsigned long
458 initcolor(const char *colstr, Display * disp, int scr)
460 Colormap cmap = DefaultColormap(disp, scr);
461 XColor color;
462 if(!XAllocNamedColor(disp, cmap, colstr, &color, &color))
463 eprint("error, cannot allocate color '%s'\n", colstr);
464 return color.pixel;