JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
changed how manage client works
[dwm.git] / client.c
1 /*
2  * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3  * See LICENSE file for license details.
4  */
5
6 #include <stdlib.h>
7 #include <string.h>
8 #include <X11/Xatom.h>
9
10 #include "util.h"
11 #include "wm.h"
12
13 static void
14 update_client_name(Client *c)
15 {
16         XTextProperty name;
17         int n;
18         char **list = NULL;
19
20         name.nitems = 0;
21         c->name[0] = 0;
22         XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
23         if(!name.nitems)
24                 XGetWMName(dpy, c->win, &name);
25         if(!name.nitems)
26                 return;
27         if(name.encoding == XA_STRING)
28                 strncpy(c->name, (char *)name.value, sizeof(c->name));
29         else {
30                 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
31                                 && n > 0 && *list)
32                 {
33                         strncpy(c->name, *list, sizeof(c->name));
34                         XFreeStringList(list);
35                 }
36         }
37         XFree(name.value);
38 }
39
40 void
41 manage(Window w, XWindowAttributes *wa)
42 {
43         Client *c, **l;
44         XSetWindowAttributes twa;
45         long msize;
46
47         c = emallocz(sizeof(Client));
48         c->win = w;
49         c->r[RFloat].x = wa->x;
50         c->r[RFloat].y = wa->y;
51         c->r[RFloat].width = wa->width;
52         c->r[RFloat].height = wa->height;
53         c->border = wa->border_width;
54         XSetWindowBorderWidth(dpy, c->win, 0);
55         XGetTransientForHint(dpy, c->win, &c->trans);
56         if(!XGetWMNormalHints(dpy, c->win, &c->size, &msize) || !c->size.flags)
57                 c->size.flags = PSize;
58         c->fixedsize =
59                 (c->size.flags & PMinSize && c->size.flags & PMaxSize
60                  && c->size.min_width == c->size.max_width
61                  && c->size.min_height == c->size.max_height);
62         update_client_name(c);
63         twa.override_redirect = 1;
64         twa.background_pixmap = ParentRelative;
65         twa.event_mask = ExposureMask;
66
67         c->title = XCreateWindow(dpy, root, c->r[RFloat].x, c->r[RFloat].y,
68                         c->r[RFloat].width, barrect.height, 0,
69                         DefaultDepth(dpy, screen), CopyFromParent,
70                         DefaultVisual(dpy, screen),
71                         CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
72
73         for(l=&clients; *l; l=&(*l)->next);
74         c->next = *l; /* *l == nil */
75         *l = c;
76         XMapRaised(dpy, c->win);
77         XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
78         XFlush(dpy);
79 }
80
81 static int
82 dummy_error_handler(Display *dpy, XErrorEvent *error)
83 {
84         return 0;
85 }
86
87 void
88 unmanage(Client *c)
89 {
90         Client **l;
91
92         XGrabServer(dpy);
93         XSetErrorHandler(dummy_error_handler);
94
95         XUnmapWindow(dpy, c->win);
96         XDestroyWindow(dpy, c->title);
97
98         for(l=&clients; *l && *l != c; l=&(*l)->next);
99         eassert(*l == c);
100         *l = c->next;
101         free(c);
102
103         XFlush(dpy);
104         XSetErrorHandler(error_handler);
105         XUngrabServer(dpy);
106         /*flush_masked_events(EnterWindowMask); ? */
107 }
108
109
110 Client *
111 getclient(Window w)
112 {
113         Client *c;
114         for(c = clients; c; c = c->next)
115                 if(c->win == w)
116                         return c;
117         return NULL;
118 }