about summary refs log tree commit diff stats
path: root/util.c
blob: 4914cf5696ff3a74d03afe35bae128bc7ed358b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
 * See LICENSE file for license details.
 */
#include "dwm.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

/* extern */

void *
emallocz(unsigned int size) {
	void *res = calloc(1, size);

	if(!res)
		eprint("fatal: could not malloc() %u bytes\n", size);
	return res;
}

void
eprint(const char *errstr, ...) {
	va_list ap;

	va_start(ap, errstr);
	vfprintf(stderr, errstr, ap);
	va_end(ap);
	exit(EXIT_FAILURE);
}

void
spawn(const char *arg) {
	static char *shell = NULL;

	if(!shell && !(shell = getenv("SHELL")))
		shell = "/bin/sh";
	if(!arg)
		return;
	/* The double-fork construct avoids zombie processes and keeps the code
	 * clean from stupid signal handlers. */
	if(fork() == 0) {
		if(fork() == 0) {
			if(dpy)
				close(ConnectionNumber(dpy));
			setsid();
			execl(shell, shell, "-c", arg, (char *)NULL);
			fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
			perror(" failed");
		}
		exit(0);
	}
	wait(0);
}
atic unsigned int nlayouts = 0; LAYOUTS /* extern */ void arrange(void) { Client *c; for(c = clients; c; c = c->next) if(isvisible(c)) unban(c); else ban(c); lt->arrange(); focus(NULL); restack(); } void focusnext(const char *arg) { Client *c; if(!sel) return; for(c = sel->next; c && !isvisible(c); c = c->next); if(!c) for(c = clients; c && !isvisible(c); c = c->next); if(c) { focus(c); restack(); } } void focusprev(const char *arg) { Client *c; if(!sel) return; for(c = sel->prev; c && !isvisible(c); c = c->prev); if(!c) { for(c = clients; c && c->next; c = c->next); for(; c && !isvisible(c); c = c->prev); } if(c) { focus(c); restack(); } } const char * getsymbol(void) { return lt->symbol; } Bool isfloating(void) { return lt->arrange == floating; } Bool isarrange(void (*func)()) { return func == lt->arrange; } void initlayouts(void) { unsigned int i, w; lt = &layout[0]; nlayouts = sizeof layout / sizeof layout[0]; for(blw = i = 0; i < nlayouts; i++) { w = textw(layout[i].symbol); if(w > blw) blw = w; } } Client * nexttiled(Client *c) { for(; c && (c->isfloating || !isvisible(c)); c = c->next); return c; } void restack(void) { Client *c; XEvent ev; XWindowChanges wc; drawstatus(); if(!sel) return; if(sel->isfloating || lt->arrange == floating) XRaiseWindow(dpy, sel->win); if(lt->arrange != floating) { wc.stack_mode = Below; wc.sibling = barwin; if(!sel->isfloating) { XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc); wc.sibling = sel->win; } for(c = nexttiled(clients); c; c = nexttiled(c->next)) { if(c == sel) continue; XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc); wc.sibling = c->win; } } XSync(dpy, False); while(XCheckMaskEvent(dpy, EnterWindowMask, &ev)); } void setlayout(const char *arg) { int i; if(!arg) { lt++; if(lt == layout + nlayouts) lt = layout; } else { i = atoi(arg); if(i < 0 || i >= nlayouts) return; lt = &layout[i]; } if(sel) arrange(); else drawstatus(); } void togglebar(const char *arg) { if(bpos == BarOff) bpos = (BARPOS == BarOff) ? BarTop : BARPOS; else bpos = BarOff; updatebarpos(); arrange(); } void togglemax(const char *arg) { XEvent ev; if(!sel || (lt->arrange != floating && !sel->isfloating) || sel->isfixed) return; if((sel->ismax = !sel->ismax)) { sel->rx = sel->x; sel->ry = sel->y; sel->rw = sel->w; sel->rh = sel->h; resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True); } else resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True); drawstatus(); while(XCheckMaskEvent(dpy, EnterWindowMask, &ev)); }