commit - f7cc10d779f671cfc3ac8d06a30c6f44daf41265
commit + 787957df3c63e9a734a66d1059bfa0b01e2748fd
blob - a6b088a6ca740ad6618e2faadc3302e9bf0aa1c0
blob + c10fca2ba7bf5071888a1b9ecfc7ce86b1855d2f
--- bxwm.c
+++ bxwm.c
* See LICENSE.md and README.md for details.
*/
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
-#include <stdarg.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
-#include <X11/cursorfont.h>
#include <X11/Xatom.h>
+#include <X11/cursorfont.h>
#include "config.h"
/* Forward Declarations */
/* Utilities */
+static void die(const char *fmt, ...);
static void logmsg(const char *fmt, ...);
static void spawn(const char *cmd);
static int xerror(Display *dpy, XErrorEvent *ee);
XEvent ev;
- if (!(dpy = XOpenDisplay(NULL))) exit(1);
+ die("bxwm: cannot open display");
root = DefaultRootWindow(dpy);
server_fd = socket_init();
return 0;
}
+static void
+die(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ cleanup(1);
+}
+
/* Uncomment to enable debug logging */
/* #define DEBUG */
int
socket_init(void) {
struct sockaddr_un addr;
- int server_fd;
- char socket_path[256];
char *home = getenv("HOME");
if (home == NULL) {
- fprintf(stderr, "bxwm: HOME not set\n");
- exit(1);
+ die("bxwm: HOME not set");
}
snprintf(socket_path, sizeof(socket_path), "%s%s", home, SOCKET_SUFFIX);
server_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (server_fd < 0) {
- perror("bxwm: socket");
- exit(1);
+ die("bxwm: bind: %s", strerror(errno));
}
memset(&addr, 0, sizeof(addr));
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
if (bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
- perror("bxwm: bind");
- close(server_fd);
- exit(1);
+ die("bxwm: bind: %s", strerror(errno));
}
if (listen(server_fd, SOMAXCONN) < 0) {
- perror("bxwm: listen");
- close(server_fd);
- unlink(socket_path);
- exit(1);
+ die("bxwm: listen: %s", strerror(errno));
}
return server_fd;
XSetErrorHandler(xerror);
if (XGetSelectionOwner(dpy, XInternAtom(dpy, "WM_S0", False))) {
- fprintf(stderr, "bxwm: another window manager is running\n");
- cleanup(1);
+ die("bxwm: another window manager is running");
}
if (!XParseColor(dpy, cmap, BORDER_FOCUSED, &color) ||
!XAllocColor(dpy, cmap, &color)) {
- fprintf(stderr, "bxwm: cannot allocate focus color '%s'\n", BORDER_FOCUSED);
- cleanup(1);
+ die("bxwm: cannot allocate focus color '%s'", BORDER_UNFOCUSED);
}
focus_pixel = color.pixel;
if (!XParseColor(dpy, cmap, BORDER_UNFOCUSED, &color) ||
!XAllocColor(dpy, cmap, &color)) {
- fprintf(stderr, "bxwm: cannot allocate unfocus color '%s'\n", BORDER_UNFOCUSED);
- cleanup(1);
+ die("bxwm: cannot allocate unfocus color '%s'", BORDER_UNFOCUSED);
}
unfocus_pixel = color.pixel;
clients = realloc(clients, ++num_clients * sizeof(Client));
if (!clients) {
- fprintf(stderr, "bxwm: realloc failed\n");
- cleanup(1);
+ die("bxwm: realloc failed");
}
c = &clients[num_clients - 1];
c->win = w;