/* * cli.c: manages argument parsing for livid. * Copyright (C) 2026 Kit Sylvester * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "livid.h" #include #include #include #include static char doc[] = "A trashy GTK4 image viewer for the spiteful.\v" "Display FILES or the contents of the current directory. " "Directories are expanded non-recursively unless --recursive is given."; static char args_doc[] = "[FILE|DIR]..."; static struct argp_option options[] = { {"fullscreen", 'F', NULL, 0, "Start in fullscreen", 0}, {"geometry", 'g', "GEOM", 0, "Window geometry: WxH", 0}, {"borderless", 'x', NULL, 0, "Create a borderless window", 0}, {"image-bg", 'B', "STYLE", 0, "Background for transparency: checks, default, or #RRGGBB", 0}, {"draw-filename", 'd', NULL, 0, "Draw the filename overlay", 0}, {"draw-zoom", 'e', NULL, 0, "Keep the zoom overlay visible", 0}, {"title", '^', "FORMAT", 0, "Window title format string", 0}, {"auto-zoom", 'Z', NULL, 0, "Zoom images to fit the window", 0}, {"zoom", 300, "VAL", 0, "Initial zoom: percent, max or fill", 0}, {"zoom-step", 301, "N", 0, "Zoom step in percent (default 25)", 0}, {"quiet", 'q', NULL, 0, "Do not report non-fatal load errors", 0}, {"verbose", 'V', NULL, 0, "Report useful information", 0}, {"version", 'v', NULL, 0, "Output version information and exit", 0}, {"recursive", 'r', NULL, 0, "Recursively expand directories", 0}, {"sort", 'S', "MODE", 0, "Sort: none, name or mtime", 0}, {"reverse", 'n', NULL, 0, "Reverse the sort order", 0}, {"randomize", 'z', NULL, 0, "Randomize the filelist", 0}, {"version-sort", 1, NULL, 0, "Natural (version) sort for name sorting", 0}, {"start-at", '|', "FILENAME", 0, "Start at FILENAME in the filelist", 0}, {"auto-reload", 'R', NULL, 0, "Reload the current image when its file changes", 0}, {0}}; static void parse_bg_hex(const char *arg, struct argp_state *state) { if (arg[0] == '#') { arg++; } size_t len = strlen(arg); if (!(len == 3 || len == 6) || strspn(arg, "0123456789aAbBcCdDeEfF") != len) { argp_error(state, "Background colour must be #RGB or #RRGGBB"); } int d[6] = {0}; for (size_t i = 0; i < len; ++i) { d[i] = g_ascii_xdigit_value(arg[i]); } GdkRGBA *c = &g_state.bg_colour; c->alpha = 1.0F; if (len == 6) { c->red = (float)((d[0] << 4) | d[1]) / 255.0F; c->green = (float)((d[2] << 4) | d[3]) / 255.0F; c->blue = (float)((d[4] << 4) | d[5]) / 255.0F; } else { c->red = (float)(d[0] * 17) / 255.0F; c->green = (float)(d[1] * 17) / 255.0F; c->blue = (float)(d[2] * 17) / 255.0F; } } static void parse_zoom_arg(const char *arg, struct argp_state *state) { if (strcmp(arg, "max") == 0) { g_state.fit = LIVID_FIT_AUTO; g_state.zoom = 1.0; return; } if (strcmp(arg, "fill") == 0) { g_state.fit = LIVID_FIT_FILL; g_state.zoom = 1.0; return; } char *end = NULL; double pct = strtod(arg, &end); if (end == arg || *end != '\0' || pct <= 0) { argp_error(state, "Zoom must be a positive percent, max or fill"); } g_state.fit = LIVID_FIT_NONE; g_state.zoom = pct / 100.0; } static void parse_bg_arg(const char *arg, struct argp_state *state) { if (strcmp(arg, "checks") == 0) { g_state.bg = LIVID_BG_CHECKS; return; } if (strcmp(arg, "default") == 0) { g_state.bg = LIVID_BG_DEFAULT; return; } g_state.bg = LIVID_BG_SOLID; parse_bg_hex(arg, state); } static void parse_sort_arg(const char *arg, struct argp_state *state) { if (strcmp(arg, "none") == 0) { g_state.sort = LIVID_SORT_NONE; return; } if (strcmp(arg, "name") == 0) { g_state.sort = LIVID_SORT_NAME; return; } if (strcmp(arg, "mtime") == 0) { g_state.sort = LIVID_SORT_MTIME; return; } argp_error(state, "Sort must be none, name or mtime"); } static gboolean livid_parse_geometry(const char *str, int *w, int *h) { char *end = NULL; long pw = strtol(str, &end, 10); if (end == str || *end != 'x') { return FALSE; } char *end2 = NULL; long ph = strtol(end + 1, &end2, 10); if (end2 == end + 1 || *end2 != '\0') { return FALSE; } *w = (int)pw; *h = (int)ph; return TRUE; } typedef struct { GPtrArray *positional; gboolean saw_version; } cli_input; // NOLINTNEXTLINE(readability-function-size) static error_t parse_opt(int key, char *arg, struct argp_state *state) { cli_input *in = state->input; switch (key) { case 'F': g_state.fullscreen = TRUE; break; case 'g': { int w = 0; int h = 0; if (!livid_parse_geometry(arg, &w, &h)) { argp_error(state, "Invalid geometry: %s", arg); } if (w <= 0 || h <= 0) { argp_error(state, "Geometry dimensions must be positive"); } g_state.geometry_w = w; g_state.geometry_h = h; g_state.has_geometry = TRUE; break; } case 'x': g_state.borderless = TRUE; break; case 'B': parse_bg_arg(arg, state); break; case 'd': g_state.draw_filename = TRUE; break; case 'e': g_state.draw_zoom = TRUE; break; case '^': g_free(g_state.title_fmt); g_state.title_fmt = g_strdup(arg); break; case 'Z': g_state.fit = LIVID_FIT_AUTO; break; case 300: parse_zoom_arg(arg, state); break; case 301: g_state.zoom_step = (int)strtol(arg, NULL, 10); if (g_state.zoom_step <= 0) { argp_error(state, "Zoom step must be positive"); } break; case 'z': g_state.randomize = TRUE; break; case 'q': g_state.quiet = TRUE; break; case 'V': g_state.verbose = TRUE; break; case 'r': g_state.recursive = TRUE; break; case 'S': parse_sort_arg(arg, state); break; case 'n': g_state.reverse = TRUE; break; case 1: g_state.version_sort = TRUE; break; case '|': g_free(g_state.start_at); g_state.start_at = g_strdup(arg); break; case 'R': g_state.auto_reload = TRUE; break; case 'v': in->saw_version = TRUE; break; case ARGP_KEY_ARG: g_ptr_array_add(in->positional, g_strdup(arg)); break; case ARGP_KEY_END: break; default: return ARGP_ERR_UNKNOWN; } return 0; } static struct argp argp_parser = {options, parse_opt, args_doc, doc, NULL, NULL, NULL}; gboolean cli_parse(int argc, char **argv) { g_state.fit = LIVID_FIT_SCALE_DOWN; g_state.zoom = 1.0; g_state.zoom_step = 25; g_state.bg = LIVID_BG_DEFAULT; g_state.bg_colour.alpha = 1.0F; g_state.sort = LIVID_SORT_NONE; g_state.title_fmt = g_strdup("livid - [%u/%l] %n"); g_state.current = -1; cli_input in = {0}; in.positional = g_ptr_array_new_with_free_func(g_free); argp_parse(&argp_parser, argc, argv, 0, NULL, &in); if (in.saw_version) { (void)printf("livid %s (%s)\n", LIVID_VERSION, LIVID_TAGLINE); g_ptr_array_free(in.positional, TRUE); g_free(g_state.title_fmt); g_state.title_fmt = NULL; exit(EXIT_SUCCESS); } int n_pos = (int)in.positional->len; char **args = (char **)g_ptr_array_free(in.positional, FALSE); filelist_expand(args, n_pos); for (int i = 0; i < n_pos; ++i) { g_free(args[i]); } g_free((gpointer)args); if (g_state.files == NULL || g_state.files->len == 0) { (void)fprintf(stderr, "No images to display.\n"); filelist_cleanup(); return FALSE; } return TRUE; }