about summary refs log tree commit diff stats
path: root/util.c
blob: 7d7cb1aa829a5b780be0cb44e810f4394b53b86f (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
55
56
57
58
59
60
61
62
63
/* (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 *
erealloc(void *ptr, unsigned int size) {
	void *res = realloc(ptr, size);

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

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

	if(!shell && !(shell = getenv("SHELL")))
		shell = "/bin/sh";
	if(!arg->cmd)
		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->cmd, (char *)NULL);
			fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg->cmd);
			perror(" failed");
		}
		exit(0);
	}
	wait(0);
}
:corner :dx :dy penup setxy (first :corner)+:dx (last :corner)+:dy end to button.rectangle :x :y repeat 2 [fd :y rt 90 fd :x rt 90] end ; ----------------------------- to caption :corner :size :caption if emptyp :caption [stop] localmake "oldscrunch scrunch if not namep "caption.scrunch [localmake "caption.scrunch 1] localmake "myscrunch map [? * :caption.scrunch] :oldscrunch localmake "y (last :labelcharsize)*:caption.scrunch setpc 7 ifelse listp :caption [ button.offset :corner 0 ((14-:y)+((last :size)-25)/3) if :caption.scrunch <> 1 [apply "setscrunch :myscrunch] label last :caption if :caption.scrunch <> 1 [apply "setscrunch :oldscrunch] button.offset :corner 0 ((14-:y)+:y+2*((last :size)-25)/3) if :caption.scrunch <> 1 [apply "setscrunch :myscrunch] label first :caption if :caption.scrunch <> 1 [apply "setscrunch :oldscrunch] ] [ button.offset :corner 0 ((17-:y)+((last :size)-17)/2) if :caption.scrunch <> 1 [apply "setscrunch :myscrunch] label :caption if :caption.scrunch <> 1 [apply "setscrunch :oldscrunch] ] end to center.caption :corner :size :caption if emptyp :caption [stop] localmake "oldscrunch scrunch if not namep "caption.scrunch [localmake "caption.scrunch 1] localmake "myscrunch map [? * :caption.scrunch] :oldscrunch localmake "halfx (first :labelcharsize)*:caption.scrunch/2 localmake "y (last :labelcharsize)*:caption.scrunch setpc 7 ifelse listp :caption [ button.offset :corner (1+(first :size)/2-:halfx*(count last :caption)) ~ ((14-:y)+((last :size)-25)/3) if :caption.scrunch <> 1 [apply "setscrunch :myscrunch] label last :caption if :caption.scrunch <> 1 [apply "setscrunch :oldscrunch] button.offset :corner (1+(first :size)/2-:halfx*(count first :caption)) ~ ((14-:y)+:y+2*((last :size)-25)/3) if :caption.scrunch <> 1 [apply "setscrunch :myscrunch] label first :caption if :caption.scrunch <> 1 [apply "setscrunch :oldscrunch] ] [ button.offset :corner (1+(first :size)/2-:halfx*(count :caption)) ~ ((17-:y)+((last :size)-17)/2) if :caption.scrunch <> 1 [apply "setscrunch :myscrunch] label :caption if :caption.scrunch <> 1 [apply "setscrunch :oldscrunch] ] end ; ----------------------------- to action.loop [:buttonact [button.mouseclick]] [:keyact [button.readkey]] action.once forever [wait 100] end to action.off ern [buttonact keyact] ern "keyact end to action.once if keyp [button.readkey] if buttonp [button.mouseclick] end to button.readkey [:char rc] [:button 0] [:buttonact []] [:keyact []] foreach :buttons [ localmake "key item 3 ? ifelse equalp :key "DEL [ if memberp (ascii :char) [8 127] [run last ? action.once stop] ] [ ifelse equalp :key "RET [ if memberp (ascii :char) [10 13] [run last ? action.once stop] ] [ if equalp :char :key [run last ? action.once stop] ] ] ] end to button.mouseclick [:mousepos clickpos] [:button button] [:char 0] ~ [:buttonact []] [:keyact []] while [buttonp] [] ; wait for release of button foreach :buttons [ if apply "button.inrange ? [run last ? action.once stop] ] end to button.inrange :corner :size :key :action (foreach :mousepos :corner :size [ if ?1 < ?2 [output "false] if ?1 > (?2 + ?3) [output "false] ]) output "true end