all repos — dwm @ 86d9851427710ca0f3ab95ae9b6c20361b56e516

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
  6#include <stdlib.h>
  7#include <stdio.h>
  8#include <string.h>
  9#include <X11/Xatom.h>
 10#include <X11/Xutil.h>
 11
 12#include "dwm.h"
 13
 14void (*arrange)(Arg *) = tiling;
 15
 16static Rule rule[] = {
 17	/* class			instance	tags						floating */
 18	{ "Firefox-bin",	"Gecko",	{ [Twww] = "www" },			False },
 19};
 20
 21static Client *
 22next(Client *c)
 23{
 24	for(; c && !c->tags[tsel]; c = c->next);
 25	return c;
 26}
 27
 28void
 29zoom(Arg *arg)
 30{
 31	Client **l, *old;
 32
 33	if(!(old = sel))
 34		return;
 35
 36	for(l = &clients; *l && *l != sel; l = &(*l)->next);
 37	*l = sel->next;
 38
 39	old->next = clients; /* pop */
 40	clients = old;
 41	sel = old;
 42	arrange(NULL);
 43	focus(sel);
 44}
 45
 46void
 47max(Arg *arg)
 48{
 49	if(!sel)
 50		return;
 51	sel->x = sx;
 52	sel->y = sy;
 53	sel->w = sw - 2 * sel->border;
 54	sel->h = sh - 2 * sel->border;
 55	craise(sel);
 56	resize(sel, False);
 57	discard_events(EnterWindowMask);
 58}
 59
 60void
 61view(Arg *arg)
 62{
 63	Client *c;
 64
 65	tsel = arg->i;
 66	arrange(NULL);
 67
 68	for(c = clients; c; c = next(c->next))
 69		draw_client(c);
 70}
 71
 72void
 73tappend(Arg *arg)
 74{
 75	if(!sel)
 76		return;
 77
 78	sel->tags[arg->i] = tags[arg->i];
 79	arrange(NULL);
 80}
 81
 82void
 83ttrunc(Arg *arg)
 84{
 85	int i;
 86	if(!sel)
 87		return;
 88
 89	for(i = 0; i < TLast; i++)
 90		sel->tags[i] = NULL;
 91	tappend(arg);
 92}
 93
 94static void
 95ban_client(Client *c)
 96{
 97	XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
 98	XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
 99}
100
101void
102floating(Arg *arg)
103{
104	Client *c;
105
106	arrange = floating;
107	for(c = clients; c; c = c->next) {
108		if(c->tags[tsel])
109			resize(c, True);
110		else
111			ban_client(c);
112	}
113	if(sel && !sel->tags[tsel]) {
114		if((sel = next(clients))) {
115			craise(sel);
116			focus(sel);
117		}
118	}
119	discard_events(EnterWindowMask);
120}
121
122void
123tiling(Arg *arg)
124{
125	Client *c;
126	int n, i, w, h;
127
128	w = sw - mw;
129	arrange = tiling;
130	for(n = 0, c = clients; c; c = c->next)
131		if(c->tags[tsel] && !c->floating)
132			n++;
133
134	h = (n > 1) ? sh / (n - 1) : sh;
135
136	for(i = 0, c = clients; c; c = c->next) {
137		if(c->tags[tsel]) {
138			if(c->floating) {
139				craise(c);
140				resize(c, True);
141				continue;
142			}
143			if(n == 1) {
144				c->x = sx;
145				c->y = sy;
146				c->w = sw - 2 * c->border;
147				c->h = sh - 2 * c->border;
148			}
149			else if(i == 0) {
150				c->x = sx;
151				c->y = sy;
152				c->w = mw - 2 * c->border;
153				c->h = sh - 2 * c->border;
154			}
155			else {
156				c->x = sx + mw;
157				c->y = sy + (i - 1) * h;
158				c->w = w - 2 * c->border;
159				c->h = h - 2 * c->border;
160			}
161			resize(c, False);
162			i++;
163		}
164		else
165			ban_client(c);
166	}
167	if(sel && !sel->tags[tsel]) {
168		if((sel = next(clients))) {
169			craise(sel);
170			focus(sel);
171		}
172	}
173	discard_events(EnterWindowMask);
174}
175
176void
177prevc(Arg *arg)
178{
179	Client *c;
180
181	if(!sel)
182		return;
183
184	if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
185		craise(c);
186		focus(c);
187	}
188}
189
190void
191nextc(Arg *arg)
192{
193	Client *c;
194   
195	if(!sel)
196		return;
197
198	if(!(c = next(sel->next)))
199		c = next(clients);
200	if(c) {
201		craise(c);
202		c->revert = sel;
203		focus(c);
204	}
205}
206
207void
208ckill(Arg *arg)
209{
210	if(!sel)
211		return;
212	if(sel->proto & WM_PROTOCOL_DELWIN)
213		send_message(sel->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
214	else
215		XKillClient(dpy, sel->win);
216}
217
218static void
219resize_title(Client *c)
220{
221	int i;
222
223	c->tw = 0;
224	for(i = 0; i < TLast; i++)
225		if(c->tags[i])
226			c->tw += textw(c->tags[i]) + dc.font.height;
227	c->tw += textw(c->name) + dc.font.height;
228	if(c->tw > c->w)
229		c->tw = c->w + 2;
230	c->tx = c->x + c->w - c->tw + 2;
231	c->ty = c->y;
232	XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
233}
234
235void
236update_name(Client *c)
237{
238	XTextProperty name;
239	int n;
240	char **list = NULL;
241
242	name.nitems = 0;
243	c->name[0] = 0;
244	XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
245	if(!name.nitems)
246		XGetWMName(dpy, c->win, &name);
247	if(!name.nitems)
248		return;
249	if(name.encoding == XA_STRING)
250		strncpy(c->name, (char *)name.value, sizeof(c->name));
251	else {
252		if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
253				&& n > 0 && *list)
254		{
255			strncpy(c->name, *list, sizeof(c->name));
256			XFreeStringList(list);
257		}
258	}
259	XFree(name.value);
260	resize_title(c);
261}
262
263void
264update_size(Client *c)
265{
266	XSizeHints size;
267	long msize;
268	if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
269		size.flags = PSize;
270	c->flags = size.flags;
271	if(c->flags & PBaseSize) {
272		c->basew = size.base_width;
273		c->baseh = size.base_height;
274	}
275	else
276		c->basew = c->baseh = 0;
277	if(c->flags & PResizeInc) {
278		c->incw = size.width_inc;
279		c->inch = size.height_inc;
280	}
281	else
282		c->incw = c->inch = 0;
283	if(c->flags & PMaxSize) {
284		c->maxw = size.max_width;
285		c->maxh = size.max_height;
286	}
287	else
288		c->maxw = c->maxh = 0;
289	if(c->flags & PMinSize) {
290		c->minw = size.min_width;
291		c->minh = size.min_height;
292	}
293	else
294		c->minw = c->minh = 0;
295	if(c->flags & PWinGravity)
296		c->grav = size.win_gravity;
297	else
298		c->grav = NorthWestGravity;
299}
300
301void
302craise(Client *c)
303{
304	XRaiseWindow(dpy, c->win);
305	XRaiseWindow(dpy, c->title);
306}
307
308void
309lower(Client *c)
310{
311	XLowerWindow(dpy, c->title);
312	XLowerWindow(dpy, c->win);
313}
314
315void
316focus(Client *c)
317{
318	Client *old = sel;
319
320	sel = c;
321	if(old && old != c)
322		draw_client(old);
323	draw_client(c);
324	XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
325	XFlush(dpy);
326	discard_events(EnterWindowMask);
327}
328
329static void
330init_tags(Client *c)
331{
332	XClassHint ch;
333	static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
334	unsigned int i, j;
335	Bool matched = False;
336
337	if(!len) {
338		c->tags[tsel] = tags[tsel];
339		return;
340	}
341
342	if(XGetClassHint(dpy, c->win, &ch)) {
343		if(ch.res_class && ch.res_name) {
344			for(i = 0; i < len; i++)
345				if(!strncmp(rule[i].class, ch.res_class, sizeof(rule[i].class))
346					&& !strncmp(rule[i].instance, ch.res_name, sizeof(rule[i].instance)))
347				{
348					for(j = 0; j < TLast; j++)
349						c->tags[j] = rule[i].tags[j];
350					c->floating = rule[i].floating;
351					matched = True;
352					break;
353				}
354		}
355		if(ch.res_class)
356			XFree(ch.res_class);
357		if(ch.res_name)
358			XFree(ch.res_name);
359	}
360
361	if(!matched)
362		c->tags[tsel] = tags[tsel];
363}
364
365void
366manage(Window w, XWindowAttributes *wa)
367{
368	Client *c, **l;
369	XSetWindowAttributes twa;
370	Window trans;
371
372	c = emallocz(sizeof(Client));
373	c->win = w;
374	c->tx = c->x = wa->x;
375	c->ty = c->y = wa->y;
376	c->tw = c->w = wa->width;
377	c->h = wa->height;
378	c->th = th;
379	c->border = 1;
380	c->proto = win_proto(c->win);
381	update_size(c);
382	XSelectInput(dpy, c->win,
383			StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
384	XGetTransientForHint(dpy, c->win, &trans);
385	twa.override_redirect = 1;
386	twa.background_pixmap = ParentRelative;
387	twa.event_mask = ExposureMask;
388
389	c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
390			0, DefaultDepth(dpy, screen), CopyFromParent,
391			DefaultVisual(dpy, screen),
392			CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
393
394	update_name(c);
395	init_tags(c);
396
397	for(l = &clients; *l; l = &(*l)->next);
398	c->next = *l; /* *l == nil */
399	*l = c;
400
401	XSetWindowBorderWidth(dpy, c->win, 1);
402	XMapRaised(dpy, c->win);
403	XMapRaised(dpy, c->title);
404	XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
405			GrabModeAsync, GrabModeSync, None, None);
406	XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
407			GrabModeAsync, GrabModeSync, None, None);
408	XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
409			GrabModeAsync, GrabModeSync, None, None);
410
411	if(!c->floating)
412		c->floating = trans
413			|| ((c->maxw == c->minw) && (c->maxh == c->minh));
414
415	arrange(NULL);
416	if(c->tags[tsel])
417		focus(c);
418	else
419		ban_client(c);
420}
421
422void
423gravitate(Client *c, Bool invert)
424{
425	int dx = 0, dy = 0;
426
427	switch(c->grav) {
428	case StaticGravity:
429	case NorthWestGravity:
430	case NorthGravity:
431	case NorthEastGravity:
432		dy = c->border;
433		break;
434	case EastGravity:
435	case CenterGravity:
436	case WestGravity:
437		dy = -(c->h / 2) + c->border;
438		break;
439	case SouthEastGravity:
440	case SouthGravity:
441	case SouthWestGravity:
442		dy = -c->h;
443		break;
444	default:
445		break;
446	}
447
448	switch (c->grav) {
449	case StaticGravity:
450	case NorthWestGravity:
451	case WestGravity:
452	case SouthWestGravity:
453		dx = c->border;
454		break;
455	case NorthGravity:
456	case CenterGravity:
457	case SouthGravity:
458		dx = -(c->w / 2) + c->border;
459		break;
460	case NorthEastGravity:
461	case EastGravity:
462	case SouthEastGravity:
463		dx = -(c->w + c->border);
464		break;
465	default:
466		break;
467	}
468
469	if(invert) {
470		dx = -dx;
471		dy = -dy;
472	}
473	c->x += dx;
474	c->y += dy;
475}
476
477
478void
479resize(Client *c, Bool inc)
480{
481	XConfigureEvent e;
482
483	if(inc) {
484		if(c->incw)
485			c->w -= (c->w - c->basew) % c->incw;
486		if(c->inch)
487			c->h -= (c->h - c->baseh) % c->inch;
488	}
489	if(c->minw && c->w < c->minw)
490		c->w = c->minw;
491	if(c->minh && c->h < c->minh)
492		c->h = c->minh;
493	if(c->maxw && c->w > c->maxw)
494		c->w = c->maxw;
495	if(c->maxh && c->h > c->maxh)
496		c->h = c->maxh;
497	resize_title(c);
498	XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
499	e.type = ConfigureNotify;
500	e.event = c->win;
501	e.window = c->win;
502	e.x = c->x;
503	e.y = c->y;
504	e.width = c->w;
505	e.height = c->h;
506	e.border_width = c->border;
507	e.above = None;
508	e.override_redirect = False;
509	XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
510	XFlush(dpy);
511}
512
513static int
514dummy_error_handler(Display *dsply, XErrorEvent *err)
515{
516	return 0;
517}
518
519void
520unmanage(Client *c)
521{
522	Client **l;
523
524	XGrabServer(dpy);
525	XSetErrorHandler(dummy_error_handler);
526
527	XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
528	XDestroyWindow(dpy, c->title);
529
530	for(l = &clients; *l && *l != c; l = &(*l)->next);
531	*l = c->next;
532	for(l = &clients; *l; l = &(*l)->next)
533		if((*l)->revert == c)
534			(*l)->revert = NULL;
535	if(sel == c)
536		sel = sel->revert ? sel->revert : clients;
537
538	free(c);
539
540	XFlush(dpy);
541	XSetErrorHandler(error_handler);
542	XUngrabServer(dpy);
543	arrange(NULL);
544	if(sel)
545		focus(sel);
546}
547
548Client *
549gettitle(Window w)
550{
551	Client *c;
552	for(c = clients; c; c = c->next)
553		if(c->title == w)
554			return c;
555	return NULL;
556}
557
558Client *
559getclient(Window w)
560{
561	Client *c;
562	for(c = clients; c; c = c->next)
563		if(c->win == w)
564			return c;
565	return NULL;
566}
567
568void
569draw_client(Client *c)
570{
571	int i;
572	if(c == sel) {
573		XUnmapWindow(dpy, c->title);
574		XSetWindowBorder(dpy, c->win, dc.fg);
575		return;
576	}
577
578	XSetWindowBorder(dpy, c->win, dc.bg);
579	XMapWindow(dpy, c->title);
580
581	dc.x = dc.y = 0;
582	dc.h = c->th;
583
584	dc.w = 0;
585	for(i = 0; i < TLast; i++) {
586		if(c->tags[i]) {
587			dc.x += dc.w;
588			dc.w = textw(c->tags[i]) + dc.font.height;
589			draw(True, c->tags[i]);
590		}
591	}
592	dc.x += dc.w;
593	dc.w = textw(c->name) + dc.font.height;
594	draw(True, c->name);
595	XCopyArea(dpy, dc.drawable, c->title, dc.gc,
596			0, 0, c->tw, c->th, 0, 0);
597	XFlush(dpy);
598}