diff options
Diffstat (limited to 'src/options.c')
-rw-r--r-- | src/options.c | 558 |
1 files changed, 0 insertions, 558 deletions
diff --git a/src/options.c b/src/options.c deleted file mode 100644 index ed5886a..0000000 --- a/src/options.c +++ /dev/null @@ -1,558 +0,0 @@ -/* options.c -- Program options - * This file is part of redshift-ng. - * - * redshift-ng 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. - * - * redshift-ng 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 redshift-ng. If not, see <http://www.gnu.org/licenses/>. - * - * Copyright (c) 2017 Jon Lund Steffensen <jonlst@gmail.com> - * Copyright (c) 2025 Mattias Andrée <m@maandree.se> - */ -#include "common.h" - -/* Angular elevation of the sun at which the colour temperature - transition period starts and ends (in degress). - Transition during twilight, and while the sun is lower than - 3.0 degrees above the horizon. */ -#define TRANSITION_LOW LIBRED_SOLAR_ELEVATION_CIVIL_DUSK_DAWN -#define TRANSITION_HIGH 3.0 - -/* Default values for parameters. */ -#define DEFAULT_DAY_TEMPERATURE 6500UL -#define DEFAULT_NIGHT_TEMPERATURE 4500UL -#define DEFAULT_BRIGHTNESS NEUTRAL_BRIGHTNESS -#define DEFAULT_GAMMA NEUTRAL_GAMMA - - -/* TODO missing translation */ -USAGE("[-b day:night] [-c file] [-g r:g:b] [-l latitude:longitude | -l provider[:options]]" - " [-m method[:options]] [-o | -O temperature | -t day:night | -x] [-pPrv] | -hV"); - - -/* A brightness string contains either one floating point value, - or two values separated by a colon. */ -static void -parse_brightness_string(const char *str, double *bright_day, double *bright_night) -{ - char *s = strchr(str, ':'); - if (s) { - *s++ = '\0'; - *bright_day = atof(str); - *bright_night = atof(s); - } else { - *bright_day = *bright_night = atof(str); - } -} - -/* A gamma string contains either one floating point value, - or three values separated by colon. */ -static int -parse_gamma_string(const char *str, double gamma[3]) -{ - char *s = strchr(str, ':'); - if (!s) { - /* Use value for all channels */ - double g = atof(str); - gamma[0] = gamma[1] = gamma[2] = g; - } else { - /* Parse separate value for each channel */ - char *g_s; - *s++ = '\0'; - g_s = s; - s = strchr(s, ':'); - if (!s) - return -1; - - *s++ = '\0'; - gamma[0] = atof(str); /* Red */ - gamma[1] = atof(g_s); /* Blue */ - gamma[2] = atof(s); /* Green */ - } - - return 0; -} - -/* Parse transition time string e.g. "04:50". Returns negative on failure, - otherwise the parsed time is returned as seconds since midnight. */ -static int -parse_transition_time(const char *str, const char **end) -{ - char *min = NULL; - long hours, minutes; - - errno = 0; - hours = strtol(str, (void *)&min, 10); - if (errno || min == str || min[0] != ':' || hours < 0 || hours >= 24) - return -1; - - min += 1; - errno = 0; - minutes = strtol(min, (void *)end, 10); - if (errno || *end == min || minutes < 0 || minutes >= 60) - return -1; - - return minutes * 60 + hours * 3600; -} - -/* Parse transition range string e.g. "04:50-6:20". Returns negative on - failure, otherwise zero. Parsed start and end times are returned as seconds - since midnight. */ -static int -parse_transition_range(const char *str, struct time_range *range) -{ - const char *next = NULL, *end = NULL; - int start_time, end_time; - - start_time = parse_transition_time(str, &next); - if (start_time < 0) - return -1; - - if (!*next) { - end_time = start_time; - } else if (*next == '-') { - end_time = parse_transition_time(&next[1], &end); - if (end_time < 0 || *end) - return -1; - } else { - return -1; - } - - range->start = start_time; - range->end = end_time; - - return 0; -} - -/* Print help text. */ -static void -print_help(void) -{ - /* TRANSLATORS: help output 1 - LAT is latitude, LON is longitude, - DAY is temperature at daytime, - NIGHT is temperature at night - no-wrap */ - printf(_("Usage: %s -l LAT:LON -t DAY:NIGHT [OPTIONS...]\n"), argv0); - printf("\n"); - - /* TRANSLATORS: help output 2 - no-wrap */ - printf(_("Set color temperature of display according to time of day.\n")); - printf("\n"); - - /* TRANSLATORS: help output 3 - no-wrap */ - printf(_(" -h\t\tDisplay this help message\n" - " -v\t\tVerbose output\n" - " -V\t\tShow program version\n")); - printf("\n"); - - /* TRANSLATORS: help output 4 - `list' must not be translated - no-wrap */ - printf(_(" -b DAY:NIGHT\tScreen brightness to apply (between 0.1 and 1.0)\n" - " -c FILE\tLoad settings from specified configuration file\n" - " -g R:G:B\tAdditional gamma correction to apply\n" - " -l LAT:LON\tYour current location\n" - " -l PROVIDER\tSelect provider for automatic location updates\n" - " \t\t(Type `list' to see available providers)\n" - " -m METHOD\tMethod to use to set color temperature\n" - " \t\t(Type `list' to see available methods)\n" - " -o\t\tOne shot mode (do not continuously adjust color temperature)\n" - " -O TEMP\tOne shot manual mode (set color temperature)\n" - " -p\t\tPrint mode (only print parameters and exit)\n" - " -P\t\tReset existing gamma ramps before applying new color effect\n" - " -x\t\tReset mode (remove adjustment from screen)\n" - " -r\t\tDisable fading between color temperatures\n" - " -t DAY:NIGHT\tColor temperature to set at daytime/night\n")); - printf("\n"); - - /* TRANSLATORS: help output 5 */ - printf(_("The neutral temperature is %luK. Using this value will not change the color\n" - "temperature of the display. Setting the color temperature to a value higher\n" - "than this results in more blue light, and setting a lower value will result in\n" - "more red light.\n"), - NEUTRAL_TEMPERATURE); - - printf("\n"); - - /* TRANSLATORS: help output 6 */ - printf(_("Default values:\n\n" - " Daytime temperature: %luK\n" - " Night temperature: %luK\n"), - DEFAULT_DAY_TEMPERATURE, DEFAULT_NIGHT_TEMPERATURE); - - printf("\n"); -} - -/* Print list of adjustment methods. */ -static void -print_method_list(void) -{ - size_t i; - fputs(_("Available adjustment methods:\n"), stdout); - for (i = 0; gamma_methods[i]; i++) - printf(" %s\n", gamma_methods[i]->name); - - fputs("\n", stdout); - fputs(_("Specify colon-separated options with `-m METHOD:OPTIONS'.\n"), stdout); - /* TRANSLATORS: `help' must not be translated. */ - fputs(_("Try `-m METHOD:help' for help.\n"), stdout); -} - -/* Print list of location providers. */ -static void -print_provider_list(void) -{ - size_t i; - fputs(_("Available location providers:\n"), stdout); - for (i = 0; location_providers[i]; i++) - printf(" %s\n", location_providers[i]->name); - - fputs("\n", stdout); - fputs(_("Specify colon-separated options with`-l PROVIDER:OPTIONS'.\n"), stdout); - /* TRANSLATORS: `help' must not be translated. */ - fputs(_("Try `-l PROVIDER:help' for help.\n"), stdout); -} - -/* Return the gamma method with the given name. */ -GCC_ONLY(__attribute__((__pure__, __returns_nonnull__))) -static const struct gamma_method * -find_gamma_method(const char *name) -{ - size_t i; - for (i = 0; gamma_methods[i]; i++) - if (!strcasecmp(name, gamma_methods[i]->name)) - return gamma_methods[i]; - /* TRANSLATORS: This refers to the method used to adjust colours e.g. VidMode */ - eprintf(_("Unknown adjustment method `%s'."), name); -} - -/* Return location provider with the given name. */ -GCC_ONLY(__attribute__((__pure__, __returns_nonnull__))) -static const struct location_provider * -find_location_provider(const char *name) -{ - size_t i; - for (i = 0; location_providers[i]; i++) - if (!strcasecmp(name, location_providers[i]->name)) - return location_providers[i]; - eprintf(_("Unknown location provider `%s'."), name); -} - -/* Initialize options struct. */ -void -options_init(struct options *options) -{ - options->config_filepath = NULL; - - /* Default elevation values. */ - options->scheme.high = TRANSITION_HIGH; - options->scheme.low = TRANSITION_LOW; - - /* Settings for day, night and transition period. - Initialized to indicate that the values are not set yet. */ - options->scheme.use_time = 0; - options->scheme.dawn.start = -1; - options->scheme.dawn.end = -1; - options->scheme.dusk.start = -1; - options->scheme.dusk.end = -1; - - options->scheme.day.temperature = 0; - options->scheme.day.gamma[0] = NAN; - options->scheme.day.brightness = NAN; - - options->scheme.night.temperature = 0; - options->scheme.night.gamma[0] = NAN; - options->scheme.night.brightness = NAN; - - /* Temperature for manual mode */ - options->temp_set = 0; - - options->method = NULL; - options->method_args = NULL; - - options->provider = NULL; - options->provider_args = NULL; - - options->use_fade = -1; - options->preserve_gamma = 1; - options->mode = PROGRAM_MODE_CONTINUAL; - options->verbose = 0; -} - -/* Parse command line arguments. */ -void -options_parse_args(struct options *options, int argc, char *argv[]) -{ - const char *provider_name; - char *s, *end, *value; - int r; - - ARGBEGIN { - case 'b': - parse_brightness_string(ARG(), &options->scheme.day.brightness, &options->scheme.night.brightness); - break; - - case 'c': - free(options->config_filepath); - options->config_filepath = estrdup(ARG()); - break; - - case 'g': - r = parse_gamma_string(ARG(), options->scheme.day.gamma); - if (r < 0) { - weprintf(_("Malformed gamma argument.")); - eprintf(_("Try `-h' for more information.")); - } - /* Set night gamma to the same value as day gamma. - To set these to distinct values use the config file. */ - memcpy(options->scheme.night.gamma, options->scheme.day.gamma, sizeof(options->scheme.night.gamma)); - break; - - case 'h': - print_help(); - exit(0); - - case 'l': - value = ARG(); - - /* Print list of providers if argument is `list' */ - if (!strcasecmp(value, "list")) { - print_provider_list(); - exit(0); - } - - provider_name = NULL; - - /* Don't save the result of strtof(); we simply want - to know if value can be parsed as a float. */ - errno = 0; - strtof(value, &end); - if (!errno && *end == ':') { - /* Use instead as arguments to `manual'. */ - provider_name = "manual"; - options->provider_args = value; - } else { - /* Split off provider arguments. */ - s = strchr(value, ':'); - if (s) { - *s++ = '\0'; - options->provider_args = s; - } - - provider_name = value; - } - - /* Lookup provider from name. */ - options->provider = find_location_provider(provider_name); - - /* Print provider help if arg is `help'. */ - if (options->provider_args && !strcasecmp(options->provider_args, "help")) { - options->provider->print_help(stdout); - exit(0); - } - break; - - case 'm': - value = ARG(); - - /* Print list of methods if argument is `list' */ - if (!strcasecmp(value, "list")) { - print_method_list(); - exit(0); - } - - /* Split off method arguments. */ - s = strchr(value, ':'); - if (s) { - *s++ = '\0'; - options->method_args = s; - } - - /* Find adjustment method by name. */ - options->method = find_gamma_method(value); - - /* Print method help if arg is `help'. */ - if (options->method_args && !strcasecmp(options->method_args, "help")) { - options->method->print_help(stdout); - exit(0); - } - break; - - case 'o': - options->mode = PROGRAM_MODE_ONE_SHOT; - break; - - case 'O': - options->mode = PROGRAM_MODE_MANUAL; - options->temp_set = atoi(ARG()); - break; - - case 'p': - options->mode = PROGRAM_MODE_PRINT; - break; - - case 'P': - options->preserve_gamma = 0; - break; - - case 'r': - options->use_fade = 0; - break; - - case 't': - value = ARG(); - s = strchr(value, ':'); - if (!s) { - weprintf(_("Malformed temperature argument.")); - eprintf(_("Try `-h' for more information.")); - } - *s++ = '\0'; - options->scheme.day.temperature = atoi(value); - options->scheme.night.temperature = atoi(s); - break; - - case 'v': - options->verbose = 1; - break; - - case 'V': - printf("%s\n", PACKAGE_STRING); - exit(0); - break; - - case 'x': - options->mode = PROGRAM_MODE_RESET; - break; - - default: - usage(); - } ARGEND; - - if (argc) - usage(); -} - -/* Parse a single key-value pair from the configuration file. */ -static void -parse_config_file_option(const char *key, const char *value, struct options *options) -{ - if (!strcasecmp(key, "temp-day")) { - if (!options->scheme.day.temperature) - options->scheme.day.temperature = atoi(value); - } else if (!strcasecmp(key, "temp-night")) { - if (!options->scheme.night.temperature) - options->scheme.night.temperature = atoi(value); - } else if (!strcasecmp(key, "transition") || !strcasecmp(key, "fade")) { - /* "fade" is preferred, "transition" is deprecated as the setting key. */ - if (options->use_fade < 0) - options->use_fade = !!atoi(value); - } else if (!strcasecmp(key, "brightness")) { - if (isnan(options->scheme.day.brightness)) - options->scheme.day.brightness = atof(value); - if (isnan(options->scheme.night.brightness)) - options->scheme.night.brightness = atof(value); - } else if (!strcasecmp(key, "brightness-day")) { - if (isnan(options->scheme.day.brightness)) - options->scheme.day.brightness = atof(value); - } else if (!strcasecmp(key, "brightness-night")) { - if (isnan(options->scheme.night.brightness)) - options->scheme.night.brightness = atof(value); - } else if (!strcasecmp(key, "elevation-high")) { - options->scheme.high = atof(value); - } else if (!strcasecmp(key, "elevation-low")) { - options->scheme.low = atof(value); - } else if (!strcasecmp(key, "gamma")) { - if (isnan(options->scheme.day.gamma[0])) { - if (parse_gamma_string(value, options->scheme.day.gamma) < 0) - eprintf(_("Malformed gamma setting.")); - memcpy(options->scheme.night.gamma, options->scheme.day.gamma, sizeof(options->scheme.night.gamma)); - } - } else if (!strcasecmp(key, "gamma-day")) { - if (isnan(options->scheme.day.gamma[0])) { - if (parse_gamma_string(value, options->scheme.day.gamma) < 0) - eprintf(_("Malformed gamma setting.")); - } - } else if (!strcasecmp(key, "gamma-night")) { - if (isnan(options->scheme.night.gamma[0])) { - if (parse_gamma_string(value, options->scheme.night.gamma) < 0) - eprintf(_("Malformed gamma setting.")); - } - } else if (!strcasecmp(key, "preserve-gamma")) { - if (options->preserve_gamma == 1) - options->preserve_gamma = !!atoi(value); - } else if (!strcasecmp(key, "adjustment-method")) { - if (!options->method) - options->method = find_gamma_method(value); - } else if (!strcasecmp(key, "location-provider")) { - if (!options->provider) - options->provider = find_location_provider(value); - } else if (!strcasecmp(key, "dawn-time")) { - if (options->scheme.dawn.start < 0) { - if (parse_transition_range(value, &options->scheme.dawn) < 0) - eprintf(_("Malformed dawn-time setting `%s'."), value); - } - } else if (!strcasecmp(key, "dusk-time")) { - if (options->scheme.dusk.start < 0) { - if (parse_transition_range(value, &options->scheme.dusk) < 0) - eprintf(_("Malformed dusk-time setting `%s'."), value); - } - } else { - weprintf(_("Unknown configuration setting `%s'."), key); - } -} - -/* Parse options defined in the config file. */ -void -options_parse_config_file(struct options *options, struct config_ini_state *config_state) -{ - struct config_ini_section *section; - struct config_ini_setting *setting; - - /* Read global config settings. */ - section = config_ini_get_section(config_state, "redshift"); - if (!section) - return; - - for (setting = section->settings; setting; setting = setting->next) - parse_config_file_option(setting->name, setting->value, options); -} - -/* Replace unspecified options with default values. */ -void -options_set_defaults(struct options *options) -{ - if (!options->scheme.day.temperature) - options->scheme.day.temperature = DEFAULT_DAY_TEMPERATURE; - if (!options->scheme.night.temperature) - options->scheme.night.temperature = DEFAULT_NIGHT_TEMPERATURE; - - if (isnan(options->scheme.day.brightness)) - options->scheme.day.brightness = DEFAULT_BRIGHTNESS; - if (isnan(options->scheme.night.brightness)) - options->scheme.night.brightness = DEFAULT_BRIGHTNESS; - - if (isnan(options->scheme.day.gamma[0])) { - options->scheme.day.gamma[0] = DEFAULT_GAMMA; - options->scheme.day.gamma[1] = DEFAULT_GAMMA; - options->scheme.day.gamma[2] = DEFAULT_GAMMA; - } - if (isnan(options->scheme.night.gamma[0])) { - options->scheme.night.gamma[0] = DEFAULT_GAMMA; - options->scheme.night.gamma[1] = DEFAULT_GAMMA; - options->scheme.night.gamma[2] = DEFAULT_GAMMA; - } - - if (options->use_fade < 0) - options->use_fade = 1; -} |