all repos — dwm @ 05a618b06e8fd8807b9a5e7c0bcef33c68d00044

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