JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added protocol killing stuff
[dwm.git] / client.c
index 62f4d98..caa523c 100644 (file)
--- a/client.c
+++ b/client.c
@@ -3,14 +3,15 @@
  * See LICENSE file for license details.
  */
 
+#include <stdlib.h>
 #include <string.h>
 #include <X11/Xatom.h>
 
 #include "util.h"
 #include "wm.h"
 
-static void
-update_client_name(Client *c)
+void
+update_name(Client *c)
 {
        XTextProperty name;
        int n;
@@ -36,10 +37,24 @@ update_client_name(Client *c)
        XFree(name.value);
 }
 
-Client *
-create_client(Window w, XWindowAttributes *wa)
+void
+focus(Client *c)
 {
-       Client *c;
+       Client **l;
+       for(l=&stack; *l && *l != c; l=&(*l)->snext);
+       eassert(*l == c);
+       *l = c->snext;
+       c->snext = stack;
+       stack = c;
+       XRaiseWindow(dpy, c->win);
+       XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
+       XFlush(dpy);
+}
+
+void
+manage(Window w, XWindowAttributes *wa)
+{
+       Client *c, **l;
        XSetWindowAttributes twa;
        long msize;
 
@@ -58,7 +73,7 @@ create_client(Window w, XWindowAttributes *wa)
                (c->size.flags & PMinSize && c->size.flags & PMaxSize
                 && c->size.min_width == c->size.max_width
                 && c->size.min_height == c->size.max_height);
-       update_client_name(c);
+       update_name(c);
        twa.override_redirect = 1;
        twa.background_pixmap = ParentRelative;
        twa.event_mask = ExposureMask;
@@ -68,24 +83,48 @@ create_client(Window w, XWindowAttributes *wa)
                        DefaultDepth(dpy, screen), CopyFromParent,
                        DefaultVisual(dpy, screen),
                        CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
-       XFlush(dpy);
 
-#if 0
-       for(t=&client, i=0; *t; t=&(*t)->next, i++);
-       c->next = *t; /* *t == nil */
-       *t = c;
-#endif
-       return c;
+       for(l=&clients; *l; l=&(*l)->next);
+       c->next = *l; /* *l == nil */
+       *l = c;
+       c->snext = stack;
+       stack = c;
+       XMapWindow(dpy, c->win);
+       focus(c);
+}
+
+static int
+dummy_error_handler(Display *dpy, XErrorEvent *error)
+{
+       return 0;
 }
 
 void
-manage(Client *c)
+unmanage(Client *c)
 {
-       XMapRaised(dpy, c->win);
-       XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
+       Client **l;
+
+       XGrabServer(dpy);
+       XSetErrorHandler(dummy_error_handler);
+
+       XUnmapWindow(dpy, c->win);
+       XDestroyWindow(dpy, c->title);
+
+       for(l=&clients; *l && *l != c; l=&(*l)->next);
+       eassert(*l == c);
+       *l = c->next;
+       for(l=&stack; *l && *l != c; l=&(*l)->snext);
+       eassert(*l == c);
+       *l = c->snext;
+       free(c);
+
        XFlush(dpy);
+       XSetErrorHandler(error_handler);
+       XUngrabServer(dpy);
+       flush_events(EnterWindowMask);
 }
 
+
 Client *
 getclient(Window w)
 {
@@ -95,3 +134,4 @@ getclient(Window w)
                        return c;
        return NULL;
 }
+