all repos — dwm @ 79cd408844c62963aa0eec45bb0414ed66f06f6f

fork of suckless dynamic window manager

client.c (view raw)

  1/*
  2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3 * See LICENSE file for license details.
  4 */
  5#include "dwm.h"
  6
  7#include <stdlib.h>
  8#include <string.h>
  9#include <X11/Xatom.h>
 10#include <X11/Xutil.h>
 11
 12/* static functions */
 13
 14static void
 15resizetitle(Client *c)
 16{
 17	int i;
 18
 19	c->bw = 0;
 20	for(i = 0; i < TLast; i++)
 21		if(c->tags[i])
 22			c->bw += textw(c->tags[i]);
 23	c->bw += textw(c->name);
 24	if(c->bw > *c->w)
 25		c->bw = *c->w + 2;
 26	c->bx = *c->x + *c->w - c->bw + 2;
 27	c->by = *c->y;
 28	XMoveResizeWindow(dpy, c->title, c->bx, c->by, c->bw, c->bh);
 29}
 30
 31static int
 32xerrordummy(Display *dsply, XErrorEvent *ee)
 33{
 34	return 0;
 35}
 36
 37/* extern functions */
 38
 39void
 40ban(Client *c)
 41{
 42	XMoveWindow(dpy, c->win, *c->x + 2 * sw, *c->y);
 43	XMoveWindow(dpy, c->title, c->bx + 2 * sw, c->by);
 44}
 45
 46void
 47focus(Client *c)
 48{
 49	Client *old = sel;
 50	XEvent ev;
 51
 52	sel = c;
 53	if(old && old != c)
 54		drawtitle(old);
 55	drawtitle(c);
 56	XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
 57	XSync(dpy, False);
 58	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
 59}
 60
 61void
 62focusnext(Arg *arg)
 63{
 64	Client *c;
 65   
 66	if(!sel)
 67		return;
 68
 69	if(!(c = getnext(sel->next, tsel)))
 70		c = getnext(clients, tsel);
 71	if(c) {
 72		higher(c);
 73		c->revert = sel;
 74		focus(c);
 75	}
 76}
 77
 78void
 79focusprev(Arg *arg)
 80{
 81	Client *c;
 82
 83	if(!sel)
 84		return;
 85
 86	if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
 87		higher(c);
 88		focus(c);
 89	}
 90}
 91
 92Client *
 93getclient(Window w)
 94{
 95	Client *c;
 96	for(c = clients; c; c = c->next)
 97		if(c->win == w)
 98			return c;
 99	return NULL;
100}
101
102Client *
103getctitle(Window w)
104{
105	Client *c;
106	for(c = clients; c; c = c->next)
107		if(c->title == w)
108			return c;
109	return NULL;
110}
111
112void
113gravitate(Client *c, Bool invert)
114{
115	int dx = 0, dy = 0;
116
117	switch(c->grav) {
118	case StaticGravity:
119	case NorthWestGravity:
120	case NorthGravity:
121	case NorthEastGravity:
122		dy = c->border;
123		break;
124	case EastGravity:
125	case CenterGravity:
126	case WestGravity:
127		dy = -(*c->h / 2) + c->border;
128		break;
129	case SouthEastGravity:
130	case SouthGravity:
131	case SouthWestGravity:
132		dy = -(*c->h);
133		break;
134	default:
135		break;
136	}
137
138	switch (c->grav) {
139	case StaticGravity:
140	case NorthWestGravity:
141	case WestGravity:
142	case SouthWestGravity:
143		dx = c->border;
144		break;
145	case NorthGravity:
146	case CenterGravity:
147	case SouthGravity:
148		dx = -(*c->w / 2) + c->border;
149		break;
150	case NorthEastGravity:
151	case EastGravity:
152	case SouthEastGravity:
153		dx = -(*c->w + c->border);
154		break;
155	default:
156		break;
157	}
158
159	if(invert) {
160		dx = -dx;
161		dy = -dy;
162	}
163	*c->x += dx;
164	*c->y += dy;
165}
166
167void
168higher(Client *c)
169{
170	XRaiseWindow(dpy, c->win);
171	XRaiseWindow(dpy, c->title);
172}
173
174void
175killclient(Arg *arg)
176{
177	if(!sel)
178		return;
179	if(sel->proto & WM_PROTOCOL_DELWIN)
180		sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
181	else
182		XKillClient(dpy, sel->win);
183}
184
185void
186lower(Client *c)
187{
188	XLowerWindow(dpy, c->title);
189	XLowerWindow(dpy, c->win);
190}
191
192void
193manage(Window w, XWindowAttributes *wa)
194{
195	int diff;
196	Client *c;
197	XSetWindowAttributes twa;
198	Window trans;
199
200	c = emallocz(sizeof(Client));
201	c->win = w;
202	c->bx = c->fx = c->tx = wa->x;
203	c->by = c->fy = c->ty = wa->y;
204	c->bw = c->fw = c->tw = wa->width;
205	c->fh = c->th = wa->height;
206	c->bh = bh;
207
208	diff = sw - c->fw;
209	c->fx = random() % (diff ? diff : 1);
210	diff = sh - c->fh - bh;
211	c->fy = random() % (diff ? diff : 1);
212
213	if(c->fy < bh)
214		c->by = c->fy = c->ty = bh;
215
216	c->border = 1;
217	c->proto = getproto(c->win);
218	setsize(c);
219	XSelectInput(dpy, c->win,
220			StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
221	XGetTransientForHint(dpy, c->win, &trans);
222	twa.override_redirect = 1;
223	twa.background_pixmap = ParentRelative;
224	twa.event_mask = ExposureMask;
225
226	c->title = XCreateWindow(dpy, root, c->bx, c->by, c->bw, c->bh,
227			0, DefaultDepth(dpy, screen), CopyFromParent,
228			DefaultVisual(dpy, screen),
229			CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
230
231	settags(c);
232
233	c->next = clients;
234	clients = c;
235
236	XGrabButton(dpy, Button1, ControlMask, c->win, False, ButtonPressMask,
237			GrabModeAsync, GrabModeSync, None, None);
238	XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
239			GrabModeAsync, GrabModeSync, None, None);
240	XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
241			GrabModeAsync, GrabModeSync, None, None);
242	XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
243			GrabModeAsync, GrabModeSync, None, None);
244
245	if(!c->isfloat)
246		c->isfloat = trans
247			|| ((c->maxw == c->minw) && (c->maxh == c->minh));
248
249	setgeom(c);
250	settitle(c);
251
252	arrange(NULL);
253
254	/* mapping the window now prevents flicker */
255	if(c->tags[tsel]) {
256		XMapRaised(dpy, c->win);
257		XMapRaised(dpy, c->title);
258		focus(c);
259	}
260	else {
261		ban(c);
262		XMapRaised(dpy, c->win);
263		XMapRaised(dpy, c->title);
264		XSync(dpy, False);
265	}
266}
267
268void
269maximize(Arg *arg)
270{
271	if(!sel)
272		return;
273	*sel->x = sx;
274	*sel->y = sy + bh;
275	*sel->w = sw - 2 * sel->border;
276	*sel->h = sh - 2 * sel->border - bh;
277	higher(sel);
278	resize(sel, False, TopLeft);
279}
280
281void
282pop(Client *c)
283{
284	Client **l;
285	for(l = &clients; *l && *l != c; l = &(*l)->next);
286	*l = c->next;
287
288	c->next = clients; /* pop */
289	clients = c;
290	arrange(NULL);
291}
292
293void
294resize(Client *c, Bool inc, Corner sticky)
295{
296	XConfigureEvent e;
297	int right = *c->x + *c->w;
298	int bottom = *c->y + *c->h;
299
300	if(inc) {
301		if(c->incw)
302			*c->w -= (*c->w - c->basew) % c->incw;
303		if(c->inch)
304			*c->h -= (*c->h - c->baseh) % c->inch;
305	}
306	if(*c->x > sw) /* might happen on restart */
307		*c->x = sw - *c->w;
308	if(*c->y > sh)
309		*c->y = sh - *c->h;
310	if(c->minw && *c->w < c->minw)
311		*c->w = c->minw;
312	if(c->minh && *c->h < c->minh)
313		*c->h = c->minh;
314	if(c->maxw && *c->w > c->maxw)
315		*c->w = c->maxw;
316	if(c->maxh && *c->h > c->maxh)
317		*c->h = c->maxh;
318	if(sticky == TopRight || sticky == BottomRight)
319		*c->x = right - *c->w;
320	if(sticky == BottomLeft || sticky == BottomRight)
321		*c->y = bottom - *c->h;
322	resizetitle(c);
323	XSetWindowBorderWidth(dpy, c->win, 1);
324	XMoveResizeWindow(dpy, c->win, *c->x, *c->y, *c->w, *c->h);
325	e.type = ConfigureNotify;
326	e.event = c->win;
327	e.window = c->win;
328	e.x = *c->x;
329	e.y = *c->y;
330	e.width = *c->w;
331	e.height = *c->h;
332	e.border_width = c->border;
333	e.above = None;
334	e.override_redirect = False;
335	XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
336	XSync(dpy, False);
337}
338
339void
340setgeom(Client *c)
341{
342	if((arrange == dotile) && !c->isfloat) {
343		c->x = &c->tx;
344		c->y = &c->ty;
345		c->w = &c->tw;
346		c->h = &c->th;
347	}
348	else {
349		c->x = &c->fx;
350		c->y = &c->fy;
351		c->w = &c->fw;
352		c->h = &c->fh;
353	}
354}
355
356void
357setsize(Client *c)
358{
359	XSizeHints size;
360	long msize;
361	if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
362		size.flags = PSize;
363	c->flags = size.flags;
364	if(c->flags & PBaseSize) {
365		c->basew = size.base_width;
366		c->baseh = size.base_height;
367	}
368	else
369		c->basew = c->baseh = 0;
370	if(c->flags & PResizeInc) {
371		c->incw = size.width_inc;
372		c->inch = size.height_inc;
373	}
374	else
375		c->incw = c->inch = 0;
376	if(c->flags & PMaxSize) {
377		c->maxw = size.max_width;
378		c->maxh = size.max_height;
379	}
380	else
381		c->maxw = c->maxh = 0;
382	if(c->flags & PMinSize) {
383		c->minw = size.min_width;
384		c->minh = size.min_height;
385	}
386	else
387		c->minw = c->minh = 0;
388	if(c->flags & PWinGravity)
389		c->grav = size.win_gravity;
390	else
391		c->grav = NorthWestGravity;
392}
393
394void
395settitle(Client *c)
396{
397	XTextProperty name;
398	int n;
399	char **list = NULL;
400
401	name.nitems = 0;
402	c->name[0] = 0;
403	XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
404	if(!name.nitems)
405		XGetWMName(dpy, c->win, &name);
406	if(!name.nitems)
407		return;
408	if(name.encoding == XA_STRING)
409		strncpy(c->name, (char *)name.value, sizeof(c->name));
410	else {
411		if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
412				&& n > 0 && *list)
413		{
414			strncpy(c->name, *list, sizeof(c->name));
415			XFreeStringList(list);
416		}
417	}
418	XFree(name.value);
419	resizetitle(c);
420}
421
422void
423unmanage(Client *c)
424{
425	Client **l;
426
427	XGrabServer(dpy);
428	XSetErrorHandler(xerrordummy);
429
430	XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
431	XDestroyWindow(dpy, c->title);
432
433	for(l = &clients; *l && *l != c; l = &(*l)->next);
434	*l = c->next;
435	for(l = &clients; *l; l = &(*l)->next)
436		if((*l)->revert == c)
437			(*l)->revert = NULL;
438	if(sel == c)
439		sel = sel->revert ? sel->revert : clients;
440
441	free(c);
442
443	XSync(dpy, False);
444	XSetErrorHandler(xerror);
445	XUngrabServer(dpy);
446	arrange(NULL);
447	if(sel)
448		focus(sel);
449}
450
451void
452zoom(Arg *arg)
453{
454	Client *c;
455
456	if(!sel)
457		return;
458
459	if(sel == getnext(clients, tsel) && sel->next)  {
460		if((c = getnext(sel->next, tsel)))
461			sel = c;
462	}
463
464	pop(sel);
465	focus(sel);
466}