Use config for sys-x11 and wm-wmii
authorAndy Spencer <andy753421@gmail.com>
Thu, 6 Oct 2011 06:55:47 +0000 (06:55 +0000)
committerAndy Spencer <andy753421@gmail.com>
Thu, 6 Oct 2011 06:55:47 +0000 (06:55 +0000)
conf.c
conf.h
sys-x11.c
wm-wmii.c

diff --git a/conf.c b/conf.c
index da0f5c9..6a13c44 100644 (file)
--- a/conf.c
+++ b/conf.c
@@ -74,9 +74,11 @@ static void conf_set(const char *key, int num, const char *str)
        if (str) {
                entry->type = string;
                entry->str  = strdup(str);
+               printf("set_str: %s = %s\n", key, str);
        } else {
                entry->type = number;
                entry->num  = num;
+               printf("set_num: %s = %d\n", key, num);
        }
        tsearch(entry, &conf, conf_cmp);
 }
@@ -98,7 +100,7 @@ static char *strtrim(char *str)
 static void load_file(const char *path)
 {
        char line[256]={}, section[256]={};
-       char key[256]={}, val[256]={};
+       char key[256]={}, val[256]={}, fullkey[256]={};
        FILE *fd = fopen(path, "rt");
        if (!fd) return;
        printf("load_file: %s\n", path);
@@ -121,27 +123,32 @@ static void load_file(const char *path)
                        /* Check for numbers/plain strings */
                        memcpy(key, line, equal-line);
                        memcpy(val, lquote+1, rquote-lquote-1);
-                       char *_key = strtrim(key);
-                       conf_set_str(_key, val);
-                       printf("  [%s.%s] = [%s]\n", section, _key, val);
+                       snprintf(fullkey, sizeof(key), "%s.%s",
+                                       section, strtrim(key));
+                       if (!strchr(fullkey, ' ')) {
+                               conf_set_str(fullkey, val);
+                               printf("  [%s] = [%s]\n", fullkey, val);
+                       }
                }
                else if (section[0] && equal) {
                        /* Check for strings */
                        memcpy(key, line, equal-line);
                        strcpy(val, equal+1);
-                       char *_key = strtrim(key);
-                       char *_val = strtrim(val);
-                       char *end;
-                       int num = strtol(_val, &end, 10);
-                       if (end != val && *end == '\0')
-                               conf_set_int(_key, num);
-                       else if (!strcasecmp(_val, "true"))
-                               conf_set_int(_key, 1);
-                       else if (!strcasecmp(_val, "false"))
-                               conf_set_int(_key, 0);
-                       else
-                               conf_set_str(_key, _val);
-                       printf("  [%s.%s] = [%s]\n", section, _key, _val);
+                       snprintf(fullkey, sizeof(key), "%s.%s",
+                                       section, strtrim(key));
+                       if (!strchr(fullkey, ' ')) {
+                               char *end, *trim = strtrim(val);
+                               int num = strtol(trim, &end, 10);
+                               if (end != val && *end == '\0')
+                                       conf_set_int(fullkey, num);
+                               else if (!strcasecmp(trim, "true"))
+                                       conf_set_int(fullkey, 1);
+                               else if (!strcasecmp(trim, "false"))
+                                       conf_set_int(fullkey, 0);
+                               else
+                                       conf_set_str(fullkey, trim);
+                               printf("  [%s] = [%s]\n", fullkey, trim);
+                       }
                }
        }
        fclose(fd);
@@ -150,13 +157,13 @@ static void load_file(const char *path)
 /* Load config from command line options */
 static struct option long_options[] = {
        /* name hasarg flag val */
-       {"border",  2, NULL, 'b'},
-       {"margin",  2, NULL, 'm'},
-       {"capture", 0, NULL, 'c'},
-       {"int",     1, NULL, 'i'},
-       {"str",     1, NULL, 's'},
-       {"help",    0, NULL, 'h'},
-       {NULL,      0, NULL,  0 },
+       {"no-capture", 0, NULL, 'n'},
+       {"border",     2, NULL, 'b'},
+       {"margin",     2, NULL, 'm'},
+       {"int",        1, NULL, 'i'},
+       {"str",        1, NULL, 's'},
+       {"help",       0, NULL, 'h'},
+       {NULL,         0, NULL,  0 },
 };
 
 static void usage(int argc, char **argv)
@@ -165,9 +172,9 @@ static void usage(int argc, char **argv)
        printf("  %s [OPTION...]\n", argv[0]);
        printf("\n");
        printf("Options:\n");
+       printf("  -n, --no-capture   Do not arrange pre existing windows\n");
        printf("  -b, --border=n     Draw an n pixel window border\n");
        printf("  -m, --margin=n     Leave an n pixel margin around windows\n");
-       printf("  -c, --capture      Automatically arrange all existing windows\n");
        printf("  -i, --int=key=num  Set integer config option\n");
        printf("  -s, --str=key=str  Set string config option\n");
        printf("  -h, --help         Print usage information\n");
@@ -177,19 +184,19 @@ static void load_args(int argc, char **argv)
 {
        char *key, *val;
        while (1) {
-               int c = getopt_long(argc, argv, "b:m:ch", long_options, NULL);
+               int c = getopt_long(argc, argv, "nb:m:i:s:h", long_options, NULL);
                if (c == -1)
                        break;
                switch (c) {
+               case 'n':
+                       conf_set_int("main.no-capture", 1);
+                       break;
                case 'b':
                        conf_set_int("main.border", str2num(optarg, 2));
                        break;
                case 'm':
                        conf_set_int("main.margin", str2num(optarg, 15));
                        break;
-               case 'c':
-                       conf_set_int("main.capture", 1);
-                       break;
                case 'i':
                case 's':
                        key = strdup(optarg);
@@ -215,10 +222,11 @@ static void load_args(int argc, char **argv)
 
 
 /* Configuration file functions */
-int conf_get_int(const char *key)
+int conf_get_int(const char *key, int def)
 {
        entry_t *entry = conf_get(key);
-       return entry ? entry->num : 0;
+       return entry && entry->type == number
+               ? entry->num : def;
 }
 
 void conf_set_int(const char *key, int value)
@@ -226,10 +234,11 @@ void conf_set_int(const char *key, int value)
        conf_set(key, value, NULL);
 }
 
-const char *conf_get_str(const char *key)
+const char *conf_get_str(const char *key, const char *def)
 {
        entry_t *entry = conf_get(key);
-       return entry ? entry->str : NULL;
+       return entry && entry->type == string
+               ? entry->str : def;
 }
 
 void conf_set_str(const char *key, const char *value)
diff --git a/conf.h b/conf.h
index 5782695..bd258d0 100644 (file)
--- a/conf.h
+++ b/conf.h
@@ -13,9 +13,9 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  */
 
-int conf_get_int(const char *key);
+int conf_get_int(const char *key, int def);
 
-const char *conf_get_str(const char *key);
+const char *conf_get_str(const char *key, const char *def);
 
 void conf_set_int(const char *key, int value);
 
index 6c43780..02e90a8 100644 (file)
--- a/sys-x11.c
+++ b/sys-x11.c
 #include <X11/extensions/Xinerama.h>
 
 #include "util.h"
+#include "conf.h"
 #include "sys.h"
 #include "wm.h"
 
-#ifndef BORDER
-#define BORDER 2
-#endif
+/* Configuration */
+static int BORDER     = 2;
+static int NO_CAPTURE = 0;
 
 /* Internal structures */
 struct win_sys {
@@ -540,6 +541,10 @@ win_t *sys_init(void)
        Display *dpy;
        Window   xid;
 
+       /* Load configuration */
+       BORDER     = conf_get_int("main.border",     BORDER);
+       NO_CAPTURE = conf_get_int("main.no-capture", NO_CAPTURE);
+
        /* Open the display */
        if (!(dpy = XOpenDisplay(NULL)))
                error("Unable to get display");
@@ -567,18 +572,20 @@ win_t *sys_init(void)
 void sys_run(win_t *root)
 {
        /* Add each initial window */
-       unsigned int nkids;
-       Window par, xid, *kids = NULL;
-       if (XQueryTree(root->sys->dpy, root->sys->xid,
-                               &par, &xid, &kids, &nkids)) {
-               for(int i = 0; i < nkids; i++) {
-                       win_t *win = win_find(root->sys->dpy, kids[i], 1);
-                       if (win && win_viewable(win) && !strut_add(root,win))
-                               wm_insert(win);
+       if (!NO_CAPTURE) {
+               unsigned int nkids;
+               Window par, xid, *kids = NULL;
+               if (XQueryTree(root->sys->dpy, root->sys->xid,
+                                       &par, &xid, &kids, &nkids)) {
+                       for(int i = 0; i < nkids; i++) {
+                               win_t *win = win_find(root->sys->dpy, kids[i], 1);
+                               if (win && win_viewable(win) && !strut_add(root,win))
+                                       wm_insert(win);
+                       }
+                       XFree(kids);
                }
-               XFree(kids);
+               wm_update(); // For struts
        }
-       wm_update(); // For struts
 
        /* Main loop */
        running = 1;
index ac13333..aab2921 100644 (file)
--- a/wm-wmii.c
+++ b/wm-wmii.c
 #include <stdlib.h>
 
 #include "util.h"
+#include "conf.h"
 #include "sys.h"
 #include "wm.h"
 
+/* Configuration */
 #ifndef MODKEY
 #define MODKEY alt
 #endif
-#ifndef MARGIN
-#define MARGIN 0
-#endif
-#ifndef STACK
-#define STACK  25
-#endif
+static int MARGIN = 0;
+static int STACK  = 25;
 
 /* Enums */
 typedef enum {
@@ -886,6 +884,10 @@ void wm_init(win_t *root)
 {
        printf("wm_init: %p\n", root);
 
+       /* Load configuration */
+       MARGIN = conf_get_int("main.margin", MARGIN);
+       STACK  = conf_get_int("main.stack",  STACK);
+
        /* Hack, fix screen order */
        list_t *screens = sys_info(root);
        list_t *left  = screens;