JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
new colorscheme (16-bit compliant)
[dwm.git] / client.c
index 0251445..c716625 100644 (file)
--- a/client.c
+++ b/client.c
@@ -1,10 +1,6 @@
-/* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
- * See LICENSE file for license details.
- */
+/* See LICENSE file for copyright and license details. */
 #include "dwm.h"
 #include <stdlib.h>
-#include <string.h>
-#include <X11/Xatom.h>
 #include <X11/Xutil.h>
 
 /* static */
@@ -18,6 +14,7 @@ attachstack(Client *c) {
 static void
 detachstack(Client *c) {
        Client **tc;
+
        for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
        *tc = c->snext;
 }
@@ -77,28 +74,11 @@ isprotodel(Client *c) {
 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 void
-togglemax(Client *c) {
-       XEvent ev;
-
-       if(c->isfixed)
-               return;
-       if((c->ismax = !c->ismax)) {
-               c->rx = c->x;
-               c->ry = c->y;
-               c->rw = c->w;
-               c->rh = c->h;
-               resize(c, wax, way, waw - 2 * BORDERPX, wah - 2 * BORDERPX, True);
-       }
-       else
-               resize(c, c->rx, c->ry, c->rw, c->rh, True);
-       while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
-}
-
 static int
 xerrordummy(Display *dsply, XErrorEvent *ee) {
        return 0;
@@ -115,6 +95,14 @@ attach(Client *c) {
 }
 
 void
+ban(Client *c) {
+       if(c->isbanned)
+               return;
+       XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
+       c->isbanned = True;
+}
+
+void
 configure(Client *c) {
        XConfigureEvent ce;
 
@@ -145,8 +133,8 @@ detach(Client *c) {
 
 void
 focus(Client *c) {
-       if(c && !isvisible(c))
-               return;
+       if((!c && selscreen) || (c && !isvisible(c)))
+               for(c = stack; c && !isvisible(c); c = c->snext);
        if(sel && sel != c) {
                grabbuttons(sel, False);
                XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
@@ -157,7 +145,7 @@ focus(Client *c) {
                grabbuttons(c, True);
        }
        sel = c;
-       drawstatus();
+       drawbar();
        if(!selscreen)
                return;
        if(c) {
@@ -169,51 +157,30 @@ focus(Client *c) {
 }
 
 void
-focusnext(Arg *arg) {
-       Client *c;
-   
-       if(!sel)
-               return;
-       for(c = sel->next; c && !isvisible(c); c = c->next);
-       if(!c)
-               for(c = clients; c && !isvisible(c); c = c->next);
-       if(c) {
-               focus(c);
-               restack();
-       }
-}
-
-void
-focusprev(Arg *arg) {
-       Client *c;
+killclient(const char *arg) {
+       XEvent ev;
 
        if(!sel)
                return;
-       for(c = sel->prev; c && !isvisible(c); c = c->prev);
-       if(!c) {
-               for(c = clients; c && c->next; c = c->next);
-               for(; c && !isvisible(c); c = c->prev);
-       }
-       if(c) {
-               focus(c);
-               restack();
+       if(isprotodel(sel)) {
+               ev.type = ClientMessage;
+               ev.xclient.window = sel->win;
+               ev.xclient.message_type = wmatom[WMProtocols];
+               ev.xclient.format = 32;
+               ev.xclient.data.l[0] = wmatom[WMDelete];
+               ev.xclient.data.l[1] = CurrentTime;
+               XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
        }
-}
-
-void
-killclient(Arg *arg) {
-       if(!sel)
-               return;
-       if(isprotodel(sel))
-               sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
        else
                XKillClient(dpy, sel->win);
 }
 
 void
 manage(Window w, XWindowAttributes *wa) {
-       Client *c, *t;
+       unsigned int i;
+       Client *c, *t = NULL;
        Window trans;
+       Status rettrans;
        XWindowChanges wc;
 
        c = emallocz(sizeof(Client));
@@ -223,13 +190,13 @@ manage(Window w, XWindowAttributes *wa) {
        c->y = wa->y;
        c->w = wa->width;
        c->h = wa->height;
+       c->oldborder = wa->border_width;
        if(c->w == sw && c->h == sh) {
-               c->border = 0;
                c->x = sx;
                c->y = sy;
+               c->border = wa->border_width;
        }
        else {
-               c->border = BORDERPX;
                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)
@@ -238,69 +205,54 @@ manage(Window w, XWindowAttributes *wa) {
                        c->x = wax;
                if(c->y < way)
                        c->y = way;
+               c->border = BORDERPX;
        }
+       wc.border_width = c->border;
+       XConfigureWindow(dpy, w, CWBorderWidth, &wc);
+       XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
+       configure(c); /* propagates border_width, if size doesn't change */
        updatesizehints(c);
-       XSelectInput(dpy, c->win,
+       XSelectInput(dpy, w,
                StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
-       XGetTransientForHint(dpy, c->win, &trans);
        grabbuttons(c, False);
-       wc.border_width = c->border;
-       XConfigureWindow(dpy, c->win, CWBorderWidth, &wc);
-       XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
-       configure(c); /* propagates border_width, if size doesn't change */
        updatetitle(c);
-       for(t = clients; t && t->win != c->win; t = t->next);
-       settags(c, t);
-       if(!c->isfloat)
-               c->isfloat = (t != 0) || c->isfixed;
+       if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
+               for(t = clients; t && t->win != trans; t = t->next);
+       if(t)
+               for(i = 0; i < ntags; i++)
+                       c->tags[i] = t->tags[i];
+       applyrules(c);
+       if(!c->isfloating)
+               c->isfloating = (rettrans == Success) || c->isfixed;
        attach(c);
        attachstack(c);
-       c->isbanned = True;
-       XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
+       XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
+       ban(c);
        XMapWindow(dpy, c->win);
        setclientstate(c, NormalState);
-       if(isvisible(c))
-               focus(c);
        arrange();
 }
 
-Client *
-nexttiled(Client *c) {
-       for(; c && (c->isfloat || !isvisible(c)); c = c->next);
-       return c;
-}
-
 void
 resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
-       float actual, dx, dy, max, min;
-       XWindowChanges wc;
+       double dx, dy, max, min, ratio;
+       XWindowChanges wc; 
 
-       if(w <= 0 || h <= 0)
-               return;
        if(sizehints) {
-               if(c->minw && w < c->minw)
-                       w = c->minw;
-               if(c->minh && h < c->minh)
-                       h = c->minh;
-               if(c->maxw && w > c->maxw)
-                       w = c->maxw;
-               if(c->maxh && h > c->maxh)
-                       h = c->maxh;
-               /* inspired by algorithm from fluxbox */
-               if(c->minay > 0 && c->maxay && (h - c->baseh) > 0) {
-                       dx = (float)(w - c->basew);
-                       dy = (float)(h - c->baseh);
-                       min = (float)(c->minax) / (float)(c->minay);
-                       max = (float)(c->maxax) / (float)(c->maxay);
-                       actual = dx / dy;
-                       if(max > 0 && min > 0 && actual > 0) {
-                               if(actual < min) {
+               if(c->minay > 0 && c->maxay > 0 && (h - c->baseh) > 0 && (w - c->basew) > 0) {
+                       dx = (double)(w - c->basew);
+                       dy = (double)(h - c->baseh);
+                       min = (double)(c->minax) / (double)(c->minay);
+                       max = (double)(c->maxax) / (double)(c->maxay);
+                       ratio = dx / dy;
+                       if(max > 0 && min > 0 && ratio > 0) {
+                               if(ratio < min) {
                                        dy = (dx * min + dy) / (min * min + 1);
                                        dx = dy * min;
                                        w = (int)dx + c->basew;
                                        h = (int)dy + c->baseh;
                                }
-                               else if(actual > max) {
+                               else if(ratio > max) {
                                        dy = (dx * min + dy) / (max * max + 1);
                                        dx = dy * min;
                                        w = (int)dx + c->basew;
@@ -308,15 +260,21 @@ resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
                                }
                        }
                }
+               if(c->minw && w < c->minw)
+                       w = c->minw;
+               if(c->minh && h < c->minh)
+                       h = c->minh;
+               if(c->maxw && w > c->maxw)
+                       w = c->maxw;
+               if(c->maxh && h > c->maxh)
+                       h = c->maxh;
                if(c->incw)
                        w -= (w - c->basew) % c->incw;
                if(c->inch)
                        h -= (h - c->baseh) % c->inch;
        }
-       if(w == sw && h == sh)
-               c->border = 0;
-       else
-               c->border = BORDERPX;
+       if(w <= 0 || h <= 0)
+               return;
        /* offscreen appearance fixes */
        if(x > sw)
                x = sw - w - 2 * c->border;
@@ -339,6 +297,37 @@ resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
 }
 
 void
+unban(Client *c) {
+       if(!c->isbanned)
+               return;
+       XMoveWindow(dpy, c->win, c->x, c->y);
+       c->isbanned = False;
+}
+
+void
+unmanage(Client *c) {
+       XWindowChanges wc;
+
+       wc.border_width = c->oldborder;
+       /* The server grab construct avoids race conditions. */
+       XGrabServer(dpy);
+       XSetErrorHandler(xerrordummy);
+       XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
+       detach(c);
+       detachstack(c);
+       if(sel == c)
+               focus(NULL);
+       XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
+       setclientstate(c, WithdrawnState);
+       free(c->tags);
+       free(c);
+       XSync(dpy, False);
+       XSetErrorHandler(xerror);
+       XUngrabServer(dpy);
+       arrange();
+}
+
+void
 updatesizehints(Client *c) {
        long msize;
        XSizeHints size;
@@ -350,6 +339,10 @@ updatesizehints(Client *c) {
                c->basew = size.base_width;
                c->baseh = size.base_height;
        }
+       else if(c->flags & PMinSize) {
+               c->basew = size.min_width;
+               c->baseh = size.min_height;
+       }
        else
                c->basew = c->baseh = 0;
        if(c->flags & PResizeInc) {
@@ -368,88 +361,26 @@ updatesizehints(Client *c) {
                c->minw = size.min_width;
                c->minh = size.min_height;
        }
+       else if(c->flags & PBaseSize) {
+               c->minw = size.base_width;
+               c->minh = size.base_height;
+       }
        else
                c->minw = c->minh = 0;
        if(c->flags & PAspect) {
                c->minax = size.min_aspect.x;
-               c->minay = size.min_aspect.y;
                c->maxax = size.max_aspect.x;
+               c->minay = size.min_aspect.y;
                c->maxay = size.max_aspect.y;
        }
        else
-               c->minax = c->minay = c->maxax = c->maxay = 0;
+               c->minax = c->maxax = c->minay = c->maxay = 0;
        c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
                        && c->maxw == c->minw && c->maxh == c->minh);
 }
 
 void
 updatetitle(Client *c) {
-       char **list = NULL;
-       int n;
-       XTextProperty name;
-
-       name.nitems = 0;
-       c->name[0] = 0;
-       XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
-       if(!name.nitems)
-               XGetWMName(dpy, c->win, &name);
-       if(!name.nitems)
-               return;
-       if(name.encoding == XA_STRING)
-               strncpy(c->name, (char *)name.value, sizeof c->name);
-       else {
-               if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
-               && n > 0 && *list)
-               {
-                       strncpy(c->name, *list, sizeof c->name);
-                       XFreeStringList(list);
-               }
-       }
-       XFree(name.value);
-}
-
-void
-unmanage(Client *c) {
-       Client *nc;
-
-       /* The server grab construct avoids race conditions. */
-       XGrabServer(dpy);
-       XSetErrorHandler(xerrordummy);
-       detach(c);
-       detachstack(c);
-       if(sel == c) {
-               for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
-               focus(nc);
-       }
-       XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
-       setclientstate(c, WithdrawnState);
-       free(c->tags);
-       free(c);
-       XSync(dpy, False);
-       XSetErrorHandler(xerror);
-       XUngrabServer(dpy);
-       arrange();
-}
-
-void
-zoom(Arg *arg) {
-       unsigned int n;
-       Client *c;
-
-       if(!sel)
-               return;
-       if(sel->isfloat || (arrange == dofloat)) {
-               togglemax(sel);
-               return;
-       }
-       for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
-               n++;
-
-       if((c = sel) == nexttiled(clients))
-               if(!(c = nexttiled(c->next)))
-                       return;
-       detach(c);
-       attach(c);
-       focus(c);
-       arrange();
+       if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
+               gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
 }