all repos — dwm @ 96d7fe16eaf6b656800f08da3156bacd75ca3b08

fork of suckless dynamic window manager

layout.c (view raw)

  1/* See LICENSE file for copyright and license details. */
  2#include "dwm.h"
  3#include <stdlib.h>
  4#include <string.h>
  5#include <X11/Xatom.h>
  6#include <X11/Xutil.h>
  7
  8/* static */
  9
 10typedef struct {
 11	const char *symbol;
 12	void (*arrange)(void);
 13} Layout;
 14
 15unsigned int blw = 0;
 16static char prop[128];
 17static unsigned int ltidx = 0; /* default */
 18
 19static void
 20floating(void) { /* default floating layout */
 21	Client *c;
 22
 23	for(c = clients; c; c = c->next)
 24		if(isvisible(c))
 25			resize(c, c->x, c->y, c->w, c->h, True);
 26}
 27
 28static unsigned int nlayouts = 0;
 29
 30LAYOUTS
 31
 32/* extern */
 33
 34void
 35arrange(void) {
 36	Client *c;
 37
 38	for(c = clients; c; c = c->next)
 39		if(isvisible(c))
 40			unban(c);
 41		else
 42			ban(c);
 43	layouts[ltidx].arrange();
 44	focus(NULL);
 45	restack();
 46}
 47
 48void
 49focusnext(const char *arg) {
 50	Client *c;
 51
 52	if(!sel)
 53		return;
 54	for(c = sel->next; c && !isvisible(c); c = c->next);
 55	if(!c)
 56		for(c = clients; c && !isvisible(c); c = c->next);
 57	if(c) {
 58		focus(c);
 59		restack();
 60	}
 61}
 62
 63void
 64focusprev(const char *arg) {
 65	Client *c;
 66
 67	if(!sel)
 68		return;
 69	for(c = sel->prev; c && !isvisible(c); c = c->prev);
 70	if(!c) {
 71		for(c = clients; c && c->next; c = c->next);
 72		for(; c && !isvisible(c); c = c->prev);
 73	}
 74	if(c) {
 75		focus(c);
 76		restack();
 77	}
 78}
 79
 80const char *
 81getsymbol(void)
 82{
 83	return layouts[ltidx].symbol;
 84}
 85
 86Bool
 87isfloating(void) {
 88	return layouts[ltidx].arrange == floating;
 89}
 90
 91Bool
 92isarrange(void (*func)())
 93{
 94	return func == layouts[ltidx].arrange;
 95}
 96
 97void
 98initlayouts(void) {
 99	unsigned int i, w;
100
101	nlayouts = sizeof layouts / sizeof layouts[0];
102	for(blw = i = 0; i < nlayouts; i++) {
103		w = textw(layouts[i].symbol);
104		if(w > blw)
105			blw = w;
106	}
107}
108
109void
110loaddwmprops(void) {
111	unsigned int i;
112
113	if(gettextprop(root, dwmprops, prop, sizeof prop)) {
114		for(i = 0; i < ntags && i < sizeof prop - 1 && prop[i] != '\0'; i++)
115			seltags[i] = prop[i] == '1';
116		if(i < sizeof prop - 1 && prop[i] != '\0') {
117			if(prop[i] < nlayouts)
118				ltidx = prop[i];
119		}
120	}
121}
122
123Client *
124nexttiled(Client *c) {
125	for(; c && (c->isfloating || !isvisible(c)); c = c->next);
126	return c;
127}
128
129void
130restack(void) {
131	Client *c;
132	XEvent ev;
133	XWindowChanges wc;
134
135	drawstatus();
136	if(!sel)
137		return;
138	if(sel->isfloating || isfloating())
139		XRaiseWindow(dpy, sel->win);
140	if(!isfloating()) {
141		wc.stack_mode = Below;
142		wc.sibling = barwin;
143		if(!sel->isfloating) {
144			XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
145			wc.sibling = sel->win;
146		}
147		for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
148			if(c == sel)
149				continue;
150			XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
151			wc.sibling = c->win;
152		}
153	}
154	XSync(dpy, False);
155	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
156}
157
158void
159savedwmprops(void) {
160	unsigned int i;
161
162	for(i = 0; i < ntags && i < sizeof prop - 1; i++)
163		prop[i] = seltags[i] ? '1' : '0';
164	if(i < sizeof prop - 1)
165		prop[i++] = (char)ltidx;
166	prop[i] = '\0';
167	XChangeProperty(dpy, root, dwmprops, XA_STRING, 8,
168			PropModeReplace, (unsigned char *)prop, i);
169}
170
171void
172setlayout(const char *arg) {
173	int i;
174
175	if(!arg) {
176		if(++ltidx == nlayouts)
177			ltidx = 0;;
178	}
179	else {
180		i = atoi(arg);
181		if(i < 0 || i >= nlayouts)
182			return;
183		ltidx = i;
184	}
185	if(sel)
186		arrange();
187	else
188		drawstatus();
189	savedwmprops();
190}
191
192void
193togglebar(const char *arg) {
194	if(bpos == BarOff)
195		bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
196	else
197		bpos = BarOff;
198	updatebarpos();
199	arrange();
200}
201
202void
203togglemax(const char *arg) {
204	XEvent ev;
205
206	if(!sel || (!isfloating() && !sel->isfloating) || sel->isfixed)
207		return;
208	if((sel->ismax = !sel->ismax)) {
209		sel->rx = sel->x;
210		sel->ry = sel->y;
211		sel->rw = sel->w;
212		sel->rh = sel->h;
213		resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
214	}
215	else
216		resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
217	drawstatus();
218	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
219}