awesome/config.c

471 lines
16 KiB
C
Raw Normal View History

/*
2007-09-12 14:29:51 +02:00
* config.c - configuration management
*
* Copyright © 2007 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
2007-09-12 14:29:51 +02:00
*/
2007-09-05 20:15:00 +02:00
/**
* \defgroup ui_callback
*/
#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 "layout.h"
#include "tag.h"
#include "draw.h"
2007-09-15 15:26:51 +02:00
#include "util.h"
2007-09-15 15:34:28 +02:00
#include "statusbar.h"
#include "screen.h"
2007-09-05 20:15:00 +02:00
#include "layouts/tile.h"
2007-09-21 00:30:32 +02:00
#include "layouts/max.h"
2007-09-05 20:15:00 +02:00
#include "layouts/floating.h"
2007-10-01 19:22:57 +02:00
static XColor initxcolor(Display *, int, const char *);
2007-09-05 20:15:00 +02:00
static unsigned int get_numlockmask(Display *);
/** 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", layout_tile},
{"tileleft", layout_tileleft},
2007-09-21 00:30:32 +02:00
{"max", layout_max},
{"floating", layout_floating},
2007-09-10 12:02:45 +02:00
{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", uicb_spawn},
2007-09-28 13:39:46 +02:00
{"exec", uicb_exec},
2007-09-05 20:15:00 +02:00
/* client.c */
{"killclient", uicb_killclient},
{"moveresize", uicb_moveresize},
{"settrans", uicb_settrans},
{"setborder", uicb_setborder},
{"swapnext", uicb_swapnext},
{"swapprev", uicb_swapprev},
2007-09-05 20:15:00 +02:00
/* tag.c */
{"tag", uicb_tag},
{"togglefloating", uicb_togglefloating},
{"toggleview", uicb_toggleview},
{"toggletag", uicb_toggletag},
{"view", uicb_view},
{"view_tag_prev_selected", uicb_tag_prev_selected},
{"view_tag_previous", uicb_tag_viewprev},
{"view_tag_next", uicb_tag_viewnext},
2007-09-05 20:15:00 +02:00
/* layout.c */
{"setlayout", uicb_setlayout},
{"focusnext", uicb_focusnext},
{"focusprev", uicb_focusprev},
{"togglemax", uicb_togglemax},
{"toggleverticalmax", uicb_toggleverticalmax},
{"togglehorizontalmax", uicb_togglehorizontalmax},
{"zoom", uicb_zoom},
/* layouts/tile.c */
{"setmwfact", uicb_setmwfact},
2007-09-11 15:53:03 +02:00
{"setnmaster", uicb_setnmaster},
{"setncols", uicb_setncols},
/* screen.c */
{"focusnextscreen", uicb_focusnextscreen},
{"focusprevscreen", uicb_focusprevscreen},
2007-09-27 21:51:05 +02:00
{"movetoscreen", uicb_movetoscreen},
2007-09-10 12:06:54 +02:00
/* awesome.c */
2007-09-05 20:15:00 +02:00
{"quit", uicb_quit},
2007-09-15 15:34:28 +02:00
/* statusbar.c */
{"togglebar", uicb_togglebar},
2007-09-05 20:15:00 +02:00
{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++)
2007-09-21 17:32:00 +02:00
if(!a_strcmp(keyname, KeyModList[i].name))
2007-09-05 20:15:00 +02:00
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++)
2007-09-21 17:32:00 +02:00
if(!a_strcmp(funcname, list[i].name))
2007-09-05 20:15:00 +02:00
return list[i].func;
return NULL;
}
/** Parse configuration file and initialize some stuff
* \param disp Display ref
* \param scr Screen number
* \param drawcontext Draw context
*/
void
parse_config(Display * disp, int scr, DC * drawcontext, const char *confpatharg, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
/* Main configuration object for parsing*/
config_t awesomelibconf;
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 = 0, j = 0;
double f = 0.0;
2007-09-05 20:15:00 +02:00
const char *tmp, *homedir;
char *confpath;
KeySym tmp_key;
ssize_t confpath_len;
2007-10-01 19:22:57 +02:00
XColor colorbuf;
2007-09-05 20:15:00 +02:00
if(confpatharg)
confpath = a_strdup(confpatharg);
else
{
homedir = getenv("HOME");
confpath_len = a_strlen(homedir) + a_strlen(AWESOME_CONFIG_FILE) + 2;
confpath = p_new(char, confpath_len);
a_strcpy(confpath, confpath_len, homedir);
a_strcat(confpath, confpath_len, "/");
a_strcat(confpath, confpath_len, AWESOME_CONFIG_FILE);
}
2007-09-05 20:15:00 +02:00
2007-09-10 12:06:54 +02:00
config_init(&awesomelibconf);
2007-09-05 20:15:00 +02:00
2007-09-26 20:16:04 +02:00
a_strcpy(awesomeconf->statustext, sizeof(awesomeconf->statustext), "awesome-" VERSION);
2007-10-01 15:31:43 +02:00
/* store display */
awesomeconf->display = disp;
/* set screen */
awesomeconf->screen = scr;
2007-10-01 15:23:05 +02:00
awesomeconf->phys_screen = get_phys_screen(disp, scr);
2007-09-10 12:06:54 +02:00
if(config_read_file(&awesomelibconf, confpath) == CONFIG_FALSE)
fprintf(stderr, "awesome: 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 */
tmp = config_lookup_string(&awesomelibconf, "awesome.font");
2007-10-01 19:22:57 +02:00
drawcontext->font = XftFontOpenName(disp, awesomeconf->phys_screen, tmp ? tmp : "sans-12");
if(!drawcontext->font)
eprint("awesome: cannot init font\n");
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)
2007-09-26 20:16:04 +02:00
{
fprintf(stderr, "awesome: layouts not found in configuration file, setting default\n");
awesomeconf->nlayouts = 2;
awesomeconf->layouts = p_new(Layout, awesomeconf->nlayouts + 1);
awesomeconf->layouts[0].symbol = a_strdup("[]=");
awesomeconf->layouts[0].arrange = layout_tile;
awesomeconf->layouts[1].symbol = a_strdup("<><");
awesomeconf->layouts[1].arrange = layout_floating;
awesomeconf->layouts[2].symbol = NULL;
awesomeconf->layouts[2].arrange = NULL;
}
else
{
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++)
{
awesomeconf->layouts[i].arrange =
name_func_lookup(config_setting_get_string_elem(confsublayouts, 1), LayoutsList);
if(!awesomeconf->layouts[i].arrange)
{
fprintf(stderr, "awesome: unknown layout #%d in configuration file\n", i);
awesomeconf->layouts[i].symbol = NULL;
continue;
}
awesomeconf->layouts[i].symbol = a_strdup(config_setting_get_string_elem(confsublayouts, 0));
}
awesomeconf->layouts[i].symbol = NULL;
awesomeconf->layouts[i].arrange = NULL;
}
awesomeconf->current_layout = awesomeconf->layouts;
if(!awesomeconf->nlayouts || !awesomeconf->current_layout->arrange)
eprint("awesome: fatal: no default layout available\n");
for(i = 0; i < awesomeconf->nlayouts; i++)
{
2007-10-01 19:22:57 +02:00
j = drawcontext->font->height +
textwidth(disp, drawcontext->font,
awesomeconf->layouts[i].symbol, a_strlen(awesomeconf->layouts[i].symbol));
if(j > awesomeconf->statusbar.width)
awesomeconf->statusbar.width = j;
}
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)
2007-09-26 20:16:04 +02:00
{
fprintf(stderr, "awesome: tags not found in configuration file, setting default\n");
awesomeconf->ntags = 3;
awesomeconf->tags = p_new(Tag, awesomeconf->ntags);
awesomeconf->tags[0].name = a_strdup("this");
awesomeconf->tags[1].name = a_strdup("is");
awesomeconf->tags[2].name = a_strdup("awesome");
awesomeconf->tags[0].selected = True;
awesomeconf->tags[1].selected = False;
awesomeconf->tags[2].selected = False;
awesomeconf->tags[0].was_selected = False;
awesomeconf->tags[1].was_selected = False;
awesomeconf->tags[2].was_selected = False;
awesomeconf->tags[0].layout = awesomeconf->layouts;
awesomeconf->tags[1].layout = awesomeconf->layouts;
awesomeconf->tags[2].layout = awesomeconf->layouts;
}
else
{
awesomeconf->ntags = config_setting_length(conftags);
2007-09-24 15:37:52 +02:00
awesomeconf->tags = p_new(Tag, awesomeconf->ntags);
for(i = 0; (tmp = config_setting_get_string_elem(conftags, i)); i++)
{
2007-09-24 15:37:52 +02:00
awesomeconf->tags[i].name = a_strdup(tmp);
awesomeconf->tags[i].selected = False;
awesomeconf->tags[i].was_selected = False;
/** \todo add support for default tag/layout in configuration file */
2007-09-24 15:37:52 +02:00
awesomeconf->tags[i].layout = awesomeconf->layouts;
}
}
if(!awesomeconf->ntags)
eprint("awesome: fatal: no tags found in configuration file\n");
/* select first tag by default */
2007-09-24 15:37:52 +02:00
awesomeconf->tags[0].selected = True;
awesomeconf->tags[0].was_selected = 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)
2007-09-26 20:16:04 +02:00
{
awesomeconf->nrules = 0;
fprintf(stderr, "awesome: no rules found in configuration file\n");
2007-09-26 20:16:04 +02:00
}
else
2007-09-05 20:15:00 +02:00
{
awesomeconf->nrules = config_setting_length(confrules);
awesomeconf->rules = p_new(Rule, awesomeconf->nrules);
for(i = 0; (confsubrules = config_setting_get_elem(confrules, i)); i++)
{
awesomeconf->rules[i].prop = a_strdup(config_setting_get_string(config_setting_get_member(confsubrules, "name")));
awesomeconf->rules[i].tags = a_strdup(config_setting_get_string(config_setting_get_member(confsubrules, "tags")));
if(awesomeconf->rules[i].tags && !a_strlen(awesomeconf->rules[i].tags))
awesomeconf->rules[i].tags = NULL;
awesomeconf->rules[i].isfloating =
config_setting_get_bool(config_setting_get_member(confsubrules, "float"));
}
2007-09-05 20:15:00 +02:00
}
/* modkey */
tmp_key = key_mask_lookup(config_lookup_string(&awesomelibconf, "awesome.modkey"));
awesomeconf->modkey = tmp_key ? tmp_key : Mod1Mask;
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)
2007-09-26 20:16:04 +02:00
{
awesomeconf->nkeys = 0;
fprintf(stderr, "awesome: no keys found in configuration file\n");
2007-09-26 20:16:04 +02:00
}
else
2007-09-05 20:15:00 +02:00
{
awesomeconf->nkeys = config_setting_length(confkeys);
awesomeconf->keys = p_new(Key, awesomeconf->nkeys);
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++)
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 =
name_func_lookup(config_setting_get_string_elem(confsubkeys, 2), KeyfuncList);
awesomeconf->keys[i].arg = a_strdup(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(tmp && !a_strncmp(tmp, "off", 6))
2007-09-13 23:17:50 +02:00
awesomeconf->statusbar_default_position = BarOff;
else if(tmp && !a_strncmp(tmp, "bottom", 6))
2007-09-13 23:17:50 +02:00
awesomeconf->statusbar_default_position = BarBot;
else
2007-09-10 12:06:54 +02:00
awesomeconf->statusbar_default_position = BarTop;
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");
2007-09-26 20:16:04 +02:00
if(awesomeconf->opacity_unfocused >= 100 || awesomeconf->opacity_unfocused == 0)
2007-09-10 12:06:54 +02:00
awesomeconf->opacity_unfocused = -1;
2007-09-05 20:15:00 +02:00
/* snap */
i = config_lookup_int(&awesomelibconf, "awesome.snap");
awesomeconf->snap = i ? i : 8;
2007-09-05 20:15:00 +02:00
/* nmaster */
i = config_lookup_int(&awesomelibconf, "awesome.nmaster");
awesomeconf->nmaster = i ? i : 1;
2007-09-05 20:15:00 +02:00
/* ncols */
i = config_lookup_int(&awesomelibconf, "awesome.ncols");
awesomeconf->ncols = i ? i : 1;
2007-09-05 20:15:00 +02:00
/* mwfact */
f = config_lookup_float(&awesomelibconf, "awesome.mwfact");
awesomeconf->mwfact = f ? f : 0.6;
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 */
tmp = config_lookup_string(&awesomelibconf, "awesome.normal_border_color");
2007-10-01 19:22:57 +02:00
colorbuf = initxcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#dddddd");
drawcontext->norm[ColBorder] = colorbuf.pixel;
tmp = config_lookup_string(&awesomelibconf, "awesome.normal_bg_color");
2007-10-01 19:22:57 +02:00
colorbuf = initxcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#000000");
drawcontext->norm[ColBG] = colorbuf.pixel;
tmp = config_lookup_string(&awesomelibconf, "awesome.normal_fg_color");
2007-10-01 19:22:57 +02:00
colorbuf = initxcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#ffffff");
drawcontext->norm[ColFG] = colorbuf.pixel;
drawcontext->text_normal = colorbuf;
tmp = config_lookup_string(&awesomelibconf, "awesome.focus_border_color");
2007-10-01 19:22:57 +02:00
colorbuf = initxcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#008b8b");
drawcontext->sel[ColBorder] = colorbuf.pixel;
tmp = config_lookup_string(&awesomelibconf, "awesome.focus_bg_color");
2007-10-01 19:22:57 +02:00
colorbuf = initxcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#008b8b");
drawcontext->sel[ColBG] = colorbuf.pixel;
tmp = config_lookup_string(&awesomelibconf, "awesome.focus_fg_color");
2007-10-01 19:22:57 +02:00
colorbuf = initxcolor(disp, awesomeconf->phys_screen, tmp ? tmp : "#ffffff");
drawcontext->sel[ColFG] = colorbuf.pixel;
drawcontext->text_selected = colorbuf;
2007-09-05 20:15:00 +02:00
config_destroy(&awesomelibconf);
2007-09-05 20:15:00 +02:00
p_delete(&confpath);
}
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
*/
2007-10-01 19:22:57 +02:00
static XColor
initxcolor(Display *disp, int scr, const char *colstr)
2007-09-05 20:15:00 +02:00
{
XColor color;
2007-10-01 19:22:57 +02:00
if(!XAllocNamedColor(disp, DefaultColormap(disp, scr), colstr, &color, &color))
die("awesome: error, cannot allocate color '%s'\n", colstr);
2007-10-01 19:22:57 +02:00
return color;
2007-09-05 20:15:00 +02:00
}