aboutsummaryrefslogtreecommitdiffstats
path: root/calmwm.c
blob: 2f3c4e421da226dbc759f65ef13f6b6f599e56a8 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * calmwm - the calm window manager
 *
 * Copyright (c) 2004 Marius Aamodt Eriksen <marius@monkey.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * $OpenBSD: calmwm.c,v 1.114 2020/03/24 14:48:29 okan Exp $
 */

#include <sys/types.h>
#include "queue.h"
#include <sys/wait.h>

#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <locale.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifndef __dead
#define __dead __attribute__((noreturn))
#endif

#include "calmwm.h"

Display			*X_Dpy;
Time			 Last_Event_Time = CurrentTime;
Atom			 cwmh[CWMH_NITEMS];
Atom			 ewmh[EWMH_NITEMS];
struct screen_q		 Screenq = TAILQ_HEAD_INITIALIZER(Screenq);
struct conf		 Conf;
volatile sig_atomic_t	 cwm_status;

__dead void	usage(void);
static void	sighdlr(int);
static int	x_errorhandler(Display *, XErrorEvent *);
static int	x_init(const char *);
static void	x_teardown(void);
static int	x_wmerrorhandler(Display *, XErrorEvent *);

int
main(int argc, char **argv)
{
	char		*display_name = NULL;
	char		**saved_argv;
	int		 ch, xfd, nflag = 0;
	struct pollfd	 pfd[1];

	if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
		warnx("no locale support");
	mbtowc(NULL, NULL, MB_CUR_MAX);

	conf_init(&Conf);

	saved_argv = argv;
	while ((ch = getopt(argc, argv, "c:d:nv")) != -1) {
		switch (ch) {
		case 'c':
			free(Conf.conf_file);
			Conf.conf_file = xstrdup(optarg);
			break;
		case 'd':
			display_name = optarg;
			break;
		case 'n':
			nflag = 1;
			break;
		case 'v':
			Conf.debug++;
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (signal(SIGCHLD, sighdlr) == SIG_ERR ||
	    signal(SIGHUP, sighdlr) == SIG_ERR ||
	    signal(SIGINT, sighdlr) == SIG_ERR ||
	    signal(SIGTERM, sighdlr) == SIG_ERR)
		err(1, "signal");

	if (parse_config(Conf.conf_file, &Conf) == -1) {
		warnx("error parsing config file");
		if (nflag)
			return 1;
	}
	if (nflag)
		return 0;

	xfd = x_init(display_name);
	cwm_status = CWM_RUNNING;

#ifdef __OpenBSD__
	if (pledge("stdio rpath proc exec", NULL) == -1)
		err(1, "pledge");
#endif

	memset(&pfd, 0, sizeof(pfd));
	pfd[0].fd = xfd;
	pfd[0].events = POLLIN;
	while (cwm_status == CWM_RUNNING) {
		xev_process();
		if (poll(pfd, 1, -1) == -1) {
			if (errno != EINTR)
				warn("poll");
		}
	}
	x_teardown();
	if (cwm_status == CWM_EXEC_WM) {
		execvp(saved_argv[0], saved_argv);
		warnx("failed to re-exec");
	}

	return 0;
}

static int
x_init(const char *dpyname)
{
	int	i;

	if ((X_Dpy = XOpenDisplay(dpyname)) == NULL)
		errx(1, "unable to open display \"%s\"", XDisplayName(dpyname));

	XSetErrorHandler(x_wmerrorhandler);
	XSelectInput(X_Dpy, DefaultRootWindow(X_Dpy), SubstructureRedirectMask);
	XSync(X_Dpy, False);
	XSetErrorHandler(x_errorhandler);

	Conf.xrandr = XRRQueryExtension(X_Dpy, &Conf.xrandr_event_base, &i);

	xu_atom_init();
	conf_cursor(&Conf);

	for (i = 0; i < ScreenCount(X_Dpy); i++)
		screen_init(i);

	return ConnectionNumber(X_Dpy);
}

static void
x_teardown(void)
{
	struct screen_ctx	*sc;
	unsigned int		 i;

	conf_clear(&Conf);

	TAILQ_FOREACH(sc, &Screenq, entry) {
		for (i = 0; i < CWM_COLOR_NITEMS; i++)
			XftColorFree(X_Dpy, DefaultVisual(X_Dpy, sc->which),
			    DefaultColormap(X_Dpy, sc->which),
			    &sc->xftcolor[i]);
		XftFontClose(X_Dpy, sc->xftfont);
		XUngrabKey(X_Dpy, AnyKey, AnyModifier, sc->rootwin);
	}
	XUngrabPointer(X_Dpy, CurrentTime);
	XUngrabKeyboard(X_Dpy, CurrentTime);
	for (i = 0; i < CF_NITEMS; i++)
		XFreeCursor(X_Dpy, Conf.cursor[i]);
	XSync(X_Dpy, False);
	XSetInputFocus(X_Dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
	XCloseDisplay(X_Dpy);
}

static int
x_wmerrorhandler(Display *dpy, XErrorEvent *e)
{
	errx(1, "root window unavailable - perhaps another wm is running?");
	return 0;
}

static int
x_errorhandler(Display *dpy, XErrorEvent *e)
{
#ifdef DEBUG
	char msg[80], number[80], req[80];

	XGetErrorText(X_Dpy, e->error_code, msg, sizeof(msg));
	(void)snprintf(number, sizeof(number), "%d", e->request_code);
	XGetErrorDatabaseText(X_Dpy, "XRequest", number,
	    "<unknown>", req, sizeof(req));

	warnx("%s(0x%x): %s", req, (unsigned int)e->resourceid, msg);
#endif
	return 0;
}

static void
sighdlr(int sig)
{
	pid_t	 pid;
	int	 save_errno = errno, status;

	switch (sig) {
	case SIGCHLD:
		/* Collect dead children. */
		while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0 ||
		    (pid < 0 && errno == EINTR))
			;
		break;
	case SIGHUP:
		cwm_status = CWM_EXEC_WM;
		break;
	case SIGINT:
	case SIGTERM:
		cwm_status = CWM_QUIT;
		break;
	}

	errno = save_errno;
}

__dead void
usage(void)
{
	extern char	*__progname;

	(void)fprintf(stderr, "usage: %s [-nv] [-c file] [-d display]\n",
	    __progname);
	exit(1);
}