JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
made some changes more concistent
[dwm.git] / client.c
index 5309d0e..f3c9bf2 100644 (file)
--- a/client.c
+++ b/client.c
@@ -7,7 +7,7 @@
 #include <X11/Xatom.h>
 #include <X11/Xutil.h>
 
-/* static functions */
+/* static */
 
 static void
 detachstack(Client *c) {
@@ -53,12 +53,19 @@ grabbuttons(Client *c, Bool focused) {
                                GrabModeAsync, GrabModeSync, None, None);
 }
 
+static void
+setclientstate(Client *c, long state) {
+       long data[] = {state, None};
+       XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
+                       PropModeReplace, (unsigned char *)data, 2);
+}
+
 static int
 xerrordummy(Display *dsply, XErrorEvent *ee) {
        return 0;
 }
 
-/* extern functions */
+/* extern */
 
 void
 configure(Client *c) {
@@ -79,27 +86,28 @@ configure(Client *c) {
 
 void
 focus(Client *c) {
-       Client *old = sel;
-
        if(c && !isvisible(c))
                return;
-
-       if(old && old != c) {
-               grabbuttons(old, False);
-               XSetWindowBorder(dpy, old->win, dc.norm[ColBorder]);
+       if(sel && sel != c) {
+               grabbuttons(sel, False);
+               XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
        }
        if(c) {
                detachstack(c);
                c->snext = stack;
                stack = c;
                grabbuttons(c, True);
+       }
+       sel = c;
+       drawstatus();
+       if(!selscreen)
+               return;
+       if(c) {
                XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
                XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
        }
-       else if(issel)
+       else
                XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
-       sel = c;
-       drawstatus();
 }
 
 Client *
@@ -141,14 +149,14 @@ manage(Window w, XWindowAttributes *wa) {
        }
        else {
                c->border = BORDERPX;
-               if(c->x < wax)
-                       c->x = wax;
-               if(c->y < way)
-                       c->y = way;
                if(c->x + c->w + 2 * c->border > wax + waw)
                        c->x = wax + waw - c->w - 2 * c->border;
                if(c->y + c->h + 2 * c->border > way + wah)
                        c->y = way + wah - c->h - 2 * c->border;
+               if(c->x < wax)
+                       c->x = wax;
+               if(c->y < way)
+                       c->y = way;
        }
        updatesizehints(c);
        c->proto = getproto(c->win);
@@ -156,6 +164,7 @@ manage(Window w, XWindowAttributes *wa) {
                StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
        XGetTransientForHint(dpy, c->win, &trans);
        grabbuttons(c, False);
+       XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
        updatetitle(c);
        settags(c, getclient(trans));
        if(!c->isfloat)
@@ -167,6 +176,7 @@ manage(Window w, XWindowAttributes *wa) {
        stack = clients = c;
        XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
        XMapWindow(dpy, c->win);
+       setclientstate(c, NormalState);
        if(isvisible(c))
                focus(c);
        arrange();
@@ -176,6 +186,8 @@ void
 resize(Client *c, Bool sizehints) {
        XWindowChanges wc;
 
+       if(c->w <= 0 || c->h <= 0)
+               return;
        if(sizehints) {
                if(c->incw)
                        c->w -= (c->w - c->basew) % c->incw;
@@ -195,14 +207,14 @@ resize(Client *c, Bool sizehints) {
        else
                c->border = BORDERPX;
        /* offscreen appearance fixes */
-       if(c->x + c->w + 2 * c->border < sx)
-               c->x = sx;
-       if(c->y + c->h + 2 * c->border < sy)
-               c->y = sy;
        if(c->x > sw)
                c->x = sw - c->w - 2 * c->border;
        if(c->y > sh)
                c->y = sh - c->h - 2 * c->border;
+       if(c->x + c->w + 2 * c->border < sx)
+               c->x = sx;
+       if(c->y + c->h + 2 * c->border < sy)
+               c->y = sy;
        wc.x = c->x;
        wc.y = c->y;
        wc.width = c->w;
@@ -289,6 +301,7 @@ unmanage(Client *c) {
                focus(nc);
        }
        XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
+       setclientstate(c, WithdrawnState);
        free(c->tags);
        free(c);
        XSync(dpy, False);