get_display_info take screen and screen is stored in statusbar struct
[awesome.git] / config.c
bloba724920a381a9f04b60460a5062ec0e5772e5d85
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 */
41 static void initfont(const char *, Display *, DC *);
42 static unsigned long initcolor(const char *colstr, Display *, int);
43 static unsigned int get_numlockmask(Display *);
45 /** Main configuration object for parsing*/
46 config_t awesomelibconf;
48 /** Link a name to a function */
49 typedef struct
51 const char *name;
52 void *func;
53 } NameFuncLink;
55 /** Link a name to a key symbol */
56 typedef struct
58 const char *name;
59 KeySym keysym;
60 } KeyMod;
62 /** List of keyname and corresponding X11 mask codes */
63 static const KeyMod KeyModList[] =
65 {"Shift", ShiftMask},
66 {"Lock", LockMask},
67 {"Control", ControlMask},
68 {"Mod1", Mod1Mask},
69 {"Mod2", Mod2Mask},
70 {"Mod3", Mod3Mask},
71 {"Mod4", Mod4Mask},
72 {"Mod5", Mod5Mask},
73 {"None", 0}
76 /** List of available layouts and link between name and functions */
77 static const NameFuncLink LayoutsList[] =
79 {"tile", tile},
80 {"tileleft", tileleft},
81 {"floating", floating},
82 {"spiral", spiral},
83 {"dwindle", dwindle},
84 {NULL, NULL}
87 /** List of available UI bindable callbacks and functions */
88 static const NameFuncLink KeyfuncList[] = {
89 /* util.c */
90 {"spawn", uicb_spawn},
91 /* client.c */
92 {"killclient", uicb_killclient},
93 {"moveresize", uicb_moveresize},
94 {"settrans", uicb_settrans},
95 /* tag.c */
96 {"tag", uicb_tag},
97 {"togglefloating", uicb_togglefloating},
98 {"toggleview", uicb_toggleview},
99 {"toggletag", uicb_toggletag},
100 {"view", uicb_view},
101 {"viewprevtags", uicb_viewprevtags},
102 /* layout.c */
103 {"setlayout", uicb_setlayout},
104 {"focusnext", uicb_focusnext},
105 {"focusprev", uicb_focusprev},
106 {"togglemax", uicb_togglemax},
107 {"toggleverticalmax", uicb_toggleverticalmax},
108 {"togglehorizontalmax", uicb_togglehorizontalmax},
109 {"zoom", uicb_zoom},
110 /* layouts/tile.c */
111 {"setmwfact", uicb_setmwfact},
112 {"setnmaster", uicb_setnmaster},
113 {"setncols", uicb_setncols},
114 /* awesome.c */
115 {"quit", uicb_quit},
116 /* statusbar.c */
117 {"togglebar", uicb_togglebar},
118 {NULL, NULL}
121 /** Lookup for a key mask from its name
122 * \param keyname Key name
123 * \return Key mask or 0 if not found
125 static KeySym
126 key_mask_lookup(const char *keyname)
128 int i;
130 if(keyname)
131 for(i = 0; KeyModList[i].name; i++)
132 if(!strcmp(keyname, KeyModList[i].name))
133 return KeyModList[i].keysym;
135 return 0;
138 /** Lookup for a function pointer from its name
139 * in the given NameFuncLink list
140 * \param funcname Function name
141 * \param list Function and name link list
142 * \return function pointer
144 static void *
145 name_func_lookup(const char *funcname, const NameFuncLink * list)
147 int i;
149 if(funcname && list)
150 for(i = 0; list[i].name; i++)
151 if(!strcmp(funcname, list[i].name))
152 return list[i].func;
154 return NULL;
157 /** Set default configuration
158 * \param awesomeconf awesome config ref
160 static void
161 set_default_config(awesome_config *awesomeconf)
163 strcpy(awesomeconf->statustext, "awesome-" VERSION);
164 awesomeconf->statusbar.width = 0;
165 awesomeconf->statusbar.height = 0;
166 awesomeconf->opacity_unfocused = -1;
167 awesomeconf->nkeys = 0;
168 awesomeconf->nrules = 0;
171 /** Parse configuration file and initialize some stuff
172 * \param disp Display ref
173 * \param scr Screen number
174 * \param drawcontext Draw context
176 void
177 parse_config(Display * disp, int scr, DC * drawcontext, awesome_config *awesomeconf)
179 config_setting_t *conftags;
180 config_setting_t *conflayouts, *confsublayouts;
181 config_setting_t *confrules, *confsubrules;
182 config_setting_t *confkeys, *confsubkeys, *confkeysmasks, *confkeymaskelem;
183 int i = 0, j = 0;
184 double f = 0.0;
185 const char *tmp, *homedir;
186 char *confpath;
187 KeySym tmp_key;
189 set_default_config(awesomeconf);
191 homedir = getenv("HOME");
192 confpath = p_new(char, strlen(homedir) + strlen(AWESOME_CONFIG_FILE) + 2);
193 strcpy(confpath, homedir);
194 strcat(confpath, "/");
195 strcat(confpath, AWESOME_CONFIG_FILE);
197 config_init(&awesomelibconf);
199 if(config_read_file(&awesomelibconf, confpath) == CONFIG_FALSE)
200 eprint("awesome: error parsing configuration file at line %d: %s\n",
201 config_error_line(&awesomelibconf), config_error_text(&awesomelibconf));
203 /* font */
204 tmp = config_lookup_string(&awesomelibconf, "awesome.font");
205 initfont(tmp ? tmp : "-*-fixed-medium-r-normal-*-13-*-*-*-*-*-*-*", disp, drawcontext);
207 /* layouts */
208 conflayouts = config_lookup(&awesomelibconf, "awesome.layouts");
210 if(!conflayouts)
211 fprintf(stderr, "layouts not found in configuration file\n");
212 else
214 awesomeconf->nlayouts = config_setting_length(conflayouts);
215 awesomeconf->layouts = p_new(Layout, awesomeconf->nlayouts + 1);
216 for(i = 0; (confsublayouts = config_setting_get_elem(conflayouts, i)); i++)
218 awesomeconf->layouts[i].arrange =
219 name_func_lookup(config_setting_get_string_elem(confsublayouts, 1), LayoutsList);
220 if(!awesomeconf->layouts[i].arrange)
222 fprintf(stderr, "awesome: unknown layout #%d in configuration file\n", i);
223 awesomeconf->layouts[i].symbol = NULL;
224 continue;
226 awesomeconf->layouts[i].symbol = config_setting_get_string_elem(confsublayouts, 0);
228 j = textw(drawcontext->font.set, drawcontext->font.xfont, awesomeconf->layouts[i].symbol, drawcontext->font.height);
229 if(j > awesomeconf->statusbar.width)
230 awesomeconf->statusbar.width = j;
232 awesomeconf->layouts[i].symbol = NULL;
233 awesomeconf->layouts[i].arrange = NULL;
237 if(!awesomeconf->layouts[0].arrange)
238 eprint("awesome: fatal: no default layout available\n");
239 /** \todo put this in set_default_layout */
240 awesomeconf->current_layout = awesomeconf->layouts;
242 /* tags */
243 conftags = config_lookup(&awesomelibconf, "awesome.tags");
245 if(!conftags)
246 eprint("awesome: fatal: no tags found in configuration file\n");
247 awesomeconf->ntags = config_setting_length(conftags);
248 awesomeconf->tags = p_new(const char *, awesomeconf->ntags);
249 awesomeconf->selected_tags = p_new(Bool, awesomeconf->ntags);
250 awesomeconf->prev_selected_tags = p_new(Bool, awesomeconf->ntags);
251 awesomeconf->tag_layouts = p_new(Layout *, awesomeconf->ntags);
253 for(i = 0; (tmp = config_setting_get_string_elem(conftags, i)); i++)
255 awesomeconf->tags[i] = tmp;
256 awesomeconf->selected_tags[i] = False;
257 awesomeconf->prev_selected_tags[i] = False;
258 /** \todo add support for default tag/layout in configuration file */
259 awesomeconf->tag_layouts[i] = awesomeconf->layouts;
262 /* select first tag by default */
263 awesomeconf->selected_tags[0] = True;
264 awesomeconf->prev_selected_tags[0] = True;
266 /* rules */
267 confrules = config_lookup(&awesomelibconf, "awesome.rules");
269 if(!confrules)
270 fprintf(stderr, "awesome: no rules found in configuration file\n");
271 else
273 awesomeconf->nrules = config_setting_length(confrules);
274 awesomeconf->rules = p_new(Rule, awesomeconf->nrules);
275 for(i = 0; (confsubrules = config_setting_get_elem(confrules, i)); i++)
277 awesomeconf->rules[i].prop = config_setting_get_string(config_setting_get_member(confsubrules, "name"));
278 awesomeconf->rules[i].tags = config_setting_get_string(config_setting_get_member(confsubrules, "tags"));
279 if(awesomeconf->rules[i].tags && !strlen(awesomeconf->rules[i].tags))
280 awesomeconf->rules[i].tags = NULL;
281 awesomeconf->rules[i].isfloating =
282 config_setting_get_bool(config_setting_get_member(confsubrules, "float"));
286 /* modkey */
287 tmp_key = key_mask_lookup(config_lookup_string(&awesomelibconf, "awesome.modkey"));
288 awesomeconf->modkey = tmp_key ? tmp_key : Mod1Mask;
290 /* find numlock mask */
291 awesomeconf->numlockmask = get_numlockmask(disp);
293 /* keys */
294 confkeys = config_lookup(&awesomelibconf, "awesome.keys");
296 if(!confkeys)
297 fprintf(stderr, "awesome: no keys found in configuration file\n");
298 else
300 awesomeconf->nkeys = config_setting_length(confkeys);
301 awesomeconf->keys = p_new(Key, awesomeconf->nkeys);
303 for(i = 0; (confsubkeys = config_setting_get_elem(confkeys, i)); i++)
305 confkeysmasks = config_setting_get_elem(confsubkeys, 0);
306 for(j = 0; (confkeymaskelem = config_setting_get_elem(confkeysmasks, j)); j++)
307 awesomeconf->keys[i].mod |= key_mask_lookup(config_setting_get_string(confkeymaskelem));
308 awesomeconf->keys[i].keysym = XStringToKeysym(config_setting_get_string_elem(confsubkeys, 1));
309 awesomeconf->keys[i].func =
310 name_func_lookup(config_setting_get_string_elem(confsubkeys, 2), KeyfuncList);
311 awesomeconf->keys[i].arg = config_setting_get_string_elem(confsubkeys, 3);
315 /* barpos */
316 tmp = config_lookup_string(&awesomelibconf, "awesome.barpos");
318 if(tmp && !strncmp(tmp, "off", 6))
319 awesomeconf->statusbar_default_position = BarOff;
320 else if(tmp && !strncmp(tmp, "bottom", 6))
321 awesomeconf->statusbar_default_position = BarBot;
322 else
323 awesomeconf->statusbar_default_position = BarTop;
325 awesomeconf->statusbar.position = awesomeconf->statusbar_default_position;
327 /* borderpx */
328 awesomeconf->borderpx = config_lookup_int(&awesomelibconf, "awesome.borderpx");
330 /* opacity */
331 awesomeconf->opacity_unfocused = config_lookup_int(&awesomelibconf, "awesome.opacity_unfocused");
332 if(awesomeconf->opacity_unfocused >= 100)
333 awesomeconf->opacity_unfocused = -1;
335 /* snap */
336 i = config_lookup_int(&awesomelibconf, "awesome.snap");
337 awesomeconf->snap = i ? i : 8;
339 /* nmaster */
340 i = config_lookup_int(&awesomelibconf, "awesome.nmaster");
341 awesomeconf->nmaster = i ? i : 1;
343 /* ncols */
344 i = config_lookup_int(&awesomelibconf, "awesome.ncols");
345 awesomeconf->ncols = i ? i : 1;
347 /* mwfact */
348 f = config_lookup_float(&awesomelibconf, "awesome.mwfact");
349 awesomeconf->mwfact = f ? f : 0.6;
351 /* resize_hints */
352 awesomeconf->resize_hints = config_lookup_float(&awesomelibconf, "awesome.resize_hints");
354 /* colors */
355 tmp = config_lookup_string(&awesomelibconf, "awesome.normal_border_color");
356 drawcontext->norm[ColBorder] = initcolor(tmp ? tmp : "#dddddd", disp, scr);
358 tmp = config_lookup_string(&awesomelibconf, "awesome.normal_bg_color");
359 drawcontext->norm[ColBG] = initcolor(tmp ? tmp : "#000000", disp, scr);
361 tmp = config_lookup_string(&awesomelibconf, "awesome.normal_fg_color");
362 drawcontext->norm[ColFG] = initcolor(tmp ? tmp : "#ffffff", disp, scr);
364 tmp = config_lookup_string(&awesomelibconf, "awesome.focus_border_color");
365 drawcontext->sel[ColBorder] = initcolor(tmp ? tmp : "#008b8b", disp, scr);
367 tmp = config_lookup_string(&awesomelibconf, "awesome.focus_bg_color");
368 drawcontext->sel[ColBG] = initcolor(tmp ? tmp : "#008b8b", disp, scr);
370 tmp = config_lookup_string(&awesomelibconf, "awesome.focus_fg_color");
371 drawcontext->sel[ColFG] = initcolor(tmp ? tmp : "#ffffff", disp, scr);
373 p_delete(&confpath);
376 /** Initialize font from X side and store in draw context
377 * \param fontstr Font name
378 * \param disp Display ref
379 * \param drawcontext Draw context
381 static void
382 initfont(const char *fontstr, Display * disp, DC * drawcontext)
384 char *def, **missing;
385 int i, n;
387 missing = NULL;
388 if(drawcontext->font.set)
389 XFreeFontSet(disp, drawcontext->font.set);
390 drawcontext->font.set = XCreateFontSet(disp, fontstr, &missing, &n, &def);
391 if(missing)
393 while(n--)
394 fprintf(stderr, "awesome: missing fontset: %s\n", missing[n]);
395 XFreeStringList(missing);
397 if(drawcontext->font.set)
399 XFontSetExtents *font_extents;
400 XFontStruct **xfonts;
401 char **font_names;
402 drawcontext->font.ascent = drawcontext->font.descent = 0;
403 font_extents = XExtentsOfFontSet(drawcontext->font.set);
404 n = XFontsOfFontSet(drawcontext->font.set, &xfonts, &font_names);
405 for(i = 0, drawcontext->font.ascent = 0, drawcontext->font.descent = 0; i < n; i++)
407 if(drawcontext->font.ascent < (*xfonts)->ascent)
408 drawcontext->font.ascent = (*xfonts)->ascent;
409 if(drawcontext->font.descent < (*xfonts)->descent)
410 drawcontext->font.descent = (*xfonts)->descent;
411 xfonts++;
414 else
416 if(drawcontext->font.xfont)
417 XFreeFont(disp, drawcontext->font.xfont);
418 drawcontext->font.xfont = NULL;
419 if(!(drawcontext->font.xfont = XLoadQueryFont(disp, fontstr))
420 || !(drawcontext->font.xfont = XLoadQueryFont(disp, "fixed")))
421 eprint("error, cannot load font: '%s'\n", fontstr);
422 drawcontext->font.ascent = drawcontext->font.xfont->ascent;
423 drawcontext->font.descent = drawcontext->font.xfont->descent;
425 drawcontext->font.height = drawcontext->font.ascent + drawcontext->font.descent;
428 static unsigned int
429 get_numlockmask(Display *disp)
431 XModifierKeymap *modmap;
432 unsigned int mask = 0;
433 int i, j;
435 modmap = XGetModifierMapping(disp);
436 for(i = 0; i < 8; i++)
437 for(j = 0; j < modmap->max_keypermod; j++)
439 if(modmap->modifiermap[i * modmap->max_keypermod + j]
440 == XKeysymToKeycode(disp, XK_Num_Lock))
441 mask = (1 << i);
444 XFreeModifiermap(modmap);
446 return mask;
449 /** Initialize color from X side
450 * \param colorstr Color code
451 * \param disp Display ref
452 * \param scr Screen number
453 * \return XColor pixel
455 static unsigned long
456 initcolor(const char *colstr, Display * disp, int scr)
458 Colormap cmap = DefaultColormap(disp, scr);
459 XColor color;
460 if(!XAllocNamedColor(disp, cmap, colstr, &color, &color))
461 eprint("error, cannot allocate color '%s'\n", colstr);
462 return color.pixel;