JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Reexec scrotwm on alt-q, also enumerate and manage all existing windows when
authorRyan McBride <mcbride@countersiege.com>
Thu, 15 Jan 2009 02:14:08 +0000 (02:14 +0000)
committerRyan McBride <mcbride@countersiege.com>
Thu, 15 Jan 2009 02:14:08 +0000 (02:14 +0000)
scrotwm starts.

scrotwm.c

index 5890eab..20c9e0e 100644 (file)
--- a/scrotwm.c
+++ b/scrotwm.c
@@ -83,7 +83,7 @@
 #define        SWM_D_FOCUS             0x0004
 #define        SWM_D_MISC              0x0008
 
-uint32_t               swm_debug = 0
+u_int32_t              swm_debug = 0
                            | SWM_D_EVENT
                            | SWM_D_WS
                            | SWM_D_FOCUS
@@ -130,6 +130,7 @@ char                        *bar_fonts[] = {
 /* terminal + args */
 char                           *spawn_term[] = { "xterm", NULL };
 char                           *spawn_menu[] = { "dmenu_run", NULL };
+char                           *spawn_scrotwm[] = { "scrotwm", NULL };
 
 struct ws_win {
        TAILQ_ENTRY(ws_win)     entry;
@@ -356,6 +357,17 @@ quit(union arg *args)
        running = 0;
 }
 
+
+void
+restart(union arg *args)
+{      
+       XCloseDisplay(display);
+       execvp(args->argv[0], args->argv);
+       fprintf(stderr, "execvp failed\n");
+       perror(" failed");
+       quit(NULL);
+}
+
 void
 spawn(union arg *args)
 {
@@ -622,6 +634,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_m,           focus,          {.id = SWM_ARG_ID_FOCUSMAIN} },
        { MODKEY,               XK_1,           switchws,       {.id = 0} },
        { MODKEY,               XK_2,           switchws,       {.id = 1} },
@@ -740,28 +753,40 @@ buttonpress(XEvent *e)
 #endif
 }
 
-void
-configurerequest(XEvent *e)
+struct ws_win *
+manage_window(Window id)
 {
-       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;
+               if (win->id == id)
+                       return (win);   /* already being managed */
        }
 
-       XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask |
-           FocusChangeMask | ExposureMask);
-
        if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
                errx(1, "calloc: failed to allocate memory for new window");
 
-       win->id = ev->window;
+       win->id = id;
        TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
+
+       XSelectInput(display, id, ButtonPressMask | EnterWindowMask |
+           FocusChangeMask | ExposureMask);
+
+       return win;
+}
+
+
+void
+configurerequest(XEvent *e)
+{
+       XConfigureRequestEvent  *ev = &e->xconfigurerequest;
+       Window                  trans;
+       struct ws_win           *win;
+
+       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);
@@ -964,7 +989,9 @@ main(int argc, char *argv[])
        char                    conf[PATH_MAX], *cfile = NULL;
        struct stat             sb;
        XEvent                  e;
-       int                     i;
+       unsigned int            i, num;
+       Window                  d1, d2, *wins = NULL;
+       XWindowAttributes       wa;
 
        fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
        if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
@@ -1002,16 +1029,29 @@ main(int argc, char *argv[])
 
        /* make work space 1 active */
        ws[0].visible = 1;
-       ws[0].restack = 0;
-       ws[0].focus = NULL;
+       ws[0].restack = 1;
        TAILQ_INIT(&ws[0].winlist);
        for (i = 1; i < SWM_WS_MAX; i++) {
                ws[i].visible = 0;
-               ws[i].restack = 0;
+               ws[i].restack = 1;
                ws[i].focus = NULL;
                TAILQ_INIT(&ws[i].winlist);
        }
 
+       /* grab existing windows */
+       if (XQueryTree(display, root, &d1, &d2, &wins, &num)) {
+               for (i = 0; i < num; i++) {
+                        if (!XGetWindowAttributes(display, wins[i], &wa)
+                           || wa.override_redirect ||
+                           XGetTransientForHint(display, wins[i], &d1))
+                               continue;
+                       manage_window(wins[i]);
+                }
+                if(wins)
+                        XFree(wins);
+        }
+       ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
+
        /* setup status bar */
        bar_setup();
 
@@ -1021,6 +1061,7 @@ main(int argc, char *argv[])
            FocusChangeMask | PropertyChangeMask | ExposureMask);
 
        grabkeys();
+       stack();
 
        while (running) {
                XNextEvent(display, &e);