all repos — dwm @ 58f2fe3f6af3d6f8c925125c721a2d1800d750dc

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	Client *c;
196	XSetWindowAttributes twa;
197	Window trans;
198
199	c = emallocz(sizeof(Client));
200	c->win = w;
201	c->bx = c->fx = c->tx = wa->x;
202	c->by = c->fy = c->ty = wa->y;
203	if(c->fy < bh)
204		c->by = c->fy = c->ty += bh;
205	c->bw = c->fw = c->tw = wa->width;
206	c->fh = c->th = wa->height;
207	c->bh = bh;
208	c->border = 1;
209	c->proto = getproto(c->win);
210	setsize(c);
211	XSelectInput(dpy, c->win,
212			StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
213	XGetTransientForHint(dpy, c->win, &trans);
214	twa.override_redirect = 1;
215	twa.background_pixmap = ParentRelative;
216	twa.event_mask = ExposureMask;
217
218	c->title = XCreateWindow(dpy, root, c->bx, c->by, c->bw, c->bh,
219			0, DefaultDepth(dpy, screen), CopyFromParent,
220			DefaultVisual(dpy, screen),
221			CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
222
223	settags(c);
224
225	c->next = clients;
226	clients = c;
227
228	XGrabButton(dpy, Button1, ControlMask, c->win, False, ButtonPressMask,
229			GrabModeAsync, GrabModeSync, None, None);
230	XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
231			GrabModeAsync, GrabModeSync, None, None);
232	XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
233			GrabModeAsync, GrabModeSync, None, None);
234	XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
235			GrabModeAsync, GrabModeSync, None, None);
236
237	if(!c->isfloat)
238		c->isfloat = trans
239			|| ((c->maxw == c->minw) && (c->maxh == c->minh));
240
241	setgeom(c);
242	settitle(c);
243
244	arrange(NULL);
245
246	/* mapping the window now prevents flicker */
247	if(c->tags[tsel]) {
248		XMapRaised(dpy, c->win);
249		XMapRaised(dpy, c->title);
250		focus(c);
251	}
252	else {
253		ban(c);
254		XMapRaised(dpy, c->win);
255		XMapRaised(dpy, c->title);
256		XSync(dpy, False);
257	}
258}
259
260void
261maximize(Arg *arg)
262{
263	if(!sel)
264		return;
265	*sel->x = sx;
266	*sel->y = sy + bh;
267	*sel->w = sw - 2 * sel->border;
268	*sel->h = sh - 2 * sel->border - bh;
269	higher(sel);
270	resize(sel, False);
271}
272
273void
274pop(Client *c)
275{
276	Client **l;
277	for(l = &clients; *l && *l != c; l = &(*l)->next);
278	*l = c->next;
279
280	c->next = clients; /* pop */
281	clients = c;
282	arrange(NULL);
283}
284
285void
286resize(Client *c, Bool inc)
287{
288	XConfigureEvent e;
289
290	if(inc) {
291		if(c->incw)
292			*c->w -= (*c->w - c->basew) % c->incw;
293		if(c->inch)
294			*c->h -= (*c->h - c->baseh) % c->inch;
295	}
296	if(*c->x > sw) /* might happen on restart */
297		*c->x = sw - *c->w;
298	if(*c->y > sh)
299		*c->y = sh - *c->h;
300	if(c->minw && *c->w < c->minw)
301		*c->w = c->minw;
302	if(c->minh && *c->h < c->minh)
303		*c->h = c->minh;
304	if(c->maxw && *c->w > c->maxw)
305		*c->w = c->maxw;
306	if(c->maxh && *c->h > c->maxh)
307		*c->h = c->maxh;
308	resizetitle(c);
309	XSetWindowBorderWidth(dpy, c->win, 1);
310	XMoveResizeWindow(dpy, c->win, *c->x, *c->y, *c->w, *c->h);
311	e.type = ConfigureNotify;
312	e.event = c->win;
313	e.window = c->win;
314	e.x = *c->x;
315	e.y = *c->y;
316	e.width = *c->w;
317	e.height = *c->h;
318	e.border_width = c->border;
319	e.above = None;
320	e.override_redirect = False;
321	XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
322	XSync(dpy, False);
323}
324
325void
326setgeom(Client *c)
327{
328	if((arrange == dotile) && !c->isfloat) {
329		c->x = &c->tx;
330		c->y = &c->ty;
331		c->w = &c->tw;
332		c->h = &c->th;
333	}
334	else {
335		c->x = &c->fx;
336		c->y = &c->fy;
337		c->w = &c->fw;
338		c->h = &c->fh;
339	}
340}
341
342void
343setsize(Client *c)
344{
345	XSizeHints size;
346	long msize;
347	if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
348		size.flags = PSize;
349	c->flags = size.flags;
350	if(c->flags & PBaseSize) {
351		c->basew = size.base_width;
352		c->baseh = size.base_height;
353	}
354	else
355		c->basew = c->baseh = 0;
356	if(c->flags & PResizeInc) {
357		c->incw = size.width_inc;
358		c->inch = size.height_inc;
359	}
360	else
361		c->incw = c->inch = 0;
362	if(c->flags & PMaxSize) {
363		c->maxw = size.max_width;
364		c->maxh = size.max_height;
365	}
366	else
367		c->maxw = c->maxh = 0;
368	if(c->flags & PMinSize) {
369		c->minw = size.min_width;
370		c->minh = size.min_height;
371	}
372	else
373		c->minw = c->minh = 0;
374	if(c->flags & PWinGravity)
375		c->grav = size.win_gravity;
376	else
377		c->grav = NorthWestGravity;
378}
379
380void
381settitle(Client *c)
382{
383	XTextProperty name;
384	int n;
385	char **list = NULL;
386
387	name.nitems = 0;
388	c->name[0] = 0;
389	XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
390	if(!name.nitems)
391		XGetWMName(dpy, c->win, &name);
392	if(!name.nitems)
393		return;
394	if(name.encoding == XA_STRING)
395		strncpy(c->name, (char *)name.value, sizeof(c->name));
396	else {
397		if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
398				&& n > 0 && *list)
399		{
400			strncpy(c->name, *list, sizeof(c->name));
401			XFreeStringList(list);
402		}
403	}
404	XFree(name.value);
405	resizetitle(c);
406}
407
408void
409unmanage(Client *c)
410{
411	Client **l;
412
413	XGrabServer(dpy);
414	XSetErrorHandler(xerrordummy);
415
416	XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
417	XDestroyWindow(dpy, c->title);
418
419	for(l = &clients; *l && *l != c; l = &(*l)->next);
420	*l = c->next;
421	for(l = &clients; *l; l = &(*l)->next)
422		if((*l)->revert == c)
423			(*l)->revert = NULL;
424	if(sel == c)
425		sel = sel->revert ? sel->revert : clients;
426
427	free(c);
428
429	XSync(dpy, False);
430	XSetErrorHandler(xerror);
431	XUngrabServer(dpy);
432	arrange(NULL);
433	if(sel)
434		focus(sel);
435}
436
437void
438zoom(Arg *arg)
439{
440	Client *c;
441
442	if(!sel)
443		return;
444
445	if(sel == getnext(clients, tsel) && sel->next)  {
446		if((c = getnext(sel->next, tsel)))
447			sel = c;
448	}
449
450	pop(sel);
451	focus(sel);
452}