all repos — dwm @ dba23062bad40afb1a90f60b6897cf9e1ca5035b

fork of suckless dynamic window manager

tag.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
 14static Rule rule[] = {
 15	/* class			instance	tags						dofloat */
 16	{ "Firefox-bin",	"Gecko",	{ [Twww] = "www" },			False },
 17};
 18
 19void (*arrange)(Arg *) = dotile;
 20
 21Client *
 22getnext(Client *c)
 23{
 24	for(; c && !c->tags[tsel]; c = c->next);
 25	return c;
 26}
 27
 28void
 29settags(Client *c)
 30{
 31	XClassHint ch;
 32	static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
 33	unsigned int i, j;
 34	Bool matched = False;
 35
 36	if(!len) {
 37		c->tags[tsel] = tags[tsel];
 38		return;
 39	}
 40
 41	if(XGetClassHint(dpy, c->win, &ch)) {
 42		if(ch.res_class && ch.res_name) {
 43			for(i = 0; i < len; i++)
 44				if(!strncmp(rule[i].class, ch.res_class, sizeof(rule[i].class))
 45					&& !strncmp(rule[i].instance, ch.res_name, sizeof(rule[i].instance)))
 46				{
 47					for(j = 0; j < TLast; j++)
 48						c->tags[j] = rule[i].tags[j];
 49					c->dofloat = rule[i].dofloat;
 50					matched = True;
 51					break;
 52				}
 53		}
 54		if(ch.res_class)
 55			XFree(ch.res_class);
 56		if(ch.res_name)
 57			XFree(ch.res_name);
 58	}
 59
 60	if(!matched)
 61		c->tags[tsel] = tags[tsel];
 62}
 63
 64void
 65view(Arg *arg)
 66{
 67	tsel = arg->i;
 68	arrange(NULL);
 69	drawall();
 70}
 71
 72void
 73dofloat(Arg *arg)
 74{
 75	Client *c;
 76
 77	arrange = dofloat;
 78	for(c = clients; c; c = c->next) {
 79		if(c->tags[tsel])
 80			resize(c, True);
 81		else
 82			ban(c);
 83	}
 84	if(sel && !sel->tags[tsel]) {
 85		if((sel = getnext(clients))) {
 86			higher(sel);
 87			focus(sel);
 88		}
 89	}
 90	drawall();
 91}
 92
 93void
 94dotile(Arg *arg)
 95{
 96	Client *c;
 97	int n, i, w, h;
 98
 99	w = sw - mw;
100	arrange = dotile;
101	for(n = 0, c = clients; c; c = c->next)
102		if(c->tags[tsel] && !c->dofloat)
103			n++;
104
105	if(n > 1)
106		h = (sh - bh) / (n - 1);
107	else
108		h = sh - bh;
109
110	for(i = 0, c = clients; c; c = c->next) {
111		if(c->tags[tsel]) {
112			if(c->dofloat) {
113				higher(c);
114				resize(c, True);
115				continue;
116			}
117			if(n == 1) {
118				c->x = sx;
119				c->y = sy + bh;
120				c->w = sw - 2 * c->border;
121				c->h = sh - 2 * c->border - bh;
122			}
123			else if(i == 0) {
124				c->x = sx;
125				c->y = sy + bh;
126				c->w = mw - 2 * c->border;
127				c->h = sh - 2 * c->border - bh;
128			}
129			else {
130				c->x = sx + mw;
131				c->y = sy + (i - 1) * h + bh;
132				c->w = w - 2 * c->border;
133				c->h = h - 2 * c->border;
134			}
135			resize(c, False);
136			i++;
137		}
138		else
139			ban(c);
140	}
141	if(!sel || (sel && !sel->tags[tsel])) {
142		if((sel = getnext(clients))) {
143			higher(sel);
144			focus(sel);
145		}
146	}
147	drawall();
148}
149
150void
151appendtag(Arg *arg)
152{
153	if(!sel)
154		return;
155
156	sel->tags[arg->i] = tags[arg->i];
157	arrange(NULL);
158}
159
160void
161replacetag(Arg *arg)
162{
163	int i;
164	if(!sel)
165		return;
166
167	for(i = 0; i < TLast; i++)
168		sel->tags[i] = NULL;
169	appendtag(arg);
170}
171