JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
somehow if( and for( remained in the code borrowed from dwm. Kill that dead.
[spectrwm.git] / scrotwm.c
index eefbd2a..7a13f55 100644 (file)
--- a/scrotwm.c
+++ b/scrotwm.c
@@ -100,6 +100,8 @@ u_int32_t           swm_debug = 0
 #define MODKEY                 Mod1Mask
 #define CLEANMASK(mask)         (mask & ~(numlockmask | LockMask))
 
+char                   **start_argv;
+Atom                   astate;
 int                    (*xerrorxlib)(Display *, XErrorEvent *);
 int                    other_wm;
 int                    screen;
@@ -131,7 +133,6 @@ char                        *bar_fonts[] = {
 /* terminal + args */
 char                           *spawn_term[] = { "xterm", NULL };
 char                           *spawn_menu[] = { "dmenu_run", NULL };
-char                           *spawn_scrotwm[] = { "scrotwm", NULL };
 
 /* layout manager data */
 struct swm_geometry {
@@ -141,6 +142,19 @@ struct swm_geometry {
        int                     h;
 };
 
+
+struct ws_win {
+       TAILQ_ENTRY(ws_win)     entry;
+       Window                  id;
+       struct swm_geometry     g;
+       int                     floating;
+       int                     transient;
+       XWindowAttributes       wa;
+       XSizeHints              sh;
+};
+TAILQ_HEAD(ws_win_list, ws_win);
+
+/* layout handlers */
 void   stack(void);
 void   vertical_init(int);
 void   vertical_resize(int);
@@ -149,33 +163,22 @@ void      horizontal_init(int);
 void   horizontal_resize(int);
 void   horizontal_stack(struct swm_geometry *);
 void   max_init(int);
+void   max_focus(struct ws_win *);
 void   max_stack(struct swm_geometry *);
 
 struct layout {
        void                    (*l_init)(int); /* init/reset */
        void                    (*l_stack)(struct swm_geometry *);
        void                    (*l_resize)(int);
+       void                    (*l_focus)(struct ws_win *);
 } layouts[] =  {
-       /* init                 stack,                  resize*/
-       { vertical_init,        vertical_stack,         vertical_resize},
-       { horizontal_init,      horizontal_stack,       horizontal_resize},
-       /* XXX not working yet
-        * { max_init,          max_stack,              NULL},
-        */
-       { NULL,                 NULL},
-};
-
-struct ws_win {
-       TAILQ_ENTRY(ws_win)     entry;
-       Window                  id;
-       struct swm_geometry     g;
-       int                     floating;
-       int                     transient;
-       XWindowAttributes       wa;
-       XSizeHints              sh;
+       /* init                 stack,                  resize */
+       { vertical_init,        vertical_stack,         vertical_resize,        NULL},
+       { horizontal_init,      horizontal_stack,       horizontal_resize,      NULL},
+       { NULL,                 max_stack,              NULL,                   max_focus},
+       { NULL,                 NULL,                   NULL,                   NULL},
 };
 
-TAILQ_HEAD(ws_win_list, ws_win);
 
 /* define work spaces */
 #define SWM_WS_MAX             (10)
@@ -329,7 +332,7 @@ bar_toggle(union arg *args)
                XUnmapWindow(display, bar_window);
        } else {
                bar_enabled = 1;
-               XMapWindow(display, bar_window);
+               XMapRaised(display, bar_window);
        }
        XSync(display, False);
        for (i = 0; i < SWM_WS_MAX; i++)
@@ -359,7 +362,7 @@ bar_setup(void)
        XSetFont(display, bar_gc, bar_fs->fid);
        XSelectInput(display, bar_window, VisibilityChangeMask);
        if (bar_enabled) {
-               XMapWindow(display, bar_window);
+               XMapRaised(display, bar_window);
        }
        DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
 
@@ -368,6 +371,27 @@ bar_setup(void)
        bar_print();
 }
 
+void
+config_win(struct ws_win *win)
+{
+       XConfigureEvent         ce;
+
+       DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
+           win->id, win->g.x, win->g.y, win->g.w, win->g.h);
+       ce.type = ConfigureNotify;
+       ce.display = display;
+       ce.event = win->id;
+       ce.window = win->id;
+       ce.x = win->g.x;
+       ce.y = win->g.y;
+       ce.width = win->g.w;
+       ce.height = win->g.h;
+       ce.border_width = 1; /* XXX store this! */
+       ce.above = None;
+       ce.override_redirect = False;
+       XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
+}
+
 int
 count_win(int wsid, int count_transient)
 {
@@ -375,6 +399,8 @@ count_win(int wsid, int count_transient)
        int                     count = 0;
 
        TAILQ_FOREACH (win, &ws[wsid].winlist, entry) {
+               if (count_transient == 0 && win->floating)
+                       continue;
                if (count_transient == 0 && win->transient)
                        continue;
                count++;
@@ -390,12 +416,13 @@ quit(union arg *args)
        running = 0;
 }
 
-
 void
 restart(union arg *args)
 {
+       DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
+
        XCloseDisplay(display);
-       execvp(args->argv[0], args->argv);
+       execvp(start_argv[0], start_argv);
        fprintf(stderr, "execvp failed\n");
        perror(" failed");
        quit(NULL);
@@ -409,9 +436,9 @@ spawn(union arg *args)
         * The double-fork construct avoids zombie processes and keeps the code
         * clean from stupid signal handlers.
         */
-       if(fork() == 0) {
-               if(fork() == 0) {
-                       if(display)
+       if (fork() == 0) {
+               if (fork() == 0) {
+                       if (display)
                                close(ConnectionNumber(display));
                        setsid();
                        execvp(args->argv[0], args->argv);
@@ -564,6 +591,9 @@ focus(union arg *args)
 
        unfocus_win(winlostfocus);
        focus_win(winfocus);
+       /* XXX if we hook in focus_win(), we get a nasty cycle */
+       if (ws[current_ws].cur_layout->l_focus != NULL)
+               ws[current_ws].cur_layout->l_focus(winfocus);
        XSync(display, False);
 }
 
@@ -575,6 +605,8 @@ cycle_layout(union arg *args)
        ws[current_ws].cur_layout++;
        if (ws[current_ws].cur_layout->l_stack == NULL)
                ws[current_ws].cur_layout = &layouts[0];
+       ignore_enter = 1;
+
        stack();
 }
 
@@ -621,6 +653,22 @@ stack_floater(struct ws_win *win)
        unsigned int            mask;
        XWindowChanges          wc;
 
+#if 0
+       bzero(&wc, sizeof wc);
+       wc.border_width = 1;
+       mask = CWX | CWY | CWBorderWidth;
+
+       win->g.w = wc.width = win->wa.width;
+       win->g.h = wc.height = win->wa.height;
+       win->g.x = wc.x = (ws[current_ws].g.w - win->wa.width) / 2;
+       win->g.y = wc.y = (ws[current_ws].g.h - win->wa.height) / 2;
+
+       DNPRINTF(SWM_D_EVENT, "stack_floater: win %d x %d y %d w %d h %d\n",
+           win, wc.x, wc.y, wc.width, wc.height);
+
+       XConfigureWindow(display, win->id, mask, &wc);
+return;
+#endif
        bzero(&wc, sizeof wc);
        wc.border_width = 1;
        mask = CWX | CWY | CWBorderWidth;
@@ -638,7 +686,7 @@ stack_floater(struct ws_win *win)
                if (win->sh.min_width < ws[current_ws].g.w / 10)
                        win->sh.min_width = ws[current_ws].g.w / 3;
                if (win->sh.min_height < ws[current_ws].g.h / 10)
-                       win->wa.height = ws[current_ws].g.h / 3;
+                       win->sh.height = ws[current_ws].g.h / 3;
 
                win->g.w = wc.width = win->sh.min_width * 2;
                win->g.h = wc.height = win->sh.min_height * 2;
@@ -711,7 +759,6 @@ vertical_resize(int id)
        stack();
 }
 
-/* I know this sucks but it works well enough */
 void
 vertical_stack(struct swm_geometry *g) {
        XWindowChanges          wc;
@@ -722,11 +769,10 @@ vertical_stack(struct swm_geometry *g) {
 
        DNPRINTF(SWM_D_EVENT, "vertical_stack: workspace: %d\n", current_ws);
 
-       winfocus->id = root;
-
        winno = count_win(current_ws, 0);
        if (winno == 0)
                return;
+       winfocus->id = root;
 
        if (winno > 1)
                gg.w = vertical_msize[current_ws];
@@ -824,11 +870,10 @@ horizontal_stack(struct swm_geometry *g) {
 
        DNPRINTF(SWM_D_EVENT, "horizontal_stack: workspace: %d\n", current_ws);
 
-       winfocus->id = root;
-
        winno = count_win(current_ws, 0);
        if (winno == 0)
                return;
+       winfocus->id = root;
 
        if (winno > 1)
                gg.h = horizontal_msize[current_ws];
@@ -882,36 +927,43 @@ horizontal_stack(struct swm_geometry *g) {
        focus_win(winfocus); /* this has to be done outside of the loop */
 }
 
-/* fullscreen view */
 void
-max_init(int ws_idx)
+max_focus(struct ws_win *fwin)
 {
-       DNPRINTF(SWM_D_EVENT, "max_init: workspace: %d\n", ws_idx);
+       struct ws_win *win;
+
+       TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
+               if (win->transient == 0 && win->floating == 0 && win == fwin)
+                       XMapRaised(display, win->id);
+               else
+                       XUnmapWindow(display, win->id);
+       }
 }
 
+/* fullscreen view */
 void
 max_stack(struct swm_geometry *g) {
        XWindowChanges          wc;
        struct swm_geometry     gg = *g;
-       struct ws_win           wf, *win, *winfocus = &wf;
-       int                     i, winno;
-       unsigned int mask;
+       struct ws_win           *win, *winfocus;
+       unsigned int            mask;
 
        DNPRINTF(SWM_D_EVENT, "max_stack: workspace: %d\n", current_ws);
 
-       winfocus->id = root;
-
-       winno = count_win(current_ws, 0);
-       if (winno == 0)
+       if (count_win(current_ws, 0) == 0)
                return;
 
-       TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
-               if (i == 1 && win->transient == 0 && win->floating != 0) {
-                       gg.h -= 2;
-                       gg.w -= 2;
-
-                       winfocus = win;
+       winfocus = ws[current_ws].focus;
+       if (!winfocus)
+               winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
 
+       TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
+               if (win->transient != 0 || win->floating != 0) {
+                       if (win == winfocus)
+                               stack_floater(win); /* XXX maximize? */
+                       else
+                               XUnmapWindow(display, win->id);
+               } else {
                        bzero(&wc, sizeof wc);
                        wc.border_width = 1;
                        win->g.x = wc.x = gg.x;
@@ -921,12 +973,11 @@ max_stack(struct swm_geometry *g) {
                        mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
                        XConfigureWindow(display, win->id, mask, &wc);
 
-                       XMapRaised(display, win->id);
-               } else {
-                       /* hide all but the master window */
-                       XUnmapWindow(display, win->id);
+                       if (winfocus == win)
+                               XMapRaised(display, win->id);
+                       else
+                               XUnmapWindow(display, win->id);
                }
-               i++;
        }
 
        focus_win(winfocus); /* this has to be done outside of the loop */
@@ -979,7 +1030,7 @@ struct key {
        { MODKEY | ShiftMask,   XK_Return,      spawn,          {.argv = spawn_term} },
        { MODKEY,               XK_p,           spawn,          {.argv = spawn_menu} },
        { MODKEY | ShiftMask,   XK_q,           quit,           {0} },
-       { MODKEY,               XK_q,           restart,        {.argv = spawn_scrotwm } },
+       { MODKEY,               XK_q,           restart,        {0} },
        { MODKEY,               XK_m,           focus,          {.id = SWM_ARG_ID_FOCUSMAIN} },
        { MODKEY,               XK_1,           switchws,       {.id = 0} },
        { MODKEY,               XK_2,           switchws,       {.id = 1} },
@@ -1036,9 +1087,9 @@ grabkeys(void)
        updatenumlockmask();
 
        XUngrabKey(display, AnyKey, AnyModifier, root);
-       for(i = 0; i < LENGTH(keys); i++) {
-               if((code = XKeysymToKeycode(display, keys[i].keysym)))
-                       for(j = 0; j < LENGTH(modifiers); j++)
+       for (i = 0; i < LENGTH(keys); i++) {
+               if ((code = XKeysymToKeycode(display, keys[i].keysym)))
+                       for (j = 0; j < LENGTH(modifiers); j++)
                                XGrabKey(display, code,
                                    keys[i].mod | modifiers[j], root,
                                    True, GrabModeAsync, GrabModeAsync);
@@ -1060,8 +1111,8 @@ keypress(XEvent *e)
        DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
 
        keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
-       for(i = 0; i < LENGTH(keys); i++)
-               if(keysym == keys[i].keysym
+       for (i = 0; i < LENGTH(keys); i++)
+               if (keysym == keys[i].keysym
                   && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
                   && keys[i].func)
                        keys[i].func(&(keys[i].args));
@@ -1098,10 +1149,23 @@ buttonpress(XEvent *e)
 #endif
 }
 
+void
+set_win_state(struct ws_win *win, long state)
+{
+       long                    data[] = {state, None};
+
+       DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
+
+       XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
+           (unsigned char *)data, 2);
+}
+
 struct ws_win *
 manage_window(Window id)
 {
+       Window                  trans;
        struct ws_win           *win;
+       XClassHint              ch;
 
        TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
                if (win->id == id)
@@ -1113,54 +1177,75 @@ manage_window(Window id)
 
        win->id = id;
        TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
+       ws[current_ws].focus = win; /* make new win focused */
+
+       XGetTransientForHint(display, win->id, &trans);
+       if (trans) {
+               win->transient = trans;
+               DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
+                   (unsigned)win->id, win->transient);
+       }
+       XGetWindowAttributes(display, win->id, &win->wa);
+       XGetNormalHints(display, win->id, &win->sh);
+
+       /* XXX */
+       bzero(&ch, sizeof ch);
+       if (XGetClassHint(display, win->id, &ch)) {
+               /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
+               if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
+                       win->floating = 1;
+               }
+               if (ch.res_class)
+                       XFree(ch.res_class);
+               if (ch.res_name)
+                       XFree(ch.res_name);
+       }
 
        XSelectInput(display, id, ButtonPressMask | EnterWindowMask |
            FocusChangeMask | ExposureMask);
 
-       return win;
+       set_win_state(win, NormalState);
+
+       return (win);
 }
 
 void
 configurerequest(XEvent *e)
 {
        XConfigureRequestEvent  *ev = &e->xconfigurerequest;
-       Window                  trans;
        struct ws_win           *win;
+       int                     new = 1;
+       XWindowChanges          wc;
 
-       DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
-
-
-       win = manage_window(ev->window);
-       ws[current_ws].focus = win; /* make new win focused */
-
-       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);
-       }
-       XGetWindowAttributes(display, win->id, &win->wa);
-       XGetNormalHints(display, win->id, &win->sh);
-#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;
+       TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
+               if (win->id == ev->window) {
+                       new = 0;
+                       break;
                }
-               if(ch.res_class)
-                       XFree(ch.res_class);
-               if(ch.res_name)
-                       XFree(ch.res_name);
        }
-#endif
-       stack();
+
+       if (new) {
+               DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
+                   ev->window);
+               bzero(&wc, sizeof wc);
+               wc.x = ev->x;
+               wc.y = ev->y;
+               wc.width = ev->width;
+               wc.height = ev->height;
+               wc.border_width = ev->border_width;
+               wc.sibling = ev->above;
+               wc.stack_mode = ev->detail;
+               XConfigureWindow(display, ev->window, ev->value_mask, &wc);
+       } else {
+               DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
+                   ev->window);
+               config_win(win);
+       }
 }
 
 void
 configurenotify(XEvent *e)
 {
-
        DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
            e->xconfigure.window);
 }
@@ -1185,6 +1270,7 @@ destroynotify(XEvent *e)
                                ws[current_ws].focus = NULL;
        
                        TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
+                       set_win_state(win, WithdrawnState);
                        free(win);
                        break;
                }
@@ -1201,7 +1287,7 @@ enternotify(XEvent *e)
 
        DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
 
-       if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
+       if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
            ev->window != root)
                return;
        if (ignore_enter) {
@@ -1224,8 +1310,6 @@ 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;
        /*
@@ -1247,16 +1331,23 @@ mappingnotify(XEvent *e)
        DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
 
        XRefreshKeyboardMapping(ev);
-       if(ev->request == MappingKeyboard)
+       if (ev->request == MappingKeyboard)
                grabkeys();
 }
 
 void
 maprequest(XEvent *e)
 {
+       XMapRequestEvent        *ev = &e->xmaprequest;
+       XWindowAttributes       wa;
+
        DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
            e->xmaprequest.window);
 
+       if (!XGetWindowAttributes(display, ev->window, &wa))
+               return;
+       if (wa.override_redirect)
+               return;
        manage_window(e->xmaprequest.window);
        stack();
 }
@@ -1310,8 +1401,7 @@ xerror_start(Display *d, XErrorEvent *ee)
 int
 xerror(Display *d, XErrorEvent *ee)
 {
-       fprintf(stderr, "error: %p %p\n", display, ee);
-
+       /* fprintf(stderr, "error: %p %p\n", display, ee); */
        return (-1);
 }
 
@@ -1325,7 +1415,7 @@ active_wm(void)
        XSelectInput(display, DefaultRootWindow(display),
            SubstructureRedirectMask);
        XSync(display, False);
-       if(other_wm)
+       if (other_wm)
                return (1);
 
        XSetErrorHandler(xerror);
@@ -1333,6 +1423,26 @@ active_wm(void)
        return (0);
 }
 
+long
+getstate(Window w)
+{
+       int                     format, status;
+       long                    result = -1;
+       unsigned char           *p = NULL;
+       unsigned long           n, extra;
+       Atom                    real;
+
+       astate = XInternAtom(display, "WM_STATE", False);
+       status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
+           &real, &format, &n, &extra, (unsigned char **)&p);
+       if (status != Success)
+               return (-1);
+       if (n != 0)
+               result = *p;
+       XFree(p);
+       return (result);
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -1344,11 +1454,12 @@ main(int argc, char *argv[])
        Window                  d1, d2, *wins = NULL;
        XWindowAttributes       wa;
 
+       start_argv = argv;
        fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
-       if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
+       if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
                warnx("no locale support");
 
-       if(!(display = XOpenDisplay(0)))
+       if (!(display = XOpenDisplay(0)))
                errx(1, "can not open display");
 
        if (active_wm())
@@ -1356,6 +1467,7 @@ main(int argc, char *argv[])
 
        screen = DefaultScreen(display);
        root = RootWindow(display, screen);
+       astate = XInternAtom(display, "WM_STATE", False);
 
        /* look for local and global conf file */
        pwd = getpwuid(getuid());
@@ -1398,14 +1510,25 @@ main(int argc, char *argv[])
 
        /* grab existing windows */
        if (XQueryTree(display, root, &d1, &d2, &wins, &num)) {
+               /* normal windows */
+               for (i = 0; i < num; i++) {
+                        XGetWindowAttributes(display, wins[i], &wa);
+                       if (!XGetWindowAttributes(display, wins[i], &wa) ||
+                           wa.override_redirect || XGetTransientForHint(display, wins[i], &d1))
+                               continue;
+                       if (wa.map_state == IsViewable || getstate(wins[i]) == NormalState)
+                               manage_window(wins[i]);
+               }
+               /* transient windows */
                for (i = 0; i < num; i++) {
-                        if (!XGetWindowAttributes(display, wins[i], &wa)
-                           || wa.override_redirect ||
-                           XGetTransientForHint(display, wins[i], &d1))
+                       if (!XGetWindowAttributes(display, wins[i], &wa))
                                continue;
-                       manage_window(wins[i]);
+                       if (XGetTransientForHint(display, wins[i], &d1) &&
+                           (wa.map_state == IsViewable || getstate(wins[i]) ==
+                           NormalState))
+                               manage_window(wins[i]);
                 }
-                if(wins)
+                if (wins)
                         XFree(wins);
         }
        ws[0].focus = TAILQ_FIRST(&ws[0].winlist);