diff options
author | Mattias Andrée <m@maandree.se> | 2025-03-05 19:26:58 +0100 |
---|---|---|
committer | Mattias Andrée <m@maandree.se> | 2025-03-05 19:26:58 +0100 |
commit | b42c9662303d1a33ebe038d1895c08945cdbd40f (patch) | |
tree | c05b169b1d28e8d42b111290f9181d2cbad1138b /src/redshift.c | |
parent | Merge redshift.h into common.h (diff) | |
download | redshift-ng-b42c9662303d1a33ebe038d1895c08945cdbd40f.tar.gz redshift-ng-b42c9662303d1a33ebe038d1895c08945cdbd40f.tar.bz2 redshift-ng-b42c9662303d1a33ebe038d1895c08945cdbd40f.tar.xz |
Cleanup and style update (avoid typedef)
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'src/redshift.c')
-rw-r--r-- | src/redshift.c | 148 |
1 files changed, 60 insertions, 88 deletions
diff --git a/src/redshift.c b/src/redshift.c index 60e2441..92066e1 100644 --- a/src/redshift.c +++ b/src/redshift.c @@ -16,19 +16,10 @@ Copyright (c) 2009-2017 Jon Lund Steffensen <jonlst@gmail.com> */ - -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <string.h> -#include <math.h> -#include <locale.h> -#include <errno.h> -#include <time.h> +#include "common.h" +#include "config-ini.h" +#include "solar.h" +#include "options.h" /* poll.h is not available on Windows but there is no Windows location provider using polling. On Windows, we just define some stubs to make things compile. @@ -45,25 +36,6 @@ struct pollfd { int poll(struct pollfd *fds, int nfds, int timeout) { abort(); return -1; } #endif -#if defined(HAVE_SIGNAL_H) && !defined(__WIN32__) -# include <signal.h> -#endif - -#ifdef ENABLE_NLS -# include <libintl.h> -# define _(s) gettext(s) -# define N_(s) (s) -#else -# define _(s) s -# define N_(s) s -# define gettext(s) s -#endif - -#include "common.h" -#include "config-ini.h" -#include "solar.h" -#include "options.h" - /* pause() is not defined on windows platform but is not needed either. Use a noop macro instead. */ #ifdef __WIN32__ @@ -104,8 +76,8 @@ static const char *period_names[] = { /* Determine which period we are currently in based on time offset. */ -static period_t -get_period_from_time(const transition_scheme_t *transition, int time_offset) +static enum period +get_period_from_time(const struct transition_scheme *transition, int time_offset) { if (time_offset < transition->dawn.start || time_offset >= transition->dusk.end) { @@ -119,9 +91,9 @@ get_period_from_time(const transition_scheme_t *transition, int time_offset) } /* Determine which period we are currently in based on solar elevation. */ -static period_t +static enum period get_period_from_elevation( - const transition_scheme_t *transition, double elevation) + const struct transition_scheme *transition, double elevation) { if (elevation < transition->low) { return PERIOD_NIGHT; @@ -135,7 +107,7 @@ get_period_from_elevation( /* Determine how far through the transition we are based on time offset. */ static double get_transition_progress_from_time( - const transition_scheme_t *transition, int time_offset) + const struct transition_scheme *transition, int time_offset) { if (time_offset < transition->dawn.start || time_offset >= transition->dusk.end) { @@ -156,7 +128,7 @@ get_transition_progress_from_time( /* Determine how far through the transition we are based on elevation. */ static double get_transition_progress_from_elevation( - const transition_scheme_t *transition, double elevation) + const struct transition_scheme *transition, double elevation) { if (elevation < transition->low) { return 0.0; @@ -184,7 +156,7 @@ get_seconds_since_midnight(double timestamp) /* Print verbose description of the given period. */ static void -print_period(period_t period, double transition) +print_period(enum period period, double transition) { switch (period) { case PERIOD_NONE: @@ -202,7 +174,7 @@ print_period(period_t period, double transition) /* Print location */ static void -print_location(const location_t *location) +print_location(const struct location *location) { /* TRANSLATORS: Abbreviation for `north' */ const char *north = _("N"); @@ -224,10 +196,10 @@ print_location(const location_t *location) /* Interpolate color setting structs given alpha. */ static void interpolate_color_settings( - const color_setting_t *first, - const color_setting_t *second, + const struct color_setting *first, + const struct color_setting *second, double alpha, - color_setting_t *result) + struct color_setting *result) { alpha = CLAMP(0.0, alpha, 1.0); @@ -244,12 +216,12 @@ interpolate_color_settings( /* Interpolate color setting structs transition scheme. */ static void interpolate_transition_scheme( - const transition_scheme_t *transition, + const struct transition_scheme *transition, double alpha, - color_setting_t *result) + struct color_setting *result) { - const color_setting_t *day = &transition->day; - const color_setting_t *night = &transition->night; + const struct color_setting *day = &transition->day; + const struct color_setting *night = &transition->night; alpha = CLAMP(0.0, alpha, 1.0); interpolate_color_settings(night, day, alpha, result); @@ -259,8 +231,8 @@ interpolate_transition_scheme( Used to determine if a fade should be applied in continual mode. */ static int color_setting_diff_is_major( - const color_setting_t *first, - const color_setting_t *second) + const struct color_setting *first, + const struct color_setting *second) { return (abs(first->temperature - second->temperature) > 25 || fabsf(first->brightness - second->brightness) > 0.1 || @@ -271,7 +243,7 @@ color_setting_diff_is_major( /* Reset color setting to default values. */ static void -color_setting_reset(color_setting_t *color) +color_setting_reset(struct color_setting *color) { color->temperature = NEUTRAL_TEMP; color->gamma[0] = 1.0; @@ -282,8 +254,8 @@ color_setting_reset(color_setting_t *color) static int -provider_try_start(const location_provider_t *provider, - location_state_t **state, config_ini_state_t *config, +provider_try_start(const struct location_provider *provider, + LOCATION_STATE **state, struct config_ini_state *config, char *args) { int r; @@ -296,10 +268,10 @@ provider_try_start(const location_provider_t *provider, } /* Set provider options from config file. */ - config_ini_section_t *section = + struct config_ini_section *section = config_ini_get_section(config, provider->name); if (section != NULL) { - config_ini_setting_t *setting = section->settings; + struct config_ini_setting *setting = section->settings; while (setting != NULL) { r = provider->set_option(*state, setting->name, setting->value); @@ -374,8 +346,8 @@ provider_try_start(const location_provider_t *provider, } static int -method_try_start(const gamma_method_t *method, gamma_state_t **state, - program_mode_t mode, config_ini_state_t *config, char *args) +method_try_start(const struct gamma_method *method, GAMMA_STATE **state, + enum program_mode mode, struct config_ini_state *config, char *args) { int r; @@ -387,10 +359,10 @@ method_try_start(const gamma_method_t *method, gamma_state_t **state, } /* Set method options from config file. */ - config_ini_section_t *section = + struct config_ini_section *section = config_ini_get_section(config, method->name); if (section != NULL) { - config_ini_setting_t *setting = section->settings; + struct config_ini_setting *setting = section->settings; while (setting != NULL) { r = method->set_option( *state, setting->name, setting->value); @@ -468,7 +440,7 @@ gamma_is_valid(const float gamma[3]) Prints error message on stderr and returns 0 if invalid, otherwise returns 1. */ static int -location_is_valid(const location_t *location) +location_is_valid(const struct location *location) { /* Latitude */ if (location->lat < MIN_LAT || location->lat > MAX_LAT) { @@ -497,8 +469,8 @@ location_is_valid(const location_t *location) 0 if timeout was reached, 1 if location became available. */ static int provider_get_location( - const location_provider_t *provider, location_state_t *state, - int timeout, location_t *loc) + const struct location_provider *provider, LOCATION_STATE *state, + int timeout, struct location *loc) { int available = 0; struct pollfd pollfds[1]; @@ -566,11 +538,11 @@ ease_fade(double t) current time and continuously updates the screen to the appropriate color temperature. */ static int -run_continual_mode(const location_provider_t *provider, - location_state_t *location_state, - const transition_scheme_t *scheme, - const gamma_method_t *method, - gamma_state_t *method_state, +run_continual_mode(const struct location_provider *provider, + LOCATION_STATE *location_state, + const struct transition_scheme *scheme, + const struct gamma_method *method, + GAMMA_STATE *method_state, int use_fade, int preserve_gamma, int verbose) { int r; @@ -578,7 +550,7 @@ run_continual_mode(const location_provider_t *provider, /* Short fade parameters */ int fade_length = 0; int fade_time = 0; - color_setting_t fade_start_interp; + struct color_setting fade_start_interp; r = signals_install_handlers(); if (r < 0) { @@ -587,17 +559,17 @@ run_continual_mode(const location_provider_t *provider, /* Save previous parameters so we can avoid printing status updates if the values did not change. */ - period_t prev_period = PERIOD_NONE; + enum period prev_period = PERIOD_NONE; /* Previous target color setting and current actual color setting. Actual color setting takes into account the current color fade. */ - color_setting_t prev_target_interp; + struct color_setting prev_target_interp; color_setting_reset(&prev_target_interp); - color_setting_t interp; + struct color_setting interp; color_setting_reset(&interp); - location_t loc = { NAN, NAN }; + struct location loc = { NAN, NAN }; int need_location = !scheme->use_time; if (need_location) { fputs(_("Waiting for initial location" @@ -665,7 +637,7 @@ run_continual_mode(const location_provider_t *provider, return -1; } - period_t period; + enum period period; double transition_prog; if (scheme->use_time) { int time_offset = get_seconds_since_midnight(now); @@ -686,7 +658,7 @@ run_continual_mode(const location_provider_t *provider, /* Use transition progress to get target color temperature. */ - color_setting_t target_interp; + struct color_setting target_interp; interpolate_transition_scheme( scheme, transition_prog, &target_interp); @@ -807,7 +779,7 @@ run_continual_mode(const location_provider_t *provider, /* Get new location and availability information. */ - location_t new_loc; + struct location new_loc; int new_available; r = provider->handle( location_state, &new_loc, @@ -869,7 +841,7 @@ main(int argc, char *argv[]) #endif /* List of gamma methods. */ - const gamma_method_t gamma_methods[] = { + const struct gamma_method gamma_methods[] = { #ifdef ENABLE_COOPGAMMA coopgamma_gamma_method, #endif @@ -893,7 +865,7 @@ main(int argc, char *argv[]) }; /* List of location providers. */ - const location_provider_t location_providers[] = { + const struct location_provider location_providers[] = { #ifdef ENABLE_GEOCLUE2 geoclue2_location_provider, #endif @@ -910,13 +882,13 @@ main(int argc, char *argv[]) setvbuf(stdout, NULL, _IOLBF, 0); setvbuf(stderr, NULL, _IOLBF, 0); - options_t options; + struct options options; options_init(&options); options_parse_args( &options, argc, argv, gamma_methods, location_providers); /* Load settings from config file. */ - config_ini_state_t config_state; + struct config_ini_state config_state; r = config_ini_init(&config_state, options.config_filepath); if (r < 0) { fputs("Unable to load config file.\n", stderr); @@ -954,7 +926,7 @@ main(int argc, char *argv[]) /* Initialize location provider if needed. If provider is NULL try all providers until one that works is found. */ - location_state_t *location_state; + LOCATION_STATE *location_state; /* Location is not needed for reset mode and manual mode. */ int need_location = @@ -972,7 +944,7 @@ main(int argc, char *argv[]) /* Try all providers, use the first that works. */ for (int i = 0; location_providers[i].name != NULL; i++) { - const location_provider_t *p = + const struct location_provider *p = &location_providers[i]; fprintf(stderr, _("Trying location provider `%s'...\n"), @@ -1085,11 +1057,11 @@ main(int argc, char *argv[]) options.scheme.night.gamma[2]); } - transition_scheme_t *scheme = &options.scheme; + struct transition_scheme *scheme = &options.scheme; /* Initialize gamma adjustment method. If method is NULL try all methods until one that works is found. */ - gamma_state_t *method_state; + GAMMA_STATE *method_state; /* Gamma adjustment not needed for print mode */ if (options.mode != PROGRAM_MODE_PRINT) { @@ -1102,7 +1074,7 @@ main(int argc, char *argv[]) } else { /* Try all methods, use the first that works. */ for (int i = 0; gamma_methods[i].name != NULL; i++) { - const gamma_method_t *m = &gamma_methods[i]; + const struct gamma_method *m = &gamma_methods[i]; if (!m->autostart) continue; r = method_try_start( @@ -1132,7 +1104,7 @@ main(int argc, char *argv[]) case PROGRAM_MODE_ONE_SHOT: case PROGRAM_MODE_PRINT: { - location_t loc = { NAN, NAN }; + struct location loc = { NAN, NAN }; if (need_location) { fputs(_("Waiting for current location" " to become available...\n"), stderr); @@ -1161,7 +1133,7 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } - period_t period; + enum period period; double transition_prog; if (options.scheme.use_time) { int time_offset = get_seconds_since_midnight(now); @@ -1185,7 +1157,7 @@ main(int argc, char *argv[]) } /* Use transition progress to set color temperature */ - color_setting_t interp; + struct color_setting interp; interpolate_transition_scheme( scheme, transition_prog, &interp); @@ -1227,7 +1199,7 @@ main(int argc, char *argv[]) } /* Adjust temperature */ - color_setting_t manual = scheme->day; + struct color_setting manual = scheme->day; manual.temperature = options.temp_set; r = options.method->set_temperature( method_state, &manual, options.preserve_gamma); @@ -1249,7 +1221,7 @@ main(int argc, char *argv[]) case PROGRAM_MODE_RESET: { /* Reset screen */ - color_setting_t reset; + struct color_setting reset; color_setting_reset(&reset); r = options.method->set_temperature(method_state, &reset, 0); |