all repos — dwm @ 0e5c8198bc5a69e87b0114b81d6569188828edfa

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