calmwm.c: fix undefined macros on Linux

This commit is contained in:
cælōrum spectātrīx 2026-01-18 19:21:13 +01:00
parent da63c8d00e
commit a32eb91c6c

256
calmwm.c
View file

@ -36,6 +36,10 @@
#include "calmwm.h" #include "calmwm.h"
#define __dead
#define INFTIM (-1)
#define WAIT_ANY (-1)
Display *X_Dpy; Display *X_Dpy;
Time Last_Event_Time = CurrentTime; Time Last_Event_Time = CurrentTime;
Atom cwmh[CWMH_NITEMS]; Atom cwmh[CWMH_NITEMS];
@ -54,184 +58,184 @@ static int x_wmerrorhandler(Display *, XErrorEvent *);
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
char *display_name = NULL; char *display_name = NULL;
char *fallback; char *fallback;
int ch, xfd, nflag = 0; int ch, xfd, nflag = 0;
struct pollfd pfd[1]; struct pollfd pfd[1];
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
warnx("no locale support"); warnx("no locale support");
mbtowc(NULL, NULL, MB_CUR_MAX); mbtowc(NULL, NULL, MB_CUR_MAX);
conf_init(&Conf); conf_init(&Conf);
fallback = u_argv(argv); fallback = u_argv(argv);
Conf.wm_argv = u_argv(argv); Conf.wm_argv = u_argv(argv);
while ((ch = getopt(argc, argv, "c:d:nv")) != -1) { while ((ch = getopt(argc, argv, "c:d:nv")) != -1) {
switch (ch) { switch (ch) {
case 'c': case 'c':
free(Conf.conf_file); free(Conf.conf_file);
Conf.conf_file = xstrdup(optarg); Conf.conf_file = xstrdup(optarg);
break; break;
case 'd': case 'd':
display_name = optarg; display_name = optarg;
break; break;
case 'n': case 'n':
nflag = 1; nflag = 1;
break; break;
case 'v': case 'v':
Conf.debug++; Conf.debug++;
break; break;
default: default:
usage(); usage();
} }
} }
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (signal(SIGCHLD, sighdlr) == SIG_ERR || if (signal(SIGCHLD, sighdlr) == SIG_ERR ||
signal(SIGHUP, sighdlr) == SIG_ERR || signal(SIGHUP, sighdlr) == SIG_ERR ||
signal(SIGINT, sighdlr) == SIG_ERR || signal(SIGINT, sighdlr) == SIG_ERR ||
signal(SIGTERM, sighdlr) == SIG_ERR) signal(SIGTERM, sighdlr) == SIG_ERR)
err(1, "signal"); err(1, "signal");
if (parse_config(Conf.conf_file, &Conf) == -1) { if (parse_config(Conf.conf_file, &Conf) == -1) {
warnx("error parsing config file"); warnx("error parsing config file");
if (nflag) if (nflag)
return 1; return 1;
} }
if (nflag) if (nflag)
return 0; return 0;
xfd = x_init(display_name); xfd = x_init(display_name);
cwm_status = CWM_RUNNING; cwm_status = CWM_RUNNING;
if (pledge("stdio rpath proc exec", NULL) == -1) if (pledge("stdio rpath proc exec", NULL) == -1)
err(1, "pledge"); err(1, "pledge");
memset(&pfd, 0, sizeof(pfd)); memset(&pfd, 0, sizeof(pfd));
pfd[0].fd = xfd; pfd[0].fd = xfd;
pfd[0].events = POLLIN; pfd[0].events = POLLIN;
while (cwm_status == CWM_RUNNING) { while (cwm_status == CWM_RUNNING) {
xev_process(); xev_process();
if (poll(pfd, 1, INFTIM) == -1) { if (poll(pfd, 1, INFTIM) == -1) {
if (errno != EINTR) if (errno != EINTR)
warn("poll"); warn("poll");
} }
} }
x_teardown(); x_teardown();
if (cwm_status == CWM_EXEC_WM) { if (cwm_status == CWM_EXEC_WM) {
u_exec(Conf.wm_argv); u_exec(Conf.wm_argv);
warnx("'%s' failed to start, starting fallback", Conf.wm_argv); warnx("'%s' failed to start, starting fallback", Conf.wm_argv);
u_exec(fallback); u_exec(fallback);
} }
return 0; return 0;
} }
static int static int
x_init(const char *dpyname) x_init(const char *dpyname)
{ {
int i; int i;
if ((X_Dpy = XOpenDisplay(dpyname)) == NULL) if ((X_Dpy = XOpenDisplay(dpyname)) == NULL)
errx(1, "unable to open display \"%s\"", XDisplayName(dpyname)); errx(1, "unable to open display \"%s\"", XDisplayName(dpyname));
XSetErrorHandler(x_wmerrorhandler); XSetErrorHandler(x_wmerrorhandler);
XSelectInput(X_Dpy, DefaultRootWindow(X_Dpy), SubstructureRedirectMask); XSelectInput(X_Dpy, DefaultRootWindow(X_Dpy), SubstructureRedirectMask);
XSync(X_Dpy, False); XSync(X_Dpy, False);
XSetErrorHandler(x_errorhandler); XSetErrorHandler(x_errorhandler);
Conf.xrandr = XRRQueryExtension(X_Dpy, &Conf.xrandr_event_base, &i); Conf.xrandr = XRRQueryExtension(X_Dpy, &Conf.xrandr_event_base, &i);
xu_atom_init(); xu_atom_init();
conf_cursor(&Conf); conf_cursor(&Conf);
for (i = 0; i < ScreenCount(X_Dpy); i++) for (i = 0; i < ScreenCount(X_Dpy); i++)
screen_init(i); screen_init(i);
return ConnectionNumber(X_Dpy); return ConnectionNumber(X_Dpy);
} }
static void static void
x_teardown(void) x_teardown(void)
{ {
struct screen_ctx *sc; struct screen_ctx *sc;
unsigned int i; unsigned int i;
conf_clear(&Conf); conf_clear(&Conf);
TAILQ_FOREACH(sc, &Screenq, entry) { TAILQ_FOREACH(sc, &Screenq, entry) {
for (i = 0; i < CWM_COLOR_NITEMS; i++) for (i = 0; i < CWM_COLOR_NITEMS; i++)
XftColorFree(X_Dpy, DefaultVisual(X_Dpy, sc->which), XftColorFree(X_Dpy, DefaultVisual(X_Dpy, sc->which),
DefaultColormap(X_Dpy, sc->which), DefaultColormap(X_Dpy, sc->which),
&sc->xftcolor[i]); &sc->xftcolor[i]);
XftFontClose(X_Dpy, sc->xftfont); XftFontClose(X_Dpy, sc->xftfont);
XUngrabKey(X_Dpy, AnyKey, AnyModifier, sc->rootwin); XUngrabKey(X_Dpy, AnyKey, AnyModifier, sc->rootwin);
} }
XUngrabPointer(X_Dpy, CurrentTime); XUngrabPointer(X_Dpy, CurrentTime);
XUngrabKeyboard(X_Dpy, CurrentTime); XUngrabKeyboard(X_Dpy, CurrentTime);
for (i = 0; i < CF_NITEMS; i++) for (i = 0; i < CF_NITEMS; i++)
XFreeCursor(X_Dpy, Conf.cursor[i]); XFreeCursor(X_Dpy, Conf.cursor[i]);
XSync(X_Dpy, False); XSync(X_Dpy, False);
XSetInputFocus(X_Dpy, PointerRoot, RevertToPointerRoot, CurrentTime); XSetInputFocus(X_Dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
XCloseDisplay(X_Dpy); XCloseDisplay(X_Dpy);
} }
static int static int
x_wmerrorhandler(Display *dpy, XErrorEvent *e) x_wmerrorhandler(Display *dpy, XErrorEvent *e)
{ {
errx(1, "root window unavailable - perhaps another wm is running?"); errx(1, "root window unavailable - perhaps another wm is running?");
return 0; return 0;
} }
static int static int
x_errorhandler(Display *dpy, XErrorEvent *e) x_errorhandler(Display *dpy, XErrorEvent *e)
{ {
#ifdef DEBUG #ifdef DEBUG
char msg[80], number[80], req[80]; char msg[80], number[80], req[80];
XGetErrorText(X_Dpy, e->error_code, msg, sizeof(msg)); XGetErrorText(X_Dpy, e->error_code, msg, sizeof(msg));
(void)snprintf(number, sizeof(number), "%d", e->request_code); (void)snprintf(number, sizeof(number), "%d", e->request_code);
XGetErrorDatabaseText(X_Dpy, "XRequest", number, XGetErrorDatabaseText(X_Dpy, "XRequest", number,
"<unknown>", req, sizeof(req)); "<unknown>", req, sizeof(req));
warnx("%s(0x%x): %s", req, (unsigned int)e->resourceid, msg); warnx("%s(0x%x): %s", req, (unsigned int)e->resourceid, msg);
#endif #endif
return 0; return 0;
} }
static void static void
sighdlr(int sig) sighdlr(int sig)
{ {
pid_t pid; pid_t pid;
int save_errno = errno, status; int save_errno = errno, status;
switch (sig) { switch (sig) {
case SIGCHLD: case SIGCHLD:
/* Collect dead children. */ /* Collect dead children. */
while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0 || while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0 ||
(pid < 0 && errno == EINTR)) (pid < 0 && errno == EINTR))
; ;
break; break;
case SIGHUP: case SIGHUP:
cwm_status = CWM_EXEC_WM; cwm_status = CWM_EXEC_WM;
break; break;
case SIGINT: case SIGINT:
case SIGTERM: case SIGTERM:
cwm_status = CWM_QUIT; cwm_status = CWM_QUIT;
break; break;
} }
errno = save_errno; errno = save_errno;
} }
__dead void __dead void
usage(void) usage(void)
{ {
extern char *__progname; extern char *__progname;
(void)fprintf(stderr, "usage: %s [-nv] [-c file] [-d display]\n", (void)fprintf(stderr, "usage: %s [-nv] [-c file] [-d display]\n",
__progname); __progname);
exit(1); exit(1);
} }