JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Add initial floating support and a forgotten makefile...
[spectrwm.git] / scrotwm.c
index 598a3af..b8dca30 100644 (file)
--- a/scrotwm.c
+++ b/scrotwm.c
 #include <time.h>
 #include <signal.h>
 #include <string.h>
+#include <util.h>
+#include <pwd.h>
 
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <sys/wait.h>
 #include <sys/queue.h>
+#include <sys/param.h>
 
 #include <X11/cursorfont.h>
 #include <X11/keysym.h>
@@ -101,19 +105,31 @@ int                       width, height;
 int                    running = 1;
 int                    ignore_enter = 0;
 unsigned int           numlockmask = 0;
-unsigned long          col_focus = 0xff0000;   /* XXX should this be per ws? */
-unsigned long          col_unfocus = 0x888888;
+unsigned long          color_focus = 0xff0000; /* XXX should this be per ws? */
+unsigned long          color_unfocus = 0x888888;
 Display                        *display;
 Window                 root;
 
 /* status bar */
 int                    bar_enabled = 1;
-int                    bar_height = 12;
+int                    bar_height = 0;
+unsigned long          bar_border = 0x008080;
+unsigned long          bar_color = 0x000000;
+unsigned long          bar_font_color = 0xa0a0a0;
 Window                 bar_window;
 GC                     bar_gc;
 XGCValues              bar_gcv;
 XFontStruct            *bar_fs;
 char                   bar_text[128];
+char                   *bar_fonts[] = {
+                           "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
+                           "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
+                           NULL
+};
+
+/* terminal + args */
+char                           *spawn_term[] = { "xterm", NULL };
+char                           *spawn_menu[] = { "dmenu_run", NULL };
 
 struct ws_win {
        TAILQ_ENTRY(ws_win)     entry;
@@ -122,6 +138,8 @@ struct ws_win {
        int                     y;
        int                     width;
        int                     height;
+       int                     floating;
+       int                     transient;
 };
 
 TAILQ_HEAD(ws_win_list, ws_win);
@@ -132,7 +150,6 @@ struct workspace {
        int                     visible;        /* workspace visible */
        int                     restack;        /* restack on switch */
        struct ws_win           *focus;         /* which win has focus */
-       int                     winno;          /* total nr of windows */
        struct ws_win_list      winlist;        /* list of windows in ws */
 } ws[SWM_WS_MAX];
 int                    current_ws = 0;
@@ -149,14 +166,97 @@ union arg {
 
 void   stack(void);
 
+#define        SWM_CONF_WS     "\n= \t"
+#define SWM_CONF_FILE  "scrotwm.conf"
+int
+conf_load(char *filename)
+{
+       FILE                    *config;
+       char                    *line, *cp, *var, *val;
+       size_t                   len, lineno = 0;
+
+       DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
+
+       if (filename == NULL)
+               return (1);
+
+       if ((config = fopen(filename, "r")) == NULL)
+               return (1);
+
+       for (;;) {
+               if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
+                       if (feof(config))
+                               break;
+               cp = line;
+               cp += (long)strspn(cp, SWM_CONF_WS);
+               if (cp[0] == '\0') {
+                       /* empty line */
+                       free(line);
+                       continue;
+               }
+               if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
+                       break;
+               cp += (long)strspn(cp, SWM_CONF_WS);
+               if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
+                       break;
+
+               DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
+               switch (var[0]) {
+               case 'b':
+                       if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
+                               bar_enabled = atoi(val);
+                       else if (!strncmp(var, "bar_border",
+                           strlen("bar_border")))
+                               bar_border = strtol(val, NULL, 16);
+                       else if (!strncmp(var, "bar_color",
+                           strlen("bar_color")))
+                               bar_color = strtol(val, NULL, 16);
+                       else if (!strncmp(var, "bar_font_color",
+                           strlen("bar_font_color")))
+                               bar_font_color = strtol(val, NULL, 16);
+                       else if (!strncmp(var, "bar_font", strlen("bar_font")))
+                               asprintf(&bar_fonts[0], "%s", val);
+                       else
+                               goto bad;
+                       break;
+
+               case 'c':
+                       if (!strncmp(var, "color_focus", strlen("color_focus")))
+                               color_focus = strtol(val, NULL, 16);
+                       else if (!strncmp(var, "color_unfocus",
+                           strlen("color_unfocus")))
+                               color_unfocus = strtol(val, NULL, 16);
+                       else
+                               goto bad;
+                       break;
+
+               case 's':
+                       if (!strncmp(var, "spawn_term", strlen("spawn_term")))
+                               asprintf(&spawn_term[0], "%s", val); /* XXX args? */
+                       break;
+               default:
+                       goto bad;
+               }
+               free(line);
+       }
+
+       fclose(config);
+       return (0);
+bad:
+       errx(1, "invalid conf file entry: %s=%s", var, val);
+}
+
 void
 bar_print(void)
 {
        time_t                  tmt;
        struct tm               tm;
 
+       if (bar_enabled == 0)
+               return;
+
        /* clear old text */
-       XSetForeground(display, bar_gc, 0x000000);
+       XSetForeground(display, bar_gc, bar_color);
        XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
            strlen(bar_text));
 
@@ -164,7 +264,7 @@ bar_print(void)
        time(&tmt);
        localtime_r(&tmt, &tm);
        strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
-       XSetForeground(display, bar_gc, 0xa0a0a0);
+       XSetForeground(display, bar_gc, bar_font_color);
        XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
            strlen(bar_text));
        XSync(display, False);
@@ -180,6 +280,75 @@ bar_signal(int sig)
 }
 
 void
+bar_toggle(union arg *args)
+{
+       int i;
+
+       DNPRINTF(SWM_D_MISC, "bar_toggle\n");
+
+       if (bar_enabled) {
+               bar_enabled = 0;
+               height += bar_height; /* correct screen height */
+               XUnmapWindow(display, bar_window);
+       } else {
+               bar_enabled = 1;
+               height -= bar_height; /* correct screen height */
+               XMapWindow(display, bar_window);
+       }
+       XSync(display, False);
+       for (i = 0; i < SWM_WS_MAX; i++)
+               ws[i].restack = 1;
+
+       stack();
+       bar_print(); /* must be after stack */
+}
+
+void
+bar_setup(void)
+{
+       int                     i;
+
+       for (i = 0; bar_fonts[i] != NULL; i++) {
+               bar_fs = XLoadQueryFont(display, bar_fonts[i]);
+               if (bar_fs)
+                       break;
+       }
+       if (bar_fonts[i] == NULL)
+                       errx(1, "couldn't load font");
+       bar_height = bar_fs->ascent + bar_fs->descent + 3;
+
+       bar_window = XCreateSimpleWindow(display, root, 0, 0, width,
+           bar_height - 2, 1, bar_border, bar_color);
+       bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
+       XSetFont(display, bar_gc, bar_fs->fid);
+       XSelectInput(display, bar_window, VisibilityChangeMask);
+       if (bar_enabled) {
+               height -= bar_height; /* correct screen height */
+               XMapWindow(display, bar_window);
+       }
+       DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
+
+       if (signal(SIGALRM, bar_signal) == SIG_ERR)
+               err(1, "could not install bar_signal");
+       bar_print();
+}
+
+int
+count_win(int wsid, int count_transient)
+{
+       struct ws_win           *win;
+       int                     count = 0;
+
+       TAILQ_FOREACH (win, &ws[wsid].winlist, entry) {
+               if (count_transient == 0 && win->transient)
+                       continue;
+               count++;
+       }
+       DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
+
+       return (count);
+}
+void
 quit(union arg *args)
 {
        DNPRINTF(SWM_D_MISC, "quit\n");
@@ -212,7 +381,7 @@ void
 focus_win(struct ws_win *win)
 {
        DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
-       XSetWindowBorder(display, win->id, col_focus);
+       XSetWindowBorder(display, win->id, color_focus);
        XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
        ws[current_ws].focus = win;
 }
@@ -221,7 +390,7 @@ void
 unfocus_win(struct ws_win *win)
 {
        DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
-       XSetWindowBorder(display, win->id, col_unfocus);
+       XSetWindowBorder(display, win->id, color_unfocus);
        if (ws[current_ws].focus == win)
                ws[current_ws].focus = NULL;
 }
@@ -239,7 +408,7 @@ switchws(union arg *args)
 
        /* map new window first to prevent ugly blinking */
        TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
-               XMapWindow(display, win->id);
+               XMapRaised(display, win->id);
        ws[wsid].visible = 1;
 
        TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
@@ -251,6 +420,7 @@ switchws(union arg *args)
        ignore_enter = 1;
        if (ws[wsid].restack) {
                stack();
+               bar_print();
        } else {
                if (ws[wsid].focus != NULL)
                        focus_win(ws[wsid].focus);
@@ -264,34 +434,29 @@ focus(union arg *args)
        struct ws_win           *winfocus, *winlostfocus;
 
        DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
-       if (ws[current_ws].focus == NULL || ws[current_ws].winno == 0)
+       if (ws[current_ws].focus == NULL || count_win(current_ws, 1) == 0)
                return;
 
        winlostfocus = ws[current_ws].focus;
 
        switch (args->id) {
        case SWM_ARG_ID_FOCUSPREV:
-               if (ws[current_ws].focus ==
-                   TAILQ_FIRST(&ws[current_ws].winlist))
+               ws[current_ws].focus =
+                   TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
+               if (ws[current_ws].focus == NULL)
                        ws[current_ws].focus =
                            TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
-               else
-                       ws[current_ws].focus =TAILQ_PREV(ws[current_ws].focus,
-                           ws_win_list, entry);
                break;
 
        case SWM_ARG_ID_FOCUSNEXT:
-               if (ws[current_ws].focus == TAILQ_LAST(&ws[current_ws].winlist,
-                   ws_win_list))
+               ws[current_ws].focus = TAILQ_NEXT(ws[current_ws].focus, entry);
+               if (ws[current_ws].focus == NULL)
                        ws[current_ws].focus =
                            TAILQ_FIRST(&ws[current_ws].winlist);
-               else
-                       ws[current_ws].focus =
-                           TAILQ_NEXT(ws[current_ws].focus, entry);
                break;
 
        case SWM_ARG_ID_FOCUSMAIN:
-               ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);;
+               ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
                break;
 
        default:
@@ -310,27 +475,32 @@ stack(void)
 {
        XWindowChanges          wc;
        struct ws_win           wf, *win, *winfocus = &wf;
-       int                     i, h, w, x, y, hrh;
+       int                     i, h, w, x, y, hrh, winno;
+       int floater = 0;
+       unsigned int mask;
 
        DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
 
        winfocus->id = root;
 
-       if (ws[current_ws].winno == 0)
+       ws[current_ws].restack = 0;
+
+       winno = count_win(current_ws, 0);
+       if (winno == 0)
                return;
 
-       if (ws[current_ws].winno > 1)
+       if (winno > 1)
                w = width / 2;
        else
                w = width;
 
-       if (ws[current_ws].winno > 2)
-               hrh = height / (ws[current_ws].winno - 1);
+       if (winno > 2)
+               hrh = height / (winno - 1);
        else
                hrh = 0;
 
        x = 0;
-       y = bar_height;
+       y = bar_enabled ? bar_height : 0;
        h = height;
        i = 0;
        TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
@@ -356,19 +526,31 @@ stack(void)
                        }
                }
 
+               if (win->transient != 0 || win->floating != 0)
+                       floater = 1;
+               else
+                       floater = 0;
+
                bzero(&wc, sizeof wc);
-               win->x = wc.x = x;
-               win->y = wc.y = y;
-               win->width = wc.width = w;
-               win->height = wc.height = h;
                wc.border_width = 1;
-               XConfigureWindow(display, win->id, CWX | CWY | CWWidth |
-                   CWHeight | CWBorderWidth, &wc);
+               if (floater == 0) {
+                       win->x = wc.x = x;
+                       win->y = wc.y = y;
+                       win->width = wc.width = w;
+                       win->height = wc.height = h;
+                       mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
+               } else {
+                       win->x = wc.x = width / 2;
+                       win->y = wc.y = height / 2;
+                       mask = CWX | CWY | CWBorderWidth;
+               }
+               XConfigureWindow(display, win->id, mask, &wc);
+
                if (win == ws[current_ws].focus)
                        winfocus = win;
                else
                        unfocus_win(win);
-               XMapWindow(display, win->id);
+               XMapRaised(display, win->id);
                i++;
        }
 
@@ -413,21 +595,15 @@ send_to_ws(union arg *args)
                ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
 
        TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
-       ws[current_ws].winno--;
 
        TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
-       if (ws[wsid].winno == 0)
+       if (count_win(wsid, 1) == 0)
                ws[wsid].focus = win;
-       ws[wsid].winno++;
        ws[wsid].restack = 1;
 
        stack();
 }
 
-
-/* terminal + args */
-char                           *term[] = { "xterm", NULL };
-
 /* key definitions */
 struct key {
        unsigned int            mod;
@@ -437,7 +613,8 @@ struct key {
 } keys[] = {
        /* modifier             key     function                argument */
        { MODKEY,               XK_Return,      swap_to_main,   {0} },
-       { MODKEY | ShiftMask,   XK_Return,      spawn,          {.argv = term } },
+       { MODKEY | ShiftMask,   XK_Return,      spawn,          {.argv = spawn_term} },
+       { MODKEY,               XK_p,           spawn,          {.argv = spawn_menu} },
        { MODKEY | ShiftMask,   XK_q,           quit,           {0} },
        { MODKEY,               XK_m,           focus,          {.id = SWM_ARG_ID_FOCUSMAIN} },
        { MODKEY,               XK_1,           switchws,       {.id = 0} },
@@ -460,8 +637,9 @@ struct key {
        { MODKEY | ShiftMask,   XK_8,           send_to_ws,     {.id = 7} },
        { MODKEY | ShiftMask,   XK_9,           send_to_ws,     {.id = 8} },
        { MODKEY | ShiftMask,   XK_0,           send_to_ws,     {.id = 9} },
-       { MODKEY,               XK_Tab,         focus,          {.id = SWM_ARG_ID_FOCUSPREV} },
-       { MODKEY | ShiftMask,   XK_Tab,         focus,          {.id = SWM_ARG_ID_FOCUSNEXT} },
+       { MODKEY,               XK_b,           bar_toggle,     {0} },
+       { MODKEY,               XK_Tab,         focus,          {.id = SWM_ARG_ID_FOCUSNEXT} },
+       { MODKEY | ShiftMask,   XK_Tab,         focus,          {.id = SWM_ARG_ID_FOCUSPREV} },
 };
 
 void
@@ -560,12 +738,18 @@ void
 configurerequest(XEvent *e)
 {
        XConfigureRequestEvent  *ev = &e->xconfigurerequest;
+       Window                  trans;
        struct ws_win           *win;
 
        DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
 
+       TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
+               if (ev->window == win->id)
+                       return;
+       }
+
        XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask |
-           FocusChangeMask);
+           FocusChangeMask | ExposureMask);
 
        if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
                errx(1, "calloc: failed to allocate memory for new window");
@@ -573,7 +757,26 @@ configurerequest(XEvent *e)
        win->id = ev->window;
        TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
        ws[current_ws].focus = win; /* make new win focused */
-       ws[current_ws].winno++;
+
+       XGetTransientForHint(display, win->id, &trans);
+       if (trans) {
+               win->transient = trans;
+               DNPRINTF(SWM_D_MISC, "configurerequest: win %u transient %u\n",
+                   (unsigned)win->id, win->transient);
+       }
+#if 0
+       XClassHint ch = { 0 };
+       if(XGetClassHint(display, win->id, &ch)) {
+               fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name);
+               if (!strcmp(ch.res_class, "Gvim") && !strcmp(ch.res_name, "gvim")) {
+                       win->floating = 0;
+               }
+               if(ch.res_class)
+                       XFree(ch.res_class);
+               if(ch.res_name)
+                       XFree(ch.res_name);
+       }
+#endif
        stack();
 }
 
@@ -603,7 +806,6 @@ destroynotify(XEvent *e)
        
                        TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
                        free(win);
-                       ws[current_ws].winno--;
                        break;
                }
        }
@@ -642,9 +844,10 @@ focusin(XEvent *e)
 
        DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
 
+       XSync(display, False); /* not sure this helps redrawing graphic apps */
+
        if (ev->window == root)
                return;
-
        /*
         * kill grab for now so that we can cut and paste , this screws up
         * click to focus
@@ -688,6 +891,16 @@ unmapnotify(XEvent *e)
        DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
 }
 
+void
+visibilitynotify(XEvent *e)
+{
+       DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window);
+
+       if (e->xvisibility.window == bar_window &&
+           e->xvisibility.state == VisibilityUnobscured)
+               bar_print();
+}
+
 void                   (*handler[LASTEvent])(XEvent *) = {
                                [Expose] = expose,
                                [KeyPress] = keypress,
@@ -701,6 +914,7 @@ void                        (*handler[LASTEvent])(XEvent *) = {
                                [MapRequest] = maprequest,
                                [PropertyNotify] = propertynotify,
                                [UnmapNotify] = unmapnotify,
+                               [VisibilityNotify] = visibilitynotify,
 };
 
 int
@@ -739,6 +953,9 @@ active_wm(void)
 int
 main(int argc, char *argv[])
 {
+       struct passwd           *pwd;
+       char                    conf[PATH_MAX], *cfile = NULL;
+       struct stat             sb;
        XEvent                  e;
        int                     i;
 
@@ -757,52 +974,47 @@ main(int argc, char *argv[])
        width = DisplayWidth(display, screen) - 2;
        height = DisplayHeight(display, screen) - 2;
 
+       /* look for local and global conf file */
+       pwd = getpwuid(getuid());
+       if (pwd == NULL)
+               errx(1, "invalid user %d", getuid());
+
+       snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
+       if (stat(conf, &sb) != -1) {
+               if (S_ISREG(sb.st_mode))
+                       cfile = conf;
+       } else {
+               /* try global conf file */
+               snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
+               if (!stat(conf, &sb))
+                       if (S_ISREG(sb.st_mode))
+                               cfile = conf;
+       }
+       if (cfile)
+               conf_load(cfile);
+
        /* make work space 1 active */
        ws[0].visible = 1;
        ws[0].restack = 0;
        ws[0].focus = NULL;
-       ws[0].winno = 0;
        TAILQ_INIT(&ws[0].winlist);
        for (i = 1; i < SWM_WS_MAX; i++) {
                ws[i].visible = 0;
                ws[i].restack = 0;
                ws[i].focus = NULL;
-               ws[i].winno = 0;
                TAILQ_INIT(&ws[i].winlist);
        }
 
        /* setup status bar */
-       bar_fs = XLoadQueryFont(display,
-           "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*");
-       if (bar_fs == NULL) {
-               /* load a font that is default */
-               bar_fs = XLoadQueryFont(display,
-                   "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*");
-               if (bar_fs == NULL)
-                       errx(1, "couldn't load font");
-       }
-       bar_height = bar_fs->ascent + bar_fs->descent + 3;
+       bar_setup();
 
        XSelectInput(display, root, SubstructureRedirectMask |
            SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
            EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
-           FocusChangeMask | PropertyChangeMask);
+           FocusChangeMask | PropertyChangeMask | ExposureMask);
 
        grabkeys();
 
-       bar_window = XCreateSimpleWindow(display, root, 0, 0, width,
-           bar_height - 2, 1, 0x008080, 0x000000);
-       bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
-       XSetFont(display, bar_gc, bar_fs->fid);
-       if (bar_enabled) {
-               height -= bar_height; /* correct screen height */
-               XMapWindow(display, bar_window);
-       }
-
-       if (signal(SIGALRM, bar_signal) == SIG_ERR)
-               err(1, "could not install bar_signal");
-       bar_print();
-
        while (running) {
                XNextEvent(display, &e);
                if (handler[e.type])