From 05aafde11a64224917c935432156323faf8cf0ba Mon Sep 17 00:00:00 2001 From: Martin Duquesnoy Date: Thu, 25 Aug 2011 21:19:43 +0200 Subject: [PATCH] Add conf parsage: keybind section available, config.h associative struct/func --- wmfs2/src/config.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++ wmfs2/src/config.h | 53 +++++++++++++++++++++++++++++++++++++++++ wmfs2/src/event.c | 14 +++++------ wmfs2/src/event.h | 1 + wmfs2/src/util.c | 6 +++++ wmfs2/src/util.h | 3 ++- wmfs2/src/wmfs.c | 36 ++++++++++++++++++++++++---- wmfs2/src/wmfs.h | 2 ++ 8 files changed, 160 insertions(+), 14 deletions(-) diff --git a/wmfs2/src/config.c b/wmfs2/src/config.c index 93cd014..2e4cf4d 100755 --- a/wmfs2/src/config.c +++ b/wmfs2/src/config.c @@ -3,8 +3,67 @@ * For license, see COPYING. */ +#include "config.h" +#include "wmfs.h" +#include "parse.h" + +#define CONFIG_DEFAULT_PATH ".config/wmfs/wmfsrc2" /* tmp */ + +static void +config_keybind(void) +{ + int i; + size_t j, n; + struct conf_sec *sec, **ks; + struct opt_type *opt; + Keybind *k; + + sec = fetch_section_first(NULL, "keys"); + ks = fetch_section(sec, "key"); + n = fetch_section_count(ks); + + SLIST_INIT(&W->h.keybind); + + for(i = 0; i < n; ++i) + { + opt = fetch_opt(ks[i], "", "mod"); + + k = xcalloc(1, sizeof(Keybind)); + + for(j = 0; j < fetch_opt_count(opt); ++j) + k->mod |= modkey_keysym(opt[j].str); + + free(opt); + + k->keysym = XStringToKeysym(fetch_opt_first(ks[i], "None", "key").str); + + if(!(k->func = uicb_name_func(fetch_opt_first(ks[i], "", "func").str))) + { + warnx("configuration : Unknown Function \"%s\".", fetch_opt_first(ks[i], "", "func").str); + k->func = uicb_spawn; + } + + k->cmd = fetch_opt_first(ks[i], "", "cmd").str; + + SLIST_INSERT_HEAD(&W->h.keybind, k, next); + + k = NULL; + } + + free(ks); +} + void config_init(void) { + char *path; + xasprintf(&path, "%s/"CONFIG_DEFAULT_PATH, getenv("HOME")); + + if(get_conf(path) == -1) + errx(1, "parsing configuration file (%s) failed.", path); + + config_keybind(); + + free(path); } diff --git a/wmfs2/src/config.h b/wmfs2/src/config.h index 8146690..d76cc1d 100755 --- a/wmfs2/src/config.h +++ b/wmfs2/src/config.h @@ -7,6 +7,59 @@ #ifndef CONFIG_H #define CONFIG_H +#include +#include + #include "wmfs.h" +#include "util.h" + +static const struct { char *name; void (*func)(Uicb cmd); } uicb_list[] = +{ + { "spawn", uicb_spawn }, + { "quit", uicb_quit }, + { NULL, NULL } +}; + +static inline void* +uicb_name_func(Uicb name) +{ + int i = 0; + + for(; uicb_list[i].func; ++i) + if(!strcmp(name, uicb_list[i].name)) + return uicb_list[i].func; + + return NULL; +} + +static const struct { const char *name; KeySym keysym; } key_list[] = +{ + {"Control", ControlMask }, + {"Shift", ShiftMask }, + {"Lock", LockMask }, + {"Alt", Mod1Mask }, + {"Mod1", Mod1Mask }, + {"Mod2", Mod2Mask }, + {"Mod3", Mod3Mask }, + {"Mod4", Mod4Mask }, + {"Super", Mod4Mask }, + {"Home", Mod4Mask }, + {"Mod5", Mod5Mask }, + {NULL, NoSymbol } +}; + +static inline KeySym +modkey_keysym(const char *name) +{ + int i = 0; + + for(; key_list[i].name; ++i) + if(!strcmp(name, key_list[i].name)) + return key_list[i].keysym; + + return NoSymbol; +} + +void config_init(void); #endif /* CONFIG_H */ diff --git a/wmfs2/src/event.c b/wmfs2/src/event.c index e3b3f17..5fa5196 100755 --- a/wmfs2/src/event.c +++ b/wmfs2/src/event.c @@ -169,7 +169,7 @@ event_unmapnotify(XEvent *e) static void event_motionnotify(XEvent *e) { - /* + /* XMotionEvent *ev = &e->xmotion; Client *c; @@ -184,14 +184,12 @@ static void event_keypress(XEvent *e) { XKeyPressedEvent *ev = &e->xkey; - KeySym keysym; - Keybind *k; + KeySym keysym = XKeycodeToKeysym(EVDPY(e), (KeyCode)ev->keycode, 0); Flags m = ~(W->numlockmask | LockMask); - - keysym = XKeycodeToKeysym(EVDPY(e), (KeyCode)ev->keycode, 0); + Keybind *k; SLIST_FOREACH(k, &W->h.keybind, next) - if(k->keysym == keysym && (k->mod & m) == (ev->state & m)) + if(k->keysym == keysym && KEYPRESS_MASK(k->mod) == KEYPRESS_MASK(ev->state)) if(k->func) k->func(k->cmd); } @@ -201,13 +199,13 @@ event_expose(XEvent *e) { XExposeEvent *ev = &e->xexpose; Barwin *b; -/* + SLIST_FOREACH(b, &W->h.barwin, next) if(b->win == ev->window) { barwin_refresh(b); return; - }*/ + } } static void diff --git a/wmfs2/src/event.h b/wmfs2/src/event.h index 0db4ff6..3ee8942 100755 --- a/wmfs2/src/event.h +++ b/wmfs2/src/event.h @@ -10,6 +10,7 @@ #define MAX_EV 256 +#define KEYPRESS_MASK(m) (m & ~(W->numlockmask | LockMask)) #define HANDLE_EVENT(e) event_handle[(e)->type](e); void event_init(void); diff --git a/wmfs2/src/util.c b/wmfs2/src/util.c index 1410001..85147cb 100755 --- a/wmfs2/src/util.c +++ b/wmfs2/src/util.c @@ -118,3 +118,9 @@ spawn(const char *format, ...) return pid; } + +void +uicb_spawn(Uicb cmd) +{ + spawn("%s", cmd); +} diff --git a/wmfs2/src/util.h b/wmfs2/src/util.h index 9808ce4..4ffc4c0 100755 --- a/wmfs2/src/util.h +++ b/wmfs2/src/util.h @@ -11,7 +11,7 @@ /* Todo FREE_LIST(type, head, function_remove) */ #define FREE_LIST(type, head) \ do { \ - type *t; \ + struct type *t; \ while(!SLIST_EMPTY(&head)) { \ t = SLIST_FIRST(&head); \ SLIST_REMOVE_HEAD(&head, next); \ @@ -26,5 +26,6 @@ void *xcalloc(size_t nmemb, size_t size); int xasprintf(char **strp, const char *fmt, ...); char *xstrdup(const char *str); pid_t spawn(const char *format, ...); +void uicb_spawn(Uicb cmd); #endif /* UTIL_H */ diff --git a/wmfs2/src/wmfs.c b/wmfs2/src/wmfs.c index 01338af..7d062d8 100755 --- a/wmfs2/src/wmfs.c +++ b/wmfs2/src/wmfs.c @@ -3,13 +3,14 @@ * For license, see COPYING. */ +#include #include #include #include "wmfs.h" #include "event.h" #include "util.h" -/*#include "infobar.h" */ +#include "config.h" int wmfs_error_handler(Display *d, XErrorEvent *event) @@ -56,7 +57,7 @@ static void wmfs_xinit(void) { char **misschar, **names, *defstring; - int d, i = 0, j = 0; + int d, i, j; XModifierKeymap *mm; XFontStruct **xfs = NULL; XSetWindowAttributes at; @@ -108,8 +109,8 @@ wmfs_xinit(void) */ mm = XGetModifierMapping(W->dpy); - for(; i < 8; i++) - for(; j < mm->max_keypermod; ++j) + for(i = 0; i < 8; i++) + for(j = 0; j < mm->max_keypermod; ++j) if(mm->modifiermap[i * mm->max_keypermod + j] == XKeysymToKeycode(W->dpy, XK_Num_Lock)) W->numlockmask = (1 << i); @@ -217,6 +218,8 @@ wmfs_init(void) /* EWMH init */ ewmh_init(); + config_init(); + /* Event init */ event_init(); @@ -232,7 +235,6 @@ void wmfs_quit(void) { /* X stuffs */ - XFreeGC(W->dpy, W->gc); XFreeFontSet(W->dpy, W->font.fontset); screen_free(); @@ -242,9 +244,18 @@ wmfs_quit(void) free(W->net_atom); free(W); + /* Conf stuffs */ + FREE_LIST(Keybind, W->h.keybind); + W->running = false; } +void +uicb_quit(Uicb cmd) +{ + (void)cmd; + W->running = false; +} int main(int argc, char **argv) @@ -259,6 +270,21 @@ main(int argc, char **argv) } /* Opt */ + /* + int i; + while((i = getopt(argc, argv, "hviC:")) != -1) + { + switch(i) + { + case 'h': + break; + case 'v': + break; + case 'C': + break; + } + } + */ /* Core */ wmfs_init(); diff --git a/wmfs2/src/wmfs.h b/wmfs2/src/wmfs.h index 2067dc8..535fcb6 100755 --- a/wmfs2/src/wmfs.h +++ b/wmfs2/src/wmfs.h @@ -187,6 +187,8 @@ int wmfs_error_handler(Display *d, XErrorEvent *event); int wmfs_error_handler_dummy(Display *d, XErrorEvent *event); void wmfs_grab_keys(void); void wmfs_quit(void); +void uicb_quit(Uicb cmd); + /* Single global variable */ struct Wmfs *W;