From 4c3cd7fde636946bb806d9b2d025c59418fa4e85 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 6 Mar 2025 16:57:49 +0100 Subject: style and some minor fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/colorramp.c | 1 - src/common.h | 9 +- src/config-ini.c | 141 ++++++++------------ src/gamma-coopgamma.c | 171 ++++++++++--------------- src/gamma-drm.c | 34 ++--- src/gamma-dummy.c | 2 +- src/gamma-quartz.c | 9 +- src/gamma-randr.c | 29 ++--- src/gamma-vidmode.c | 23 ++-- src/gamma-w32gdi.c | 21 ++- src/hooks.c | 2 +- src/location-corelocation.m | 277 +++++++++++++++++++--------------------- src/location-geoclue2.c | 18 +-- src/location-manual.c | 16 +-- src/options.c | 304 ++++++++++++++++++-------------------------- src/pipeutils.c | 4 +- src/redshift.c | 8 +- src/signals.c | 84 ++++++------ 18 files changed, 489 insertions(+), 664 deletions(-) (limited to 'src') diff --git a/src/colorramp.c b/src/colorramp.c index 295bf41..2387e01 100644 --- a/src/colorramp.c +++ b/src/colorramp.c @@ -20,7 +20,6 @@ */ #include "common.h" - #if defined(__GNUC__) # pragma GCC diagnostic ignored "-Wfloat-equal" #endif diff --git a/src/common.h b/src/common.h index 170f97a..70e77b1 100644 --- a/src/common.h +++ b/src/common.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -51,7 +52,6 @@ # define localtime_r(T, TM) localtime_s((TM), (T)) #else # include -# include # include #endif @@ -265,15 +265,10 @@ void pipeutils_signal(int write_fd); void pipeutils_handle_signal(int read_fd); -#ifdef WINDOWS -extern int exiting; -extern int disable; -#else extern volatile sig_atomic_t exiting; extern volatile sig_atomic_t disable; -#endif -int signals_install_handlers(void); +void signals_install_handlers(void); int systemtime_get_time(double *now); diff --git a/src/config-ini.c b/src/config-ini.c index 4cac7e2..1dcda44 100644 --- a/src/config-ini.c +++ b/src/config-ini.c @@ -15,6 +15,7 @@ along with Redshift. If not, see . Copyright (c) 2010-2018 Jon Lund Steffensen + Copyright (c) 2025 Mattias Andrée */ #include "common.h" @@ -42,13 +43,11 @@ open_config_file(const char *filepath) if (f == NULL && (env = getenv("XDG_CONFIG_HOME")) != NULL && env[0] != '\0') { - snprintf(cp, sizeof(cp), - "%s/redshift/redshift.conf", env); + snprintf(cp, sizeof(cp), "%s/redshift/redshift.conf", env); f = fopen(cp, "r"); if (f == NULL) { /* Fall back to formerly used path. */ - snprintf(cp, sizeof(cp), - "%s/redshift.conf", env); + snprintf(cp, sizeof(cp), "%s/redshift.conf", env); f = fopen(cp, "r"); } } @@ -56,20 +55,17 @@ open_config_file(const char *filepath) #ifdef WINDOWS if (f == NULL && (env = getenv("localappdata")) != NULL && env[0] != '\0') { - snprintf(cp, sizeof(cp), - "%s\\redshift.conf", env); + snprintf(cp, sizeof(cp), "%s\\redshift.conf", env); f = fopen(cp, "r"); } #endif if (f == NULL && (env = getenv("HOME")) != NULL && env[0] != '\0') { - snprintf(cp, sizeof(cp), - "%s/.config/redshift/redshift.conf", env); + snprintf(cp, sizeof(cp), "%s/.config/redshift/redshift.conf", env); f = fopen(cp, "r"); if (f == NULL) { /* Fall back to formerly used path. */ - snprintf(cp, sizeof(cp), - "%s/.config/redshift.conf", env); + snprintf(cp, sizeof(cp), "%s/.config/redshift.conf", env); f = fopen(cp, "r"); } } @@ -80,31 +76,28 @@ open_config_file(const char *filepath) if (pwd != NULL) { char *home = pwd->pw_dir; if ((home != NULL) && (*home != '\0')) { - snprintf(cp, sizeof(cp), - "%s/.config/redshift/redshift.conf", home); + snprintf(cp, sizeof(cp), "%s/.config/redshift/redshift.conf", home); f = fopen(cp, "r"); - if (f == NULL) { + if (!f) { /* Fall back to formerly used path. */ - snprintf(cp, sizeof(cp), - "%s/.config/redshift.conf", home); + snprintf(cp, sizeof(cp), "%s/.config/redshift.conf", home); f = fopen(cp, "r"); } } else { - fprintf(stderr, _("Cannot determine your home directory, " - "it is from the system's user table.\n")); + weprintf(_("Cannot determine your home directory," + " it is from the system's user table.")); } } else if (errno) { - perror("getpwuid"); + weprintf("getpwuid:"); } else { - fprintf(stderr, _("Cannot determine your home directory, " - "your user ID is missing from the system's user table.\n")); + weprintf(_("Cannot determine your home directory, your" + " user ID is missing from the system's user table.")); /* errno can either be set to any number of error codes, or be zero if the user does not have a passwd entry. */ } } - if (f == NULL && (env = getenv("XDG_CONFIG_DIRS")) != NULL && - env[0] != '\0') { + if (f == NULL && (env = getenv("XDG_CONFIG_DIRS")) != NULL && *env) { char *begin = env; char *end; int len; @@ -114,33 +107,32 @@ open_config_file(const char *filepath) len = (int)(end - begin); if (len > 0) { - snprintf(cp, sizeof(cp), - "%.*s/redshift/redshift.conf", len, begin); + snprintf(cp, sizeof(cp), "%.*s/redshift/redshift.conf", len, begin); f = fopen(cp, "r"); - if (f != NULL) { + if (f == NULL) { /* Fall back to formerly used path. */ - snprintf(cp, sizeof(cp), - "%.*s/redshift.conf", len, begin); + snprintf(cp, sizeof(cp), "%.*s/redshift.conf", len, begin); f = fopen(cp, "r"); } - if (f != NULL) break; + if (f != NULL) + break; } - if (end[0] == '\0') break; - begin = end + 1; + if (*end) + break; + begin = &end[1]; } } if (f == NULL) { - snprintf(cp, sizeof(cp), - "%s/redshift.conf", "/etc"); + snprintf(cp, sizeof(cp), "%s/redshift.conf", "/etc"); f = fopen(cp, "r"); } #endif } else { f = fopen(filepath, "r"); if (f == NULL) { - perror("fopen"); + weprintf("fopen:"); return NULL; } } @@ -161,29 +153,29 @@ config_ini_init(struct config_ini_state *state, const char *filepath) f = open_config_file(filepath); if (f == NULL) { /* Only a serious error if a file was explicitly requested. */ - if (filepath != NULL) return -1; - return 0; + return filepath ? 0 : -1; } for (;;) { /* Handle the file input linewise. */ char *r = fgets(line, sizeof(line), f); - if (r == NULL) break; + if (!r) + break; /* Strip leading blanks and trailing newline. */ s = line + strspn(line, " \t"); s[strcspn(s, "\r\n")] = '\0'; /* Skip comments and empty lines. */ - if (s[0] == ';' || s[0] == '#' || s[0] == '\0') continue; + if (s[0] == ';' || s[0] == '#' || s[0] == '\0') + continue; if (s[0] == '[') { /* Read name of section. */ - const char *name = s+1; + const char *name = &s[1]; char *end = strchr(s, ']'); if (end == NULL || end[1] != '\0' || end == name) { - fputs(_("Malformed section header in config" - " file.\n"), stderr); + weprintf(_("Malformed section header in config file.")); fclose(f); config_ini_free(state); return -1; @@ -192,12 +184,7 @@ config_ini_init(struct config_ini_state *state, const char *filepath) *end = '\0'; /* Create section. */ - section = malloc(sizeof(struct config_ini_section)); - if (section == NULL) { - fclose(f); - config_ini_free(state); - return -1; - } + section = emalloc(sizeof(*section)); /* Insert into section list. */ section->name = NULL; @@ -221,9 +208,8 @@ config_ini_init(struct config_ini_state *state, const char *filepath) /* Split assignment at equals character. */ end = strchr(s, '='); - if (end == NULL || end == s) { - fputs(_("Malformed assignment in config" - " file.\n"), stderr); + if (!end || end == s) { + weprintf(_("Malformed assignment in config file.")); fclose(f); config_ini_free(state); return -1; @@ -232,21 +218,15 @@ config_ini_init(struct config_ini_state *state, const char *filepath) *end++ = '\0'; value = end; - if (section == NULL) { - fputs(_("Assignment outside section in config" - " file.\n"), stderr); + if (!section) { + weprintf(_("Assignment outside section in config file.")); fclose(f); config_ini_free(state); return -1; } /* Create section. */ - setting = malloc(sizeof(struct config_ini_setting)); - if (setting == NULL) { - fclose(f); - config_ini_free(state); - return -1; - } + setting = emalloc(sizeof(*setting)); /* Insert into section list. */ setting->name = NULL; @@ -255,23 +235,13 @@ config_ini_init(struct config_ini_state *state, const char *filepath) section->settings = setting; /* Copy name of setting. */ - setting->name = malloc(end - s); - if (setting->name == NULL) { - fclose(f); - config_ini_free(state); - return -1; - } + setting->name = emalloc(end - s); memcpy(setting->name, s, end - s); /* Copy setting value. */ value_len = strlen(value) + 1; - setting->value = malloc(value_len); - if (setting->value == NULL) { - fclose(f); - config_ini_free(state); - return -1; - } + setting->value = emalloc(value_len); memcpy(setting->value, value, value_len); } @@ -285,36 +255,27 @@ config_ini_init(struct config_ini_state *state, const char *filepath) void config_ini_free(struct config_ini_state *state) { - struct config_ini_section *section = state->sections; - - while (section != NULL) { - struct config_ini_setting *setting = section->settings; - struct config_ini_section *section_prev = section; - - while (setting != NULL) { - struct config_ini_setting *setting_prev = setting; + struct config_ini_section *section, *section_next; + struct config_ini_setting *setting, *setting_next; + for (section = state->sections; section; section = section_next) { + section_next = section->next; + for (setting = section->settings; setting; setting = setting_next) { + setting_next = setting->next; free(setting->name); free(setting->value); - setting = setting->next; - free(setting_prev); + free(setting); } - free(section->name); - section = section->next; - free(section_prev); + free(section); } } struct config_ini_section * config_ini_get_section(struct config_ini_state *state, const char *name) { - struct config_ini_section *section = state->sections; - while (section != NULL) { - if (strcasecmp(section->name, name) == 0) { + struct config_ini_section *section; + for (section = state->sections; section; section = section->next) + if (!strcasecmp(section->name, name)) return section; - } - section = section->next; - } - return NULL; } diff --git a/src/gamma-coopgamma.c b/src/gamma-coopgamma.c index fe01cb5..6c3da38 100644 --- a/src/gamma-coopgamma.c +++ b/src/gamma-coopgamma.c @@ -72,7 +72,8 @@ restore_signal_blockage(int signo, const struct signal_blockage *blockage) static int update(struct gamma_state *state) { - for (size_t i = 0; i < state->n_crtcs; i++) + size_t i; + for (i = 0; i < state->n_crtcs; i++) libcoopgamma_set_gamma_sync(&state->crtcs[i].filter, &state->ctx); return 0; } @@ -81,21 +82,34 @@ update(struct gamma_state *state) static void print_error(struct gamma_state *state) { - const char* side = state->ctx.error.server_side ? _("server-side") : _("client-side"); + unsigned long long int ec = (unsigned long long int)state->ctx.error.number; if (state->ctx.error.custom) { - if (state->ctx.error.number != 0 && state->ctx.error.description != NULL) - fprintf(stderr, "%s error number %llu: %s\n", - side, (unsigned long long int)state->ctx.error.number, - state->ctx.error.description); - else if (state->ctx.error.number != 0) - fprintf(stderr, _("%s error number %llu\n"), - side, (unsigned long long int)state->ctx.error.number); - else if (state->ctx.error.description != NULL) - fprintf(stderr, _("%s error: %s\n"), side, state->ctx.error.description); - } else if (state->ctx.error.description != NULL) { - fprintf(stderr, _("%s error: %s\n"), side, state->ctx.error.description); + if (ec && state->ctx.error.description) { + if (state->ctx.error.server_side) + weprintf(_("Server-side error number %llu: %s."), ec, state->ctx.error.description); + else + weprintf(_("Client-side error number %llu: %s."), ec, state->ctx.error.description); + } else if (ec) { + if (state->ctx.error.server_side) + weprintf(_("Server-side error number %llu."), ec); + else + weprintf(_("Client-side error number %llu."), ec); + } else if (state->ctx.error.description) { + if (state->ctx.error.server_side) + weprintf(_("Server-side error: %s."), state->ctx.error.description); + else + weprintf(_("Client-side error: %s."), state->ctx.error.description); + } + } else if (state->ctx.error.description) { + if (state->ctx.error.server_side) + weprintf(_("Server-side error: %s."), state->ctx.error.description); + else + weprintf(_("Client-side error: %s."), state->ctx.error.description); } else { - fprintf(stderr, _("%s error: %s\n"), side, strerror(state->ctx.error.number)); + if (state->ctx.error.server_side) + weprintf(_("Server-side error: %s."), strerror(state->ctx.error.number)); + else + weprintf(_("Client-side error: %s."), strerror(state->ctx.error.number)); } } @@ -106,14 +120,10 @@ coopgamma_init(struct gamma_state **state) struct gamma_state *s; struct signal_blockage signal_blockage; - *state = malloc(sizeof(**state)); - if (*state == NULL) return -1; - - s = *state; + s = *state = ecalloc(1, sizeof(**state)); - memset(s, 0, sizeof(*s)); if (libcoopgamma_context_initialise(&s->ctx)) { - perror("libcoopgamma_context_initialise"); + weprintf("libcoopgamma_context_initialise:"); return -1; } @@ -122,9 +132,9 @@ coopgamma_init(struct gamma_state **state) return -1; s->methods = libcoopgamma_get_methods(); if (s->methods == NULL) { - perror("libcoopgamma_get_methods"); + weprintf("libcoopgamma_get_methods:"); if (restore_signal_blockage(SIGCHLD, &signal_blockage) < 0) - exit(EXIT_FAILURE); + exit(1); return -1; } if (restore_signal_blockage(SIGCHLD, &signal_blockage) < 0) @@ -168,12 +178,11 @@ coopgamma_start(struct gamma_state *state, enum program_mode mode) return -1; if (libcoopgamma_connect(state->method, state->site, &state->ctx) < 0) { if (errno) - perror("libcoopgamma_connect"); + weprintf("libcoopgamma_connect:"); else - fprintf(stderr, _("libcoopgamma_connect: could not " - "start coopgamma server\n")); + weprintf(_("libcoopgamma_connect: could not start coopgamma server.")); if (restore_signal_blockage(SIGCHLD, &signal_blockage) < 0) - exit(EXIT_FAILURE); + exit(1); return -1; } if (restore_signal_blockage(SIGCHLD, &signal_blockage) < 0) @@ -189,62 +198,42 @@ coopgamma_start(struct gamma_state *state, enum program_mode mode) /* List available output if edid=list was used */ if (state->list_outputs) { - if (outputs == NULL) { + if (!outputs) { print_error(state); return -1; } printf(_("Available outputs:\n")); for (i = 0; outputs[i]; i++) printf(" %s\n", outputs[i]); - if (ferror(stdout)) { - perror("printf"); - exit(EXIT_FAILURE); - } - exit(EXIT_SUCCESS); + if (ferror(stdout)) + eprintf("printf:"); + exit(0); } /* Translate crtc=N to edid=EDID */ for (i = 0; i < state->n_outputs; i++) { - if (state->outputs[i].edid != NULL) + if (state->outputs[i].edid) continue; if (state->outputs[i].index >= n_outputs) { - fprintf(stderr, _("monitor number %zu does not exist," - "available monitors are [0, %zu]\n"), - state->outputs[i].index, n_outputs - 1); - return -1; - } - state->outputs[i].edid = strdup(outputs[state->outputs[i].index]); - if (state->outputs[i].edid == NULL) { - perror("strdup"); + weprintf(_("Monitor number %zu does not exist, available monitors are [0, %zu]"), + state->outputs[i].index, n_outputs - 1); return -1; } + state->outputs[i].edid = estrdup(outputs[state->outputs[i].index]); } /* Use all outputs if none were specified */ if (state->n_outputs == 0) { state->n_outputs = state->a_outputs = n_outputs; - state->outputs = malloc(n_outputs * sizeof(*state->outputs)); - if (state->outputs == NULL) { - perror("malloc"); - return -1; - } - for (i = 0; i < n_outputs; i++) { - state->outputs[i].edid = strdup(outputs[i]); - if (state->outputs[i].edid == NULL) { - perror("strdup"); - return -1; - } - } + state->outputs = emalloc(n_outputs * sizeof(*state->outputs)); + for (i = 0; i < n_outputs; i++) + state->outputs[i].edid = estrdup(outputs[i]); } free(outputs); /* Initialise information for each output */ - state->crtcs = calloc(state->n_outputs, sizeof(*state->crtcs)); - if (state->crtcs == NULL) { - perror("calloc"); - return -1; - } + state->crtcs = ecalloc(state->n_outputs, sizeof(*state->crtcs)); for (i = 0; i < state->n_outputs; i++) { libcoopgamma_crtc_info_t info; struct coopgamma_crtc_state *crtc = state->crtcs + state->n_crtcs; @@ -263,19 +252,17 @@ coopgamma_start(struct gamma_state *state, enum program_mode mode) if (libcoopgamma_get_gamma_info_sync(crtc->filter.crtc, &info, &state->ctx) < 0) { int saved_errno = errno; - fprintf(stderr, _("failed to retrieve information for output `%s':\n"), - outputs[i]); + weprintf(_("Failed to retrieve information for output `%s':\n"), outputs[i]); errno = saved_errno; print_error(state); return -1; } if (!info.cooperative) { - fprintf(stderr, _("coopgamma is not available\n")); + weprintf(_("coopgamma is not available.\n")); return -1; } if (info.supported == LIBCOOPGAMMA_NO) { - fprintf(stderr, _("output `%s' does not support gamma " - "adjustments, skipping\n"), outputs[i]); + weprintf(_("Output `%s' does not support gamma adjustments, skipping."), outputs[i]); continue; } @@ -417,16 +404,14 @@ coopgamma_set_option(struct gamma_state *state, const char *key, const char *val errno = 0; priority = strtoll(value, &end, 10); if (errno || *end || priority < INT64_MIN || priority > INT64_MAX) { - fprintf(stderr, _("value of method parameter `crtc' " - "must be a integer in [%lli, %lli]\n"), - (long long int)INT64_MIN, (long long int)INT64_MAX); + weprintf(_("Value of method parameter `crtc' must be a integer in [%lli, %lli]."), + (long long int)INT64_MIN, (long long int)INT64_MAX); return -1; } state->priority = priority; } else if (!strcasecmp(key, "method")) { if (state->method != NULL) { - fprintf(stderr, _("method parameter `method' " - "can only be used once\n")); + weprintf(_("Method parameter `method' can only be used once.")); return -1; } if (!strcasecmp(value, "list")) { @@ -435,62 +420,40 @@ coopgamma_set_option(struct gamma_state *state, const char *key, const char *val printf(_("Available adjustment methods for coopgamma:\n")); for (i = 0; state->methods[i]; i++) printf(" %s\n", state->methods[i]); - if (ferror(stdout)) { - perror("printf"); - exit(EXIT_FAILURE); - } + if (ferror(stdout)) + eprintf("printf:"); exit(EXIT_SUCCESS); } - state->method = strdup(value); - if (state->method == NULL) { - perror("strdup"); - return -1; - } + state->method = estrdup(value); } else if (!strcasecmp(key, "display")) { if (state->site != NULL) { - fprintf(stderr, _("method parameter `display' " - "can only be used once\n")); - return -1; - } - state->site = strdup(value); - if (state->site == NULL) { - perror("strdup"); + weprintf(_("Method parameter `display' can only be used once.")); return -1; } + state->site = estrdup(value); } else if (!strcasecmp(key, "edid") || !strcasecmp(key, "crtc")) { if (state->n_outputs == state->a_outputs) { state->a_outputs += 8; - state->outputs = realloc(state->outputs, - state->a_outputs * sizeof(*state->outputs)); - if (state->outputs == NULL) { - perror("realloc"); - return -1; - } + state->outputs = erealloc(state->outputs, state->a_outputs * sizeof(*state->outputs)); } if (!strcasecmp(key, "edid")) { - state->outputs[state->n_outputs].edid = strdup(value); - if (state->outputs[state->n_outputs].edid == NULL) { - perror("strdup"); - return -1; - } + state->outputs[state->n_outputs].edid = estrdup(value); if (!strcasecmp(state->outputs[state->n_outputs].edid, "list")) state->list_outputs = 1; } else { state->outputs[state->n_outputs].edid = NULL; errno = 0; state->outputs[state->n_outputs].index = (size_t)strtoul(value, &end, 10); - if (!*end && errno == ERANGE && - state->outputs[state->n_outputs].index == SIZE_MAX) { + if (!*end && errno == ERANGE && state->outputs[state->n_outputs].index == SIZE_MAX) { state->outputs[state->n_outputs].index = SIZE_MAX; } else if (errno || *end) { - fprintf(stderr, _("value of method parameter `crtc' " - "must be a non-negative integer\n")); + weprintf(_("Value of method parameter `crtc' must be a non-negative integer.")); return -1; } } state->n_outputs++; } else { - fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key); + fprintf(stderr, _("Unknown method parameter: `%s'."), key); return -1; } @@ -521,18 +484,16 @@ coopgamma_set_temperature(struct gamma_state *state, const struct color_setting filter = &state->crtcs[i].filter; /* Copy ramps for previous CRTC if its ramps is of same size and depth */ - if (last_filter != NULL && + if (last_filter && last_filter->ramps.u8.red_size == filter->ramps.u8.red_size && last_filter->ramps.u8.green_size == filter->ramps.u8.green_size && last_filter->ramps.u8.blue_size == filter->ramps.u8.blue_size) { - memcpy(filter->ramps.u8.red, last_filter->ramps.u8.red, - state->crtcs[i].rampsize); + memcpy(filter->ramps.u8.red, last_filter->ramps.u8.red, state->crtcs[i].rampsize); continue; } /* Otherwise, create calculate the ramps */ - memcpy(filter->ramps.u8.red, state->crtcs[i].plain_ramps.u8.red, - state->crtcs[i].rampsize); + memcpy(filter->ramps.u8.red, state->crtcs[i].plain_ramps.u8.red, state->crtcs[i].rampsize); switch (filter->depth) { #define X(SUFFIX, TYPE, MAX, TRUE_MAX, DEPTH)\ case DEPTH:\ diff --git a/src/gamma-drm.c b/src/gamma-drm.c index a4cc75e..b059eef 100644 --- a/src/gamma-drm.c +++ b/src/gamma-drm.c @@ -81,16 +81,14 @@ drm_start(struct gamma_state *state, enum program_mode mode) if (state->fd < 0) { /* TODO check if access permissions, normally root or membership of the video group is required. */ - perror("open"); - fprintf(stderr, _("Failed to open DRM device: %s\n"), - pathname); + weprintf(_("Failed to open DRM device `%s':"), pathname); return -1; } /* Acquire mode resources. */ state->res = drmModeGetResources(state->fd); if (state->res == NULL) { - fprintf(stderr, _("Failed to get DRM mode resources\n")); + weprintf(_("Failed to get DRM mode resources.")); close(state->fd); state->fd = -1; return -1; @@ -100,14 +98,10 @@ drm_start(struct gamma_state *state, enum program_mode mode) crtc_count = state->res->count_crtcs; if (state->crtc_num >= 0) { if (state->crtc_num >= crtc_count) { - fprintf(stderr, _("CRTC %d does not exist. "), - state->crtc_num); - if (crtc_count > 1) { - fprintf(stderr, _("Valid CRTCs are [0-%d].\n"), - crtc_count-1); - } else { - fprintf(stderr, _("Only CRTC 0 exists.\n")); - } + if (crtc_count > 1) + weprintf(_("CRTC %i does not exist, valid CRTCs are [0-%i].\n"), state->crtc_num, crtc_count-1); + else + weprintf(_("CRTC %i does not exist, only CRTC 0 exists.\n"), state->crtc_num); close(state->fd); state->fd = -1; drmModeFreeResources(state->res); @@ -145,15 +139,14 @@ drm_start(struct gamma_state *state, enum program_mode mode) crtcs->crtc_id = state->res->crtcs[crtcs->crtc_num]; crtc_info = drmModeGetCrtc(state->fd, crtcs->crtc_id); if (crtc_info == NULL) { - fprintf(stderr, _("CRTC %i lost, skipping\n"), crtcs->crtc_num); + weprintf(_("CRTC %i lost, skipping"), crtcs->crtc_num); continue; } crtcs->gamma_size = crtc_info->gamma_size; drmModeFreeCrtc(crtc_info); if (crtcs->gamma_size <= 1) { - fprintf(stderr, _("Could not get gamma ramp size for CRTC %i\n" - "on graphics card %i, ignoring device.\n"), - crtcs->crtc_num, state->card_num); + weprintf(_("Could not get gamma ramp size for CRTC %i on graphics card" + " %i, ignoring device."), crtcs->crtc_num, state->card_num); continue; } /* Valgrind complains about us reading uninitialize memory if we just use malloc. */ @@ -164,9 +157,8 @@ drm_start(struct gamma_state *state, enum program_mode mode) int r = drmModeCrtcGetGamma(state->fd, crtcs->crtc_id, crtcs->gamma_size, crtcs->r_gamma, crtcs->g_gamma, crtcs->b_gamma); if (r < 0) { - fprintf(stderr, _("DRM could not read gamma ramps on CRTC %i on\n" - "graphics card %i, ignoring device.\n"), - crtcs->crtc_num, state->card_num); + weprintf(_("DRM could not read gamma ramps on CRTC %i on graphics card" + " %i, ignoring device."), crtcs->crtc_num, state->card_num); free(crtcs->r_gamma); crtcs->r_gamma = NULL; } @@ -246,11 +238,11 @@ drm_set_option(struct gamma_state *state, const char *key, const char *value) } else if (!strcasecmp(key, "crtc")) { state->crtc_num = atoi(value); if (state->crtc_num < 0) { - weprintf(_("CRTC must be a non-negative integer\n")); + weprintf(_("CRTC must be a non-negative integer")); return -1; } } else { - weprintf(_("Unknown method parameter: `%s'.\n"), key); + weprintf(_("Unknown method parameter: `%s'."), key); return -1; } diff --git a/src/gamma-dummy.c b/src/gamma-dummy.c index 39f09a2..20f4c49 100644 --- a/src/gamma-dummy.c +++ b/src/gamma-dummy.c @@ -59,7 +59,7 @@ gamma_dummy_set_option(struct gamma_state *state, const char *key, const char *v { (void) state; (void) value; - weprintf(_("Unknown method parameter: `%s'.\n"), key); + weprintf(_("Unknown method parameter: `%s'."), key); return -1; } diff --git a/src/gamma-quartz.c b/src/gamma-quartz.c index 30a50d6..0632951 100644 --- a/src/gamma-quartz.c +++ b/src/gamma-quartz.c @@ -83,7 +83,7 @@ quartz_start(struct gamma_state *state, program_mode_t mode) ramp_size = CGDisplayGammaTableCapacity(display); if (!ramp_size) { - weprintf(_("Gamma ramp size too small: %i\n"), ramp_size); + weprintf(_("Gamma ramp size too small: %i"), ramp_size); return -1; } @@ -99,7 +99,7 @@ quartz_start(struct gamma_state *state, program_mode_t mode) /* Copy the ramps to allocated space */ error = CGGetDisplayTransferByTable(display, ramp_size, gamma_r, gamma_g, gamma_b, &sample_count); if (error != kCGErrorSuccess || sample_count != ramp_size) { - weprintf(_("Unable to save current gamma ramp.\n")); + weprintf(_("Unable to save current gamma ramp.")); return -1; } } @@ -135,10 +135,9 @@ static int quartz_set_option(struct gamma_state *state, const char *key, const char *value) { if (!strcasecmp(key, "preserve")) { - weprintf(_("Parameter `%s` is now always on; Use the `%s`" - " command-line option to disable.\n"), key, "-P"); + weprintf(_("Parameter `%s' is now always on; use the `%s' command-line option to disable."), key, "-P"); } else { - weprintf(_("Unknown method parameter: `%s'.\n"), key); + weprintf(_("Unknown method parameter: `%s'."), key); return -1; } diff --git a/src/gamma-randr.c b/src/gamma-randr.c index c8ea754..214acab 100644 --- a/src/gamma-randr.c +++ b/src/gamma-randr.c @@ -55,10 +55,10 @@ randr_init(struct gamma_state **state) xcb_randr_query_version_reply_t *ver_reply; xcb_generic_error_t *error; struct gamma_state *s; + int ec; /* Initialize state. */ - *state = malloc(sizeof(**state)); - if (*state == NULL) return -1; + *state = emalloc(sizeof(**state)); s = *state; s->screen_num = -1; @@ -78,8 +78,8 @@ randr_init(struct gamma_state **state) /* TODO What does it mean when both error and ver_reply is NULL? Apparently, we have to check both to avoid seg faults. */ if (error || ver_reply == NULL) { - int ec = (error != 0) ? error->error_code : -1; - weprintf(_("`%s' returned error %d\n"), "RANDR Query Version", ec); + ec = (error != 0) ? error->error_code : -1; + weprintf(_("`%s' returned error %i\n"), "RANDR Query Version", ec); xcb_disconnect(s->conn); free(s); return -1; @@ -140,7 +140,7 @@ randr_start(struct gamma_state *state, enum program_mode mode) res_reply = xcb_randr_get_screen_resources_current_reply(state->conn, res_cookie, &error); if (error) { - weprintf(_("`%s' returned error %d\n"), "RANDR Get Screen Resources Current", error->error_code); + weprintf(_("`%s' returned error %i\n"), "RANDR Get Screen Resources Current", error->error_code); return -1; } @@ -172,7 +172,7 @@ randr_start(struct gamma_state *state, enum program_mode mode) gamma_size_reply = xcb_randr_get_crtc_gamma_size_reply(state->conn, gamma_size_cookie, &error); if (error) { - weprintf(_("`%s' returned error %d\n"), "RANDR Get CRTC Gamma Size", error->error_code); + weprintf(_("`%s' returned error %i\n"), "RANDR Get CRTC Gamma Size", error->error_code); return -1; } @@ -182,7 +182,7 @@ randr_start(struct gamma_state *state, enum program_mode mode) free(gamma_size_reply); if (ramp_size == 0) { - weprintf(_("Gamma ramp size too small: %i\n"), ramp_size); + weprintf(_("Gamma ramp size too small: %i"), ramp_size); return -1; } @@ -191,7 +191,7 @@ randr_start(struct gamma_state *state, enum program_mode mode) gamma_get_reply = xcb_randr_get_crtc_gamma_reply(state->conn, gamma_get_cookie, &error); if (error) { - weprintf(_("`%s' returned error %d\n"), "RANDR Get CRTC Gamma", error->error_code); + weprintf(_("`%s' returned error %i\n"), "RANDR Get CRTC Gamma", error->error_code); return -1; } @@ -236,7 +236,7 @@ randr_restore(struct gamma_state *state) error = xcb_request_check(state->conn, gamma_set_cookie); if (error) { - weprintf(_("`%s' returned error %d\n"), "RANDR Set CRTC Gamma", error->error_code); + weprintf(_("`%s' returned error %i\n"), "RANDR Set CRTC Gamma", error->error_code); weprintf(_("Unable to restore CRTC %i\n"), i); } } @@ -318,10 +318,9 @@ randr_set_option(struct gamma_state *state, const char *key, const char *value) break; } } else if (strcasecmp(key, "preserve") == 0) { - weprintf(_("Parameter `%s` is now always on; Use the `%s`" - " command-line option to disable.\n"), key, "-P"); + weprintf(_("Parameter `%s' is now always on; use the `%s' command-line option to disable."), key, "-P"); } else { - weprintf(_("Unknown method parameter: `%s'.\n"), key); + weprintf(_("Unknown method parameter: `%s'."), key); return -1; } @@ -339,9 +338,9 @@ randr_set_temperature_for_crtc(struct gamma_state *state, int crtc_num, uint16_t *gamma_ramps, *gamma_r, *gamma_g, *gamma_b, value; if (crtc_num >= state->crtc_count || crtc_num < 0) { - fprintf(stderr, _("CRTC %d does not exist. "), crtc_num); + fprintf(stderr, _("CRTC %i does not exist. "), crtc_num); if (state->crtc_count > 1) - fprintf(stderr, _("Valid CRTCs are [0-%d].\n"), state->crtc_count-1); + fprintf(stderr, _("Valid CRTCs are [0-%i].\n"), state->crtc_count-1); else fprintf(stderr, _("Only CRTC 0 exists.\n")); @@ -381,7 +380,7 @@ randr_set_temperature_for_crtc(struct gamma_state *state, int crtc_num, error = xcb_request_check(state->conn, gamma_set_cookie); if (error) { - weprintf(_("`%s' returned error %d\n"), "RANDR Set CRTC Gamma", error->error_code); + weprintf(_("`%s' returned error %i\n"), "RANDR Set CRTC Gamma", error->error_code); free(gamma_ramps); return -1; } diff --git a/src/gamma-vidmode.c b/src/gamma-vidmode.c index 1fda89a..8cd5e04 100644 --- a/src/gamma-vidmode.c +++ b/src/gamma-vidmode.c @@ -44,7 +44,7 @@ vidmode_init(struct gamma_state **state) /* Open display */ s->display = XOpenDisplay(NULL); if (!s->display) { - weprintf(_("X request failed: %s\n"), "XOpenDisplay"); + weprintf(_("X request failed: %s"), "XOpenDisplay"); return -1; } @@ -68,19 +68,19 @@ vidmode_start(struct gamma_state *state, enum program_mode mode) /* Query extension version */ r = XF86VidModeQueryVersion(state->display, &major, &minor); if (!r) { - weprintf(_("X request failed: %s\n"), "XF86VidModeQueryVersion"); + weprintf(_("X request failed: %s"), "XF86VidModeQueryVersion"); return -1; } /* Request size of gamma ramps */ r = XF86VidModeGetGammaRampSize(state->display, state->screen_num, &state->ramp_size); if (!r) { - weprintf(_("X request failed: %s\n"), "XF86VidModeGetGammaRampSize"); + weprintf(_("X request failed: %s"), "XF86VidModeGetGammaRampSize"); return -1; } if (!state->ramp_size) { - weprintf(_("Gamma ramp size too small: %i\n"), state->ramp_size); + weprintf(_("Gamma ramp size too small: %i"), state->ramp_size); return -1; } @@ -95,7 +95,7 @@ vidmode_start(struct gamma_state *state, enum program_mode mode) r = XF86VidModeGetGammaRamp(state->display, state->screen_num, state->ramp_size, gamma_r, gamma_g, gamma_b); if (!r) { - weprintf(_("X request failed: %s\n"), "XF86VidModeGetGammaRamp"); + weprintf(_("X request failed: %s"), "XF86VidModeGetGammaRamp"); return -1; } @@ -105,12 +105,8 @@ vidmode_start(struct gamma_state *state, enum program_mode mode) static void vidmode_free(struct gamma_state *state) { - /* Free saved ramps */ free(state->saved_ramps); - - /* Close display connection */ XCloseDisplay(state->display); - free(state); } @@ -131,10 +127,9 @@ vidmode_set_option(struct gamma_state *state, const char *key, const char *value if (!strcasecmp(key, "screen")) { state->screen_num = atoi(value); } else if (!strcasecmp(key, "preserve")) { - weprintf(_("Parameter `%s` is now always on; Use the `%s` " - "command-line option to disable.\n"), key, "-P"); + weprintf(_("Parameter `%s' is now always on; use the `%s' command-line option to disable."), key, "-P"); } else { - weprintf(_("Unknown method parameter: `%s'.\n"), key); + weprintf(_("Unknown method parameter: `%s'."), key); return -1; } @@ -153,7 +148,7 @@ vidmode_restore(struct gamma_state *state) r = XF86VidModeSetGammaRamp(state->display, state->screen_num, state->ramp_size, gamma_r, gamma_g, gamma_b); if (!r) - weprintf(_("X request failed: %s\n"), "XF86VidModeSetGammaRamp"); + weprintf(_("X request failed: %s"), "XF86VidModeSetGammaRamp"); } static int @@ -190,7 +185,7 @@ vidmode_set_temperature( r = XF86VidModeSetGammaRamp(state->display, state->screen_num, state->ramp_size, gamma_r, gamma_g, gamma_b); if (!r) { - weprintf(_("X request failed: %s\n"), "XF86VidModeSetGammaRamp"); + weprintf(_("X request failed: %s"), "XF86VidModeSetGammaRamp"); free(gamma_ramps); return -1; } diff --git a/src/gamma-w32gdi.c b/src/gamma-w32gdi.c index a252e0a..5beb1a9 100644 --- a/src/gamma-w32gdi.c +++ b/src/gamma-w32gdi.c @@ -50,14 +50,14 @@ w32gdi_start(struct gamma_state *state, program_mode_t mode) /* Open device context */ hDC = GetDC(NULL); if (!hDC) { - weprintf(_("Unable to open device context.\n")); + weprintf(_("Unable to open device context.")); return -1; } /* Check support for gamma ramps */ cmcap = GetDeviceCaps(hDC, COLORMGMTCAPS); if (cmcap != CM_GAMMA_RAMP) { - weprintf(_("Display device does not support gamma ramps.\n")); + weprintf(_("Display device does not support gamma ramps.")); return -1; } @@ -66,7 +66,7 @@ w32gdi_start(struct gamma_state *state, program_mode_t mode) /* Save current gamma ramps so we can restore them at program exit */ if (!GetDeviceGammaRamp(hDC, state->saved_ramps)) { - weprintf(_("Unable to save current gamma ramp.\n")); + weprintf(_("Unable to save current gamma ramp.")); ReleaseDC(NULL, hDC); return -1; } @@ -80,9 +80,7 @@ w32gdi_start(struct gamma_state *state, program_mode_t mode) static void w32gdi_free(struct gamma_state *state) { - /* Free saved ramps */ free(state->saved_ramps); - free(state); } @@ -98,10 +96,9 @@ static int w32gdi_set_option(struct gamma_state *state, const char *key, const char *value) { if (!strcasecmp(key, "preserve")) { - weprintf(_("Parameter `%s` is now always on; Use the `%s` " - "command-line option to disable.\n"), key, "-P"); + weprintf(_("Parameter `%s' is now always on; use the `%s' command-line option to disable."), key, "-P"); } else { - weprintf(_("Unknown method parameter: `%s'.\n"), key); + weprintf(_("Unknown method parameter: `%s'."), key); return -1; } @@ -117,7 +114,7 @@ w32gdi_restore(struct gamma_state *state) /* Open device context */ hDC = GetDC(NULL); if (!hDC) { - fputs(_("Unable to open device context.\n"), stderr); + weprintf(_("Unable to open device context.")); return; } @@ -129,7 +126,7 @@ w32gdi_restore(struct gamma_state *state) if (SetDeviceGammaRamp(hDC, state->saved_ramps)) goto done; } - weprintf(_("Unable to restore gamma ramps.\n")); + weprintf(_("Unable to restore gamma ramps.")); done: /* Release device context */ @@ -147,7 +144,7 @@ w32gdi_set_temperature( /* Open device context */ hDC = GetDC(NULL); if (!hDC) { - weprintf(_("Unable to open device context.\n")); + weprintf(_("Unable to open device context.")); return -1; } @@ -182,7 +179,7 @@ w32gdi_set_temperature( if (SetDeviceGammaRamp(hDC, gamma_ramps)) goto done; } - weprintf(_("Unable to set gamma ramps.\n")); + weprintf(_("Unable to set gamma ramps.")); free(gamma_ramps); ReleaseDC(NULL, hDC); return -1; diff --git a/src/hooks.c b/src/hooks.c index cc26c7c..e55f3a1 100644 --- a/src/hooks.c +++ b/src/hooks.c @@ -92,7 +92,7 @@ hooks_signal_period_change(enum period prev_period, enum period period) output. */ switch (fork()) { case -1: - perror("fork"); + weprintf("fork:"); break; case 0: close(STDOUT_FILENO); diff --git a/src/location-corelocation.m b/src/location-corelocation.m index bf899ee..b20d32d 100644 --- a/src/location-corelocation.m +++ b/src/location-corelocation.m @@ -15,6 +15,7 @@ along with Redshift. If not, see . Copyright (c) 2014-2017 Jon Lund Steffensen + Copyright (c) 2025 Mattias Andrée */ #include "common.h" @@ -23,102 +24,96 @@ struct location_state { - NSThread *thread; - NSLock *lock; - int pipe_fd_read; - int pipe_fd_write; - int available; - int error; - struct location location; + NSThread *thread; + NSLock *lock; + int pipe_fd_read; + int pipe_fd_write; + int available; + int error; + struct location location; }; @interface LocationDelegate : NSObject -@property (strong, nonatomic) CLLocationManager *locationManager; -@property (nonatomic) struct location_state *state; + @property (strong, nonatomic) CLLocationManager *locationManager; + @property (nonatomic) struct location_state *state; @end @implementation LocationDelegate; - (void)start { - self.locationManager = [[CLLocationManager alloc] init]; - self.locationManager.delegate = self; - self.locationManager.distanceFilter = 50000; - self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; - - CLAuthorizationStatus authStatus = - [CLLocationManager authorizationStatus]; - - if (authStatus != kCLAuthorizationStatusNotDetermined && - authStatus != kCLAuthorizationStatusAuthorized) { - fputs(_("Not authorized to obtain location" - " from CoreLocation.\n"), stderr); - [self markError]; - } else { - [self.locationManager startUpdatingLocation]; - } + CLAuthorizationStatus authStatus; + + self.locationManager = [[CLLocationManager alloc] init]; + self.locationManager.delegate = self; + self.locationManager.distanceFilter = 50000; + self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; + + authStatus = [CLLocationManager authorizationStatus]; + + if (authStatus != kCLAuthorizationStatusNotDetermined && + authStatus != kCLAuthorizationStatusAuthorized) { + weprintf(_("Not authorized to obtain location from CoreLocation.")); + [self markError]; + } else { + [self.locationManager startUpdatingLocation]; + } } - (void)markError { - [self.state->lock lock]; + [self.state->lock lock]; + self.state->error = 1; + [self.state->lock unlock]; - self.state->error = 1; - - [self.state->lock unlock]; - - pipeutils_signal(self.state->pipe_fd_write); + pipeutils_signal(self.state->pipe_fd_write); } - (void)markUnavailable { - [self.state->lock lock]; - - self.state->available = 0; - - [self.state->lock unlock]; + [self.state->lock lock]; + self.state->available = 0; + [self.state->lock unlock]; - pipeutils_signal(self.state->pipe_fd_write); + pipeutils_signal(self.state->pipe_fd_write); } - (void)locationManager:(CLLocationManager *)manager - didUpdateLocations:(NSArray *)locations + didUpdateLocations:(NSArray *)locations { - CLLocation *newLocation = [locations firstObject]; + CLLocation *newLocation = [locations firstObject]; - [self.state->lock lock]; + [self.state->lock lock]; - self.state->location.lat = newLocation.coordinate.latitude; - self.state->location.lon = newLocation.coordinate.longitude; - self.state->available = 1; + self.state->location.lat = newLocation.coordinate.latitude; + self.state->location.lon = newLocation.coordinate.longitude; + self.state->available = 1; - [self.state->lock unlock]; + [self.state->lock unlock]; - pipeutils_signal(self.state->pipe_fd_write); + pipeutils_signal(self.state->pipe_fd_write); } - (void)locationManager:(CLLocationManager *)manager - didFailWithError:(NSError *)error + didFailWithError:(NSError *)error { - fprintf(stderr, _("Error obtaining location from CoreLocation: %s\n"), - [[error localizedDescription] UTF8String]); - if ([error code] == kCLErrorDenied) { - [self markError]; - } else { - [self markUnavailable]; - } + weprintf(_("Error obtaining location from CoreLocation: %s"), [[error localizedDescription] UTF8String]); + if ([error code] == kCLErrorDenied) + [self markError]; + else + [self markUnavailable]; } - (void)locationManager:(CLLocationManager *)manager - didChangeAuthorizationStatus:(CLAuthorizationStatus)status + didChangeAuthorizationStatus:(CLAuthorizationStatus)status { - if (status == kCLAuthorizationStatusNotDetermined) { - fputs(_("Waiting for authorization to obtain location...\n"), stderr); - } else if (status != kCLAuthorizationStatusAuthorized) { - fputs(_("Request for location was not authorized!\n"), stderr); - [self markError]; - } + if (status == kCLAuthorizationStatusNotDetermined) { + weprintf(_("Waiting for authorization to obtain location...")); + } else if (status != kCLAuthorizationStatusAuthorized) { + weprintf(_("Request for location was not authorized!")); + [self markError]; + } } @end @@ -128,18 +123,17 @@ struct location_state { // // Stops the run loop causing the thread to end. static void -pipe_close_callback( - CFFileDescriptorRef fdref, CFOptionFlags callBackTypes, void *info) +pipe_close_callback(CFFileDescriptorRef fdref, CFOptionFlags callBackTypes, void *info) { - CFFileDescriptorInvalidate(fdref); - CFRelease(fdref); + CFFileDescriptorInvalidate(fdref); + CFRelease(fdref); - CFRunLoopStop(CFRunLoopGetCurrent()); + CFRunLoopStop(CFRunLoopGetCurrent()); } @interface LocationThread : NSThread -@property (nonatomic) struct location_state *state; + @property (nonatomic) struct location_state *state; @end @implementation LocationThread; @@ -147,29 +141,30 @@ pipe_close_callback( // Run loop for location provider thread. - (void)main { - @autoreleasepool { - LocationDelegate *locationDelegate = [[LocationDelegate alloc] init]; - locationDelegate.state = self.state; - - // Start the location delegate on the run loop in this thread. - [locationDelegate performSelector:@selector(start) - withObject:nil afterDelay:0]; - - // Create a callback that is triggered when the pipe is closed. This will - // trigger the main loop to quit and the thread to stop. - CFFileDescriptorRef fdref = CFFileDescriptorCreate( - kCFAllocatorDefault, self.state->pipe_fd_write, false, - pipe_close_callback, NULL); - CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack); - CFRunLoopSourceRef source = CFFileDescriptorCreateRunLoopSource( - kCFAllocatorDefault, fdref, 0); - CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode); - - // Run the loop - CFRunLoopRun(); - - close(self.state->pipe_fd_write); - } + @autoreleasepool { + LocationDelegate *locationDelegate; + CFFileDescriptorRef fdref; + CFRunLoopSourceRef source; + + locationDelegate = [[LocationDelegate alloc] init]; + locationDelegate.state = self.state; + + // Start the location delegate on the run loop in this thread. + [locationDelegate performSelector:@selector(start) withObject:nil afterDelay:0]; + + // Create a callback that is triggered when the pipe is closed. This will + // trigger the main loop to quit and the thread to stop. + fdref = CFFileDescriptorCreate(kCFAllocatorDefault, self.state->pipe_fd_write, + false, pipe_close_callback, NULL); + CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack); + source = CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, fdref, 0); + CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode); + + // Run the loop + CFRunLoopRun(); + + close(self.state->pipe_fd_write); + } } @end @@ -178,103 +173,97 @@ pipe_close_callback( static int location_corelocation_init(struct location_state **state) { - *state = malloc(sizeof(struct location_state)); - if (*state == NULL) return -1; - return 0; + *state = emalloc(sizeof(**state)); + return 0; } static int location_corelocation_start(struct location_state *state) { - state->pipe_fd_read = -1; - state->pipe_fd_write = -1; + LocationThread *thread; + int pipefds[2]; - state->available = 0; - state->error = 0; - state->location.lat = 0; - state->location.lon = 0; + state->pipe_fd_read = -1; + state->pipe_fd_write = -1; - int pipefds[2]; - int r = pipeutils_create_nonblocking(pipefds); - if (r < 0) { - fputs(_("Failed to start CoreLocation provider!\n"), stderr); - return -1; - } + state->available = 0; + state->error = 0; + state->location.lat = 0; + state->location.lon = 0; - state->pipe_fd_read = pipefds[0]; - state->pipe_fd_write = pipefds[1]; + if (pipeutils_create_nonblocking(pipefds)) { + weprintf(_("Failed to start CoreLocation provider!")); + return -1; + } - pipeutils_signal(state->pipe_fd_write); + state->pipe_fd_read = pipefds[0]; + state->pipe_fd_write = pipefds[1]; - state->lock = [[NSLock alloc] init]; + pipeutils_signal(state->pipe_fd_write); - LocationThread *thread = [[LocationThread alloc] init]; - thread.state = state; - [thread start]; - state->thread = thread; + state->lock = [[NSLock alloc] init]; - return 0; + thread = [[LocationThread alloc] init]; + thread.state = state; + [thread start]; + state->thread = thread; + + return 0; } static void location_corelocation_free(struct location_state *state) { - if (state->pipe_fd_read != -1) { - close(state->pipe_fd_read); - } - - free(state); + if (state->pipe_fd_read >= 0) + close(state->pipe_fd_read); + free(state); } static void location_corelocation_print_help(FILE *f) { - fputs(_("Use the location as discovered by the Corelocation provider.\n"), f); - fputs("\n", f); + fputs(_("Use the location as discovered by the Corelocation provider.\n"), f); + fputs("\n", f); } static int location_corelocation_set_option( struct location_state *state, const char *key, const char *value) { - fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key); - return -1; + weprintf(_("Unknown method parameter: `%s'."), key); + return -1; } static int location_corelocation_get_fd(struct location_state *state) { - return state->pipe_fd_read; + return state->pipe_fd_read; } static int -location_corelocation_handle( - struct location_state *state, - location_t *location, int *available) +location_corelocation_handle(struct location_state *state, location_t *location, int *available) { - pipeutils_handle_signal(state->pipe_fd_read); - - [state->lock lock]; - - int error = state->error; - *location->lat = state->location; - *available = state->available; + int error; - [state->lock unlock]; + pipeutils_handle_signal(state->pipe_fd_read); - if (error) return -1; + [state->lock lock]; + error = state->error; + *location->lat = state->location; + *available = state->available; + [state->lock unlock]; - return 0; + return error ? -1 : 0; } const location_provider_t corelocation_location_provider = { - "corelocation", - (location_provider_init_func *)location_corelocation_init, - (location_provider_start_func *)location_corelocation_start, - (location_provider_free_func *)location_corelocation_free, - (location_provider_print_help_func *)location_corelocation_print_help, - (location_provider_set_option_func *)location_corelocation_set_option, - (location_provider_get_fd_func *)location_corelocation_get_fd, - (location_provider_handle_func *)location_corelocation_handle + "corelocation", + &location_corelocation_init, + &location_corelocation_start, + &location_corelocation_free, + &location_corelocation_print_help, + &location_corelocation_set_option, + &location_corelocation_get_fd, + &location_corelocation_handle }; diff --git a/src/location-geoclue2.c b/src/location-geoclue2.c index b1489db..c2c080a 100644 --- a/src/location-geoclue2.c +++ b/src/location-geoclue2.c @@ -86,7 +86,7 @@ geoclue_client_signal_cb(GDBusProxy *client, gchar *sender_name, gchar *signal_n NULL, "org.freedesktop.GeoClue2", location_path, "org.freedesktop.GeoClue2.Location", NULL, &error); if (!location) { - weprintf(_("Unable to obtain location: %s.\n"), error->message); + weprintf(_("Unable to obtain location: %s."), error->message); g_error_free(error); mark_error(state); return; @@ -131,7 +131,7 @@ on_name_appeared(GDBusConnection *conn, const gchar *name, "org.freedesktop.GeoClue2", "/org/freedesktop/GeoClue2/Manager", "org.freedesktop.GeoClue2.Manager", NULL, &error); if (!geoclue_manager) { - weprintf(_("Unable to obtain GeoClue Manager: %s.\n"), error->message); + weprintf(_("Unable to obtain GeoClue Manager: %s."), error->message); g_error_free(error); mark_error(state); return; @@ -142,7 +142,7 @@ on_name_appeared(GDBusConnection *conn, const gchar *name, client_path_v = g_dbus_proxy_call_sync(geoclue_manager, "GetClient", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); if (!client_path_v) { - weprintf(_("Unable to obtain GeoClue client path: %s.\n"), error->message); + weprintf(_("Unable to obtain GeoClue client path: %s."), error->message); g_error_free(error); g_object_unref(geoclue_manager); mark_error(state); @@ -156,7 +156,7 @@ on_name_appeared(GDBusConnection *conn, const gchar *name, geoclue_client = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, NULL, "org.freedesktop.GeoClue2", client_path, "org.freedesktop.GeoClue2.Client", NULL, &error); if (!geoclue_client) { - weprintf(_("Unable to obtain GeoClue Client: %s.\n"), error->message); + weprintf(_("Unable to obtain GeoClue Client: %s."), error->message); g_error_free(error); g_variant_unref(client_path_v); g_object_unref(geoclue_manager); @@ -174,7 +174,7 @@ on_name_appeared(GDBusConnection *conn, const gchar *name, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); if (!ret_v) { /* Ignore this error for now. The property is not available - in early versions of GeoClue2. */ + * in early versions of GeoClue2. */ } else { g_variant_unref(ret_v); } @@ -186,7 +186,7 @@ on_name_appeared(GDBusConnection *conn, const gchar *name, "DistanceThreshold", g_variant_new("u", 50000)), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); if (!ret_v) { - weprintf(_("Unable to set distance threshold: %s.\n"), error->message); + weprintf(_("Unable to set distance threshold: %s."), error->message); g_error_free(error); g_object_unref(geoclue_client); g_object_unref(geoclue_manager); @@ -203,7 +203,7 @@ on_name_appeared(GDBusConnection *conn, const gchar *name, error = NULL; ret_v = g_dbus_proxy_call_sync(geoclue_client, "Start", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); if (!ret_v) { - weprintf(_("Unable to start GeoClue client: %s.\n"), error->message); + weprintf(_("Unable to start GeoClue client: %s."), error->message); if (g_dbus_error_is_remote_error(error)) { dbus_error = g_dbus_error_get_remote_error( error); if (!g_strcmp0(dbus_error, DBUS_ACCESS_ERROR)) @@ -311,7 +311,7 @@ location_geoclue2_start(struct location_state *state) state->location.lon = 0; if (pipeutils_create_nonblocking(pipefds)) { - weprintf(_("Failed to start GeoClue2 provider!\n")); + weprintf(_("Failed to start GeoClue2 provider!")); return -1; } @@ -352,7 +352,7 @@ location_geoclue2_set_option(struct location_state *state, const char *key, cons { (void) state; (void) value; - weprintf(_("Unknown method parameter: `%s'.\n"), key); + weprintf(_("Unknown method parameter: `%s'."), key); return -1; } diff --git a/src/location-manual.c b/src/location-manual.c index f59bddd..14a0aad 100644 --- a/src/location-manual.c +++ b/src/location-manual.c @@ -37,11 +37,8 @@ location_manual_init(struct location_state **state) static int location_manual_start(struct location_state *state) { - /* Latitude and longitude must be set */ - if (isnan(state->loc.lat) || isnan(state->loc.lon)) { - fputs(_("Latitude and longitude must be set.\n"), stderr); - exit(EXIT_FAILURE); - } + if (isnan(state->loc.lat) || isnan(state->loc.lon)) + eprintf(_("Latitude and longitude must be set.")); return 0; } @@ -68,8 +65,7 @@ location_manual_print_help(FILE *f) } static int -location_manual_set_option(struct location_state *state, const char *key, - const char *value) +location_manual_set_option(struct location_state *state, const char *key, const char *value) { /* Parse float value */ char *end; @@ -77,8 +73,8 @@ location_manual_set_option(struct location_state *state, const char *key, errno = 0; v = strtod(value, &end); - if (errno != 0 || *end != '\0') { - fputs(_("Malformed argument.\n"), stderr); + if (errno || *end) { + weprintf(_("Malformed argument.")); return -1; } @@ -87,7 +83,7 @@ location_manual_set_option(struct location_state *state, const char *key, } else if (!strcasecmp(key, "lon")) { state->loc.lon = v; } else { - weprintf(_("Unknown method parameter: `%s'.\n"), key); + weprintf(_("Unknown method parameter: `%s'."), key); return -1; } diff --git a/src/options.c b/src/options.c index 725599e..4c8fb70 100644 --- a/src/options.c +++ b/src/options.c @@ -86,16 +86,14 @@ parse_transition_time(const char *str, const char **end) errno = 0; hours = strtol(str, (void *)&min, 10); - if (errno != 0 || min == str || min[0] != ':' || hours < 0 || hours >= 24) { + if (errno || min == str || min[0] != ':' || hours < 0 || hours >= 24) return -1; - } min += 1; errno = 0; minutes = strtol(min, (void *)end, 10); - if (errno != 0 || *end == min || minutes < 0 || minutes >= 60) { + if (errno || *end == min || minutes < 0 || minutes >= 60) return -1; - } return minutes * 60 + hours * 3600; } @@ -119,7 +117,7 @@ parse_transition_range(const char *str, struct time_range *range) const char *end = NULL; next += 1; end_time = parse_transition_time(next, &end); - if (end_time < 0 || end[0] != '\0') + if (end_time < 0 || *end) return -1; } else { return -1; @@ -203,21 +201,21 @@ print_help(const char *program_name) static void print_method_list(const struct gamma_method *gamma_methods) { + size_t i; + fputs(_("Available adjustment methods:\n"), stdout); - for (int i = 0; gamma_methods[i].name != NULL; i++) { + for (i = 0; gamma_methods[i].name != NULL; i++) printf(" %s\n", gamma_methods[i].name); - } fputs("\n", stdout); - fputs(_("Specify colon-separated options with" - " `-m METHOD:OPTIONS'.\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(const struct location_provider location_providers[]) +print_provider_list(const struct location_provider *location_providers) { fputs(_("Available location providers:\n"), stdout); for (int i = 0; location_providers[i].name != NULL; i++) { @@ -225,43 +223,32 @@ print_provider_list(const struct location_provider location_providers[]) } fputs("\n", stdout); - fputs(_("Specify colon-separated options with" - "`-l PROVIDER:OPTIONS'.\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. */ static const struct gamma_method * -find_gamma_method(const struct gamma_method gamma_methods[], const char *name) +find_gamma_method(const struct gamma_method *gamma_methods, const char *name) { - const struct gamma_method *method = NULL; - for (int i = 0; gamma_methods[i].name != NULL; i++) { - const struct gamma_method *m = &gamma_methods[i]; - if (strcasecmp(name, m->name) == 0) { - method = m; - break; - } - } - - return method; + size_t i; + for (i = 0; gamma_methods[i].name; i++) + if (!strcasecmp(name, gamma_methods[i].name)) + return &gamma_methods[i]; + return NULL; } /* Return location provider with the given name. */ static const struct location_provider * -find_location_provider( - const struct location_provider location_providers[], const char *name) +find_location_provider(const struct location_provider *location_providers, const char *name) { - const struct location_provider *provider = NULL; - for (int i = 0; location_providers[i].name != NULL; i++) { - const struct location_provider *p = &location_providers[i]; - if (strcasecmp(name, p->name) == 0) { - provider = p; - break; - } + size_t i; + for (i = 0; location_providers[i].name != NULL; i++) { + if (!strcasecmp(name, location_providers[i].name)) + return &location_providers[i]; } - - return provider; + return NULL; } @@ -308,50 +295,44 @@ options_init(struct options *options) /* Parse a single option from the command-line. */ static int -parse_command_line_option( - const char option, char *value, struct options *options, - const char *program_name, const struct gamma_method *gamma_methods, - const struct location_provider *location_providers) +parse_command_line_option(const char option, char *value, struct options *options, const char *program_name, + const struct gamma_method *gamma_methods, const struct location_provider *location_providers) { - int r; - char *s; - char *end; const char *provider_name; + char *s, *end; + int r; switch (option) { case 'b': - parse_brightness_string( - value, &options->scheme.day.brightness, - &options->scheme.night.brightness); + parse_brightness_string(value, &options->scheme.day.brightness, &options->scheme.night.brightness); break; + case 'c': free(options->config_filepath); - options->config_filepath = strdup(value); + options->config_filepath = estrdup(value); break; + case 'g': r = parse_gamma_string(value, options->scheme.day.gamma); if (r < 0) { - fputs(_("Malformed gamma argument.\n"), stderr); - fputs(_("Try `-h' for more information.\n"), stderr); + weprintf(_("Malformed gamma argument.")); + weprintf(_("Try `-h' for more information.")); return -1; } - /* 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)); + 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(program_name); - exit(EXIT_SUCCESS); - break; + exit(0); + case 'l': /* Print list of providers if argument is `list' */ - if (strcasecmp(value, "list") == 0) { + if (!strcasecmp(value, "list")) { print_provider_list(location_providers); - exit(EXIT_SUCCESS); + exit(0); } provider_name = NULL; @@ -360,15 +341,15 @@ parse_command_line_option( to know if value can be parsed as a float. */ errno = 0; strtof(value, &end); - if (errno == 0 && *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 != NULL) { - *(s++) = '\0'; + if (s) { + *s++ = '\0'; options->provider_args = s; } @@ -377,91 +358,96 @@ parse_command_line_option( /* Lookup provider from name. */ options->provider = find_location_provider(location_providers, provider_name); - if (options->provider == NULL) { - fprintf(stderr, _("Unknown location provider `%s'.\n"), - provider_name); + if (!options->provider) { + weprintf(_("Unknown location provider `%s'."), provider_name); return -1; } /* Print provider help if arg is `help'. */ - if (options->provider_args != NULL && - strcasecmp(options->provider_args, "help") == 0) { + if (options->provider_args && !strcasecmp(options->provider_args, "help")) { options->provider->print_help(stdout); - exit(EXIT_SUCCESS); + exit(0); } break; + case 'm': /* Print list of methods if argument is `list' */ - if (strcasecmp(value, "list") == 0) { + if (!strcasecmp(value, "list")) { print_method_list(gamma_methods); - exit(EXIT_SUCCESS); + exit(0); } /* Split off method arguments. */ s = strchr(value, ':'); - if (s != NULL) { - *(s++) = '\0'; + if (s) { + *s++ = '\0'; options->method_args = s; } /* Find adjustment method by name. */ options->method = find_gamma_method(gamma_methods, value); - if (options->method == NULL) { - /* TRANSLATORS: This refers to the method - used to adjust colors e.g VidMode */ - fprintf(stderr, _("Unknown adjustment method `%s'.\n"), - value); + if (!options->method) { + /* TRANSLATORS: This refers to the method used to adjust colors e.g. VidMode */ + weprintf(_("Unknown adjustment method `%s'."), value); return -1; } /* Print method help if arg is `help'. */ - if (options->method_args != NULL && - strcasecmp(options->method_args, "help") == 0) { + if (options->method_args && !strcasecmp(options->method_args, "help")) { options->method->print_help(stdout); - exit(EXIT_SUCCESS); + exit(0); } break; + case 'o': options->mode = PROGRAM_MODE_ONE_SHOT; break; + case 'O': options->mode = PROGRAM_MODE_MANUAL; options->temp_set = atoi(value); 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': s = strchr(value, ':'); - if (s == NULL) { - fputs(_("Malformed temperature argument.\n"), stderr); - fputs(_("Try `-h' for more information.\n"), stderr); + if (!s) { + weprintf(_("Malformed temperature argument.")); + weprintf(_("Try `-h' for more information.")); return -1; } - *(s++) = '\0'; + *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(EXIT_SUCCESS); break; + case 'x': options->mode = PROGRAM_MODE_RESET; break; + case '?': - fputs(_("Try `-h' for more information.\n"), stderr); + weprintf(_("Try `-h' for more information.")); return -1; - break; } return 0; @@ -469,138 +455,102 @@ parse_command_line_option( /* Parse command line arguments. */ void -options_parse_args( - struct options *options, int argc, char *argv[], - const struct gamma_method *gamma_methods, - const struct location_provider *location_providers) +options_parse_args(struct options *options, int argc, char *argv[], const struct gamma_method *gamma_methods, + const struct location_provider *location_providers) { - int r, opt; - while ((opt = getopt(argc, argv, "b:c:g:hl:m:oO:pPrt:vVx")) != -1) { - r = parse_command_line_option(opt, optarg, options, argv0, - gamma_methods, location_providers); - if (r < 0) + int opt; + while ((opt = getopt(argc, argv, "b:c:g:hl:m:oO:pPrt:vVx")) != -1) + if (parse_command_line_option(opt, optarg, options, argv0, gamma_methods, location_providers) < 0) exit(EXIT_FAILURE); - } } /* Parse a single key-value pair from the configuration file. */ static int -parse_config_file_option( - const char *key, const char *value, struct options *options, - const struct gamma_method *gamma_methods, - const struct location_provider *location_providers) +parse_config_file_option(const char *key, const char *value, struct options *options, + const struct gamma_method *gamma_methods, const struct location_provider *location_providers) { - if (strcasecmp(key, "temp-day") == 0) { - if (options->scheme.day.temperature < 0) { + if (!strcasecmp(key, "temp-day")) { + if (options->scheme.day.temperature < 0) options->scheme.day.temperature = atoi(value); - } - } else if (strcasecmp(key, "temp-night") == 0) { - if (options->scheme.night.temperature < 0) { + } else if (!strcasecmp(key, "temp-night")) { + if (options->scheme.night.temperature < 0) options->scheme.night.temperature = atoi(value); - } - } else if (strcasecmp(key, "transition") == 0 || - strcasecmp(key, "fade") == 0) { - /* "fade" is preferred, "transition" is - deprecated as the setting key. */ - if (options->use_fade < 0) { + } 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") == 0) { - if (isnan(options->scheme.day.brightness)) { + } else if (!strcasecmp(key, "brightness")) { + if (isnan(options->scheme.day.brightness)) options->scheme.day.brightness = atof(value); - } - if (isnan(options->scheme.night.brightness)) { + if (isnan(options->scheme.night.brightness)) options->scheme.night.brightness = atof(value); - } - } else if (strcasecmp(key, "brightness-day") == 0) { - if (isnan(options->scheme.day.brightness)) { + } else if (!strcasecmp(key, "brightness-day")) { + if (isnan(options->scheme.day.brightness)) options->scheme.day.brightness = atof(value); - } - } else if (strcasecmp(key, "brightness-night") == 0) { - if (isnan(options->scheme.night.brightness)) { + } else if (!strcasecmp(key, "brightness-night")) { + if (isnan(options->scheme.night.brightness)) options->scheme.night.brightness = atof(value); - } - } else if (strcasecmp(key, "elevation-high") == 0) { + } else if (!strcasecmp(key, "elevation-high")) { options->scheme.high = atof(value); - } else if (strcasecmp(key, "elevation-low") == 0) { + } else if (!strcasecmp(key, "elevation-low")) { options->scheme.low = atof(value); - } else if (strcasecmp(key, "gamma") == 0) { + } else if (!strcasecmp(key, "gamma")) { if (isnan(options->scheme.day.gamma[0])) { - int r = parse_gamma_string( - value, options->scheme.day.gamma); - if (r < 0) { - fputs(_("Malformed gamma setting.\n"), stderr); + if (parse_gamma_string(value, options->scheme.day.gamma) < 0) { + weprintf(_("Malformed gamma setting.")); return -1; } - memcpy(options->scheme.night.gamma, - options->scheme.day.gamma, - sizeof(options->scheme.night.gamma)); + memcpy(options->scheme.night.gamma, options->scheme.day.gamma, sizeof(options->scheme.night.gamma)); } - } else if (strcasecmp(key, "gamma-day") == 0) { + } else if (!strcasecmp(key, "gamma-day")) { if (isnan(options->scheme.day.gamma[0])) { - int r = parse_gamma_string( - value, options->scheme.day.gamma); - if (r < 0) { - fputs(_("Malformed gamma setting.\n"), stderr); + if (parse_gamma_string(value, options->scheme.day.gamma) < 0) { + weprintf(_("Malformed gamma setting.")); return -1; } } - } else if (strcasecmp(key, "gamma-night") == 0) { + } else if (!strcasecmp(key, "gamma-night")) { if (isnan(options->scheme.night.gamma[0])) { - int r = parse_gamma_string( - value, options->scheme.night.gamma); - if (r < 0) { - fputs(_("Malformed gamma setting.\n"), stderr); + if (parse_gamma_string(value, options->scheme.night.gamma) < 0) { + weprintf(_("Malformed gamma setting.")); return -1; } } - } else if (strcasecmp(key, "preserve-gamma") == 0) { - if (options->preserve_gamma == 1) { + } else if (!strcasecmp(key, "preserve-gamma")) { + if (options->preserve_gamma == 1) options->preserve_gamma = !!atoi(value); - } - } else if (strcasecmp(key, "adjustment-method") == 0) { - if (options->method == NULL) { - options->method = find_gamma_method( - gamma_methods, value); - if (options->method == NULL) { - fprintf(stderr, _("Unknown adjustment" - " method `%s'.\n"), value); + } else if (!strcasecmp(key, "adjustment-method")) { + if (!options->method) { + options->method = find_gamma_method(gamma_methods, value); + if (!options->method) { + weprintf(_("Unknown adjustment method `%s'."), value); return -1; } } - } else if (strcasecmp(key, "location-provider") == 0) { - if (options->provider == NULL) { - options->provider = find_location_provider( - location_providers, value); - if (options->provider == NULL) { - fprintf(stderr, _("Unknown location" - " provider `%s'.\n"), value); + } else if (!strcasecmp(key, "location-provider")) { + if (!options->provider) { + options->provider = find_location_provider(location_providers, value); + if (!options->provider) { + weprintf(_("Unknown location provider `%s'."), value); return -1; } } - } else if (strcasecmp(key, "dawn-time") == 0) { + } else if (!strcasecmp(key, "dawn-time")) { if (options->scheme.dawn.start < 0) { - int r = parse_transition_range( - value, &options->scheme.dawn); - if (r < 0) { - fprintf(stderr, _("Malformed dawn-time" - " setting `%s'.\n"), value); + if (parse_transition_range(value, &options->scheme.dawn) < 0) { + weprintf(_("Malformed dawn-time setting `%s'."), value); return -1; } } - } else if (strcasecmp(key, "dusk-time") == 0) { + } else if (!strcasecmp(key, "dusk-time")) { if (options->scheme.dusk.start < 0) { - int r = parse_transition_range( - value, &options->scheme.dusk); - if (r < 0) { - fprintf(stderr, _("Malformed dusk-time" - " setting `%s'.\n"), value); + if (parse_transition_range(value, &options->scheme.dusk) < 0) { + weprintf(_("Malformed dusk-time setting `%s'."), value); return -1; } } } else { - fprintf(stderr, _("Unknown configuration setting `%s'.\n"), - key); + weprintf(_("Unknown configuration setting `%s'."), key); } return 0; @@ -609,8 +559,7 @@ parse_config_file_option( /* Parse options defined in the config file. */ void options_parse_config_file(struct options *options, struct config_ini_state *config_state, - const struct gamma_method *gamma_methods, - const struct location_provider *location_providers) + const struct gamma_method *gamma_methods, const struct location_provider *location_providers) { struct config_ini_section *section; struct config_ini_setting *setting; @@ -620,12 +569,9 @@ options_parse_config_file(struct options *options, struct config_ini_state *conf if (!section) return; - for (setting = section->settings; setting; setting = setting->next) { - int r = parse_config_file_option(setting->name, setting->value, options, - gamma_methods, location_providers); - if (r < 0) + for (setting = section->settings; setting; setting = setting->next) + if (parse_config_file_option(setting->name, setting->value, options, gamma_methods, location_providers) < 0) exit(EXIT_FAILURE); - } } /* Replace unspecified options with default values. */ diff --git a/src/pipeutils.c b/src/pipeutils.c index 9ca19b2..163da5e 100644 --- a/src/pipeutils.c +++ b/src/pipeutils.c @@ -49,11 +49,11 @@ pipeutils_create_nonblocking(int pipefds[2]) for (i = 0; i < 2; i++) { flags = fcntl(pipefds[0], F_GETFL); if (flags == -1) { - perror("fcntl F_GETFL:"); + weprintf("fcntl F_GETFL:"); goto fail; } if (fcntl(pipefds[0], F_SETFL, flags | O_NONBLOCK)) { - perror("fcntl F_SETFL +O_NONBLOCK:"); + weprintf("fcntl F_SETFL +O_NONBLOCK:"); goto fail; } } diff --git a/src/redshift.c b/src/redshift.c index d150f06..31a8fbd 100644 --- a/src/redshift.c +++ b/src/redshift.c @@ -497,8 +497,7 @@ run_continual_mode(const struct location_provider *provider, the values did not change. */ enum period prev_period = PERIOD_NONE; - if (signals_install_handlers()) - return -1; + signals_install_handlers(); /* Previous target color setting and current actual color setting. Actual color setting takes into account the current color fade. */ @@ -876,7 +875,7 @@ main(int argc, char *argv[]) if (options.mode != PROGRAM_MODE_RESET && options.mode != PROGRAM_MODE_MANUAL) { if (options.verbose) { - printf(_("Temperatures: %dK at day, %dK at night\n"), + printf(_("Temperatures: %iK at day, %iK at night\n"), options.scheme.day.temperature, options.scheme.night.temperature); } @@ -1134,7 +1133,8 @@ main(int argc, char *argv[]) options.method, method_state, options.use_fade, options.preserve_gamma, options.verbose); - if (r < 0) exit(EXIT_FAILURE); + if (r < 0) + exit(EXIT_FAILURE); } break; } diff --git a/src/signals.c b/src/signals.c index 492c90d..8b1152e 100644 --- a/src/signals.c +++ b/src/signals.c @@ -14,46 +14,57 @@ You should have received a copy of the GNU General Public License along with Redshift. If not, see . - Copyright (c) 2009-2015 Jon Lund Steffensen - Copyright (c) 2015 Mattias Andrée + Copyright (c) 2009-2015 Jon Lund Steffensen + Copyright (c) 2015, 2025 Mattias Andrée */ #include "common.h" -#ifndef WINDOWS - volatile sig_atomic_t exiting = 0; volatile sig_atomic_t disable = 0; -/* Signal handler for exit signals */ +/** + * Signal handlar for exit signals (SIGINT, SIGTERM, SIGQUIT) + * + * @param signo The received signal + */ static void sigexit(int signo) { exiting = 1; +#ifdef WINDOWS + signal(signo, &sigexit); +#endif (void) signo; } -/* Signal handler for disable signal */ + +/** + * Signal handlar for disable signal (SIGUSR1) + * + * @param signo The received signal + */ +#ifndef WINDOWS static void sigdisable(int signo) { disable = 1; (void) signo; } - -#else - -int disable = 0; -int exiting = 0; - #endif -int +void signals_install_handlers(void) { -#ifndef WINDOWS +#ifdef WINDOWS + if (signal(SIGINT, &sigexit) == SIG_ERR) + eprintf("signal SIGINT :"); + if (signal(SIGTERM, &sigexit) == SIG_ERR) + eprintf("signal SIGTERM :"); + +#else struct sigaction sigact; sigset_t sigset; @@ -62,35 +73,20 @@ signals_install_handlers(void) sigact.sa_mask = sigset; sigact.sa_flags = 0; - /* Install signal handler for INT, TERM, QUIT signals */ - sigact.sa_handler = sigexit; - if (sigaction(SIGINT, &sigact, NULL)) { - weprintf("sigaction SIGINT &{.sa_handler=, .sa_mask={}, .sa_flags=0} NULL:"); - return -1; - } - if (sigaction(SIGTERM, &sigact, NULL)) { - weprintf("sigaction SIGTERM &{.sa_handler=, .sa_mask={}, .sa_flags=0} NULL:"); - return -1; - } - if (sigaction(SIGQUIT, &sigact, NULL)) { - weprintf("sigaction SIGQUIT &{.sa_handler=, .sa_mask={}, .sa_flags=0} NULL:"); - return -1; - } - - /* Install signal handler for USR1 signal */ - sigact.sa_handler = sigdisable; - if (sigaction(SIGUSR1, &sigact, NULL)) { - weprintf("sigaction SIGUSR1 &{.sa_handler=, .sa_mask={}, .sa_flags=0} NULL:"); - return -1; - } - - /* Ignore CHLD signal. This causes child processes (hooks) to be reaped automatically. */ - sigact.sa_handler = SIG_IGN; - if (sigaction(SIGCHLD, &sigact, NULL)) { - weprintf("sigaction SIGCHLD &{.sa_handler=SIG_IGN, .sa_mask={}, .sa_flags=0} NULL:"); - return -1; - } + sigact.sa_handler = &sigexit; + if (sigaction(SIGINT, &sigact, NULL)) + eprintf("sigaction SIGINT &{.sa_handler=, .sa_mask={}, .sa_flags=0} NULL:"); + if (sigaction(SIGTERM, &sigact, NULL)) + eprintf("sigaction SIGTERM &{.sa_handler=, .sa_mask={}, .sa_flags=0} NULL:"); + if (sigaction(SIGQUIT, &sigact, NULL)) + eprintf("sigaction SIGQUIT &{.sa_handler=, .sa_mask={}, .sa_flags=0} NULL:"); + + sigact.sa_handler = &sigdisable; + if (sigaction(SIGUSR1, &sigact, NULL)) + eprintf("sigaction SIGUSR1 &{.sa_handler=, .sa_mask={}, .sa_flags=0} NULL:"); + + sigact.sa_handler = SIG_IGN; /* cause child processes (hooks) to be reaped automatically */ + if (sigaction(SIGCHLD, &sigact, NULL)) + eprintf("sigaction SIGCHLD &{.sa_handler=SIG_IGN, .sa_mask={}, .sa_flags=0} NULL:"); #endif - - return 0; } -- cgit v1.2.3-70-g09d2