commit 4230522
sewn
·
2026-08-22 12:41:15 +0000 UTC
parent d920d26
self-resolve image mode from dir name
1 files changed,
+36,
-37
M
wawa.c
M
wawa.c
+36,
-37
1@@ -2,6 +2,7 @@
2 #include <byteswap.h>
3 #include <dirent.h>
4 #include <fcntl.h>
5+#include <libgen.h>
6 #include <stdbool.h>
7 #include <sys/mman.h>
8 #include <sys/stat.h>
9@@ -85,25 +86,6 @@ die(const char *fmt, ...)
10 exit(EXIT_FAILURE);
11 }
12
13-static bool
14-parse_color(const char *src)
15-{
16- int len;
17-
18- if (src[0] == '#')
19- src++;
20- len = strlen(src);
21- if (len != 6 && len != 8) return false;
22-
23- color = strtoul(src, NULL, 16);
24- if (len == 6)
25- color = (color << 8) | 0xFF;
26-
27- /* for colorspace conversion in output_image_load */
28- color = bswap_32(color);
29- return true;
30-}
31-
32 static void
33 image_color(unsigned char *dst, struct output *output)
34 {
35@@ -420,26 +402,42 @@ static const struct wl_registry_listener registry_listener = {
36 int
37 main(int argc, char *argv[])
38 {
39+ int len;
40+ char *mode = NULL, *path = NULL;
41+ struct stat st;
42+
43 switch (argc) {
44 case 2:
45- if (!(parse_color(argv[1]))) goto usage;
46+ path = argv[1];
47+ if (path[0] == '#')
48+ path++;
49+ len = strlen(path);
50+ if (len != 6 && len != 8) goto file;
51+
52+ color = strtoul(path, NULL, 16);
53+ if (len == 6) color = (color << 8) | 0xFF;
54+ /* for colorspace conversion in output_image_load */
55+ color = bswap_32(color);
56+
57 image_modify = image_color;
58 break;
59- case 3:
60- if (!strcmp(argv[1], "stretch")) image_modify = image_stretch;
61- else if (!strcmp(argv[1], "fit")) image_modify = image_fit;
62- else if (!strcmp(argv[1], "fill")) image_modify = image_fill;
63- else if (!strcmp(argv[1], "tile")) image_modify = image_tile;
64- else if (!strcmp(argv[1], "spread")) image_modify = image_spread;
65- else goto usage;
66-
67- struct stat st;
68- if (stat(argv[2], &st) < 0) die("fopen %s:", argv[2]);
69-
70- const char *path = argv[2];
71+ case 3:;
72 struct dirent **namelist = NULL, *name;
73 int dirn = 0;
74
75+ mode = argv[1]; path = argv[2];
76+mode:
77+ if (!strcmp(mode, "stretch")) image_modify = image_stretch;
78+ else if (!strcmp(mode, "fit")) image_modify = image_fit;
79+ else if (!strcmp(mode, "fill")) image_modify = image_fill;
80+ else if (!strcmp(mode, "tile")) image_modify = image_tile;
81+ else if (!strcmp(mode, "spread")) image_modify = image_spread;
82+ else if (!namelist) goto usage;
83+ else goto prescan;
84+
85+file:
86+ if (stat(path, &st) < 0) die("stat %s:", path);
87+
88 if (S_ISDIR(st.st_mode)) {
89 srand((intptr_t)path | (unsigned int)time(NULL));
90 scan:
91@@ -447,9 +445,10 @@ scan:
92 if (namelist) free(namelist);
93 if ((dirn = scandir(".", &namelist, NULL, alphasort)) < 0) die("scandir:");
94 if (dirn < 3) die("no images");
95- int i = 2 + rand() % (dirn - 2);
96- name = namelist[i];
97- path = name->d_name;
98+ name = namelist[2 + rand() % (dirn - 2)];
99+ mode = basename((path = name->d_name));
100+ if (argc == 2) goto mode;
101+prescan:
102 if (name->d_type == DT_DIR) goto scan;
103 }
104
105@@ -479,8 +478,8 @@ scan:
106 return EXIT_SUCCESS;
107
108 usage:
109- fprintf(stderr, "usage: %s fill|fit|spread|stretch|tile filename|dir\n"
110- " %s RRGGBBAA\n",
111+ fprintf(stderr, "usage: %s [fill|fit|spread|stretch|tile] filename|dir\n"
112+ " %s filename|dir|RRGGBB[AA]\n",
113 argv[0], argv[0]);
114 return EXIT_FAILURE;
115 }