commit 004c587da6d73a7ce5b9cee7d867be3d45a4a139
parent c34f9949c648ba8fe0ca7e2230a3bb72ced5810b
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Sun, 18 Apr 2021 01:38:47 -0700
Clean internals and remove excess options
- Remove w, h, x, y, and t options
- Use short style usage format
- Simplify ff_open by using uint32_t rather than uint8_t
- Name the standard input /dev/stdin instead of <stdin>
Diffstat:
M | lel.c | | | 67 | +++++++++++++++---------------------------------------------------- |
1 file changed, 15 insertions(+), 52 deletions(-)
diff --git a/lel.c b/lel.c
@@ -18,11 +18,9 @@
#include "arg.h"
char *argv0;
-#define APP_NAME "lel"
-#define HEADER_FORMAT "farbfeld########"
-
/* Image status flags. */
enum { NONE = 0, LOADED = 1, SCALED = 2, DRAWN = 4 };
+
/* View mode. */
enum { ASPECT = 0, FULL_ASPECT, FULL_STRETCH };
@@ -44,7 +42,6 @@ static struct img *imgs;
static struct img *cimg;
static size_t nimgs;
static int viewmode = ASPECT;
-static char *wintitle = APP_NAME;
static char *bgcolor = "#000000";
static XImage *ximg = NULL;
static Drawable xpix = 0;
@@ -58,9 +55,6 @@ static int running = 1;
static int winwidth = 0, winheight = 0;
static int winx, winy, reqwinwidth = 320, reqwinheight = 240;
static float zoominc = 0.25;
-static int tflag;
-static int wflag;
-static int hflag;
static void
die(const char *fmt, ...)
@@ -81,34 +75,25 @@ die(const char *fmt, ...)
static void
usage(void)
{
- die("%s", APP_NAME " " VERSION "\n\n"
- "usage: " APP_NAME " [OPTIONS...] [FILE]\n"
- " -a Full window, keep aspect ratio\n"
- " -f Full window, stretch (no aspect)\n"
- " -w <w> Window width\n"
- " -h <h> Window height\n"
- " -x <x> Window x position\n"
- " -y <y> Window y position\n"
- " -t <title> Use title\n"
- " -v Print version and exit\n");
+ die("usage: %s [-afv] [file...]\n", getprogname());
}
static int
ff_open(struct img *img)
{
- uint8_t hdr[17];
+ uint32_t hdr[4];
if (img->state & LOADED)
return 0;
- if (fread(hdr, 1, strlen(HEADER_FORMAT), img->fp) != strlen(HEADER_FORMAT))
+ if (fread(hdr, sizeof(*hdr), 4, img->fp) != 4)
return -1;
if (memcmp(hdr, "farbfeld", 8))
return -1;
- img->width = ntohl((hdr[8] << 0) | (hdr[9] << 8) | (hdr[10] << 16) | (hdr[11] << 24));
- img->height = ntohl((hdr[12] << 0) | (hdr[13] << 8) | (hdr[14] << 16) | (hdr[15] << 24));
+ img->width = ntohl(hdr[2]);
+ img->height = ntohl(hdr[3]);
if (img->width <= 0 || img->height <= 0)
return -1;
@@ -183,12 +168,9 @@ loadimg(void)
die("can't open image (invalid format?)\n");
if (ff_read(cimg))
die("can't read image\n");
- if (!wflag)
- reqwinwidth = cimg->width;
- if (!hflag)
- reqwinheight = cimg->height;
- if (!tflag)
- wintitle = cimg->filename;
+ reqwinwidth = cimg->width;
+ reqwinheight = cimg->height;
+ wintitle = cimg->filename;
}
static void
@@ -196,7 +178,7 @@ reloadimg(void)
{
loadimg();
XResizeWindow(dpy, win, reqwinwidth, reqwinheight);
- XStoreName(dpy, win, wintitle);
+ XStoreName(dpy, win, cimg->filename);
XFlush(dpy);
}
@@ -508,7 +490,7 @@ handleevent(XEvent *ev)
static void
setup(void)
{
- XClassHint class = { APP_NAME, APP_NAME };
+ XClassHint class = { getprogname(), getprogname() };
if (!(dpy = XOpenDisplay(NULL)))
die("can't open X display\n");
@@ -522,7 +504,7 @@ setup(void)
cmap = DefaultColormap(dpy, screen);
if (!XAllocNamedColor(dpy, cmap, bgcolor, &bg, &bg))
die("cannot allocate color\n");
- XStoreName(dpy, win, wintitle);
+ XStoreName(dpy, win, cimg->filename);
XSelectInput(dpy, win, StructureNotifyMask | ExposureMask | KeyPressMask |
ButtonPressMask);
XMapRaised(dpy, win);
@@ -540,7 +522,8 @@ run(void)
}
int
-main(int argc, char *argv[]) {
+main(int argc, char *argv[])
+{
FILE *fp;
int i, j;
@@ -551,26 +534,6 @@ main(int argc, char *argv[]) {
case 'f':
viewmode = FULL_STRETCH;
break;
- case 'h':
- hflag = 1;
- if (!(reqwinheight = atoi(EARGF(usage()))))
- usage();
- break;
- case 't':
- wintitle = EARGF(usage());
- tflag = 1;
- break;
- case 'w':
- wflag = 1;
- if (!(reqwinwidth = atoi(EARGF(usage()))))
- usage();
- break;
- case 'x':
- winx = atoi(EARGF(usage()));
- break;
- case 'y':
- winy = atoi(EARGF(usage()));
- break;
default:
usage();
break;
@@ -581,7 +544,7 @@ main(int argc, char *argv[]) {
if (!imgs)
die("calloc:");
nimgs = 1;
- imgs[0].filename = "<stdin>";
+ imgs[0].filename = "/dev/stdin";
imgs[0].fp = stdin;
imgs[0].view.zoomfact = 1.0;
} else {