commit 263fdaa4366782d38c7546b97f79bd76419913f8
parent c186e239cbc4bc29364649e179bb8dce363b04c3
Author: Louis Burda <quent.burda@gmail.com>
Date: Mon, 17 Oct 2022 01:32:28 +0200
Replaced die with linux err(x) + minor fixes
Diffstat:
M | bgdraw.c | | | 101 | +++++++++++++++++++++++++++++++++++++++---------------------------------------- |
1 file changed, 50 insertions(+), 51 deletions(-)
diff --git a/bgdraw.c b/bgdraw.c
@@ -13,6 +13,7 @@
#include <sys/time.h>
#include <unistd.h>
+#include <err.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
@@ -23,8 +24,6 @@
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
#define ARRLEN(x) (sizeof(x) / sizeof((x)[0]))
-#define OOM_CHECK(p) do { if ((p) == NULL) die("Out of memory\n"); } while (0)
-
struct image {
char *name;
char *path;
@@ -42,9 +41,7 @@ struct image {
struct image *next;
};
-static void die(const char *fmtstr, ...);
-
-static void monitor_offset(int num, int *x, int *y);
+static void monitor_offset(const char *name, int num, int *x, int *y);
static void convert_rgb_bgr(uint8_t *pix, int width, int height,
int stride, int pixlen);
@@ -68,33 +65,25 @@ static Screen *screen;
static Window root;
static Visual *vis;
static GC gc;
+static int depth;
void
-die(const char *fmtstr, ...)
-{
- va_list ap;
-
- va_start(ap, fmtstr);
- vfprintf(stderr, fmtstr, ap);
- va_end(ap);
-
- exit(1);
-}
-
-void
-monitor_offset(int num, int *x, int *y)
+monitor_offset(const char *name, int num, int *x, int *y)
{
XineramaScreenInfo *info;
int mcount, screen;
- if (!XineramaIsActive(display))
- die("No monitor support\n");
+ if (!XineramaIsActive(display)) {
+ if (debug) fprintf(stderr, "No monitor support\n");
+ *x = *y = 0;
+ return;
+ }
if (!(info = XineramaQueryScreens(display, &mcount)))
- die("Failed to query xinerama\n");
+ errx(1, "Failed to query xinerama");
if (num < 0 || num > mcount)
- die("Monitor number out of range\n");
+ errx(1, "IMG %s: Monitor number out of range", name);
*x = info[num].x_org;
*y = info[num].y_org;
@@ -119,7 +108,7 @@ convert_rgb_bgr(uint8_t *pix, int width, int height, int stride, int pixlen)
void
draw_img(struct image *img)
{
- if (debug) printf("DRAW %s\n", img->name);
+ if (debug) fprintf(stderr, "DRAW %s\n", img->name);
XPutImage(display, root, gc, img->frames[img->framei], 0, 0,
img->x, img->y, img->w, img->h);
}
@@ -142,7 +131,7 @@ load(const char *path)
FILE *file;
file = fopen(path, "r");
- if (!file) die("Failed to open path: %s\n", path);
+ if (!file) err(1, "fopen %s", path);
images = NULL;
next = &images;
@@ -160,7 +149,7 @@ load(const char *path)
load_img(&img);
*next = malloc(sizeof(struct image));
- OOM_CHECK(*next);
+ if (!*next) err(1, "malloc");
memcpy(*next, &img, sizeof(struct image));
next = &((*next)->next);
}
@@ -195,13 +184,13 @@ load_attrs(FILE *file, struct image *img, int *lineno)
end = '\0';
if (sscanf(line, "[%m[-_a-zA-Z0-9]]%c",
&img->name, &end) != 1 || end)
- die("Invalid title line %i:\n%s %c\n",
+ errx(1, "Invalid title line %i:\n%s %c",
*lineno, line, end);
continue;
}
tok = strchr(line, ' ');
- if (!tok) die("[%s] Invalid line %i: %s\n",
+ if (!tok) errx(1, "IMG %s: Invalid line %i: %s",
img->name, *lineno, line);
*tok = '\0';
@@ -210,7 +199,7 @@ load_attrs(FILE *file, struct image *img, int *lineno)
if (!strcmp(key, "pos")) {
tok = strchr(val, 'x');
- if (!tok) die("[%s] Invalid pos: %s\n",
+ if (!tok) errx(1, "IMG %s: Invalid pos: %s",
img->name, val);
*tok = '\0';
@@ -218,26 +207,26 @@ load_attrs(FILE *file, struct image *img, int *lineno)
img->y = atoi(tok + 1);
} else if (!strcmp(key, "size")) {
tok = strchr(val, 'x');
- if (!tok) die("[%s] Invalid size: %s\n",
+ if (!tok) errx(1, "IMG %s: Invalid size: %s",
img->name, val);
*tok = '\0';
if ((img->w = atoi(val)) <= 0)
- die("[%s] Invalid width: %s\n",
+ errx(1, "IMG %s: Invalid width: %s",
img->name, val);
if ((img->h = atoi(tok + 1)) <= 0)
- die("[%s] Invalid height: %s\n",
+ errx(1, "IMG %s: Invalid height: %s",
img->name, tok + 1);
} else if (!strcmp(key, "monitor")) {
- monitor_offset(atoi(val), &img->ox, &img->oy);
+ monitor_offset(img->name, atoi(val), &img->ox, &img->oy);
} else if (!strcmp(key, "frametime")) {
img->frametime = atof(val);
} else if (!strcmp(key, "path")) {
img->path = strdup(val);
- OOM_CHECK(img->path);
+ if (!img->path) err(1, "strdup");
} else {
- die("[%s] Invalid line %i:\n%s\n",
+ errx(1, "IMG %s: Invalid line %i:\n%s",
img->name, *lineno, line);
}
@@ -259,10 +248,10 @@ load_img(struct image *img)
uint8_t *frames;
FILE *file;
- if (debug) printf("LOAD %s\n", img->name);
+ if (debug) fprintf(stderr, "LOAD %s\n", img->name);
if (!(file = stbi__fopen(img->path, "rb")))
- die("[%s] Path does not exist\n", img->name);
+ errx(1, "IMG %s: Path does not exist", img->name);
stbi__start_file(&ctx, file);
@@ -270,33 +259,36 @@ load_img(struct image *img)
frames = stbi__load_gif_main(&ctx, &img->durations,
&width, &height, &img->framecnt, &img->channels, 4);
if (!frames || !img->framecnt)
- die("[%s] Failed to load gif image\n");
+ errx(1, "IMG %s: Failed to load gif image", img->name);
img->frames = malloc(sizeof(XImage*) * img->framecnt);
- OOM_CHECK(img->frames);
+ if (!img->frames) err(1, "malloc");
for (i = 0; i < img->framecnt; i++) {
pix = frames + width * height * 4 * i;
rpix = malloc(img->w * img->h * 4);
- OOM_CHECK(rpix);
+ if (!rpix) err(1, "malloc");
if (!stbir_resize_uint8(pix, width, height, 0,
rpix, img->w, img->h, 0, 4))
- die("[%s] Failed to resize image\n", img->name);
+ errx(1, "IMG %s: Failed to resize image",
+ img->name);
convert_rgb_bgr(rpix, img->w, img->h, img->w * 4, 4);
img->frames[i] = XCreateImage(display, vis,
24, ZPixmap, 0, rpix,
img->w, img->h, 32, 0);
- if (!img->frames[i]) die("Failed to create XImage\n");
+ if (!img->frames[i])
+ errx(1, "IMG %s: Failed to create XImage",
+ img->name);
}
STBI_FREE(frames);
} else {
pix = stbi__load_main(&ctx, &width, &height,
&img->channels, 4, &info, 8);
- if (!pix) die("[%s] Failed to load image\n", img->name);
+ if (!pix) errx(1, "IMG %s: Failed to load image", img->name);
if (!img->w || !img->h) {
img->w = width;
@@ -304,17 +296,18 @@ load_img(struct image *img)
}
rpix = malloc(img->w * img->h * 4);
- OOM_CHECK(rpix);
+ if (!rpix) err(1, "malloc");
if (!stbir_resize_uint8(pix, width, height, 0,
rpix, img->w, img->h, 0, 4))
- die("[%s] Failed to resize image\n", img->name);
+ errx(1, "IMG %s: Failed to resize image", img->name);
img->durations = NULL;
img->frames = malloc(sizeof(XImage*));
- OOM_CHECK(img->frames);
- img->frames[0] = XCreateImage(display, vis, 24,
+ if (!img->frames) err(1, "malloc");
+ img->frames[0] = XCreateImage(display, vis, depth,
ZPixmap, 0, rpix, img->w, img->h, 32, 0);
- if (!img->frames[0]) die("Failed to create XImage\n");
+ if (!img->frames[0])
+ errx(1, "IMG %s: Failed to create XImage", img->name);
free(pix);
}
@@ -348,15 +341,20 @@ free_img(struct image *img)
void
init(void)
{
+ XWindowAttributes attrs;
+
display = XOpenDisplay(NULL);
- if (!display) die("Failed to open display\n");
+ if (!display) errx(1, "Failed to open display");
screen = XDefaultScreenOfDisplay(display);
- if (!screen) die("Failed to get screen\n");
+ if (!screen) errx(1, "Failed to get screen");
vis = DefaultVisualOfScreen(screen);
root = RootWindowOfScreen(screen);
+ XGetWindowAttributes(display, root, &attrs);
+ depth = attrs.depth;
+
gc = XCreateGC(display, root, 0, NULL);
XSelectInput(display, root, ExposureMask);
@@ -398,10 +396,11 @@ main(int argc, const char *argv[])
for (arg = argv + 1; *arg; arg++) {
if (!strcmp(*arg, "-d")) {
debug = true;
- } else if (!strcmp(*arg, "-c")) {
+ } else if (!strcmp(*arg, "-c") && arg[1]) {
config_path = *(++arg);
} else {
- die("USAGE: bgdraw [-d] [-c CONFIG]\n");
+ printf("USAGE: bgdraw [-d] [-c CONFIG]\n");
+ return 0;
}
}