dwm-meillo

view client.c @ 687:a76799907854

removed client title bar
author Anselm R. Garbe <arg@suckless.org>
date Sun, 14 Jan 2007 22:27:29 +0100
parents fd2ea58b0b94
children 399f08187c27
line source
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
3 */
4 #include "dwm.h"
5 #include <stdlib.h>
6 #include <string.h>
7 #include <X11/Xatom.h>
8 #include <X11/Xutil.h>
10 /* static functions */
12 static void
13 detachstack(Client *c) {
14 Client **tc;
15 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
16 *tc = c->snext;
17 }
19 static void
20 grabbuttons(Client *c, Bool focused) {
21 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
23 if(focused) {
24 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
25 GrabModeAsync, GrabModeSync, None, None);
26 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
27 GrabModeAsync, GrabModeSync, None, None);
28 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
29 GrabModeAsync, GrabModeSync, None, None);
30 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
31 GrabModeAsync, GrabModeSync, None, None);
33 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
34 GrabModeAsync, GrabModeSync, None, None);
35 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
36 GrabModeAsync, GrabModeSync, None, None);
37 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
38 GrabModeAsync, GrabModeSync, None, None);
39 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
40 GrabModeAsync, GrabModeSync, None, None);
42 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
43 GrabModeAsync, GrabModeSync, None, None);
44 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
45 GrabModeAsync, GrabModeSync, None, None);
46 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
47 GrabModeAsync, GrabModeSync, None, None);
48 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
49 GrabModeAsync, GrabModeSync, None, None);
50 }
51 else
52 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
53 GrabModeAsync, GrabModeSync, None, None);
54 }
56 static int
57 xerrordummy(Display *dsply, XErrorEvent *ee) {
58 return 0;
59 }
61 /* extern functions */
63 void
64 configure(Client *c) {
65 XEvent synev;
67 synev.type = ConfigureNotify;
68 synev.xconfigure.display = dpy;
69 synev.xconfigure.event = c->win;
70 synev.xconfigure.window = c->win;
71 synev.xconfigure.x = c->x;
72 synev.xconfigure.y = c->y;
73 synev.xconfigure.width = c->w;
74 synev.xconfigure.height = c->h;
75 synev.xconfigure.border_width = c->border;
76 synev.xconfigure.above = None;
77 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
78 }
80 void
81 focus(Client *c) {
82 Client *old;
84 if(!issel || (c && !isvisible(c)))
85 return;
86 if(!sel)
87 sel = c;
88 else if(sel != c) {
89 old = sel;
90 sel = c;
91 if(old) {
92 grabbuttons(old, False);
93 drawclient(old);
94 }
95 }
96 if(c) {
97 detachstack(c);
98 c->snext = stack;
99 stack = c;
100 grabbuttons(c, True);
101 drawclient(c);
102 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
103 }
104 else
105 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
106 }
108 Client *
109 getclient(Window w) {
110 Client *c;
112 for(c = clients; c; c = c->next)
113 if(c->win == w)
114 return c;
115 return NULL;
116 }
118 void
119 killclient(Arg *arg) {
120 if(!sel)
121 return;
122 if(sel->proto & PROTODELWIN)
123 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
124 else
125 XKillClient(dpy, sel->win);
126 }
128 void
129 manage(Window w, XWindowAttributes *wa) {
130 Client *c;
131 Window trans;
133 c = emallocz(sizeof(Client));
134 c->tags = emallocz(ntags * sizeof(Bool));
135 c->win = w;
136 c->border = 0;
137 c->x = wa->x;
138 c->y = wa->y;
139 c->w = wa->width;
140 c->h = wa->height;
141 updatesizehints(c);
142 if(c->x + c->w + 2 * BORDERPX > sw)
143 c->x = sw - c->w - 2 * BORDERPX;
144 if(c->x < sx)
145 c->x = sx;
146 if(c->y + c->h + 2 * BORDERPX > sh)
147 c->y = sh - c->h - 2 * BORDERPX;
148 if(c->h != sh && c->y < bh)
149 c->y = bh;
150 c->proto = getproto(c->win);
151 XSelectInput(dpy, c->win,
152 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
153 XGetTransientForHint(dpy, c->win, &trans);
154 grabbuttons(c, False);
155 updatetitle(c);
156 settags(c, getclient(trans));
157 if(!c->isfloat)
158 c->isfloat = trans || c->isfixed;
159 if(clients)
160 clients->prev = c;
161 c->next = clients;
162 c->snext = stack;
163 stack = clients = c;
164 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
165 XMapWindow(dpy, c->win);
166 if(isvisible(c))
167 focus(c);
168 arrange();
169 }
171 void
172 resize(Client *c, Bool sizehints, Corner sticky) {
173 int bottom = c->y + c->h;
174 int right = c->x + c->w;
175 XWindowChanges wc;
177 if(sizehints) {
178 if(c->incw)
179 c->w -= (c->w - c->basew) % c->incw;
180 if(c->inch)
181 c->h -= (c->h - c->baseh) % c->inch;
182 if(c->minw && c->w < c->minw)
183 c->w = c->minw;
184 if(c->minh && c->h < c->minh)
185 c->h = c->minh;
186 if(c->maxw && c->w > c->maxw)
187 c->w = c->maxw;
188 if(c->maxh && c->h > c->maxh)
189 c->h = c->maxh;
190 }
191 if(sticky == TopRight || sticky == BotRight)
192 c->x = right - c->w;
193 if(sticky == BotLeft || sticky == BotRight)
194 c->y = bottom - c->h;
195 /* offscreen appearance fixes */
196 if(c->x + c->w < sx)
197 c->x = sx;
198 if(c->y + c->h < bh)
199 c->y = bh;
200 if(c->x > sw)
201 c->x = sw - c->w;
202 if(c->y > sh)
203 c->y = sh - c->h;
204 wc.x = c->x;
205 wc.y = c->y;
206 wc.width = c->w;
207 wc.height = c->h;
208 if(c->w == sw && c->h == sh)
209 wc.border_width = 0;
210 else
211 wc.border_width = BORDERPX;
212 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
213 configure(c);
214 XSync(dpy, False);
215 }
217 void
218 updatesizehints(Client *c) {
219 long msize;
220 XSizeHints size;
222 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
223 size.flags = PSize;
224 c->flags = size.flags;
225 if(c->flags & PBaseSize) {
226 c->basew = size.base_width;
227 c->baseh = size.base_height;
228 }
229 else
230 c->basew = c->baseh = 0;
231 if(c->flags & PResizeInc) {
232 c->incw = size.width_inc;
233 c->inch = size.height_inc;
234 }
235 else
236 c->incw = c->inch = 0;
237 if(c->flags & PMaxSize) {
238 c->maxw = size.max_width;
239 c->maxh = size.max_height;
240 }
241 else
242 c->maxw = c->maxh = 0;
243 if(c->flags & PMinSize) {
244 c->minw = size.min_width;
245 c->minh = size.min_height;
246 }
247 else
248 c->minw = c->minh = 0;
249 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
250 c->maxw == c->minw && c->maxh == c->minh);
251 }
253 void
254 updatetitle(Client *c) {
255 char **list = NULL;
256 int n;
257 XTextProperty name;
259 name.nitems = 0;
260 c->name[0] = 0;
261 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
262 if(!name.nitems)
263 XGetWMName(dpy, c->win, &name);
264 if(!name.nitems)
265 return;
266 if(name.encoding == XA_STRING)
267 strncpy(c->name, (char *)name.value, sizeof c->name);
268 else {
269 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
270 && n > 0 && *list)
271 {
272 strncpy(c->name, *list, sizeof c->name);
273 XFreeStringList(list);
274 }
275 }
276 XFree(name.value);
277 }
279 void
280 unmanage(Client *c) {
281 Client *nc;
283 /* The server grab construct avoids race conditions. */
284 XGrabServer(dpy);
285 XSetErrorHandler(xerrordummy);
286 detach(c);
287 detachstack(c);
288 if(sel == c) {
289 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
290 focus(nc);
291 }
292 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
293 free(c->tags);
294 free(c);
295 XSync(dpy, False);
296 XSetErrorHandler(xerror);
297 XUngrabServer(dpy);
298 arrange();
299 }