all repos — dwm @ 0c6062041035105c6266f6bedb286c1990516fa7

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	/* TODO deserialize ltidx if present */
102	nlayouts = sizeof layouts / sizeof layouts[0];
103	for(blw = i = 0; i < nlayouts; i++) {
104		w = textw(layouts[i].symbol);
105		if(w > blw)
106			blw = w;
107	}
108}
109
110void
111loaddwmprops(void) {
112	unsigned int i;
113	XTextProperty name;
114
115	/* check if window has set a property */
116	name.nitems = 0;
117	XGetTextProperty(dpy, root, &name, dwmprops);
118	if(name.nitems && name.encoding == XA_STRING) {
119		strncpy(prop, (char *)name.value, sizeof prop - 1);
120		prop[sizeof prop - 1] = '\0';
121		XFree(name.value);
122		for(i = 0; i < ntags && i < sizeof prop - 1 && prop[i] != '\0'; i++)
123			seltags[i] = prop[i] == '1';
124		if(i < sizeof prop - 1 && prop[i] != '\0') {
125			i = prop[i] - '0';
126			if(i < nlayouts)
127				ltidx = i;
128		}
129	}
130}
131
132Client *
133nexttiled(Client *c) {
134	for(; c && (c->isfloating || !isvisible(c)); c = c->next);
135	return c;
136}
137
138void
139restack(void) {
140	Client *c;
141	XEvent ev;
142	XWindowChanges wc;
143
144	drawstatus();
145	if(!sel)
146		return;
147	if(sel->isfloating || isfloating())
148		XRaiseWindow(dpy, sel->win);
149	if(!isfloating()) {
150		wc.stack_mode = Below;
151		wc.sibling = barwin;
152		if(!sel->isfloating) {
153			XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
154			wc.sibling = sel->win;
155		}
156		for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
157			if(c == sel)
158				continue;
159			XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
160			wc.sibling = c->win;
161		}
162	}
163	XSync(dpy, False);
164	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
165}
166
167void
168savedwmprops(void) {
169	unsigned int i;
170
171	for(i = 0; i < ntags && i < sizeof prop - 1; i++)
172		prop[i] = seltags[i] ? '1' : '0';
173	if(i < sizeof prop - 1)
174		prop[i++] = (char)ltidx;
175	prop[i] = '\0';
176	XChangeProperty(dpy, root, dwmprops, XA_STRING, 8,
177			PropModeReplace, (unsigned char *)prop, i);
178}
179
180void
181setlayout(const char *arg) {
182	int i;
183
184	if(!arg) {
185		if(++ltidx == nlayouts)
186			ltidx = 0;;
187	}
188	else {
189		i = atoi(arg);
190		if(i < 0 || i >= nlayouts)
191			return;
192		ltidx = i;
193	}
194	if(sel)
195		arrange();
196	else
197		drawstatus();
198	savedwmprops();
199}
200
201void
202togglebar(const char *arg) {
203	if(bpos == BarOff)
204		bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
205	else
206		bpos = BarOff;
207	updatebarpos();
208	arrange();
209}
210
211void
212togglemax(const char *arg) {
213	XEvent ev;
214
215	if(!sel || (!isfloating() && !sel->isfloating) || sel->isfixed)
216		return;
217	if((sel->ismax = !sel->ismax)) {
218		sel->rx = sel->x;
219		sel->ry = sel->y;
220		sel->rw = sel->w;
221		sel->rh = sel->h;
222		resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
223	}
224	else
225		resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
226	drawstatus();
227	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
228}