From 65e94ffae271dc9ef75f5227d8ec647b1dd428bd Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 2 Aug 2024 15:36:38 +0200 Subject: wbg: use getopt() for option parsing, add help and version options --- main.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 78 insertions(+), 13 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 01fb4d9..f2d341b 100644 --- a/main.c +++ b/main.c @@ -1,13 +1,14 @@ -#include -#include -#include -#include -#include +#include #include +#include +#include +#include #include +#include +#include +#include +#include #include -#include -#include #include @@ -23,6 +24,7 @@ #include "log.h" #include "shm.h" #include "version.h" +#include "wbg-features.h" #if defined(WBG_HAVE_PNG) #include "png-wbg.h" @@ -83,8 +85,9 @@ render(struct output *output) struct buffer *buf = shm_get_buffer( shm, width * scale, height * scale, (uintptr_t)output); - - if (!buf) return; + + if (!buf) + return; pixman_image_t *src = image; bool is_svg = false; @@ -111,7 +114,7 @@ render(struct output *output) pixman_image_set_filter(src, PIXMAN_FILTER_BEST, NULL, 0); } - pixman_image_composite32(PIXMAN_OP_SRC, src, NULL, buf->pix, + pixman_image_composite32(PIXMAN_OP_SRC, src, NULL, buf->pix, 0, 0, 0, 0, 0, 0, width * scale, height * scale); if (is_svg) { @@ -378,11 +381,73 @@ handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) static const struct wl_registry_listener registry_listener = { .global = &handle_global, .global_remove = &handle_global_remove, -}; + }; + +static void +usage(const char *progname) +{ + printf("Usage: %s [OPTIONS] IMAGE_FILE\n" + "\n" + "Options:\n" + " -s,--stretch stretch the image to fill the screen\n" + " -v,--version show the version number and quit\n" + , progname); +} + +static const char * +version_and_features(void) +{ + static char buf[256]; + snprintf(buf, sizeof(buf), + "version: %s %cpng %csvg %cjpg %cjxl %cwebp", + WBG_VERSION, + feature_png() ? '+' : '-', + feature_svg() ? '+' : '-', + feature_jpg() ? '+' : '-', + feature_jxl() ? '+' : '-', + feature_webp() ? '+' : '-'); + return buf; +} int -main(int argc, const char *const *argv) +main(int argc, char *const *argv) { + const char *progname = argv[0]; + + const struct option longopts[] = { + {"stretch", no_argument, 0, 's'}, + {"version", no_argument, 0, 'v'}, + {"help", no_argument, 0, 'h'}, + {NULL, no_argument, 0, 0}, + }; + + while (true) { + int c = getopt_long(argc, argv, ":svh", longopts, NULL); + if (c < 0) + break; + + switch (c) { + case 's': + break; + + case 'v': + printf("wbg version: %s\n", version_and_features()); + return EXIT_SUCCESS; + + case 'h': + usage(progname); + return EXIT_SUCCESS; + + case ':': + fprintf(stderr, "error: -%c: missing required argument\n", optopt); + return EXIT_FAILURE; + + case '?': + fprintf(stderr, "error: -%c: invalid option\n", optopt); + return EXIT_FAILURE; + } + } + if (argc != 2 && (argc != 3 || (strcmp(argv[1], "-s") != 0 && strcmp(argv[1], "--stretch") != 0))) { fprintf(stderr, "Usage: %s [-s|--stretch] \n", argv[0]); return EXIT_FAILURE; @@ -564,4 +629,4 @@ out: log_deinit(); fclose(fp); return exit_code; -} \ No newline at end of file +} -- cgit v1.2.3