awesome/config.c

432 lines
14 KiB
C
Raw Normal View History

2007-09-05 20:15:00 +02:00
/* See LICENSE file for copyright and license details. */
/**
* \defgroup ui_callback
*/
#include <stdlib.h>
#include <libconfig.h>
#include <X11/keysym.h>
2007-09-10 12:06:54 +02:00
#include "awesome.h"
2007-09-05 20:15:00 +02:00
#include "util.h"
#include "layout.h"
#include "tag.h"
#include "layouts/tile.h"
#include "layouts/grid.h"
#include "layouts/spiral.h"
#include "layouts/floating.h"
/* static */
static void initfont(const char *, Display *, DC *);
static unsigned long initcolor(const char *colstr, Display *, int);
static unsigned int get_numlockmask(Display *);
/** Main configuration object for parsing*/
2007-09-10 12:06:54 +02:00
config_t awesomelibconf;
2007-09-05 20:15:00 +02:00
/** Link a name to a function */
typedef struct
{
const char *name;
void *func;
} NameFuncLink;
/** Link a name to a key symbol */
typedef struct
{
const char *name;
KeySym keysym;
} KeyMod;
/** List of keyname and corresponding X11 mask codes */
2007-09-10 12:02:45 +02:00
static const KeyMod KeyModList[] =
{
{"Shift", ShiftMask},
{"Lock", LockMask},
{"Control", ControlMask},
{"Mod1", Mod1Mask},
{"Mod2", Mod2Mask},
{"Mod3", Mod3Mask},
{"Mod4", Mod4Mask},
{"Mod5", Mod5Mask},
{"None", 0}
2007-09-05 20:15:00 +02:00
};
/** List of available layouts and link between name and functions */
2007-09-10 12:02:45 +02:00
static const NameFuncLink LayoutsList[] =
{
{"tile", tile},
{"tileleft", tileleft},
{"floating", floating},
{"grid", grid},
{"spiral", spiral},
{"dwindle", dwindle},
{"bstack", bstack},
{"bstackportrait", bstackportrait},
{NULL, NULL}
2007-09-05 20:15:00 +02:00
};
/** List of available UI bindable callbacks and functions */
static const NameFuncLink KeyfuncList[] = {
/* util.c */
{"spawn", spawn},
/* client.c */
{"killclient", uicb_killclient},
{"moveresize", uicb_moveresize},
{"settrans", uicb_settrans},
/* config.c */
{"reload", uicb_reload},
/* tag.c */
{"tag", uicb_tag},
{"togglefloating", uicb_togglefloating},
{"toggleview", uicb_toggleview},
{"toggletag", uicb_toggletag},
{"view", uicb_view},
{"viewprevtags", uicb_viewprevtags},
/* layout.c */
{"setlayout", uicb_setlayout},
{"togglebar", uicb_togglebar},
{"focusnext", uicb_focusnext},
{"focusprev", uicb_focusprev},
{"togglemax", uicb_togglemax},
{"toggleverticalmax", uicb_toggleverticalmax},
{"togglehorizontalmax", uicb_togglehorizontalmax},
{"zoom", uicb_zoom},
/* layouts/tile.c */
{"setmwfact", uicb_setmwfact},
{"incnmaster", uicb_incnmaster},
2007-09-10 12:06:54 +02:00
/* awesome.c */
2007-09-05 20:15:00 +02:00
{"quit", uicb_quit},
{NULL, NULL}
};
/** Lookup for a key mask from its name
* \param keyname Key name
* \return Key mask or 0 if not found
*/
static KeySym
key_mask_lookup(const char *keyname)
{
int i;
if(keyname)
for(i = 0; KeyModList[i].name; i++)
if(!strcmp(keyname, KeyModList[i].name))
return KeyModList[i].keysym;
return 0;
2007-09-06 23:29:50 +02:00
}
2007-09-05 20:15:00 +02:00
/** Lookup for a function pointer from its name
* in the given NameFuncLink list
* \param funcname Function name
* \param list Function and name link list
* \return function pointer
*/
static void *
name_func_lookup(const char *funcname, const NameFuncLink * list)
{
int i;
if(funcname && list)
for(i = 0; list[i].name; i++)
if(!strcmp(funcname, list[i].name))
return list[i].func;
return NULL;
}
/** \todo remove dc */
extern DC dc;
/** Reload configuration file
* \param disp Display ref
* \param arg unused
* \ingroup ui_callback
* \todo not really working nor safe I guess
*/
void
2007-09-10 12:06:54 +02:00
uicb_reload(Display *disp, awesome_config *awesomeconf, const char *arg __attribute__ ((unused)))
2007-09-05 20:15:00 +02:00
{
2007-09-10 12:06:54 +02:00
config_destroy(&awesomelibconf);
p_delete(&awesomeconf->rules);
p_delete(&awesomeconf->tags);
p_delete(&awesomeconf->selected_tags);
p_delete(&awesomeconf->layouts);
p_delete(&awesomeconf->tag_layouts);
parse_config(disp, DefaultScreen(disp), &dc, awesomeconf);
2007-09-05 20:15:00 +02:00
}
2007-09-07 14:59:20 +02:00
/** Set default configuration
2007-09-10 12:06:54 +02:00
* \param awesomeconf awesome config ref
2007-09-07 14:59:20 +02:00
*/
2007-09-05 20:15:00 +02:00
static void
2007-09-10 12:06:54 +02:00
set_default_config(awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
2007-09-10 12:06:54 +02:00
strcpy(awesomeconf->statustext, "awesome-" VERSION);
awesomeconf->statusbar.width = 0;
awesomeconf->statusbar.height = 0;
2007-09-05 20:15:00 +02:00
}
/** Parse configuration file and initialize some stuff
* \param disp Display ref
* \param scr Screen number
* \param drawcontext Draw context
*/
void
2007-09-10 12:06:54 +02:00
parse_config(Display * disp, int scr, DC * drawcontext, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
config_setting_t *conftags;
config_setting_t *conflayouts, *confsublayouts;
config_setting_t *confrules, *confsubrules;
config_setting_t *confkeys, *confsubkeys, *confkeysmasks, *confkeymaskelem;
int i, j;
const char *tmp, *homedir;
char *confpath;
2007-09-10 12:06:54 +02:00
set_default_config(awesomeconf);
2007-09-05 20:15:00 +02:00
homedir = getenv("HOME");
confpath = p_new(char, strlen(homedir) + strlen(JDWM_CONFIG_FILE) + 2);
strcpy(confpath, homedir);
strcat(confpath, "/");
strcat(confpath, JDWM_CONFIG_FILE);
2007-09-10 12:06:54 +02:00
config_init(&awesomelibconf);
2007-09-05 20:15:00 +02:00
2007-09-10 12:06:54 +02:00
if(config_read_file(&awesomelibconf, confpath) == CONFIG_FALSE)
2007-09-05 20:15:00 +02:00
eprint("error parsing configuration file at line %d: %s\n",
2007-09-10 12:06:54 +02:00
config_error_line(&awesomelibconf), config_error_text(&awesomelibconf));
2007-09-05 20:15:00 +02:00
2007-09-07 15:04:02 +02:00
/* font */
2007-09-10 12:06:54 +02:00
initfont(config_lookup_string(&awesomelibconf, "awesome.font"), disp, drawcontext);
2007-09-07 15:04:02 +02:00
/* layouts */
2007-09-10 12:06:54 +02:00
conflayouts = config_lookup(&awesomelibconf, "awesome.layouts");
if(!conflayouts)
eprint("layouts not found in configuration file\n");
2007-09-10 12:06:54 +02:00
awesomeconf->nlayouts = config_setting_length(conflayouts);
awesomeconf->layouts = p_new(Layout, awesomeconf->nlayouts + 1);
for(i = 0; (confsublayouts = config_setting_get_elem(conflayouts, i)); i++)
{
2007-09-10 12:06:54 +02:00
awesomeconf->layouts[i].symbol = config_setting_get_string_elem(confsublayouts, 0);
awesomeconf->layouts[i].arrange =
name_func_lookup(config_setting_get_string_elem(confsublayouts, 1), LayoutsList);
2007-09-10 12:06:54 +02:00
if(!awesomeconf->layouts[i].arrange)
eprint("unknown layout in configuration file\n");
2007-09-07 15:04:02 +02:00
2007-09-10 12:06:54 +02:00
j = textw(awesomeconf->layouts[i].symbol);
if(j > awesomeconf->statusbar.width)
awesomeconf->statusbar.width = j;
}
2007-09-10 12:06:54 +02:00
awesomeconf->layouts[i].symbol = NULL;
awesomeconf->layouts[i].arrange = NULL;
/** \todo put this in set_default_layout */
2007-09-10 12:06:54 +02:00
awesomeconf->current_layout = awesomeconf->layouts;
2007-09-05 20:15:00 +02:00
/* tags */
2007-09-10 12:06:54 +02:00
conftags = config_lookup(&awesomelibconf, "awesome.tags");
2007-09-05 20:15:00 +02:00
if(!conftags)
eprint("tags not found in configuration file\n");
2007-09-10 12:06:54 +02:00
awesomeconf->ntags = config_setting_length(conftags);
awesomeconf->tags = p_new(const char *, awesomeconf->ntags);
awesomeconf->selected_tags = p_new(Bool, awesomeconf->ntags);
awesomeconf->prev_selected_tags = p_new(Bool, awesomeconf->ntags);
awesomeconf->tag_layouts = p_new(Layout *, awesomeconf->ntags);
2007-09-05 20:15:00 +02:00
for(i = 0; (tmp = config_setting_get_string_elem(conftags, i)); i++)
{
2007-09-10 12:06:54 +02:00
awesomeconf->tags[i] = tmp;
awesomeconf->selected_tags[i] = False;
awesomeconf->prev_selected_tags[i] = False;
/** \todo add support for default tag/layout in configuration file */
2007-09-10 12:06:54 +02:00
awesomeconf->tag_layouts[i] = awesomeconf->layouts;
}
/* select first tag by default */
2007-09-10 12:06:54 +02:00
awesomeconf->selected_tags[0] = True;
awesomeconf->prev_selected_tags[0] = True;
2007-09-05 20:15:00 +02:00
/* rules */
2007-09-10 12:06:54 +02:00
confrules = config_lookup(&awesomelibconf, "awesome.rules");
2007-09-05 20:15:00 +02:00
if(!confrules)
eprint("rules not found in configuration file\n");
2007-09-10 12:06:54 +02:00
awesomeconf->nrules = config_setting_length(confrules);
awesomeconf->rules = p_new(Rule, awesomeconf->nrules);
2007-09-05 20:15:00 +02:00
for(i = 0; (confsubrules = config_setting_get_elem(confrules, i)); i++)
{
2007-09-10 12:06:54 +02:00
awesomeconf->rules[i].prop = config_setting_get_string(config_setting_get_member(confsubrules, "name"));
awesomeconf->rules[i].tags = config_setting_get_string(config_setting_get_member(confsubrules, "tags"));
if(awesomeconf->rules[i].tags && !strlen(awesomeconf->rules[i].tags))
awesomeconf->rules[i].tags = NULL;
awesomeconf->rules[i].isfloating =
2007-09-05 20:15:00 +02:00
config_setting_get_bool(config_setting_get_member(confsubrules, "float"));
}
/* modkey */
2007-09-10 12:06:54 +02:00
awesomeconf->modkey = key_mask_lookup(config_lookup_string(&awesomelibconf, "awesome.modkey"));
2007-09-05 20:15:00 +02:00
/* find numlock mask */
2007-09-10 12:06:54 +02:00
awesomeconf->numlockmask = get_numlockmask(disp);
2007-09-05 20:15:00 +02:00
/* keys */
2007-09-10 12:06:54 +02:00
confkeys = config_lookup(&awesomelibconf, "awesome.keys");
2007-09-05 20:15:00 +02:00
if(!confkeys)
eprint("keys not found in configuration file\n");
2007-09-10 12:06:54 +02:00
awesomeconf->nkeys = config_setting_length(confkeys);
awesomeconf->keys = p_new(Key, awesomeconf->nkeys);
2007-09-05 20:15:00 +02:00
for(i = 0; (confsubkeys = config_setting_get_elem(confkeys, i)); i++)
{
confkeysmasks = config_setting_get_elem(confsubkeys, 0);
for(j = 0; (confkeymaskelem = config_setting_get_elem(confkeysmasks, j)); j++)
2007-09-10 12:06:54 +02:00
awesomeconf->keys[i].mod |= key_mask_lookup(config_setting_get_string(confkeymaskelem));
awesomeconf->keys[i].keysym = XStringToKeysym(config_setting_get_string_elem(confsubkeys, 1));
awesomeconf->keys[i].func =
2007-09-05 20:15:00 +02:00
name_func_lookup(config_setting_get_string_elem(confsubkeys, 2), KeyfuncList);
2007-09-10 12:06:54 +02:00
awesomeconf->keys[i].arg = config_setting_get_string_elem(confsubkeys, 3);
2007-09-05 20:15:00 +02:00
}
/* barpos */
2007-09-10 12:06:54 +02:00
tmp = config_lookup_string(&awesomelibconf, "awesome.barpos");
2007-09-05 20:15:00 +02:00
if(!strncmp(tmp, "BarTop", 6))
2007-09-10 12:06:54 +02:00
awesomeconf->statusbar_default_position = BarTop;
2007-09-05 20:15:00 +02:00
else if(!strncmp(tmp, "BarBot", 6))
2007-09-10 12:06:54 +02:00
awesomeconf->statusbar_default_position = BarBot;
2007-09-05 20:15:00 +02:00
else if(!strncmp(tmp, "BarOff", 6))
2007-09-10 12:06:54 +02:00
awesomeconf->statusbar_default_position = BarOff;
2007-09-05 20:15:00 +02:00
2007-09-10 12:06:54 +02:00
awesomeconf->statusbar.position = awesomeconf->statusbar_default_position;
2007-09-05 20:15:00 +02:00
/* borderpx */
2007-09-10 12:06:54 +02:00
awesomeconf->borderpx = config_lookup_int(&awesomelibconf, "awesome.borderpx");
2007-09-05 20:15:00 +02:00
/* opacity */
2007-09-10 12:06:54 +02:00
awesomeconf->opacity_unfocused = config_lookup_int(&awesomelibconf, "awesome.opacity_unfocused");
if(awesomeconf->opacity_unfocused >= 100)
awesomeconf->opacity_unfocused = -1;
2007-09-05 20:15:00 +02:00
/* snap */
2007-09-10 12:06:54 +02:00
awesomeconf->snap = config_lookup_int(&awesomelibconf, "awesome.snap");
2007-09-05 20:15:00 +02:00
/* nmaster */
2007-09-10 12:06:54 +02:00
awesomeconf->nmaster = config_lookup_int(&awesomelibconf, "awesome.nmaster");
2007-09-05 20:15:00 +02:00
/* mwfact */
2007-09-10 12:06:54 +02:00
awesomeconf->mwfact = config_lookup_float(&awesomelibconf, "awesome.mwfact");
2007-09-05 20:15:00 +02:00
2007-09-10 12:01:36 +02:00
/* resize_hints */
2007-09-10 12:06:54 +02:00
awesomeconf->resize_hints = config_lookup_float(&awesomelibconf, "awesome.resize_hints");
2007-09-10 12:01:36 +02:00
2007-09-05 20:15:00 +02:00
/* colors */
2007-09-10 12:06:54 +02:00
dc.norm[ColBorder] = initcolor(config_lookup_string(&awesomelibconf, "awesome.normal_border_color"),
2007-09-05 20:15:00 +02:00
disp, scr);
2007-09-10 12:06:54 +02:00
dc.norm[ColBG] = initcolor(config_lookup_string(&awesomelibconf, "awesome.normal_bg_color"), disp, scr);
dc.norm[ColFG] = initcolor(config_lookup_string(&awesomelibconf, "awesome.normal_fg_color"), disp, scr);
dc.sel[ColBorder] = initcolor(config_lookup_string(&awesomelibconf, "awesome.focus_border_color"),
2007-09-05 20:15:00 +02:00
disp, scr);
2007-09-10 12:06:54 +02:00
dc.sel[ColBG] = initcolor(config_lookup_string(&awesomelibconf, "awesome.focus_bg_color"), disp, scr);
dc.sel[ColFG] = initcolor(config_lookup_string(&awesomelibconf, "awesome.focus_fg_color"), disp, scr);
2007-09-05 20:15:00 +02:00
p_delete(&confpath);
}
/** Initialize font from X side and store in draw context
* \param fontstr Font name
* \param disp Display ref
* \param drawcontext Draw context
*/
static void
initfont(const char *fontstr, Display * disp, DC * drawcontext)
{
char *def, **missing;
int i, n;
missing = NULL;
if(drawcontext->font.set)
XFreeFontSet(disp, drawcontext->font.set);
drawcontext->font.set = XCreateFontSet(disp, fontstr, &missing, &n, &def);
if(missing)
{
while(n--)
2007-09-10 12:06:54 +02:00
fprintf(stderr, "awesome: missing fontset: %s\n", missing[n]);
2007-09-05 20:15:00 +02:00
XFreeStringList(missing);
}
if(drawcontext->font.set)
{
XFontSetExtents *font_extents;
XFontStruct **xfonts;
char **font_names;
drawcontext->font.ascent = drawcontext->font.descent = 0;
font_extents = XExtentsOfFontSet(drawcontext->font.set);
n = XFontsOfFontSet(drawcontext->font.set, &xfonts, &font_names);
for(i = 0, drawcontext->font.ascent = 0, drawcontext->font.descent = 0; i < n; i++)
{
if(drawcontext->font.ascent < (*xfonts)->ascent)
drawcontext->font.ascent = (*xfonts)->ascent;
if(drawcontext->font.descent < (*xfonts)->descent)
drawcontext->font.descent = (*xfonts)->descent;
xfonts++;
}
}
else
{
if(drawcontext->font.xfont)
XFreeFont(disp, drawcontext->font.xfont);
drawcontext->font.xfont = NULL;
if(!(drawcontext->font.xfont = XLoadQueryFont(disp, fontstr)))
eprint("error, cannot load font: '%s'\n", fontstr);
drawcontext->font.ascent = drawcontext->font.xfont->ascent;
drawcontext->font.descent = drawcontext->font.xfont->descent;
}
drawcontext->font.height = drawcontext->font.ascent + drawcontext->font.descent;
}
static unsigned int
get_numlockmask(Display *disp)
{
XModifierKeymap *modmap;
unsigned int mask = 0;
int i, j;
modmap = XGetModifierMapping(disp);
for(i = 0; i < 8; i++)
for(j = 0; j < modmap->max_keypermod; j++)
{
if(modmap->modifiermap[i * modmap->max_keypermod + j]
== XKeysymToKeycode(disp, XK_Num_Lock))
mask = (1 << i);
}
XFreeModifiermap(modmap);
return mask;
}
/** Initialize color from X side
* \param colorstr Color code
* \param disp Display ref
* \param scr Screen number
* \return XColor pixel
*/
static unsigned long
initcolor(const char *colstr, Display * disp, int scr)
{
Colormap cmap = DefaultColormap(disp, scr);
XColor color;
if(!XAllocNamedColor(disp, cmap, colstr, &color, &color))
eprint("error, cannot allocate color '%s'\n", colstr);
return color.pixel;
}