all repos — dwm @ f60c597d653bd7eab6c620fc53d732ca75f6a880

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