JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
some more additions/fixes
[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         discard_events(EnterWindowMask);
97         XFlush(dpy);
98 }
99
100 void
101 manage(Window w, XWindowAttributes *wa)
102 {
103         Client *c, **l;
104         XSetWindowAttributes twa;
105
106         c = emallocz(sizeof(Client));
107         c->win = w;
108         c->tx = c->x = wa->x;
109         c->ty = c->y = wa->y;
110         c->tw = c->w = wa->width;
111         c->h = wa->height;
112         c->th = barrect.height;
113         update_size(c);
114         XSetWindowBorderWidth(dpy, c->win, 1);
115         XSetWindowBorder(dpy, c->win, brush.border);
116         XSelectInput(dpy, c->win, CLIENT_MASK);
117         XGetTransientForHint(dpy, c->win, &c->trans);
118         twa.override_redirect = 1;
119         twa.background_pixmap = ParentRelative;
120         twa.event_mask = ExposureMask;
121
122         c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
123                         0, DefaultDepth(dpy, screen), CopyFromParent,
124                         DefaultVisual(dpy, screen),
125                         CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
126         update_name(c);
127
128         for(l=&clients; *l; l=&(*l)->next);
129         c->next = *l; /* *l == nil */
130         *l = c;
131         c->snext = stack;
132         stack = c;
133         XMapWindow(dpy, c->win);
134         XMapWindow(dpy, c->title);
135         XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
136                         GrabModeAsync, GrabModeSync, None, None);
137         XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
138                         GrabModeAsync, GrabModeSync, None, None);
139         XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
140                         GrabModeAsync, GrabModeSync, None, None);
141         resize(c);
142         focus(c);
143 }
144
145 void
146 resize(Client *c)
147 {
148         XConfigureEvent e;
149
150         XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
151         e.type = ConfigureNotify;
152         e.event = c->win;
153         e.window = c->win;
154         e.x = c->x;
155         e.y = c->y;
156         e.width = c->w;
157         e.height = c->h;
158         e.border_width = 0;
159         e.above = None;
160         e.override_redirect = False;
161         XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
162         XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
163         XSelectInput(dpy, c->win, CLIENT_MASK);
164         XFlush(dpy);
165 }
166
167 static int
168 dummy_error_handler(Display *dpy, XErrorEvent *error)
169 {
170         return 0;
171 }
172
173 void
174 unmanage(Client *c)
175 {
176         Client **l;
177
178         XGrabServer(dpy);
179         XSetErrorHandler(dummy_error_handler);
180
181         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
182         XDestroyWindow(dpy, c->title);
183
184         for(l=&clients; *l && *l != c; l=&(*l)->next);
185         eassert(*l == c);
186         *l = c->next;
187         for(l=&stack; *l && *l != c; l=&(*l)->snext);
188         eassert(*l == c);
189         *l = c->snext;
190         free(c);
191
192         XFlush(dpy);
193         XSetErrorHandler(error_handler);
194         XUngrabServer(dpy);
195         if(stack)
196                 focus(stack);
197 }
198
199 Client *
200 gettitle(Window w)
201 {
202         Client *c;
203         for(c = clients; c; c = c->next)
204                 if(c->title == w)
205                         return c;
206         return NULL;
207 }
208
209 Client *
210 getclient(Window w)
211 {
212         Client *c;
213         for(c = clients; c; c = c->next)
214                 if(c->win == w)
215                         return c;
216         return NULL;
217 }
218
219 void
220 draw_client(Client *c)
221 {
222         if(c == stack)
223                 draw_bar();
224
225         c->tw = textwidth(&brush.font, c->name) + labelheight(&brush.font);
226         c->tx = c->x + c->w - c->tw + 2;
227         c->ty = c->y;
228         XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
229
230         brush.rect.x = brush.rect.y = 0;
231         brush.rect.width = c->tw;
232         brush.rect.height = c->th;
233
234         draw(dpy, &brush, True, c->name);
235         XCopyArea(dpy, brush.drawable, c->title, brush.gc,
236                         0, 0, c->tw, c->th, 0, 0);
237         XFlush(dpy);
238 }