aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2025-03-06 09:55:27 +0100
committerMattias Andrée <m@maandree.se>2025-03-06 09:55:27 +0100
commit4ff6090dfb9c947e894a7c1d0474b8d8d8f9031a (patch)
tree6c137a20f95f0be71d11940c6acb77e11aee6377 /src
parentFix warning (diff)
downloadredshift-ng-4ff6090dfb9c947e894a7c1d0474b8d8d8f9031a.tar.gz
redshift-ng-4ff6090dfb9c947e894a7c1d0474b8d8d8f9031a.tar.bz2
redshift-ng-4ff6090dfb9c947e894a7c1d0474b8d8d8f9031a.tar.xz
Style
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'src')
-rw-r--r--src/colorramp.c42
-rw-r--r--src/common.h29
-rw-r--r--src/config.mk2
-rw-r--r--src/gamma-drm.c32
-rw-r--r--src/gamma-dummy.c4
-rw-r--r--src/gamma-quartz.c125
-rw-r--r--src/gamma-randr.c187
-rw-r--r--src/gamma-vidmode.c93
-rw-r--r--src/gamma-w32gdi.c115
-rw-r--r--src/hooks.c28
-rw-r--r--src/location-geoclue2.c166
-rw-r--r--src/location-manual.c16
-rw-r--r--src/options.c75
-rw-r--r--src/pipeutils.c67
-rw-r--r--src/redshift.c435
-rw-r--r--src/signals.c25
-rw-r--r--src/solar.c3
-rw-r--r--src/systemtime.c4
18 files changed, 556 insertions, 892 deletions
diff --git a/src/colorramp.c b/src/colorramp.c
index 80f11fd..295bf41 100644
--- a/src/colorramp.c
+++ b/src/colorramp.c
@@ -20,22 +20,52 @@
*/
#include "common.h"
-/* Helper macro used in the fill functions */
-#define F(Y, WP, C) pow((Y) * setting->brightness * (WP), 1.0/setting->gamma[C])
+
+#if defined(__GNUC__)
+# pragma GCC diagnostic ignored "-Wfloat-equal"
+#endif
#define X(SUFFIX, TYPE, MAX, TRUE_MAX, DEPTH)\
+ /**
+ * Fill a gamma ramp
+ *
+ * @param ramp The gamma ramp
+ * @param size The gamma ramp size (number of stops)
+ * @param brightness The brightness (between 0 and 1) of the channel, which is
+ * the overall applied brightness multiplied but the effect
+ * on the channel from the colour temperature
+ * @param gamma The gamma to apply to the channel
+ */\
+ static void\
+ fill_ramp_##SUFFIX(TYPE *ramp, size_t size, double brightness, double gamma)\
+ {\
+ size_t i;\
+ double v;\
+ brightness /= size;\
+ if (gamma == 1.0) {\
+ brightness *= (MAX);\
+ for (i = 0; i < size; i++)\
+ ramp[i] = (TYPE)(i * brightness);\
+ } else {\
+ gamma = 1.0 / gamma;\
+ for (i = 0; i < size; i++) {\
+ v = pow(i * brightness, gamma) * (MAX);\
+ ramp[i] = (TYPE)v;\
+ }\
+ }\
+ }\
+ \
void\
colorramp_fill_##SUFFIX(TYPE *gamma_r, TYPE *gamma_g, TYPE *gamma_b,\
size_t size_r, size_t size_g, size_t size_b,\
const struct color_setting *setting)\
{\
double r = 1, g = 1, b = 1;\
- size_t i;\
libred_get_colour(setting->temperature, &r, &g, &b);\
- for (i = 0; i < size_r; i++) gamma_r[i] = F((double)i / size_r, r, 0) * (MAX);\
- for (i = 0; i < size_g; i++) gamma_g[i] = F((double)i / size_g, g, 1) * (MAX);\
- for (i = 0; i < size_b; i++) gamma_b[i] = F((double)i / size_b, b, 2) * (MAX);\
+ fill_ramp_##SUFFIX(gamma_r, size_r, setting->brightness * r, setting->gamma[0]);\
+ fill_ramp_##SUFFIX(gamma_g, size_g, setting->brightness * g, setting->gamma[1]);\
+ fill_ramp_##SUFFIX(gamma_b, size_b, setting->brightness * b, setting->gamma[2]);\
}
LIST_RAMPS_STOP_VALUE_TYPES(X,)
diff --git a/src/common.h b/src/common.h
index 83b15e8..170f97a 100644
--- a/src/common.h
+++ b/src/common.h
@@ -20,6 +20,9 @@
#ifndef REDSHIFT_COMMON_H
#define REDSHIFT_COMMON_H
+#include <libsimple.h>
+#include <libsimple-arg.h>
+
#ifndef WINDOWS
# if defined(__WIN32__) || defined(_WIN32)
# define WINDOWS
@@ -45,6 +48,7 @@
#endif
#ifdef WINDOWS
# include <windows.h>
+# define localtime_r(T, TM) localtime_s((TM), (T))
#else
# include <pwd.h>
# include <signal.h>
@@ -216,6 +220,17 @@ struct location_provider {
X(double, double, 1, 1, -2)
#define X(SUFFIX, TYPE, MAX, TRUE_MAX, DEPTH)\
+ /**
+ * Fill the gamma ramps
+ *
+ * @param gamma_r The gamma ramp for the red channel
+ * @param gamma_g The gamma ramp for the green channel
+ * @param gamma_b The gamma ramp for the blue channel
+ * @param size_r The number of stops in `gamma_r`
+ * @param size_g The number of stops in `gamma_g`
+ * @param size_b The number of stops in `gamma_b`
+ * @param settings The colour settings to apply (temperature, brightness, gamma)
+ */\
void colorramp_fill_##SUFFIX(TYPE *gamma_r, TYPE *gamma_g, TYPE *gamma_b,\
size_t size_r, size_t size_g, size_t size_b,\
const struct color_setting *setting)
@@ -232,14 +247,12 @@ struct config_ini_section *config_ini_get_section(struct config_ini_state *state
void options_init(struct options *options);
-void options_parse_args(
- struct options *options, int argc, char *argv[],
- const struct gamma_method *gamma_methods,
- const struct location_provider *location_providers);
-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);
+void options_parse_args(struct options *options, int argc, char *argv[],
+ const struct gamma_method *gamma_methods,
+ const struct location_provider *location_providers);
+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);
void options_set_defaults(struct options *options);
diff --git a/src/config.mk b/src/config.mk
index a473366..f50e741 100644
--- a/src/config.mk
+++ b/src/config.mk
@@ -20,4 +20,4 @@ CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_GNU_SOURCE\
-DENABLE_DRM -DENABLE_GEOCLUE2 -DENABLE_RANDR -DENABLE_VIDMODE\
-DENABLE_COOPGAMMA -D'PACKAGE="$(PACKAGE)"'
CFLAGS = $$($(PKGCONFIG_CFLAGS) $(LIBS_PKGCONFIG))
-LDFLAGS = $$($(PKGCONFIG_LDFLAGS) $(LIBS_PKGCONFIG)) -lm -lcoopgamma -lred
+LDFLAGS = $$($(PKGCONFIG_LDFLAGS) $(LIBS_PKGCONFIG)) -lm -lcoopgamma -lred -lsimple
diff --git a/src/gamma-drm.c b/src/gamma-drm.c
index 8c7bcec..a4cc75e 100644
--- a/src/gamma-drm.c
+++ b/src/gamma-drm.c
@@ -160,7 +160,7 @@ drm_start(struct gamma_state *state, enum program_mode mode)
crtcs->r_gamma = calloc(3 * crtcs->gamma_size, sizeof(uint16_t));
crtcs->g_gamma = crtcs->r_gamma + crtcs->gamma_size;
crtcs->b_gamma = crtcs->g_gamma + crtcs->gamma_size;
- if (crtcs->r_gamma != NULL) {
+ if (crtcs->r_gamma) {
int r = drmModeCrtcGetGamma(state->fd, crtcs->crtc_id, crtcs->gamma_size,
crtcs->r_gamma, crtcs->g_gamma, crtcs->b_gamma);
if (r < 0) {
@@ -171,7 +171,7 @@ drm_start(struct gamma_state *state, enum program_mode mode)
crtcs->r_gamma = NULL;
}
} else {
- perror("malloc");
+ weprintf("malloc:");
drmModeFreeResources(state->res);
state->res = NULL;
close(state->fd);
@@ -192,7 +192,7 @@ drm_restore(struct gamma_state *state)
{
struct drm_crtc_state *crtcs = state->crtcs;
while (crtcs->crtc_num >= 0) {
- if (crtcs->r_gamma != NULL) {
+ if (crtcs->r_gamma) {
drmModeCrtcSetGamma(state->fd, crtcs->crtc_id, crtcs->gamma_size,
crtcs->r_gamma, crtcs->g_gamma, crtcs->b_gamma);
}
@@ -203,7 +203,7 @@ drm_restore(struct gamma_state *state)
static void
drm_free(struct gamma_state *state)
{
- if (state->crtcs != NULL) {
+ if (state->crtcs) {
struct drm_crtc_state *crtcs = state->crtcs;
while (crtcs->crtc_num >= 0) {
free(crtcs->r_gamma);
@@ -213,7 +213,7 @@ drm_free(struct gamma_state *state)
free(state->crtcs);
state->crtcs = NULL;
}
- if (state->res != NULL) {
+ if (state->res) {
drmModeFreeResources(state->res);
state->res = NULL;
}
@@ -241,16 +241,16 @@ drm_print_help(FILE *f)
static int
drm_set_option(struct gamma_state *state, const char *key, const char *value)
{
- if (strcasecmp(key, "card") == 0) {
+ if (!strcasecmp(key, "card")) {
state->card_num = atoi(value);
- } else if (strcasecmp(key, "crtc") == 0) {
+ } else if (!strcasecmp(key, "crtc")) {
state->crtc_num = atoi(value);
if (state->crtc_num < 0) {
- fprintf(stderr, _("CRTC must be a non-negative integer\n"));
+ weprintf(_("CRTC must be a non-negative integer\n"));
return -1;
}
} else {
- fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
+ weprintf(_("Unknown method parameter: `%s'.\n"), key);
return -1;
}
@@ -275,18 +275,10 @@ drm_set_temperature(
if (crtcs->gamma_size <= 1)
continue;
if (crtcs->gamma_size != last_gamma_size) {
- if (last_gamma_size == 0) {
- r_gamma = malloc(3 * crtcs->gamma_size * sizeof(uint16_t));
+ if (crtcs->gamma_size > last_gamma_size) {
+ r_gamma = erealloc(r_gamma, 3 * crtcs->gamma_size * sizeof(uint16_t));
g_gamma = r_gamma + crtcs->gamma_size;
b_gamma = g_gamma + crtcs->gamma_size;
- } else if (crtcs->gamma_size > last_gamma_size) {
- r_gamma = realloc(r_gamma, 3 * crtcs->gamma_size * sizeof(uint16_t));
- g_gamma = r_gamma + crtcs->gamma_size;
- b_gamma = g_gamma + crtcs->gamma_size;
- }
- if (r_gamma == NULL) {
- perror(last_gamma_size == 0 ? "malloc" : "realloc");
- return -1;
}
last_gamma_size = crtcs->gamma_size;
}
@@ -294,7 +286,7 @@ drm_set_temperature(
/* Initialize gamma ramps to pure state */
ramp_size = crtcs->gamma_size;
for (i = 0; i < ramp_size; i++) {
- value = (uint16_t)((double)i/ramp_size * (UINT16_MAX+1));
+ value = (uint16_t)((double)i / (ramp_size - 1) * UINT16_MAX);
r_gamma[i] = value;
g_gamma[i] = value;
b_gamma[i] = value;
diff --git a/src/gamma-dummy.c b/src/gamma-dummy.c
index d01044d..39f09a2 100644
--- a/src/gamma-dummy.c
+++ b/src/gamma-dummy.c
@@ -31,7 +31,7 @@ gamma_dummy_start(struct gamma_state *state, enum program_mode mode)
{
(void) state;
(void) mode;
- fputs(_("WARNING: Using dummy gamma method! Display will not be affected by this gamma method.\n"), stderr);
+ weprintf(_("WARNING: Using dummy gamma method! Display will not be affected by this gamma method.\n"));
return 0;
}
@@ -59,7 +59,7 @@ gamma_dummy_set_option(struct gamma_state *state, const char *key, const char *v
{
(void) state;
(void) value;
- fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
+ weprintf(_("Unknown method parameter: `%s'.\n"), key);
return -1;
}
diff --git a/src/gamma-quartz.c b/src/gamma-quartz.c
index c8e39d6..30a50d6 100644
--- a/src/gamma-quartz.c
+++ b/src/gamma-quartz.c
@@ -37,53 +37,40 @@ struct gamma_state {
static int
quartz_init(struct gamma_state **state)
{
- *state = malloc(sizeof(struct gamma_state));
- if (*state == NULL) return -1;
-
- struct gamma_state *s = *state;
- s->displays = NULL;
-
+ *state = emalloc(sizeof(**state));
+ (*state)->displays = NULL;
return 0;
}
static int
quartz_start(struct gamma_state *state, program_mode_t mode)
{
+ float *gamma_r, *gamma_g, *gamma_b;
+ uint32_t i, display_count, ramp_size, sample_count;
+ CGDirectDisplayID *displays, display;
CGError error;
- uint32_t display_count;
/* Get display count */
error = CGGetOnlineDisplayList(0, NULL, &display_count);
- if (error != kCGErrorSuccess) return -1;
+ if (error != kCGErrorSuccess)
+ return -1;
state->display_count = display_count;
- CGDirectDisplayID* displays =
- malloc(sizeof(CGDirectDisplayID)*display_count);
- if (displays == NULL) {
- perror("malloc");
- return -1;
- }
+ displays = emalloc(sizeof(CGDirectDisplayID) * display_count);
/* Get list of displays */
- error = CGGetOnlineDisplayList(display_count, displays,
- &display_count);
+ error = CGGetOnlineDisplayList(display_count, displays, &display_count);
if (error != kCGErrorSuccess) {
free(displays);
return -1;
}
/* Allocate list of display state */
- state->displays = malloc(display_count *
- sizeof(struct quartz_display_state));
- if (state->displays == NULL) {
- perror("malloc");
- free(displays);
- return -1;
- }
+ state->displays = emalloc(display_count * sizeof(struct quartz_display_state));
/* Copy display indentifiers to display state */
- for (int i = 0; i < display_count; i++) {
+ for (i = 0; i < display_count; i++) {
state->displays[i].display = displays[i];
state->displays[i].saved_ramps = NULL;
}
@@ -91,39 +78,28 @@ quartz_start(struct gamma_state *state, program_mode_t mode)
free(displays);
/* Save gamma ramps for all displays in display state */
- for (int i = 0; i < display_count; i++) {
- CGDirectDisplayID display = state->displays[i].display;
+ for (i = 0; i < display_count; i++) {
+ display = state->displays[i].display;
- uint32_t ramp_size = CGDisplayGammaTableCapacity(display);
- if (ramp_size == 0) {
- fprintf(stderr, _("Gamma ramp size too small: %i\n"),
- ramp_size);
+ ramp_size = CGDisplayGammaTableCapacity(display);
+ if (!ramp_size) {
+ weprintf(_("Gamma ramp size too small: %i\n"), ramp_size);
return -1;
}
state->displays[i].ramp_size = ramp_size;
/* Allocate space for saved ramps */
- state->displays[i].saved_ramps =
- malloc(3 * ramp_size * sizeof(float));
- if (state->displays[i].saved_ramps == NULL) {
- perror("malloc");
- return -1;
- }
+ state->displays[i].saved_ramps = emalloc(3 * ramp_size * sizeof(float));
- float *gamma_r = &state->displays[i].saved_ramps[0*ramp_size];
- float *gamma_g = &state->displays[i].saved_ramps[1*ramp_size];
- float *gamma_b = &state->displays[i].saved_ramps[2*ramp_size];
+ gamma_r = &state->displays[i].saved_ramps[0 * ramp_size];
+ gamma_g = &state->displays[i].saved_ramps[1 * ramp_size];
+ gamma_b = &state->displays[i].saved_ramps[2 * ramp_size];
/* Copy the ramps to allocated space */
- uint32_t sample_count;
- error = CGGetDisplayTransferByTable(display, ramp_size,
- gamma_r, gamma_g, gamma_b,
- &sample_count);
- if (error != kCGErrorSuccess ||
- sample_count != ramp_size) {
- fputs(_("Unable to save current gamma ramp.\n"),
- stderr);
+ 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"));
return -1;
}
}
@@ -140,11 +116,10 @@ quartz_restore(struct gamma_state *state)
static void
quartz_free(struct gamma_state *state)
{
- if (state->displays != NULL) {
- for (int i = 0; i < state->display_count; i++) {
+ uint32_t i;
+ if (state->displays)
+ for (i = 0; i < state->display_count; i++)
free(state->displays[i].saved_ramps);
- }
- }
free(state->displays);
free(state);
}
@@ -159,13 +134,11 @@ quartz_print_help(FILE *f)
static int
quartz_set_option(struct gamma_state *state, const char *key, const char *value)
{
- if (strcasecmp(key, "preserve") == 0) {
- fprintf(stderr, _("Parameter `%s` is now always on; "
- " Use the `%s` command-line option"
- " to disable.\n"),
- key, "-P");
+ if (!strcasecmp(key, "preserve")) {
+ weprintf(_("Parameter `%s` is now always on; Use the `%s`"
+ " command-line option to disable.\n"), key, "-P");
} else {
- fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
+ weprintf(_("Unknown method parameter: `%s'.\n"), key);
return -1;
}
@@ -173,44 +146,38 @@ quartz_set_option(struct gamma_state *state, const char *key, const char *value)
}
static void
-quartz_set_temperature_for_display(
- struct gamma_state *state, int display_index,
- const color_setting_t *setting, int preserve)
+quartz_set_temperature_for_display(struct gamma_state *state, int display_index,
+ const color_setting_t *setting, int preserve)
{
+ float *gamma_ramps, *gamma_r, *gamma_g, *gamma_b, value;
CGDirectDisplayID display = state->displays[display_index].display;
uint32_t ramp_size = state->displays[display_index].ramp_size;
+ CGError error;
+ uint32_t i;
/* Create new gamma ramps */
- float *gamma_ramps = malloc(3*ramp_size*sizeof(float));
- if (gamma_ramps == NULL) {
- perror("malloc");
- return;
- }
+ gamma_ramps = emalloc(3 * ramp_size * sizeof(float));
- float *gamma_r = &gamma_ramps[0*ramp_size];
- float *gamma_g = &gamma_ramps[1*ramp_size];
- float *gamma_b = &gamma_ramps[2*ramp_size];
+ gamma_r = &gamma_ramps[0 * ramp_size];
+ gamma_g = &gamma_ramps[1 * ramp_size];
+ gamma_b = &gamma_ramps[2 * ramp_size];
if (preserve) {
/* Initialize gamma ramps from saved state */
- memcpy(gamma_ramps, state->displays[display_index].saved_ramps,
- 3*ramp_size*sizeof(float));
+ memcpy(gamma_ramps, state->displays[display_index].saved_ramps, 3 * ramp_size * sizeof(float));
} else {
/* Initialize gamma ramps to pure state */
- for (int i = 0; i < ramp_size; i++) {
- float value = (double)i/ramp_size;
+ for (i = 0; i < ramp_size; i++) {
+ value = (float)i / ramp_size;
gamma_r[i] = value;
gamma_g[i] = value;
gamma_b[i] = value;
}
}
- colorramp_fill_float(gamma_r, gamma_g, gamma_b, ramp_size,
- ramp_size, ramp_size, setting);
+ colorramp_fill_float(gamma_r, gamma_g, gamma_b, ramp_size, ramp_size, ramp_size, setting);
- CGError error =
- CGSetDisplayTransferByTable(display, ramp_size,
- gamma_r, gamma_g, gamma_b);
+ error = CGSetDisplayTransferByTable(display, ramp_size, gamma_r, gamma_g, gamma_b);
if (error != kCGErrorSuccess) {
free(gamma_ramps);
return;
@@ -223,7 +190,9 @@ static int
quartz_set_temperature(
struct gamma_state *state, const color_setting_t *setting, int preserve)
{
- for (int i = 0; i < state->display_count; i++) {
+ uint32_t i;
+
+ for (i = 0; i < state->display_count; i++) {
quartz_set_temperature_for_display(
state, i, setting, preserve);
}
diff --git a/src/gamma-randr.c b/src/gamma-randr.c
index 6c00625..c8ea754 100644
--- a/src/gamma-randr.c
+++ b/src/gamma-randr.c
@@ -42,7 +42,7 @@ struct gamma_state {
int preferred_screen;
int screen_num;
int crtc_num_count;
- int* crtc_num;
+ int *crtc_num;
unsigned int crtc_count;
struct randr_crtc_state *crtcs;
};
@@ -79,8 +79,7 @@ randr_init(struct gamma_state **state)
Apparently, we have to check both to avoid seg faults. */
if (error || ver_reply == NULL) {
int ec = (error != 0) ? error->error_code : -1;
- fprintf(stderr, _("`%s' returned error %d\n"),
- "RANDR Query Version", ec);
+ weprintf(_("`%s' returned error %d\n"), "RANDR Query Version", ec);
xcb_disconnect(s->conn);
free(s);
return -1;
@@ -88,8 +87,8 @@ randr_init(struct gamma_state **state)
if (ver_reply->major_version != RANDR_VERSION_MAJOR ||
ver_reply->minor_version < RANDR_VERSION_MINOR) {
- fprintf(stderr, _("Unsupported RANDR version (%u.%u)\n"),
- ver_reply->major_version, ver_reply->minor_version);
+ weprintf(_("Unsupported RANDR version (%u.%u)\n"),
+ ver_reply->major_version, ver_reply->minor_version);
free(ver_reply);
xcb_disconnect(s->conn);
free(s);
@@ -115,7 +114,8 @@ randr_start(struct gamma_state *state, enum program_mode mode)
(void) mode;
screen_num = state->screen_num;
- if (screen_num < 0) screen_num = state->preferred_screen;
+ if (screen_num < 0)
+ screen_num = state->preferred_screen;
/* Get screen */
setup = xcb_get_setup(state->conn);
@@ -130,42 +130,28 @@ randr_start(struct gamma_state *state, enum program_mode mode)
xcb_screen_next(&iter);
}
- if (state->screen == NULL) {
- fprintf(stderr, _("Screen %i could not be found.\n"),
- screen_num);
+ if (!state->screen) {
+ weprintf(_("Screen %i could not be found.\n"), screen_num);
return -1;
}
/* Get list of CRTCs for the screen */
- res_cookie =
- xcb_randr_get_screen_resources_current(state->conn,
- state->screen->root);
- res_reply =
- xcb_randr_get_screen_resources_current_reply(state->conn,
- res_cookie,
- &error);
+ res_cookie = xcb_randr_get_screen_resources_current(state->conn, state->screen->root);
+ res_reply = xcb_randr_get_screen_resources_current_reply(state->conn, res_cookie, &error);
if (error) {
- fprintf(stderr, _("`%s' returned error %d\n"),
- "RANDR Get Screen Resources Current",
- error->error_code);
+ weprintf(_("`%s' returned error %d\n"), "RANDR Get Screen Resources Current", error->error_code);
return -1;
}
state->crtc_count = res_reply->num_crtcs;
- state->crtcs = calloc(state->crtc_count, sizeof(struct randr_crtc_state));
- if (state->crtcs == NULL) {
- perror("malloc");
- state->crtc_count = 0;
- return -1;
- }
+ state->crtcs = ecalloc(state->crtc_count, sizeof(struct randr_crtc_state));
crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply);
/* Save CRTC identifier in state */
- for (i = 0; i < state->crtc_count; i++) {
+ for (i = 0; i < state->crtc_count; i++)
state->crtcs[i].crtc = crtcs[i];
- }
free(res_reply);
@@ -186,9 +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) {
- fprintf(stderr, _("`%s' returned error %d\n"),
- "RANDR Get CRTC Gamma Size",
- error->error_code);
+ weprintf(_("`%s' returned error %d\n"), "RANDR Get CRTC Gamma Size", error->error_code);
return -1;
}
@@ -198,8 +182,7 @@ randr_start(struct gamma_state *state, enum program_mode mode)
free(gamma_size_reply);
if (ramp_size == 0) {
- fprintf(stderr, _("Gamma ramp size too small: %i\n"),
- ramp_size);
+ weprintf(_("Gamma ramp size too small: %i\n"), ramp_size);
return -1;
}
@@ -208,8 +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) {
- fprintf(stderr, _("`%s' returned error %d\n"),
- "RANDR Get CRTC Gamma", error->error_code);
+ weprintf(_("`%s' returned error %d\n"), "RANDR Get CRTC Gamma", error->error_code);
return -1;
}
@@ -218,21 +200,12 @@ randr_start(struct gamma_state *state, enum program_mode mode)
gamma_b = xcb_randr_get_crtc_gamma_blue(gamma_get_reply);
/* Allocate space for saved gamma ramps */
- state->crtcs[i].saved_ramps =
- malloc(3*ramp_size*sizeof(uint16_t));
- if (state->crtcs[i].saved_ramps == NULL) {
- perror("malloc");
- free(gamma_get_reply);
- return -1;
- }
+ state->crtcs[i].saved_ramps = emalloc(3 * ramp_size * sizeof(uint16_t));
/* Copy gamma ramps into CRTC state */
- memcpy(&state->crtcs[i].saved_ramps[0*ramp_size], gamma_r,
- ramp_size*sizeof(uint16_t));
- memcpy(&state->crtcs[i].saved_ramps[1*ramp_size], gamma_g,
- ramp_size*sizeof(uint16_t));
- memcpy(&state->crtcs[i].saved_ramps[2*ramp_size], gamma_b,
- ramp_size*sizeof(uint16_t));
+ memcpy(&state->crtcs[i].saved_ramps[0 * ramp_size], gamma_r, ramp_size * sizeof(uint16_t));
+ memcpy(&state->crtcs[i].saved_ramps[1 * ramp_size], gamma_g, ramp_size * sizeof(uint16_t));
+ memcpy(&state->crtcs[i].saved_ramps[2 * ramp_size], gamma_b, ramp_size * sizeof(uint16_t));
free(gamma_get_reply);
}
@@ -244,9 +217,10 @@ static void
randr_restore(struct gamma_state *state)
{
xcb_generic_error_t *error;
+ int i;
/* Restore CRTC gamma ramps */
- for (int i = 0; i < state->crtc_count; i++) {
+ for (i = 0; i < state->crtc_count; i++) {
xcb_randr_crtc_t crtc = state->crtcs[i].crtc;
unsigned int ramp_size = state->crtcs[i].ramp_size;
@@ -262,9 +236,8 @@ randr_restore(struct gamma_state *state)
error = xcb_request_check(state->conn, gamma_set_cookie);
if (error) {
- fprintf(stderr, _("`%s' returned error %d\n"),
- "RANDR Set CRTC Gamma", error->error_code);
- fprintf(stderr, _("Unable to restore CRTC %i\n"), i);
+ weprintf(_("`%s' returned error %d\n"), "RANDR Set CRTC Gamma", error->error_code);
+ weprintf(_("Unable to restore CRTC %i\n"), i);
}
}
}
@@ -272,10 +245,11 @@ randr_restore(struct gamma_state *state)
static void
randr_free(struct gamma_state *state)
{
+ int i;
+
/* Free CRTC state */
- for (int i = 0; i < state->crtc_count; i++) {
+ for (i = 0; i < state->crtc_count; i++)
free(state->crtcs[i].saved_ramps);
- }
free(state->crtcs);
free(state->crtc_num);
@@ -303,9 +277,9 @@ randr_print_help(FILE *f)
static int
randr_set_option(struct gamma_state *state, const char *key, const char *value)
{
- if (strcasecmp(key, "screen") == 0) {
+ if (!strcasecmp(key, "screen")) {
state->screen_num = atoi(value);
- } else if (strcasecmp(key, "crtc") == 0) {
+ } else if (!strcasecmp(key, "crtc")) {
char *tail;
int i, parsed;
@@ -314,21 +288,17 @@ randr_set_option(struct gamma_state *state, const char *key, const char *value)
for (;;) {
errno = 0;
parsed = strtol(local_value, &tail, 0);
- if (parsed == 0 && (errno != 0 ||
- tail == local_value)) {
- fprintf(stderr, _("Unable to read screen"
- " number: `%s'.\n"), value);
+ if (parsed == 0 && (errno != 0 || tail == local_value)) {
+ weprintf(_("Unable to read screen number: `%s'.\n"), value);
return -1;
- } else {
- state->crtc_num_count += 1;
}
+ state->crtc_num_count += 1;
local_value = tail;
- if (*local_value == ',') {
+ if (*local_value == ',')
local_value += 1;
- } else if (*local_value == '\0') {
+ else if (!*local_value)
break;
- }
}
/* Configure all given crtcs */
@@ -337,27 +307,21 @@ randr_set_option(struct gamma_state *state, const char *key, const char *value)
for (i = 0; i < state->crtc_num_count; i++) {
errno = 0;
parsed = strtol(local_value, &tail, 0);
- if (parsed == 0 && (errno != 0 ||
- tail == local_value)) {
+ if (parsed == 0 && (errno != 0 || tail == local_value))
return -1;
- } else {
- state->crtc_num[i] = parsed;
- }
+ state->crtc_num[i] = parsed;
local_value = tail;
- if (*local_value == ',') {
+ if (*local_value == ',')
local_value += 1;
- } else if (*local_value == '\0') {
+ else if (!*local_value)
break;
- }
}
} else if (strcasecmp(key, "preserve") == 0) {
- fprintf(stderr, _("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.\n"), key, "-P");
} else {
- fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
+ weprintf(_("Unknown method parameter: `%s'.\n"), key);
return -1;
}
@@ -365,9 +329,8 @@ randr_set_option(struct gamma_state *state, const char *key, const char *value)
}
static int
-randr_set_temperature_for_crtc(
- struct gamma_state *state, int crtc_num, const struct color_setting *setting,
- int preserve)
+randr_set_temperature_for_crtc(struct gamma_state *state, int crtc_num,
+ const struct color_setting *setting, int preserve)
{
xcb_randr_crtc_t crtc;
xcb_void_cookie_t gamma_set_cookie;
@@ -376,14 +339,11 @@ randr_set_temperature_for_crtc(
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);
- if (state->crtc_count > 1) {
- fprintf(stderr, _("Valid CRTCs are [0-%d].\n"),
- state->crtc_count-1);
- } else {
+ fprintf(stderr, _("CRTC %d does not exist. "), crtc_num);
+ if (state->crtc_count > 1)
+ fprintf(stderr, _("Valid CRTCs are [0-%d].\n"), state->crtc_count-1);
+ else
fprintf(stderr, _("Only CRTC 0 exists.\n"));
- }
return -1;
}
@@ -392,24 +352,20 @@ randr_set_temperature_for_crtc(
ramp_size = state->crtcs[crtc_num].ramp_size;
/* Create new gamma ramps */
- gamma_ramps = malloc(3*ramp_size*sizeof(uint16_t));
- if (gamma_ramps == NULL) {
- perror("malloc");
- return -1;
- }
+ gamma_ramps = emalloc(3 * ramp_size * sizeof(uint16_t));
- gamma_r = &gamma_ramps[0*ramp_size];
- gamma_g = &gamma_ramps[1*ramp_size];
- gamma_b = &gamma_ramps[2*ramp_size];
+ gamma_r = &gamma_ramps[0 * ramp_size];
+ gamma_g = &gamma_ramps[1 * ramp_size];
+ gamma_b = &gamma_ramps[2 * ramp_size];
if (preserve) {
/* Initialize gamma ramps from saved state */
memcpy(gamma_ramps, state->crtcs[crtc_num].saved_ramps,
- 3*ramp_size*sizeof(uint16_t));
+ 3 * ramp_size * sizeof(uint16_t));
} else {
/* Initialize gamma ramps to pure state */
for (i = 0; i < ramp_size; i++) {
- value = (double)i/ramp_size * (UINT16_MAX+1);
+ value = (double)i / (ramp_size - 1) * UINT16_MAX;
gamma_r[i] = value;
gamma_g[i] = value;
gamma_b[i] = value;
@@ -420,15 +376,12 @@ randr_set_temperature_for_crtc(
ramp_size, ramp_size, setting);
/* Set new gamma ramps */
- gamma_set_cookie =
- xcb_randr_set_crtc_gamma_checked(state->conn, crtc,
- ramp_size, gamma_r,
- gamma_g, gamma_b);
+ gamma_set_cookie = xcb_randr_set_crtc_gamma_checked(state->conn, crtc, ramp_size,
+ gamma_r, gamma_g, gamma_b);
error = xcb_request_check(state->conn, gamma_set_cookie);
if (error) {
- fprintf(stderr, _("`%s' returned error %d\n"),
- "RANDR Set CRTC Gamma", error->error_code);
+ weprintf(_("`%s' returned error %d\n"), "RANDR Set CRTC Gamma", error->error_code);
free(gamma_ramps);
return -1;
}
@@ -439,24 +392,22 @@ randr_set_temperature_for_crtc(
}
static int
-randr_set_temperature(
- struct gamma_state *state, const struct color_setting *setting, int preserve)
+randr_set_temperature(struct gamma_state *state, const struct color_setting *setting, int preserve)
{
- int r;
-
- /* If no CRTC numbers have been specified,
- set temperature on all CRTCs. */
- if (state->crtc_num_count == 0) {
- for (int i = 0; i < state->crtc_count; i++) {
- r = randr_set_temperature_for_crtc(
- state, i, setting, preserve);
- if (r < 0) return -1;
+ int i, r;
+
+ /* If no CRTC numbers have been specified, set temperature on all CRTCs. */
+ if (!state->crtc_num_count) {
+ for (i = 0; i < state->crtc_count; i++) {
+ r = randr_set_temperature_for_crtc(state, i, setting, preserve);
+ if (r < 0)
+ return -1;
}
} else {
- for (int i = 0; i < state->crtc_num_count; ++i) {
- r = randr_set_temperature_for_crtc(
- state, state->crtc_num[i], setting, preserve);
- if (r < 0) return -1;
+ for (i = 0; i < state->crtc_num_count; ++i) {
+ r = randr_set_temperature_for_crtc(state, state->crtc_num[i], setting, preserve);
+ if (r < 0)
+ return -1;
}
}
diff --git a/src/gamma-vidmode.c b/src/gamma-vidmode.c
index b462480..1fda89a 100644
--- a/src/gamma-vidmode.c
+++ b/src/gamma-vidmode.c
@@ -36,17 +36,15 @@ vidmode_init(struct gamma_state **state)
{
struct gamma_state *s;
- *state = malloc(sizeof(struct gamma_state));
- if (*state == NULL) return -1;
+ s = *state = emalloc(sizeof(struct gamma_state));
- s = *state;
s->screen_num = -1;
s->saved_ramps = NULL;
/* Open display */
s->display = XOpenDisplay(NULL);
- if (s->display == NULL) {
- fprintf(stderr, _("X request failed: %s\n"), "XOpenDisplay");
+ if (!s->display) {
+ weprintf(_("X request failed: %s\n"), "XOpenDisplay");
return -1;
}
@@ -63,38 +61,31 @@ vidmode_start(struct gamma_state *state, enum program_mode mode)
(void) mode;
- if (screen_num < 0) screen_num = DefaultScreen(state->display);
+ if (screen_num < 0)
+ screen_num = DefaultScreen(state->display);
state->screen_num = screen_num;
/* Query extension version */
r = XF86VidModeQueryVersion(state->display, &major, &minor);
if (!r) {
- fprintf(stderr, _("X request failed: %s\n"),
- "XF86VidModeQueryVersion");
+ weprintf(_("X request failed: %s\n"), "XF86VidModeQueryVersion");
return -1;
}
/* Request size of gamma ramps */
- r = XF86VidModeGetGammaRampSize(state->display, state->screen_num,
- &state->ramp_size);
+ r = XF86VidModeGetGammaRampSize(state->display, state->screen_num, &state->ramp_size);
if (!r) {
- fprintf(stderr, _("X request failed: %s\n"),
- "XF86VidModeGetGammaRampSize");
+ weprintf(_("X request failed: %s\n"), "XF86VidModeGetGammaRampSize");
return -1;
}
- if (state->ramp_size == 0) {
- fprintf(stderr, _("Gamma ramp size too small: %i\n"),
- state->ramp_size);
+ if (!state->ramp_size) {
+ weprintf(_("Gamma ramp size too small: %i\n"), state->ramp_size);
return -1;
}
/* Allocate space for saved gamma ramps */
- state->saved_ramps = malloc(3*state->ramp_size*sizeof(uint16_t));
- if (state->saved_ramps == NULL) {
- perror("malloc");
- return -1;
- }
+ state->saved_ramps = emalloc(3 * state->ramp_size * sizeof(uint16_t));
gamma_r = &state->saved_ramps[0 * state->ramp_size];
gamma_g = &state->saved_ramps[1 * state->ramp_size];
@@ -102,11 +93,9 @@ vidmode_start(struct gamma_state *state, enum program_mode mode)
/* Save current gamma ramps so we can restore them at program exit. */
r = XF86VidModeGetGammaRamp(state->display, state->screen_num,
- state->ramp_size, gamma_r, gamma_g,
- gamma_b);
+ state->ramp_size, gamma_r, gamma_g, gamma_b);
if (!r) {
- fprintf(stderr, _("X request failed: %s\n"),
- "XF86VidModeGetGammaRamp");
+ weprintf(_("X request failed: %s\n"), "XF86VidModeGetGammaRamp");
return -1;
}
@@ -131,25 +120,21 @@ vidmode_print_help(FILE *f)
fputs(_("Adjust gamma ramps with the X VidMode extension.\n"), f);
fputs("\n", f);
- /* TRANSLATORS: VidMode help output
- left column must not be translated */
- fputs(_(" screen=N\t\tX screen to apply adjustments to\n"),
- f);
+ /* TRANSLATORS: VidMode help output left column must not be translated */
+ fputs(_(" screen=N\t\tX screen to apply adjustments to\n"), f);
fputs("\n", f);
}
static int
vidmode_set_option(struct gamma_state *state, const char *key, const char *value)
{
- if (strcasecmp(key, "screen") == 0) {
+ if (!strcasecmp(key, "screen")) {
state->screen_num = atoi(value);
- } else if (strcasecmp(key, "preserve") == 0) {
- fprintf(stderr, _("Parameter `%s` is now always on; "
- " Use the `%s` command-line option"
- " to disable.\n"),
- key, "-P");
+ } else if (!strcasecmp(key, "preserve")) {
+ weprintf(_("Parameter `%s` is now always on; Use the `%s` "
+ "command-line option to disable.\n"), key, "-P");
} else {
- fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
+ weprintf(_("Unknown method parameter: `%s'.\n"), key);
return -1;
}
@@ -159,33 +144,27 @@ vidmode_set_option(struct gamma_state *state, const char *key, const char *value
static void
vidmode_restore(struct gamma_state *state)
{
- uint16_t *gamma_r = &state->saved_ramps[0*state->ramp_size];
- uint16_t *gamma_g = &state->saved_ramps[1*state->ramp_size];
- uint16_t *gamma_b = &state->saved_ramps[2*state->ramp_size];
+ uint16_t *gamma_r = &state->saved_ramps[0 * state->ramp_size];
+ uint16_t *gamma_g = &state->saved_ramps[1 * state->ramp_size];
+ uint16_t *gamma_b = &state->saved_ramps[2 * state->ramp_size];
+ int r;
/* Restore gamma ramps */
- int r = XF86VidModeSetGammaRamp(state->display, state->screen_num,
- state->ramp_size, gamma_r, gamma_g,
- gamma_b);
- if (!r) {
- fprintf(stderr, _("X request failed: %s\n"),
- "XF86VidModeSetGammaRamp");
- }
+ 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");
}
static int
vidmode_set_temperature(
struct gamma_state *state, const struct color_setting *setting, int preserve)
{
- int r, i;
uint16_t value, *gamma_ramps, *gamma_r, *gamma_g, *gamma_b;
+ int r, i;
/* Create new gamma ramps */
- gamma_ramps = malloc(3*state->ramp_size*sizeof(uint16_t));
- if (gamma_ramps == NULL) {
- perror("malloc");
- return -1;
- }
+ gamma_ramps = emalloc(3 * state->ramp_size * sizeof(uint16_t));
gamma_r = &gamma_ramps[0 * state->ramp_size];
gamma_g = &gamma_ramps[1 * state->ramp_size];
@@ -193,13 +172,11 @@ vidmode_set_temperature(
if (preserve) {
/* Initialize gamma ramps from saved state */
- memcpy(gamma_ramps, state->saved_ramps,
- 3*state->ramp_size*sizeof(uint16_t));
+ memcpy(gamma_ramps, state->saved_ramps, 3 * state->ramp_size * sizeof(uint16_t));
} else {
/* Initialize gamma ramps to pure state */
for (i = 0; i < state->ramp_size; i++) {
- value = (double)i/state->ramp_size *
- (UINT16_MAX+1);
+ value = (double)i / (state->ramp_size - 1) * UINT16_MAX;
gamma_r[i] = value;
gamma_g[i] = value;
gamma_b[i] = value;
@@ -211,11 +188,9 @@ vidmode_set_temperature(
/* Set new gamma ramps */
r = XF86VidModeSetGammaRamp(state->display, state->screen_num,
- state->ramp_size, gamma_r, gamma_g,
- gamma_b);
+ state->ramp_size, gamma_r, gamma_g, gamma_b);
if (!r) {
- fprintf(stderr, _("X request failed: %s\n"),
- "XF86VidModeSetGammaRamp");
+ weprintf(_("X request failed: %s\n"), "XF86VidModeSetGammaRamp");
free(gamma_ramps);
return -1;
}
diff --git a/src/gamma-w32gdi.c b/src/gamma-w32gdi.c
index 87568f9..a252e0a 100644
--- a/src/gamma-w32gdi.c
+++ b/src/gamma-w32gdi.c
@@ -17,12 +17,11 @@
Copyright (c) 2010-2017 Jon Lund Steffensen <jonlst@gmail.com>
Copyright (c) 2025 Mattias Andrée <m@maandree.se>
*/
-#include "common.h"
-
#ifndef WINVER
# define WINVER 0x0500
#endif
-#include <windows.h>
+#include "common.h"
+
#include <wingdi.h>
#define GAMMA_RAMP_SIZE 256
@@ -37,47 +36,37 @@ struct gamma_state {
static int
w32gdi_init(struct gamma_state **state)
{
- *state = malloc(sizeof(struct gamma_state));
- if (state == NULL) return -1;
-
- struct gamma_state *s = *state;
- s->saved_ramps = NULL;
-
+ *state = emalloc(sizeof(**state));
+ (*state)->saved_ramps = NULL;
return 0;
}
static int
w32gdi_start(struct gamma_state *state, program_mode_t mode)
{
- BOOL r;
+ HDC hDC;
+ int cmcap;
/* Open device context */
- HDC hDC = GetDC(NULL);
- if (hDC == NULL) {
- fputs(_("Unable to open device context.\n"), stderr);
+ hDC = GetDC(NULL);
+ if (!hDC) {
+ weprintf(_("Unable to open device context.\n"));
return -1;
}
/* Check support for gamma ramps */
- int cmcap = GetDeviceCaps(hDC, COLORMGMTCAPS);
+ cmcap = GetDeviceCaps(hDC, COLORMGMTCAPS);
if (cmcap != CM_GAMMA_RAMP) {
- fputs(_("Display device does not support gamma ramps.\n"),
- stderr);
+ weprintf(_("Display device does not support gamma ramps.\n"));
return -1;
}
/* Allocate space for saved gamma ramps */
- state->saved_ramps = malloc(3*GAMMA_RAMP_SIZE*sizeof(WORD));
- if (state->saved_ramps == NULL) {
- perror("malloc");
- ReleaseDC(NULL, hDC);
- return -1;
- }
+ state->saved_ramps = emalloc(3 * GAMMA_RAMP_SIZE * sizeof(WORD));
/* Save current gamma ramps so we can restore them at program exit */
- r = GetDeviceGammaRamp(hDC, state->saved_ramps);
- if (!r) {
- fputs(_("Unable to save current gamma ramp.\n"), stderr);
+ if (!GetDeviceGammaRamp(hDC, state->saved_ramps)) {
+ weprintf(_("Unable to save current gamma ramp.\n"));
ReleaseDC(NULL, hDC);
return -1;
}
@@ -108,13 +97,11 @@ w32gdi_print_help(FILE *f)
static int
w32gdi_set_option(struct gamma_state *state, const char *key, const char *value)
{
- if (strcasecmp(key, "preserve") == 0) {
- fprintf(stderr, _("Parameter `%s` is now always on; "
- " Use the `%s` command-line option"
- " to disable.\n"),
- key, "-P");
+ if (!strcasecmp(key, "preserve")) {
+ weprintf(_("Parameter `%s` is now always on; Use the `%s` "
+ "command-line option to disable.\n"), key, "-P");
} else {
- fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
+ weprintf(_("Unknown method parameter: `%s'.\n"), key);
return -1;
}
@@ -124,23 +111,27 @@ w32gdi_set_option(struct gamma_state *state, const char *key, const char *value)
static void
w32gdi_restore(struct gamma_state *state)
{
+ HDC hDC;
+ int i;
+
/* Open device context */
- HDC hDC = GetDC(NULL);
- if (hDC == NULL) {
+ hDC = GetDC(NULL);
+ if (!hDC) {
fputs(_("Unable to open device context.\n"), stderr);
return;
}
/* Restore gamma ramps */
- BOOL r = FALSE;
- for (int i = 0; i < MAX_ATTEMPTS && !r; i++) {
+ for (i = 0; i < MAX_ATTEMPTS; i++) {
/* We retry a few times before giving up because some
buggy drivers fail on the first invocation of
SetDeviceGammaRamp just to succeed on the second. */
- r = SetDeviceGammaRamp(hDC, state->saved_ramps);
+ if (SetDeviceGammaRamp(hDC, state->saved_ramps))
+ goto done;
}
- if (!r) fputs(_("Unable to restore gamma ramps.\n"), stderr);
+ weprintf(_("Unable to restore gamma ramps.\n"));
+done:
/* Release device context */
ReleaseDC(NULL, hDC);
}
@@ -149,36 +140,31 @@ static int
w32gdi_set_temperature(
struct gamma_state *state, const color_setting_t *setting, int preserve)
{
- BOOL r;
+ WORD *gamma_ramps, *gamma_r, *gamma_b, *gamma_g, value;
+ HDC hDC;
+ int i;
/* Open device context */
- HDC hDC = GetDC(NULL);
- if (hDC == NULL) {
- fputs(_("Unable to open device context.\n"), stderr);
+ hDC = GetDC(NULL);
+ if (!hDC) {
+ weprintf(_("Unable to open device context.\n"));
return -1;
}
/* Create new gamma ramps */
- WORD *gamma_ramps = malloc(3*GAMMA_RAMP_SIZE*sizeof(WORD));
- if (gamma_ramps == NULL) {
- perror("malloc");
- ReleaseDC(NULL, hDC);
- return -1;
- }
+ gamma_ramps = emalloc(3 * GAMMA_RAMP_SIZE * sizeof(WORD));
- WORD *gamma_r = &gamma_ramps[0*GAMMA_RAMP_SIZE];
- WORD *gamma_g = &gamma_ramps[1*GAMMA_RAMP_SIZE];
- WORD *gamma_b = &gamma_ramps[2*GAMMA_RAMP_SIZE];
+ gamma_r = &gamma_ramps[0 * GAMMA_RAMP_SIZE];
+ gamma_g = &gamma_ramps[1 * GAMMA_RAMP_SIZE];
+ gamma_b = &gamma_ramps[2 * GAMMA_RAMP_SIZE];
if (preserve) {
/* Initialize gamma ramps from saved state */
- memcpy(gamma_ramps, state->saved_ramps,
- 3*GAMMA_RAMP_SIZE*sizeof(WORD));
+ memcpy(gamma_ramps, state->saved_ramps, 3 * GAMMA_RAMP_SIZE * sizeof(WORD));
} else {
/* Initialize gamma ramps to pure state */
- for (int i = 0; i < GAMMA_RAMP_SIZE; i++) {
- WORD value = (double)i/GAMMA_RAMP_SIZE *
- (UINT16_MAX+1);
+ for (i = 0; i < GAMMA_RAMP_SIZE; i++) {
+ value = (double)i / (GAMMA_RAMP_SIZE - 1) * UINT16_MAX;
gamma_r[i] = value;
gamma_g[i] = value;
gamma_b[i] = value;
@@ -186,23 +172,22 @@ w32gdi_set_temperature(
}
colorramp_fill_u16(gamma_r, gamma_g, gamma_b, GAMMA_RAMP_SIZE,
- GAMMA_RAMP_SIZE, GAMMA_RAMP_SIZE, setting);
+ GAMMA_RAMP_SIZE, GAMMA_RAMP_SIZE, setting);
/* Set new gamma ramps */
- r = FALSE;
- for (int i = 0; i < MAX_ATTEMPTS && !r; i++) {
+ for (i = 0; i < MAX_ATTEMPTS; i++) {
/* We retry a few times before giving up because some
buggy drivers fail on the first invocation of
SetDeviceGammaRamp just to succeed on the second. */
- r = SetDeviceGammaRamp(hDC, gamma_ramps);
- }
- if (!r) {
- fputs(_("Unable to set gamma ramps.\n"), stderr);
- free(gamma_ramps);
- ReleaseDC(NULL, hDC);
- return -1;
+ if (SetDeviceGammaRamp(hDC, gamma_ramps))
+ goto done;
}
+ weprintf(_("Unable to set gamma ramps.\n"));
+ free(gamma_ramps);
+ ReleaseDC(NULL, hDC);
+ return -1;
+done:
free(gamma_ramps);
/* Release device context */
diff --git a/src/hooks.c b/src/hooks.c
index 9a9f8ac..cc26c7c 100644
--- a/src/hooks.c
+++ b/src/hooks.c
@@ -42,14 +42,14 @@ open_hooks_dir(char *hp)
struct passwd *pwd;
#endif
- if ((env = getenv("XDG_CONFIG_HOME")) != NULL &&
- env[0] != '\0') {
+ env = getenv("XDG_CONFIG_HOME");
+ if (env && *env) {
snprintf(hp, MAX_HOOK_PATH, "%s/redshift/hooks", env);
return opendir(hp);
}
- if ((env = getenv("HOME")) != NULL &&
- env[0] != '\0') {
+ env = getenv("HOME");
+ if (env && *env) {
snprintf(hp, MAX_HOOK_PATH, "%s/.config/redshift/hooks", env);
return opendir(hp);
}
@@ -75,15 +75,16 @@ hooks_signal_period_change(enum period prev_period, enum period period)
int r;
hooks_dir = open_hooks_dir(hooksdir_path);
- if (hooks_dir == NULL) return;
+ if (!hooks_dir)
+ return;
- while ((ent = readdir(hooks_dir)) != NULL) {
+ while ((ent = readdir(hooks_dir))) {
/* Skip hidden and special files (., ..) */
- if (ent->d_name[0] == '\0' || ent->d_name[0] == '.') continue;
+ if (ent->d_name[0] == '\0' || ent->d_name[0] == '.')
+ continue;
hook_name = ent->d_name;
- snprintf(hook_path, sizeof(hook_path), "%s/%s",
- hooksdir_path, hook_name);
+ snprintf(hook_path, sizeof(hook_path), "%s/%s", hooksdir_path, hook_name);
#ifndef WINDOWS
/* Fork and exec the hook. We close stdout
@@ -97,10 +98,11 @@ hooks_signal_period_change(enum period prev_period, enum period period)
close(STDOUT_FILENO);
r = execl(hook_path, hook_name,
- "period-changed",
- period_names[prev_period],
- period_names[period], NULL);
- if (r < 0 && errno != EACCES) perror("execl");
+ "period-changed",
+ period_names[prev_period],
+ period_names[period], NULL);
+ if (r < 0 && errno != EACCES)
+ weprintf("execl %s:", hook_path);
/* Only reached on error */
_exit(EXIT_FAILURE);
diff --git a/src/location-geoclue2.c b/src/location-geoclue2.c
index a60867b..b1489db 100644
--- a/src/location-geoclue2.c
+++ b/src/location-geoclue2.c
@@ -55,9 +55,7 @@ static void
mark_error(struct location_state *state)
{
g_mutex_lock(&state->lock);
-
state->error = 1;
-
g_mutex_unlock(&state->lock);
pipeutils_signal(state->pipe_fd_write);
@@ -65,9 +63,7 @@ mark_error(struct location_state *state)
/* Handle position change callbacks */
static void
-geoclue_client_signal_cb(GDBusProxy *client, gchar *sender_name,
- gchar *signal_name, GVariant *parameters,
- gpointer user_data)
+geoclue_client_signal_cb(GDBusProxy *client, gchar *sender_name, gchar *signal_name, GVariant *parameters, gpointer user_data)
{
struct location_state *state = user_data;
const gchar *location_path;
@@ -78,26 +74,19 @@ geoclue_client_signal_cb(GDBusProxy *client, gchar *sender_name,
(void) sender_name;
/* Only handle LocationUpdated signals */
- if (g_strcmp0(signal_name, "LocationUpdated") != 0) {
+ if (g_strcmp0(signal_name, "LocationUpdated"))
return;
- }
/* Obtain location path */
g_variant_get_child(parameters, 1, "&o", &location_path);
/* Obtain location */
error = NULL;
- location = g_dbus_proxy_new_sync(
- g_dbus_proxy_get_connection(client),
- G_DBUS_PROXY_FLAGS_NONE,
- NULL,
- "org.freedesktop.GeoClue2",
- location_path,
- "org.freedesktop.GeoClue2.Location",
- NULL, &error);
- if (location == NULL) {
- g_printerr(_("Unable to obtain location: %s.\n"),
- error->message);
+ location = g_dbus_proxy_new_sync(g_dbus_proxy_get_connection(client), G_DBUS_PROXY_FLAGS_NONE,
+ NULL, "org.freedesktop.GeoClue2", location_path,
+ "org.freedesktop.GeoClue2.Location", NULL, &error);
+ if (!location) {
+ weprintf(_("Unable to obtain location: %s.\n"), error->message);
g_error_free(error);
mark_error(state);
return;
@@ -131,23 +120,18 @@ on_name_appeared(GDBusConnection *conn, const gchar *name,
GDBusProxy *geoclue_manager;
GError *error;
GVariant *ret_v;
+ gchar *dbus_error;
(void) name;
(void) name_owner;
/* Obtain GeoClue Manager */
error = NULL;
- geoclue_manager = g_dbus_proxy_new_sync(
- conn,
- G_DBUS_PROXY_FLAGS_NONE,
- NULL,
- "org.freedesktop.GeoClue2",
- "/org/freedesktop/GeoClue2/Manager",
- "org.freedesktop.GeoClue2.Manager",
- NULL, &error);
- if (geoclue_manager == NULL) {
- g_printerr(_("Unable to obtain GeoClue Manager: %s.\n"),
- error->message);
+ geoclue_manager = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
+ "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);
g_error_free(error);
mark_error(state);
return;
@@ -155,15 +139,10 @@ on_name_appeared(GDBusConnection *conn, const gchar *name,
/* Obtain GeoClue Client path */
error = NULL;
- client_path_v =
- g_dbus_proxy_call_sync(geoclue_manager,
- "GetClient",
- NULL,
- G_DBUS_CALL_FLAGS_NONE,
- -1, NULL, &error);
- if (client_path_v == NULL) {
- g_printerr(_("Unable to obtain GeoClue client path: %s.\n"),
- error->message);
+ 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);
g_error_free(error);
g_object_unref(geoclue_manager);
mark_error(state);
@@ -174,17 +153,10 @@ on_name_appeared(GDBusConnection *conn, const gchar *name,
/* Obtain GeoClue client */
error = NULL;
- 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 == NULL) {
- g_printerr(_("Unable to obtain GeoClue Client: %s.\n"),
- error->message);
+ 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);
g_error_free(error);
g_variant_unref(client_path_v);
g_object_unref(geoclue_manager);
@@ -196,16 +168,11 @@ on_name_appeared(GDBusConnection *conn, const gchar *name,
/* Set desktop id (basename of the .desktop file) */
error = NULL;
- ret_v = g_dbus_proxy_call_sync(
- geoclue_client,
- "org.freedesktop.DBus.Properties.Set",
- g_variant_new("(ssv)",
- "org.freedesktop.GeoClue2.Client",
- "DesktopId",
- g_variant_new("s", "redshift")),
- G_DBUS_CALL_FLAGS_NONE,
- -1, NULL, &error);
- if (ret_v == NULL) {
+ ret_v = g_dbus_proxy_call_sync(geoclue_client, "org.freedesktop.DBus.Properties.Set",
+ g_variant_new("(ssv)", "org.freedesktop.GeoClue2.Client",
+ "DesktopId", g_variant_new("s", "redshift")),
+ 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. */
} else {
@@ -214,18 +181,12 @@ on_name_appeared(GDBusConnection *conn, const gchar *name,
/* Set distance threshold */
error = NULL;
- ret_v = g_dbus_proxy_call_sync(
- geoclue_client,
- "org.freedesktop.DBus.Properties.Set",
- g_variant_new("(ssv)",
- "org.freedesktop.GeoClue2.Client",
- "DistanceThreshold",
- g_variant_new("u", 50000)),
- G_DBUS_CALL_FLAGS_NONE,
- -1, NULL, &error);
- if (ret_v == NULL) {
- g_printerr(_("Unable to set distance threshold: %s.\n"),
- error->message);
+ ret_v = g_dbus_proxy_call_sync(geoclue_client, "org.freedesktop.DBus.Properties.Set",
+ g_variant_new("(ssv)", "org.freedesktop.GeoClue2.Client",
+ "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);
g_error_free(error);
g_object_unref(geoclue_client);
g_object_unref(geoclue_manager);
@@ -236,26 +197,17 @@ on_name_appeared(GDBusConnection *conn, const gchar *name,
g_variant_unref(ret_v);
/* Attach signal callback to client */
- g_signal_connect(geoclue_client, "g-signal",
- G_CALLBACK(geoclue_client_signal_cb),
- user_data);
+ g_signal_connect(geoclue_client, "g-signal", G_CALLBACK(geoclue_client_signal_cb), user_data);
/* Start GeoClue client */
error = NULL;
- ret_v = g_dbus_proxy_call_sync(geoclue_client,
- "Start",
- NULL,
- G_DBUS_CALL_FLAGS_NONE,
- -1, NULL, &error);
- if (ret_v == NULL) {
- g_printerr(_("Unable to start GeoClue client: %s.\n"),
- error->message);
+ 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);
if (g_dbus_error_is_remote_error(error)) {
- gchar *dbus_error = g_dbus_error_get_remote_error(
- error);
- if (g_strcmp0(dbus_error, DBUS_ACCESS_ERROR) == 0) {
+ dbus_error = g_dbus_error_get_remote_error( error);
+ if (!g_strcmp0(dbus_error, DBUS_ACCESS_ERROR))
print_denial_message();
- }
g_free(dbus_error);
}
g_error_free(error);
@@ -270,8 +222,7 @@ on_name_appeared(GDBusConnection *conn, const gchar *name,
/* Callback when GeoClue disappears from the bus */
static void
-on_name_vanished(GDBusConnection *connection, const gchar *name,
- gpointer user_data)
+on_name_vanished(GDBusConnection *connection, const gchar *name, gpointer user_data)
{
struct location_state *state = user_data;
@@ -279,9 +230,7 @@ on_name_vanished(GDBusConnection *connection, const gchar *name,
(void) name;
g_mutex_lock(&state->lock);
-
state->available = 0;
-
g_mutex_unlock(&state->lock);
pipeutils_signal(state->pipe_fd_write);
@@ -314,13 +263,9 @@ run_geoclue2_loop(void *state_)
g_main_context_push_thread_default(context);
state->loop = g_main_loop_new(context, FALSE);
- watcher_id = g_bus_watch_name(
- G_BUS_TYPE_SYSTEM,
- "org.freedesktop.GeoClue2",
- G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
- on_name_appeared,
- on_name_vanished,
- state, NULL);
+ watcher_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM, "org.freedesktop.GeoClue2",
+ G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
+ on_name_appeared, on_name_vanished, state, NULL);
/* Listen for closure of pipe */
pipe_channel = g_io_channel_unix_new(state->pipe_fd_write);
@@ -348,8 +293,7 @@ location_geoclue2_init(struct location_state **state)
#if !GLIB_CHECK_VERSION(2, 35, 0)
g_type_init();
#endif
- *state = malloc(sizeof(**state));
- if (*state == NULL) return -1;
+ *state = emalloc(sizeof(**state));
return 0;
}
@@ -367,7 +311,7 @@ location_geoclue2_start(struct location_state *state)
state->location.lon = 0;
if (pipeutils_create_nonblocking(pipefds)) {
- fputs(_("Failed to start GeoClue2 provider!\n"), stderr);
+ weprintf(_("Failed to start GeoClue2 provider!\n"));
return -1;
}
@@ -385,14 +329,12 @@ location_geoclue2_start(struct location_state *state)
static void
location_geoclue2_free(struct location_state *state)
{
- if (state->pipe_fd_read != -1) {
+ if (state->pipe_fd_read != -1)
close(state->pipe_fd_read);
- }
/* Closing the pipe should cause the thread to exit. */
g_thread_join(state->thread);
state->thread = NULL;
-
g_mutex_clear(&state->lock);
free(state);
@@ -401,18 +343,16 @@ location_geoclue2_free(struct location_state *state)
static void
location_geoclue2_print_help(FILE *f)
{
- fputs(_("Use the location as discovered by a GeoClue2 provider.\n"),
- f);
+ fputs(_("Use the location as discovered by a GeoClue2 provider.\n"), f);
fputs("\n", f);
}
static int
-location_geoclue2_set_option(struct location_state *state,
- const char *key, const char *value)
+location_geoclue2_set_option(struct location_state *state, const char *key, const char *value)
{
(void) state;
(void) value;
- fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
+ weprintf(_("Unknown method parameter: `%s'.\n"), key);
return -1;
}
@@ -423,25 +363,19 @@ location_geoclue2_get_fd(struct location_state *state)
}
static int
-location_geoclue2_handle(
- struct location_state *state,
- struct location *location, int *available)
+location_geoclue2_handle(struct location_state *state, struct location *location, int *available)
{
int error;
pipeutils_handle_signal(state->pipe_fd_read);
g_mutex_lock(&state->lock);
-
error = state->error;
*location = state->location;
*available = state->available;
-
g_mutex_unlock(&state->lock);
- if (error) return -1;
-
- return 0;
+ return error ? -1 : 0;
}
diff --git a/src/location-manual.c b/src/location-manual.c
index 368076b..f59bddd 100644
--- a/src/location-manual.c
+++ b/src/location-manual.c
@@ -28,12 +28,9 @@ struct location_state {
static int
location_manual_init(struct location_state **state)
{
- *state = malloc(sizeof(**state));
- if (*state == NULL) return -1;
-
+ *state = emalloc(sizeof(**state));
(*state)->loc.lat = NAN;
(*state)->loc.lon = NAN;
-
return 0;
}
@@ -45,7 +42,6 @@ location_manual_start(struct location_state *state)
fputs(_("Latitude and longitude must be set.\n"), stderr);
exit(EXIT_FAILURE);
}
-
return 0;
}
@@ -86,12 +82,12 @@ location_manual_set_option(struct location_state *state, const char *key,
return -1;
}
- if (strcasecmp(key, "lat") == 0) {
+ if (!strcasecmp(key, "lat")) {
state->loc.lat = v;
- } else if (strcasecmp(key, "lon") == 0) {
+ } else if (!strcasecmp(key, "lon")) {
state->loc.lon = v;
} else {
- fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
+ weprintf(_("Unknown method parameter: `%s'.\n"), key);
return -1;
}
@@ -106,12 +102,10 @@ location_manual_get_fd(struct location_state *state)
}
static int
-location_manual_handle(
- struct location_state *state, struct location *location, int *available)
+location_manual_handle(struct location_state *state, struct location *location, int *available)
{
*location = state->loc;
*available = 1;
-
return 0;
}
diff --git a/src/options.c b/src/options.c
index 5e4ba4e..725599e 100644
--- a/src/options.c
+++ b/src/options.c
@@ -36,17 +36,15 @@
/* 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)
+parse_brightness_string(const char *str, double *bright_day, double *bright_night)
{
char *s = strchr(str, ':');
- if (s == NULL) {
- /* Same value for day and night. */
- *bright_day = *bright_night = atof(str);
- } else {
- *(s++) = '\0';
+ if (s) {
+ *s++ = '\0';
*bright_day = atof(str);
*bright_night = atof(s);
+ } else {
+ *bright_day = *bright_night = atof(str);
}
}
@@ -56,7 +54,7 @@ static int
parse_gamma_string(const char *str, double gamma[3])
{
char *s = strchr(str, ':');
- if (s == NULL) {
+ if (!s) {
/* Use value for all channels */
double g = atof(str);
gamma[0] = gamma[1] = gamma[2] = g;
@@ -66,7 +64,8 @@ parse_gamma_string(const char *str, double gamma[3])
*s++ = '\0';
g_s = s;
s = strchr(s, ':');
- if (s == NULL) return -1;
+ if (!s)
+ return -1;
*(s++) = '\0';
gamma[0] = atof(str); /* Red */
@@ -87,8 +86,7 @@ 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 != 0 || min == str || min[0] != ':' || hours < 0 || hours >= 24) {
return -1;
}
@@ -121,7 +119,8 @@ 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') return -1;
+ if (end_time < 0 || end[0] != '\0')
+ return -1;
} else {
return -1;
}
@@ -475,14 +474,12 @@ options_parse_args(
const struct gamma_method *gamma_methods,
const struct location_provider *location_providers)
{
- const char* program_name = argv[0];
- int opt;
+ int r, opt;
while ((opt = getopt(argc, argv, "b:c:g:hl:m:oO:pPrt:vVx")) != -1) {
- char option = opt;
- int r = parse_command_line_option(
- option, optarg, options, program_name, gamma_methods,
- location_providers);
- if (r < 0) exit(EXIT_FAILURE);
+ r = parse_command_line_option(opt, optarg, options, argv0,
+ gamma_methods, location_providers);
+ if (r < 0)
+ exit(EXIT_FAILURE);
}
}
@@ -611,26 +608,23 @@ 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)
+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)
{
struct config_ini_section *section;
struct config_ini_setting *setting;
/* Read global config settings. */
section = config_ini_get_section(config_state, "redshift");
- if (section == NULL) return;
-
- setting = section->settings;
- while (setting != NULL) {
- int r = parse_config_file_option(
- setting->name, setting->value, options,
- gamma_methods, location_providers);
- if (r < 0) exit(EXIT_FAILURE);
-
- setting = setting->next;
+ 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)
+ exit(EXIT_FAILURE);
}
}
@@ -638,19 +632,15 @@ options_parse_config_file(
void
options_set_defaults(struct options *options)
{
- if (options->scheme.day.temperature < 0) {
+ if (options->scheme.day.temperature < 0)
options->scheme.day.temperature = DEFAULT_DAY_TEMP;
- }
- if (options->scheme.night.temperature < 0) {
+ if (options->scheme.night.temperature < 0)
options->scheme.night.temperature = DEFAULT_NIGHT_TEMP;
- }
- if (isnan(options->scheme.day.brightness)) {
+ if (isnan(options->scheme.day.brightness))
options->scheme.day.brightness = DEFAULT_BRIGHTNESS;
- }
- if (isnan(options->scheme.night.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;
@@ -663,5 +653,6 @@ options_set_defaults(struct options *options)
options->scheme.night.gamma[2] = DEFAULT_GAMMA;
}
- if (options->use_fade < 0) options->use_fade = 1;
+ if (options->use_fade < 0)
+ options->use_fade = 1;
}
diff --git a/src/pipeutils.c b/src/pipeutils.c
index 0b21b5c..9ca19b2 100644
--- a/src/pipeutils.c
+++ b/src/pipeutils.c
@@ -15,78 +15,57 @@
along with Redshift. 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"
-#ifdef WINDOWS
-
-/* Create non-blocking set of pipe fds.
- Not supported on Windows! Always fails. */
+/* Create non-blocking set of pipe fds. */
int
pipeutils_create_nonblocking(int pipefds[2])
{
+#ifdef WINDOWS
(void) pipefds;
return -1;
-}
-
#else
-/* Create non-blocking set of pipe fds. */
-int
-pipeutils_create_nonblocking(int pipefds[2])
-{
- int flags;
+ int i, flags;
-#if defined(__linux__) && !defined(MISSING_PIPE2)
+# if defined(__linux__) && !defined(MISSING_PIPE2)
if (!pipe2(pipefds, O_NONBLOCK)) {
return 0;
} else if (errno != ENOSYS) {
- perror("pipe2 O_NONBLOCK");
+ weprintf("pipe2 <buffer> O_NONBLOCK:");
return -1;
}
-#endif
+# endif
if (pipe(pipefds)) {
- perror("pipe");
- return -1;
- }
-
- flags = fcntl(pipefds[0], F_GETFL);
- if (flags == -1) {
- perror("fcntl");
- close(pipefds[0]);
- close(pipefds[1]);
- return -1;
- }
-
- if (fcntl(pipefds[0], F_SETFL, flags | O_NONBLOCK)) {
- perror("fcntl");
- close(pipefds[0]);
- close(pipefds[1]);
- return -1;
- }
-
- flags = fcntl(pipefds[1], F_GETFL);
- if (flags == -1) {
- perror("fcntl");
- close(pipefds[0]);
- close(pipefds[1]);
+ weprintf("pipe:");
return -1;
}
- if (fcntl(pipefds[1], F_SETFL, flags | O_NONBLOCK)) {
- perror("fcntl");
- close(pipefds[0]);
- close(pipefds[1]);
- return -1;
+ for (i = 0; i < 2; i++) {
+ flags = fcntl(pipefds[0], F_GETFL);
+ if (flags == -1) {
+ perror("fcntl <pipe> F_GETFL:");
+ goto fail;
+ }
+ if (fcntl(pipefds[0], F_SETFL, flags | O_NONBLOCK)) {
+ perror("fcntl <pipe> F_SETFL +O_NONBLOCK:");
+ goto fail;
+ }
}
return 0;
-}
+fail:
+ close(pipefds[0]);
+ close(pipefds[1]);
+ return -1;
#endif
+}
/* Signal on write-end of pipe. */
void
diff --git a/src/redshift.c b/src/redshift.c
index bb01449..d150f06 100644
--- a/src/redshift.c
+++ b/src/redshift.c
@@ -15,6 +15,7 @@
along with Redshift. If not, see <http://www.gnu.org/licenses/>.
Copyright (c) 2009-2017 Jon Lund Steffensen <jonlst@gmail.com>
+ Copyright (c) 2025 Mattias Andrée <m@maandre.se>
*/
#include "common.h"
#include "solar.h"
@@ -91,65 +92,50 @@ exact_eq(double a, double b)
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) {
+ if (time_offset < transition->dawn.start || time_offset >= transition->dusk.end)
return PERIOD_NIGHT;
- } else if (time_offset >= transition->dawn.end &&
- time_offset < transition->dusk.start) {
+ else if (time_offset >= transition->dawn.end && time_offset < transition->dusk.start)
return PERIOD_DAYTIME;
- } else {
+ else
return PERIOD_TRANSITION;
- }
}
/* Determine which period we are currently in based on solar elevation. */
static enum period
-get_period_from_elevation(
- const struct transition_scheme *transition, double elevation)
+get_period_from_elevation(const struct transition_scheme *transition, double elevation)
{
- if (elevation < transition->low) {
+ if (elevation < transition->low)
return PERIOD_NIGHT;
- } else if (elevation < transition->high) {
+ else if (elevation < transition->high)
return PERIOD_TRANSITION;
- } else {
+ else
return PERIOD_DAYTIME;
- }
}
/* Determine how far through the transition we are based on time offset. */
static double
-get_transition_progress_from_time(
- const struct transition_scheme *transition, int time_offset)
+get_transition_progress_from_time(const struct transition_scheme *transition, int time_offset)
{
- if (time_offset < transition->dawn.start ||
- time_offset >= transition->dusk.end) {
+ if (time_offset < transition->dawn.start || time_offset >= transition->dusk.end)
return 0.0;
- } else if (time_offset < transition->dawn.end) {
- return (transition->dawn.start - time_offset) /
- (double)(transition->dawn.start -
- transition->dawn.end);
- } else if (time_offset > transition->dusk.start) {
- return (transition->dusk.end - time_offset) /
- (double)(transition->dusk.end -
- transition->dusk.start);
- } else {
+ else if (time_offset < transition->dawn.end)
+ return (transition->dawn.start - time_offset) / (double)(transition->dawn.start - transition->dawn.end);
+ else if (time_offset > transition->dusk.start)
+ return (transition->dusk.end - time_offset) / (double)(transition->dusk.end - transition->dusk.start);
+ else
return 1.0;
- }
}
/* Determine how far through the transition we are based on elevation. */
static double
-get_transition_progress_from_elevation(
- const struct transition_scheme *transition, double elevation)
+get_transition_progress_from_elevation(const struct transition_scheme *transition, double elevation)
{
- if (elevation < transition->low) {
+ if (elevation < transition->low)
return 0.0;
- } else if (elevation < transition->high) {
- return (transition->low - elevation) /
- (transition->low - transition->high);
- } else {
+ else if (elevation < transition->high)
+ return (transition->low - elevation) / (transition->low - transition->high);
+ else
return 1.0;
- }
}
/* Return number of seconds since midnight from timestamp. */
@@ -158,11 +144,7 @@ get_seconds_since_midnight(double timestamp)
{
time_t t = (time_t)timestamp;
struct tm tm;
-#ifdef WINDOWS
- localtime_s(&tm, &t);
-#else
localtime_r(&t, &tm);
-#endif
return tm.tm_sec + tm.tm_min * 60 + tm.tm_hour * 3600;
}
@@ -177,9 +159,7 @@ print_period(enum period period, double transition)
printf(_("Period: %s\n"), gettext(period_names[period]));
break;
case PERIOD_TRANSITION:
- printf(_("Period: %s (%.2f%% day)\n"),
- gettext(period_names[period]),
- transition*100);
+ printf(_("Period: %s (%.2f%% day)\n"), gettext(period_names[period]), transition * 100);
break;
}
}
@@ -207,44 +187,30 @@ print_location(const struct location *location)
/* Interpolate color setting structs given alpha. */
static void
-interpolate_color_settings(
- const struct color_setting *first,
- const struct color_setting *second,
- double alpha,
- struct color_setting *result)
+interpolate_color_settings(const struct color_setting *first, const struct color_setting *second,
+ double alpha, struct color_setting *result)
{
+ int i;
+
alpha = CLAMP(0.0, alpha, 1.0);
- result->temperature = (1.0-alpha)*first->temperature +
- alpha*second->temperature;
- result->brightness = (1.0-alpha)*first->brightness +
- alpha*second->brightness;
- for (int i = 0; i < 3; i++) {
- result->gamma[i] = (1.0-alpha)*first->gamma[i] +
- alpha*second->gamma[i];
- }
+ result->temperature = (1.0 - alpha) * first->temperature + alpha * second->temperature;
+ result->brightness = (1.0 - alpha) * first->brightness + alpha * second->brightness;
+ for (i = 0; i < 3; i++)
+ result->gamma[i] = (1.0 - alpha) * first->gamma[i] + alpha * second->gamma[i];
}
/* Interpolate color setting structs transition scheme. */
static void
-interpolate_transition_scheme(
- const struct transition_scheme *transition,
- double alpha,
- struct color_setting *result)
+interpolate_transition_scheme(const struct transition_scheme *transition, double alpha, struct color_setting *result)
{
- 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);
+ interpolate_color_settings(&transition->night, &transition->day, alpha, result);
}
/* Return 1 if color settings have major differences, otherwise 0.
Used to determine if a fade should be applied in continual mode. */
static int
-color_setting_diff_is_major(
- const struct color_setting *first,
- const struct color_setting *second)
+color_setting_diff_is_major(const struct color_setting *first, const struct color_setting *second)
{
return (abs(first->temperature - second->temperature) > 25 ||
fabs(first->brightness - second->brightness) > 0.1 ||
@@ -266,95 +232,75 @@ color_setting_reset(struct color_setting *color)
static int
-provider_try_start(const struct location_provider *provider,
- LOCATION_STATE **state, struct config_ini_state *config,
- char *args)
+provider_try_start(const struct location_provider *provider, LOCATION_STATE **state,
+ struct config_ini_state *config, char *args)
{
- const char *manual_keys[] = { "lat", "lon" };
+ const char *manual_keys[] = {"lat", "lon"};
struct config_ini_section *section;
- int r, i;
+ struct config_ini_setting *setting;
+ int i;
- r = provider->init(state);
- if (r < 0) {
- fprintf(stderr, _("Initialization of %s failed.\n"),
- provider->name);
+ if (provider->init(state) < 0) {
+ weprintf(_("Initialization of %s failed.\n"), provider->name);
return -1;
}
/* Set provider options from config file. */
- section = config_ini_get_section(config, provider->name);
- if (section != NULL) {
- struct config_ini_setting *setting = section->settings;
- while (setting != NULL) {
- r = provider->set_option(*state, setting->name,
- setting->value);
- if (r < 0) {
+ if ((section = config_ini_get_section(config, provider->name))) {
+ for (setting = section->settings; setting; setting = setting->next) {
+ if (provider->set_option(*state, setting->name, setting->value) < 0) {
provider->free(*state);
- fprintf(stderr, _("Failed to set %s"
- " option.\n"),
- provider->name);
- /* TRANSLATORS: `help' must not be
- translated. */
- fprintf(stderr, _("Try `-l %s:help' for more"
- " information.\n"),
- provider->name);
+ weprintf(_("Failed to set %s option.\n"), provider->name);
+ /* TRANSLATORS: `help' must not be translated. */
+ weprintf(_("Try `-l %s:help' for more information.\n"), provider->name);
return -1;
}
- setting = setting->next;
}
}
/* Set provider options from command line. */
- i = 0;
- while (args != NULL) {
+ for (i = 0; args; i++) {
char *next_arg;
const char *key;
char *value;
next_arg = strchr(args, ':');
- if (next_arg != NULL) *(next_arg++) = '\0';
+ if (next_arg)
+ *next_arg++ = '\0';
key = args;
value = strchr(args, '=');
- if (value == NULL) {
+ if (!value) {
/* The options for the "manual" method can be set
without keys on the command line for convencience
and for backwards compatability. We add the proper
keys here before calling set_option(). */
- if (strcmp(provider->name, "manual") == 0 &&
- i < sizeof(manual_keys)/sizeof(manual_keys[0])) {
+ if (!strcmp(provider->name, "manual") && i < ELEMSOF(manual_keys)) {
key = manual_keys[i];
value = args;
} else {
- fprintf(stderr, _("Failed to parse option `%s'.\n"),
- args);
+ weprintf(_("Failed to parse option `%s'.\n"), args);
return -1;
}
} else {
- *(value++) = '\0';
+ *value++ = '\0';
}
- r = provider->set_option(*state, key, value);
- if (r < 0) {
+ if (provider->set_option(*state, key, value) < 0) {
provider->free(*state);
- fprintf(stderr, _("Failed to set %s option.\n"),
- provider->name);
+ weprintf(_("Failed to set %s option.\n"), provider->name);
/* TRANSLATORS: `help' must not be translated. */
- fprintf(stderr, _("Try `-l %s:help' for more"
- " information.\n"), provider->name);
+ weprintf(_("Try `-l %s:help' for more information.\n"), provider->name);
return -1;
}
args = next_arg;
- i += 1;
}
/* Start provider. */
- r = provider->start(*state);
- if (r < 0) {
+ if (provider->start(*state) < 0) {
provider->free(*state);
- fprintf(stderr, _("Failed to start provider %s.\n"),
- provider->name);
+ weprintf(_("Failed to start provider %s.\n"), provider->name);
return -1;
}
@@ -366,35 +312,23 @@ method_try_start(const struct gamma_method *method, GAMMA_STATE **state,
enum program_mode mode, struct config_ini_state *config, char *args)
{
struct config_ini_section *section;
- int r;
+ struct config_ini_setting *setting;
- r = method->init(state);
- if (r < 0) {
- fprintf(stderr, _("Initialization of %s failed.\n"),
- method->name);
+ if (method->init(state) < 0) {
+ weprintf(_("Initialization of %s failed.\n"), method->name);
return -1;
}
/* Set method options from config file. */
- section = config_ini_get_section(config, method->name);
- if (section != NULL) {
- struct config_ini_setting *setting = section->settings;
- while (setting != NULL) {
- r = method->set_option(
- *state, setting->name, setting->value);
- if (r < 0) {
+ if ((section = config_ini_get_section(config, method->name))) {
+ for (setting = section->settings; setting; setting = setting->next) {
+ if (method->set_option(*state, setting->name, setting->value) < 0) {
method->free(*state);
- fprintf(stderr, _("Failed to set %s"
- " option.\n"),
- method->name);
- /* TRANSLATORS: `help' must not be
- translated. */
- fprintf(stderr, _("Try `-m %s:help' for more"
- " information.\n"),
- method->name);
+ weprintf(_("Failed to set %s option.\n"), method->name);
+ /* TRANSLATORS: `help' must not be translated. */
+ weprintf(_("Try `-m %s:help' for more information.\n"), method->name);
return -1;
}
- setting = setting->next;
}
}
@@ -405,26 +339,22 @@ method_try_start(const struct gamma_method *method, GAMMA_STATE **state,
char *value;
next_arg = strchr(args, ':');
- if (next_arg != NULL) *(next_arg++) = '\0';
+ if (next_arg != NULL)
+ *next_arg++ = '\0';
key = args;
value = strchr(args, '=');
- if (value == NULL) {
- fprintf(stderr, _("Failed to parse option `%s'.\n"),
- args);
+ if (!value) {
+ weprintf(_("Failed to parse option `%s'.\n"), args);
return -1;
- } else {
- *(value++) = '\0';
}
+ *value++ = '\0';
- r = method->set_option(*state, key, value);
- if (r < 0) {
+ if (method->set_option(*state, key, value) < 0) {
method->free(*state);
- fprintf(stderr, _("Failed to set %s option.\n"),
- method->name);
+ weprintf(_("Failed to set %s option.\n"), method->name);
/* TRANSLATORS: `help' must not be translated. */
- fprintf(stderr, _("Try -m %s:help' for more"
- " information.\n"), method->name);
+ weprintf(_("Try -m %s:help' for more information.\n"), method->name);
return -1;
}
@@ -432,11 +362,9 @@ method_try_start(const struct gamma_method *method, GAMMA_STATE **state,
}
/* Start method. */
- r = method->start(*state, mode);
- if (r < 0) {
+ if (method->start(*state, mode) < 0) {
method->free(*state);
- fprintf(stderr, _("Failed to start adjustment method %s.\n"),
- method->name);
+ weprintf(_("Failed to start adjustment method %s.\n"), method->name);
return -1;
}
@@ -462,18 +390,14 @@ location_is_valid(const struct location *location)
/* Latitude */
if (location->lat < MIN_LAT || location->lat > MAX_LAT) {
/* TRANSLATORS: Append degree symbols if possible. */
- fprintf(stderr,
- _("Latitude must be between %.1f and %.1f.\n"),
- MIN_LAT, MAX_LAT);
+ weprintf(_("Latitude must be between %.1f and %.1f.\n"), MIN_LAT, MAX_LAT);
return 0;
}
/* Longitude */
if (location->lon < MIN_LON || location->lon > MAX_LON) {
/* TRANSLATORS: Append degree symbols if possible. */
- fprintf(stderr,
- _("Longitude must be between"
- " %.1f and %.1f.\n"), MIN_LON, MAX_LON);
+ weprintf(_("Longitude must be between %.1f and %.1f.\n"), MIN_LON, MAX_LON);
return 0;
}
@@ -485,25 +409,19 @@ location_is_valid(const struct location *location)
is -1. Writes location to loc. Returns -1 on error,
0 if timeout was reached, 1 if location became available. */
static int
-provider_get_location(
- const struct location_provider *provider, LOCATION_STATE *state,
- int timeout, struct location *loc)
+provider_get_location(const struct location_provider *provider, LOCATION_STATE *state, int timeout, struct location *loc)
{
- int available = 0;
+ int r, available, loc_fd;
struct pollfd pollfds[1];
- while (!available) {
- int loc_fd = provider->get_fd(state);
- if (loc_fd >= 0) {
- double now;
- double later;
- int r;
+ double now, later;
+ do {
+ loc_fd = provider->get_fd(state);
+ if (loc_fd >= 0) {
/* Provider is dynamic. */
/* TODO: This should use a monotonic time source. */
- r = systemtime_get_time(&now);
- if (r < 0) {
- fputs(_("Unable to read system time.\n"),
- stderr);
+ if (systemtime_get_time(&now) < 0) {
+ weprintf(_("Unable to read system time.\n"));
return -1;
}
@@ -512,30 +430,27 @@ provider_get_location(
pollfds[0].events = POLLIN;
r = poll(pollfds, 1, timeout);
if (r < 0) {
- perror("poll");
+ weprintf("poll {{.fd=<location provider>, .events=EPOLLIN}} 1 %i:", timeout);
return -1;
} else if (r == 0) {
return 0;
}
- r = systemtime_get_time(&later);
- if (r < 0) {
- fputs(_("Unable to read system time.\n"),
- stderr);
+ if (systemtime_get_time(&later) < 0) {
+ weprintf(_("Unable to read system time.\n"));
return -1;
}
/* Adjust timeout by elapsed time */
if (timeout >= 0) {
timeout -= (later - now) * 1000;
- timeout = timeout < 0 ? 0 : timeout;
+ timeout = MAX(timeout, 0);
}
}
-
if (provider->handle(state, loc, &available) < 0)
return -1;
- }
+ } while (!available);
return 1;
}
@@ -564,8 +479,6 @@ run_continual_mode(const struct location_provider *provider,
GAMMA_STATE *method_state,
int use_fade, int preserve_gamma, int verbose)
{
- int r;
-
int done = 0;
int prev_disabled = 1;
int disabled = 0;
@@ -584,10 +497,8 @@ run_continual_mode(const struct location_provider *provider,
the values did not change. */
enum period prev_period = PERIOD_NONE;
- r = signals_install_handlers();
- if (r < 0) {
- return r;
- }
+ if (signals_install_handlers())
+ return -1;
/* Previous target color setting and current actual color setting.
Actual color setting takes into account the current color fade. */
@@ -598,20 +509,16 @@ run_continual_mode(const struct location_provider *provider,
loc = (struct location){ NAN, NAN };
need_location = !scheme->use_time;
if (need_location) {
- fputs(_("Waiting for initial location"
- " to become available...\n"), stderr);
+ weprintf(_("Waiting for initial location to become available...\n"));
/* Get initial location from provider */
- r = provider_get_location(provider, location_state, -1, &loc);
- if (r < 0) {
- fputs(_("Unable to get location"
- " from provider.\n"), stderr);
+ if (provider_get_location(provider, location_state, -1, &loc) < 0) {
+ weprintf(_("Unable to get location from provider.\n"));
return -1;
}
if (!location_is_valid(&loc)) {
- fputs(_("Invalid location returned from provider.\n"),
- stderr);
+ weprintf(_("Invalid location returned from provider.\n"));
return -1;
}
@@ -650,17 +557,14 @@ run_continual_mode(const struct location_provider *provider,
}
/* Print status change */
- if (verbose && disabled != prev_disabled) {
- printf(_("Status: %s\n"), disabled ?
- _("Disabled") : _("Enabled"));
- }
+ if (verbose && disabled != prev_disabled)
+ printf(_("Status: %s\n"), disabled ? _("Disabled") : _("Enabled"));
prev_disabled = disabled;
/* Read timestamp */
- r = systemtime_get_time(&now);
- if (r < 0) {
- fputs(_("Unable to read system time.\n"), stderr);
+ if (systemtime_get_time(&now) < 0) {
+ weprintf(_("Unable to read system time.\n"));
return -1;
}
@@ -668,58 +572,42 @@ run_continual_mode(const struct location_provider *provider,
int time_offset = get_seconds_since_midnight(now);
period = get_period_from_time(scheme, time_offset);
- transition_prog = get_transition_progress_from_time(
- scheme, time_offset);
+ transition_prog = get_transition_progress_from_time(scheme, time_offset);
} else {
/* Current angular elevation of the sun */
- double elevation = solar_elevation(
- now, loc.lat, loc.lon);
+ double elevation = solar_elevation(now, loc.lat, loc.lon);
period = get_period_from_elevation(scheme, elevation);
- transition_prog =
- get_transition_progress_from_elevation(
- scheme, elevation);
+ transition_prog = get_transition_progress_from_elevation(scheme, elevation);
}
- /* Use transition progress to get target color
- temperature. */
- interpolate_transition_scheme(
- scheme, transition_prog, &target_interp);
+ /* Use transition progress to get target color temperature. */
+ interpolate_transition_scheme(scheme, transition_prog, &target_interp);
if (disabled) {
period = PERIOD_NONE;
color_setting_reset(&target_interp);
}
- if (done) {
+ if (done)
period = PERIOD_NONE;
- }
/* Print period if it changed during this update,
or if we are in the transition period. In transition we
print the progress, so we always print it in
that case. */
- if (verbose && (period != prev_period ||
- period == PERIOD_TRANSITION)) {
+ if (verbose && (period != prev_period || period == PERIOD_TRANSITION))
print_period(period, transition_prog);
- }
/* Activate hooks if period changed */
- if (period != prev_period) {
+ if (period != prev_period)
hooks_signal_period_change(prev_period, period);
- }
/* Start fade if the parameter differences are too big to apply
instantly. */
if (use_fade) {
- if ((fade_length == 0 &&
- color_setting_diff_is_major(
- &interp,
- &target_interp)) ||
- (fade_length != 0 &&
- color_setting_diff_is_major(
- &target_interp,
- &prev_target_interp))) {
+ if (fade_length ? color_setting_diff_is_major(&target_interp, &prev_target_interp)
+ : color_setting_diff_is_major(&interp, &target_interp)) {
fade_length = FADE_LENGTH;
fade_time = 0;
fade_start_interp = interp;
@@ -731,9 +619,7 @@ run_continual_mode(const struct location_provider *provider,
double frac = ++fade_time / (double)fade_length;
double alpha = CLAMP(0.0, ease_fade(frac), 1.0);
- interpolate_color_settings(
- &fade_start_interp, &target_interp, alpha,
- &interp);
+ interpolate_color_settings(&fade_start_interp, &target_interp, alpha, &interp);
if (fade_time > fade_length) {
fade_time = 0;
@@ -747,22 +633,15 @@ run_continual_mode(const struct location_provider *provider,
if (done && fade_length == 0) break;
if (verbose) {
- if (prev_target_interp.temperature != target_interp.temperature) {
- printf(_("Color temperature: %uK\n"),
- target_interp.temperature);
- }
- if (!exact_eq(prev_target_interp.brightness, target_interp.brightness)) {
- printf(_("Brightness: %.2f\n"),
- target_interp.brightness);
- }
+ if (prev_target_interp.temperature != target_interp.temperature)
+ printf(_("Color temperature: %uK\n"), target_interp.temperature);
+ if (!exact_eq(prev_target_interp.brightness, target_interp.brightness))
+ printf(_("Brightness: %.2f\n"), target_interp.brightness);
}
/* Adjust temperature */
- r = method->set_temperature(
- method_state, &interp, preserve_gamma);
- if (r < 0) {
- fputs(_("Temperature adjustment failed.\n"),
- stderr);
+ if (method->set_temperature(method_state, &interp, preserve_gamma) < 0) {
+ weprintf(_("Temperature adjustment failed.\n"));
return -1;
}
@@ -777,25 +656,22 @@ run_continual_mode(const struct location_provider *provider,
}
/* Update location. */
- loc_fd = -1;
- if (need_location) {
- loc_fd = provider->get_fd(location_state);
- }
+ loc_fd = need_location ? provider->get_fd(location_state) : -1;
if (loc_fd >= 0) {
struct pollfd pollfds[1];
struct location new_loc;
- int new_available;
+ int r, new_available;
/* Provider is dynamic. */
pollfds[0].fd = loc_fd;
pollfds[0].events = POLLIN;
r = poll(pollfds, 1, delay);
if (r < 0) {
- if (errno == EINTR) continue;
- perror("poll");
- fputs(_("Unable to get location"
- " from provider.\n"), stderr);
+ if (errno == EINTR)
+ continue;
+ weprintf("poll:");
+ weprintf(_("Unable to get location from provider.\n"));
return -1;
} else if (r == 0) {
continue;
@@ -803,21 +679,14 @@ run_continual_mode(const struct location_provider *provider,
/* Get new location and availability
information. */
- r = provider->handle(
- location_state, &new_loc,
- &new_available);
- if (r < 0) {
- fputs(_("Unable to get location"
- " from provider.\n"), stderr);
+ if (provider->handle(location_state, &new_loc, &new_available) < 0) {
+ weprintf(_("Unable to get location from provider.\n"));
return -1;
}
- if (!new_available &&
- new_available != location_available) {
- fputs(_("Location is temporarily"
- " unavailable; Using previous"
- " location until it becomes"
- " available...\n"), stderr);
+ if (!new_available && new_available != location_available) {
+ weprintf(_("Location is temporarily unavailable; Using previous"
+ " location until it becomes available...\n"));
}
if (new_available &&
@@ -831,8 +700,7 @@ run_continual_mode(const struct location_provider *provider,
location_available = new_available;
if (!location_is_valid(&loc)) {
- fputs(_("Invalid location returned"
- " from provider.\n"), stderr);
+ weprintf(_("Invalid location returned from provider.\n"));
return -1;
}
} else {
@@ -894,6 +762,8 @@ main(int argc, char *argv[])
int need_location;
int r;
+ argv0 = argv[0];
+
#ifdef ENABLE_NLS
/* Init locale */
setlocale(LC_CTYPE, "");
@@ -930,22 +800,14 @@ main(int argc, char *argv[])
if (options.scheme.dawn.start >= 0 || options.scheme.dawn.end >= 0 ||
options.scheme.dusk.start >= 0 || options.scheme.dusk.end >= 0) {
- if (options.scheme.dawn.start < 0 ||
- options.scheme.dawn.end < 0 ||
- options.scheme.dusk.start < 0 ||
- options.scheme.dusk.end < 0) {
- fputs(_("Partial time-configuration not"
- " supported!\n"), stderr);
- exit(EXIT_FAILURE);
- }
+ if (options.scheme.dawn.start < 0 || options.scheme.dawn.end < 0 ||
+ options.scheme.dusk.start < 0 || options.scheme.dusk.end < 0)
+ eprintf(_("Partial time-configuration not supported!\n"));
if (options.scheme.dawn.start > options.scheme.dawn.end ||
options.scheme.dawn.end > options.scheme.dusk.start ||
- options.scheme.dusk.start > options.scheme.dusk.end) {
- fputs(_("Invalid dawn/dusk time configuration!\n"),
- stderr);
- exit(EXIT_FAILURE);
- }
+ options.scheme.dusk.start > options.scheme.dusk.end)
+ eprintf(_("Invalid dawn/dusk time configuration!\n"));
options.scheme.use_time = 1;
}
@@ -954,17 +816,16 @@ main(int argc, char *argv[])
try all providers until one that works is found. */
/* Location is not needed for reset mode and manual mode. */
- need_location =
- options.mode != PROGRAM_MODE_RESET &&
- options.mode != PROGRAM_MODE_MANUAL &&
- !options.scheme.use_time;
+ need_location = options.mode != PROGRAM_MODE_RESET &&
+ options.mode != PROGRAM_MODE_MANUAL &&
+ !options.scheme.use_time;
if (need_location) {
- if (options.provider != NULL) {
+ if (options.provider) {
/* Use provider specified on command line. */
- r = provider_try_start(
- options.provider, &location_state,
- &config_state, options.provider_args);
- if (r < 0) exit(EXIT_FAILURE);
+ r = provider_try_start(options.provider, &location_state,
+ &config_state, options.provider_args);
+ if (r < 0)
+ exit(EXIT_FAILURE);
} else {
/* Try all providers, use the first that works. */
for (int i = 0;
@@ -1279,14 +1140,12 @@ main(int argc, char *argv[])
}
/* Clean up gamma adjustment state */
- if (options.mode != PROGRAM_MODE_PRINT) {
+ if (options.mode != PROGRAM_MODE_PRINT)
options.method->free(method_state);
- }
/* Clean up location provider state */
- if (need_location) {
+ if (need_location)
options.provider->free(location_state);
- }
return EXIT_SUCCESS;
}
diff --git a/src/signals.c b/src/signals.c
index a502be9..492c90d 100644
--- a/src/signals.c
+++ b/src/signals.c
@@ -57,46 +57,37 @@ signals_install_handlers(void)
struct sigaction sigact;
sigset_t sigset;
- /* Install signal handler for INT and TERM signals */
memset(&sigact, 0, sizeof(sigact));
sigemptyset(&sigset);
- sigact.sa_handler = sigexit;
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)) {
- perror("sigaction");
+ weprintf("sigaction SIGINT &{.sa_handler=<function pointer>, .sa_mask={}, .sa_flags=0} NULL:");
return -1;
}
-
if (sigaction(SIGTERM, &sigact, NULL)) {
- perror("sigaction");
+ weprintf("sigaction SIGTERM &{.sa_handler=<function pointer>, .sa_mask={}, .sa_flags=0} NULL:");
return -1;
}
-
if (sigaction(SIGQUIT, &sigact, NULL)) {
- perror("sigaction");
+ weprintf("sigaction SIGQUIT &{.sa_handler=<function pointer>, .sa_mask={}, .sa_flags=0} NULL:");
return -1;
}
/* Install signal handler for USR1 signal */
sigact.sa_handler = sigdisable;
- sigact.sa_mask = sigset;
- sigact.sa_flags = 0;
-
if (sigaction(SIGUSR1, &sigact, NULL)) {
- perror("sigaction");
+ weprintf("sigaction SIGUSR1 &{.sa_handler=<function pointer>, .sa_mask={}, .sa_flags=0} NULL:");
return -1;
}
- /* Ignore CHLD signal. This causes child processes
- (hooks) to be reaped automatically. */
+ /* Ignore CHLD signal. This causes child processes (hooks) to be reaped automatically. */
sigact.sa_handler = SIG_IGN;
- sigact.sa_mask = sigset;
- sigact.sa_flags = 0;
-
if (sigaction(SIGCHLD, &sigact, NULL)) {
- perror("sigaction");
+ weprintf("sigaction SIGCHLD &{.sa_handler=SIG_IGN, .sa_mask={}, .sa_flags=0} NULL:");
return -1;
}
#endif
diff --git a/src/solar.c b/src/solar.c
index 990929d..5dd5fd9 100644
--- a/src/solar.c
+++ b/src/solar.c
@@ -316,8 +316,7 @@ solar_table_fill(double date, double lat, double lon, double *table)
/* Calulate absoute time of other phenomena */
for (int i = 2; i < SOLAR_TIME_MAX; i++) {
double angle = time_angle[i];
- double offset =
- time_of_solar_elevation(t, t_noon, lat, lon, angle);
+ double offset = time_of_solar_elevation(t, t_noon, lat, lon, angle);
table[i] = epoch_from_jd(jdn - 0.5 + offset/1440.0);
}
}
diff --git a/src/systemtime.c b/src/systemtime.c
index 2675fea..b086bc0 100644
--- a/src/systemtime.c
+++ b/src/systemtime.c
@@ -35,7 +35,7 @@ systemtime_get_time(double *t)
#elif defined(_POSIX_TIMERS) /* POSIX timers */
struct timespec now;
if (clock_gettime(CLOCK_REALTIME, &now)) {
- perror("clock_gettime");
+ weprintf("clock_gettime CLOCK_REALTIME:");
return -1;
}
*t = now.tv_sec + (now.tv_nsec / 1000000000.0);
@@ -43,7 +43,7 @@ systemtime_get_time(double *t)
#else /* other platforms */
struct timeval now;
if (gettimeofday(&now, NULL)) {
- perror("gettimeofday");
+ weprintf("gettimeofday:");
return -1;
}
*t = now.tv_sec + (now.tv_usec / 1000000.0);