JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed several other stuff, coming closer to something useful
[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 #define CLIENT_MASK             (StructureNotifyMask | PropertyChangeMask | EnterWindowMask)
14
15 void
16 update_name(Client *c)
17 {
18         XTextProperty name;
19         int n;
20         char **list = NULL;
21
22         name.nitems = 0;
23         c->name[0] = 0;
24         XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
25         if(!name.nitems)
26                 XGetWMName(dpy, c->win, &name);
27         if(!name.nitems)
28                 return;
29         if(name.encoding == XA_STRING)
30                 strncpy(c->name, (char *)name.value, sizeof(c->name));
31         else {
32                 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
33                                 && n > 0 && *list)
34                 {
35                         strncpy(c->name, *list, sizeof(c->name));
36                         XFreeStringList(list);
37                 }
38         }
39         XFree(name.value);
40 }
41
42 void
43 update_size(Client *c)
44 {
45         XSizeHints size;
46         long msize;
47         if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
48                 size.flags = PSize;
49         c->flags = size.flags;
50         if(c->flags & PBaseSize) {
51                 c->basew = size.base_width;
52                 c->baseh = size.base_height;
53         }
54         else
55                 c->basew = c->baseh = 0;
56         if(c->flags & PResizeInc) {
57                 c->incw = size.width_inc;
58                 c->inch = size.height_inc;
59         }
60         else
61                 c->incw = c->inch = 0;
62         if(c->flags & PMaxSize) {
63                 c->maxw = size.max_width;
64                 c->maxh = size.max_height;
65         }
66         else
67                 c->maxw = c->maxh = 0;
68         if(c->flags & PMinSize) {
69                 c->minw = size.min_width;
70                 c->minh = size.min_height;
71         }
72         else
73                 c->minw = c->minh = 0;
74 }
75
76 void
77 focus(Client *c)
78 {
79         Client **l, *old;
80
81         old = stack;
82         for(l=&stack; *l && *l != c; l=&(*l)->snext);
83         eassert(*l == c);
84         *l = c->snext;
85         c->snext = stack;
86         stack = c;
87         XRaiseWindow(dpy, c->win);
88         XRaiseWindow(dpy, c->title);
89         XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
90         if(old && old != c) {
91                 XMapWindow(dpy, old->title);
92                 draw_client(old);
93         }
94         XUnmapWindow(dpy, c->title);
95         draw_bar();
96         XFlush(dpy);
97 }
98
99 void
100 manage(Window w, XWindowAttributes *wa)
101 {
102         Client *c, **l;
103         XSetWindowAttributes twa;
104
105         c = emallocz(sizeof(Client));
106         c->win = w;
107         c->tx = c->x = wa->x;
108         c->ty = c->y = wa->y;
109         c->tw = c->w = wa->width;
110         c->h = wa->height;
111         c->th = barrect.height;
112         update_size(c);
113         XSetWindowBorderWidth(dpy, c->win, 1);
114         XSetWindowBorder(dpy, c->win, brush.border);
115         XSelectInput(dpy, c->win, CLIENT_MASK);
116         XGetTransientForHint(dpy, c->win, &c->trans);
117         twa.override_redirect = 1;
118         twa.background_pixmap = ParentRelative;
119         twa.event_mask = SubstructureNotifyMask | ExposureMask;
120
121         c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
122                         0, DefaultDepth(dpy, screen), CopyFromParent,
123                         DefaultVisual(dpy, screen),
124                         CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
125         update_name(c);
126
127         for(l=&clients; *l; l=&(*l)->next);
128         c->next = *l; /* *l == nil */
129         *l = c;
130         c->snext = stack;
131         stack = c;
132         XMapWindow(dpy, c->win);
133         XMapWindow(dpy, c->title);
134         XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
135                         GrabModeAsync, GrabModeSync, None, None);
136         XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
137                         GrabModeAsync, GrabModeSync, None, None);
138         XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
139                         GrabModeAsync, GrabModeSync, None, None);
140         resize(c);
141         focus(c);
142 }
143
144 void
145 resize(Client *c)
146 {
147         XConfigureEvent e;
148
149         XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
150         e.type = ConfigureNotify;
151         e.event = c->win;
152         e.window = c->win;
153         e.x = c->x;
154         e.y = c->y;
155         e.width = c->w;
156         e.height = c->h;
157         e.border_width = 0;
158         e.above = None;
159         e.override_redirect = False;
160         XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
161         XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
162         XSelectInput(dpy, c->win, CLIENT_MASK);
163         XFlush(dpy);
164 }
165
166 static int
167 dummy_error_handler(Display *dpy, XErrorEvent *error)
168 {
169         return 0;
170 }
171
172 void
173 unmanage(Client *c)
174 {
175         Client **l;
176
177         XGrabServer(dpy);
178         XSetErrorHandler(dummy_error_handler);
179
180         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
181         XDestroyWindow(dpy, c->title);
182
183         for(l=&clients; *l && *l != c; l=&(*l)->next);
184         eassert(*l == c);
185         *l = c->next;
186         for(l=&stack; *l && *l != c; l=&(*l)->snext);
187         eassert(*l == c);
188         *l = c->snext;
189         free(c);
190
191         XFlush(dpy);
192         XSetErrorHandler(error_handler);
193         XUngrabServer(dpy);
194         discard_events(EnterWindowMask);
195         if(stack)
196                 focus(stack);
197 }
198
199
200 Client *
201 getclient(Window w)
202 {
203         Client *c;
204         for(c = clients; c; c = c->next)
205                 if(c->win == w)
206                         return c;
207         return NULL;
208 }
209
210 void
211 draw_client(Client *c)
212 {
213         if(c == stack)
214                 draw_bar();
215
216         c->tw = textwidth(&brush.font, c->name) + labelheight(&brush.font);
217         c->tx = c->x + c->w - c->tw + 2;
218         c->ty = c->y;
219         XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
220
221         brush.rect.x = brush.rect.y = 0;
222         brush.rect.width = c->tw;
223         brush.rect.height = c->th;
224
225         draw(dpy, &brush, True, c->name);
226         XCopyArea(dpy, brush.drawable, c->title, brush.gc,
227                         0, 0, c->tw, c->th, 0, 0);
228         XFlush(dpy);
229 }