aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Lund Steffensen <jonlst@gmail.com>2010-05-27 02:20:29 +0200
committerJon Lund Steffensen <jonlst@gmail.com>2010-05-27 02:20:29 +0200
commit441dae4a44f60665c9f669a80304b688285c9c22 (patch)
tree572d08dfeeac1e2a4d737513d9e6c8daa4b0f343
parentLet gamma methods and location providers print help on option parameters. (diff)
downloadredshift-ng-441dae4a44f60665c9f669a80304b688285c9c22.tar.gz
redshift-ng-441dae4a44f60665c9f669a80304b688285c9c22.tar.bz2
redshift-ng-441dae4a44f60665c9f669a80304b688285c9c22.tar.xz
Split init function of methods and providers into init and start.
The set_option function can be called to set options between init and start.
-rw-r--r--src/gamma-randr.c89
-rw-r--r--src/gamma-randr.h8
-rw-r--r--src/gamma-vidmode.c60
-rw-r--r--src/gamma-vidmode.h8
-rw-r--r--src/gamma-w32gdi.c24
-rw-r--r--src/gamma-w32gdi.h8
-rw-r--r--src/location-gnome-clock.c20
-rw-r--r--src/location-gnome-clock.h8
-rw-r--r--src/location-manual.c69
-rw-r--r--src/location-manual.h9
-rw-r--r--src/redshift.c157
-rw-r--r--src/redshift.h33
12 files changed, 341 insertions, 152 deletions
diff --git a/src/gamma-randr.c b/src/gamma-randr.c
index 990ac91..175bbf2 100644
--- a/src/gamma-randr.c
+++ b/src/gamma-randr.c
@@ -41,51 +41,19 @@
int
-randr_init(randr_state_t *state, char *args)
+randr_init(randr_state_t *state)
{
- int screen_num = -1;
- int crtc_num = -1;
-
- /* Parse arguments. */
- while (args != NULL) {
- char *next_arg = strchr(args, ':');
- if (next_arg != NULL) *(next_arg++) = '\0';
-
- char *value = strchr(args, '=');
- if (value != NULL) *(value++) = '\0';
-
- if (strcasecmp(args, "screen") == 0) {
- if (value == NULL) {
- fprintf(stderr, _("Missing value for"
- " parameter: `%s'.\n"),
- args);
- return -1;
- }
- screen_num = atoi(value);
- } else if (strcasecmp(args, "crtc") == 0) {
- if (value == NULL) {
- fprintf(stderr, _("Missing value for"
- " parameter: `%s'.\n"),
- args);
- return -1;
- }
- crtc_num = atoi(value);
- } else {
- fprintf(stderr, _("Unknown method parameter: `%s'.\n"),
- args);
- return -1;
- }
+ /* Initialize state. */
+ state->screen_num = -1;
+ state->crtc_num = -1;
- args = next_arg;
- }
+ state->crtc_count = 0;
+ state->crtcs = NULL;
xcb_generic_error_t *error;
/* Open X server connection */
- int preferred_screen;
- state->conn = xcb_connect(NULL, &preferred_screen);
-
- if (screen_num < 0) screen_num = preferred_screen;
+ state->conn = xcb_connect(NULL, &state->preferred_screen);
/* Query RandR version */
xcb_randr_query_version_cookie_t ver_cookie =
@@ -112,6 +80,17 @@ randr_init(randr_state_t *state, char *args)
free(ver_reply);
+ return 0;
+}
+
+int
+randr_start(randr_state_t *state)
+{
+ xcb_generic_error_t *error;
+
+ int screen_num = state->screen_num;
+ if (screen_num < 0) screen_num = state->preferred_screen;
+
/* Get screen */
const xcb_setup_t *setup = xcb_get_setup(state->conn);
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
@@ -128,7 +107,6 @@ randr_init(randr_state_t *state, char *args)
if (state->screen == NULL) {
fprintf(stderr, _("Screen %i could not be found.\n"),
screen_num);
- xcb_disconnect(state->conn);
return -1;
}
@@ -145,16 +123,14 @@ randr_init(randr_state_t *state, char *args)
fprintf(stderr, _("`%s' returned error %d\n"),
"RANDR Get Screen Resources Current",
error->error_code);
- xcb_disconnect(state->conn);
return -1;
}
- state->crtc_num = crtc_num;
state->crtc_count = res_reply->num_crtcs;
- state->crtcs = malloc(state->crtc_count * sizeof(randr_crtc_state_t));
+ state->crtcs = calloc(state->crtc_count, sizeof(randr_crtc_state_t));
if (state->crtcs == NULL) {
perror("malloc");
- xcb_disconnect(state->conn);
+ state->crtc_count = 0;
return -1;
}
@@ -186,7 +162,6 @@ randr_init(randr_state_t *state, char *args)
fprintf(stderr, _("`%s' returned error %d\n"),
"RANDR Get CRTC Gamma Size",
error->error_code);
- xcb_disconnect(state->conn);
return -1;
}
@@ -198,7 +173,6 @@ randr_init(randr_state_t *state, char *args)
if (ramp_size == 0) {
fprintf(stderr, _("Gamma ramp size too small: %i\n"),
ramp_size);
- xcb_disconnect(state->conn);
return -1;
}
@@ -213,7 +187,6 @@ randr_init(randr_state_t *state, char *args)
if (error) {
fprintf(stderr, _("`%s' returned error %d\n"),
"RANDR Get CRTC Gamma", error->error_code);
- xcb_disconnect(state->conn);
return -1;
}
@@ -230,7 +203,6 @@ randr_init(randr_state_t *state, char *args)
if (state->crtcs[i].saved_ramps == NULL) {
perror("malloc");
free(gamma_get_reply);
- xcb_disconnect(state->conn);
return -1;
}
@@ -301,6 +273,27 @@ randr_print_help(FILE *f)
fputs("\n", f);
}
+int
+randr_set_option(randr_state_t *state, const char *key, const char *value)
+{
+ if (key == NULL) {
+ fprintf(stderr, _("Missing value for parameter: `%s'.\n"),
+ value);
+ return -1;
+ }
+
+ if (strcasecmp(key, "screen") == 0) {
+ state->screen_num = atoi(value);
+ } else if (strcasecmp(key, "crtc") == 0) {
+ state->crtc_num = atoi(value);
+ } else {
+ fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
+ return -1;
+ }
+
+ return 0;
+}
+
static int
randr_set_temperature_for_crtc(randr_state_t *state, int crtc_num, int temp,
float gamma[3])
diff --git a/src/gamma-randr.h b/src/gamma-randr.h
index c51c911..4ccad8f 100644
--- a/src/gamma-randr.h
+++ b/src/gamma-randr.h
@@ -38,15 +38,21 @@ typedef struct {
typedef struct {
xcb_connection_t *conn;
xcb_screen_t *screen;
+ int preferred_screen;
+ int screen_num;
int crtc_num;
unsigned int crtc_count;
randr_crtc_state_t *crtcs;
} randr_state_t;
-int randr_init(randr_state_t *state, char *args);
+int randr_init(randr_state_t *state);
+int randr_start(randr_state_t *state);
void randr_free(randr_state_t *state);
+
void randr_print_help(FILE *f);
+int randr_set_option(randr_state_t *state, const char *key, const char *value);
+
void randr_restore(randr_state_t *state);
int randr_set_temperature(randr_state_t *state, int temp, float gamma[3]);
diff --git a/src/gamma-vidmode.c b/src/gamma-vidmode.c
index e8f6d63..970c45d 100644
--- a/src/gamma-vidmode.c
+++ b/src/gamma-vidmode.c
@@ -37,36 +37,10 @@
int
-vidmode_init(vidmode_state_t *state, char *args)
+vidmode_init(vidmode_state_t *state)
{
- int screen_num = -1;
-
- /* Parse arguments. */
- while (args != NULL) {
- char *next_arg = strchr(args, ':');
- if (next_arg != NULL) *(next_arg++) = '\0';
-
- char *value = strchr(args, '=');
- if (value != NULL) *(value++) = '\0';
-
- if (strcasecmp(args, "screen") == 0) {
- if (value == NULL) {
- fprintf(stderr, _("Missing value for"
- " parameter: `%s'.\n"),
- args);
- return -1;
- }
- screen_num = atoi(value);
- } else {
- fprintf(stderr, _("Unknown method parameter: `%s'.\n"),
- args);
- return -1;
- }
-
- args = next_arg;
- }
-
- int r;
+ state->screen_num = -1;
+ state->saved_ramps = NULL;
/* Open display */
state->display = XOpenDisplay(NULL);
@@ -76,6 +50,15 @@ vidmode_init(vidmode_state_t *state, char *args)
return -1;
}
+ return 0;
+}
+
+int
+vidmode_start(vidmode_state_t *state)
+{
+ int r;
+ int screen_num = state->screen_num;
+
if (screen_num < 0) screen_num = DefaultScreen(state->display);
state->screen_num = screen_num;
@@ -152,6 +135,25 @@ vidmode_print_help(FILE *f)
fputs("\n", f);
}
+int
+vidmode_set_option(vidmode_state_t *state, const char *key, const char *value)
+{
+ if (key == NULL) {
+ fprintf(stderr, _("Missing value for parameter: `%s'.\n"),
+ key);
+ return -1;
+ }
+
+ if (strcasecmp(key, "screen") == 0) {
+ state->screen_num = atoi(value);
+ } else {
+ fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
+ return -1;
+ }
+
+ return 0;
+}
+
void
vidmode_restore(vidmode_state_t *state)
{
diff --git a/src/gamma-vidmode.h b/src/gamma-vidmode.h
index 8a4ef9f..18a4a88 100644
--- a/src/gamma-vidmode.h
+++ b/src/gamma-vidmode.h
@@ -32,9 +32,15 @@ typedef struct {
uint16_t *saved_ramps;
} vidmode_state_t;
-int vidmode_init(vidmode_state_t *state, char *args);
+
+int vidmode_init(vidmode_state_t *state);
+int vidmode_start(vidmode_state_t *state);
void vidmode_free(vidmode_state_t *state);
+
void vidmode_print_help(FILE *f);
+int vidmode_set_option(vidmode_state_t *state, const char *key,
+ const char *value);
+
void vidmode_restore(vidmode_state_t *state);
int vidmode_set_temperature(vidmode_state_t *state, int temp, float gamma[3]);
diff --git a/src/gamma-w32gdi.c b/src/gamma-w32gdi.c
index 2dc12f3..ac6fb0f 100644
--- a/src/gamma-w32gdi.c
+++ b/src/gamma-w32gdi.c
@@ -38,14 +38,18 @@
int
-w32gdi_init(w32gdi_state_t *state, char *args)
+w32gdi_init(w32gdi_state_t *state)
{
- BOOL r;
+ state->saved_ramps = NULL;
+ state->hDC = NULL;
- if (args != NULL) {
- fputs(_("Too many arguments.\n"), stderr);
- return -1;
- }
+ return 0;
+}
+
+int
+w32gdi_init(w32gdi_state_t *state)
+{
+ BOOL r;
/* Open device context */
state->hDC = GetDC(NULL);
@@ -88,7 +92,7 @@ w32gdi_free(w32gdi_state_t *state)
free(state->saved_ramps);
/* Release device context */
- ReleaseDC(NULL, state->hDC);
+ if (state->hDC != NULL) ReleaseDC(NULL, state->hDC);
}
@@ -99,6 +103,12 @@ w32gdi_print_help(FILE *f)
fputs("\n", f);
}
+int
+w32gdi_set_option(w32gdi_state_t *state, const char *key, const char *value)
+{
+ return -1;
+}
+
void
w32gdi_restore(w32gdi_state_t *state)
{
diff --git a/src/gamma-w32gdi.h b/src/gamma-w32gdi.h
index 082fcc8..6cb9799 100644
--- a/src/gamma-w32gdi.h
+++ b/src/gamma-w32gdi.h
@@ -29,9 +29,15 @@ typedef struct {
WORD *saved_ramps;
} w32gdi_state_t;
-int w32gdi_init(w32gdi_state_t *state, char *args);
+
+int w32gdi_init(w32gdi_state_t *state);
+int w32gdi_start(w32gdi_state_t *state);
void w32gdi_free(w32gdi_state_t *state);
+
void w32gdi_print_help(FILE *f);
+int w32gdi_set_option(w32gdi_state_t *state, const char *key,
+ const char *value);
+
void w32gdi_restore(w32gdi_state_t *state);
int w32gdi_set_temperature(w32gdi_state_t *state, int temp, float gamma[3]);
diff --git a/src/location-gnome-clock.c b/src/location-gnome-clock.c
index 1c08ce6..10b95eb 100644
--- a/src/location-gnome-clock.c
+++ b/src/location-gnome-clock.c
@@ -33,13 +33,8 @@
int
-location_gnome_clock_init(location_gnome_clock_state_t *state, char *args)
+location_gnome_clock_init(location_gnome_clock_state_t *state)
{
- if (args != NULL) {
- fputs(_("Too many arguments.\n"), stderr);
- return -1;
- }
-
g_type_init();
GError *error = NULL;
@@ -128,6 +123,12 @@ location_gnome_clock_init(location_gnome_clock_state_t *state, char *args)
return 0;
}
+int
+location_gnome_clock_start(location_gnome_clock_state_t *state)
+{
+ return 0;
+}
+
void
location_gnome_clock_free(location_gnome_clock_state_t *state)
{
@@ -141,6 +142,13 @@ location_gnome_clock_print_help(FILE *f)
}
int
+location_gnome_clock_set_option(location_gnome_clock_state_t *state,
+ const char *key, const char *value)
+{
+ return -1;
+}
+
+int
location_gnome_clock_get_location(location_gnome_clock_state_t *state,
float *lat, float *lon)
{
diff --git a/src/location-gnome-clock.h b/src/location-gnome-clock.h
index f3fc47d..052ce91 100644
--- a/src/location-gnome-clock.h
+++ b/src/location-gnome-clock.h
@@ -22,15 +22,21 @@
#include <stdio.h>
+
typedef struct {
float lat;
float lon;
} location_gnome_clock_state_t;
-int location_gnome_clock_init(location_gnome_clock_state_t *state, char *args);
+int location_gnome_clock_init(location_gnome_clock_state_t *state);
+int location_gnome_clock_start(location_gnome_clock_state_t *state);
void location_gnome_clock_free(location_gnome_clock_state_t *state);
+
void location_gnome_clock_print_help(FILE *f);
+int location_gnome_clock_set_option(location_gnome_clock_state_t *state,
+ const char *key, const char *value);
+
int location_gnome_clock_get_location(location_gnome_clock_state_t *state,
float *lat, float *lon);
diff --git a/src/location-manual.c b/src/location-manual.c
index d14fe98..1b27095 100644
--- a/src/location-manual.c
+++ b/src/location-manual.c
@@ -34,40 +34,17 @@
int
-location_manual_init(location_manual_state_t *state, char *args)
+location_manual_init(location_manual_state_t *state)
{
state->lat = NAN;
state->lon = NAN;
- /* Parse arguments. */
- int count = 0;
- while (args != NULL) {
- if (count > 1) {
- fputs(_("Too many arguments.\n"), stderr);
- return -1;
- }
-
- char *next_arg = strchr(args, ':');
- if (next_arg != NULL) *(next_arg++) = '\0';
-
- /* Parse float value */
- char *end;
- errno = 0;
- float value = strtof(args, &end);
- if (errno != 0 || *end != '\0') {
- fputs(_("Malformed argument.\n"), stderr);
- return -1;
- }
-
- switch (count) {
- case 0: state->lat = value; break;
- case 1: state->lon = value; break;
- }
-
- args = next_arg;
- count += 1;
- }
+ return 0;
+}
+int
+location_manual_start(location_manual_state_t *state)
+{
/* Latitude and longitude must be set */
if (isnan(state->lat) || isnan(state->lon)) {
fputs(_("Latitude and longitude must be set.\n"), stderr);
@@ -88,12 +65,42 @@ location_manual_print_help(FILE *f)
fputs(_("Specify location manually.\n"), f);
fputs("\n", f);
- fputs(_(" First argument is latitude,\n"
- " second argument is longitude\n"), f);
+ fputs(_(" lat=N\t\tLatitude\n"
+ " lon=N\t\tLongitude\n"), f);
fputs("\n", f);
}
int
+location_manual_set_option(location_manual_state_t *state, const char *key,
+ const char *value)
+{
+ /* Parse float value */
+ char *end;
+ errno = 0;
+ float v = strtof(value, &end);
+ if (errno != 0 || *end != '\0') {
+ fputs(_("Malformed argument.\n"), stderr);
+ return -1;
+ }
+
+ if ((key == NULL && isnan(state->lat)) ||
+ (key != NULL && strcasecmp(key, "lat") == 0)) {
+ state->lat = v;
+ } else if ((key == NULL && isnan(state->lon)) ||
+ (key != NULL && strcasecmp(key, "lon") == 0)) {
+ state->lon = v;
+ } else if (key == NULL) {
+ fputs(_("Too many arguments.\n"), stderr);
+ return -1;
+ } else {
+ fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
+ return -1;
+ }
+
+ return 0;
+}
+
+int
location_manual_get_location(location_manual_state_t *state, float *lat,
float *lon)
{
diff --git a/src/location-manual.h b/src/location-manual.h
index 9c763ac..13c77da 100644
--- a/src/location-manual.h
+++ b/src/location-manual.h
@@ -20,6 +20,8 @@
#ifndef _REDSHIFT_LOCATION_MANUAL_H
#define _REDSHIFT_LOCATION_MANUAL_H
+#include <stdio.h>
+
typedef struct {
float lat;
@@ -27,9 +29,14 @@ typedef struct {
} location_manual_state_t;
-int location_manual_init(location_manual_state_t *state, char *args);
+int location_manual_init(location_manual_state_t *state);
+int location_manual_start(location_manual_state_t *state);
void location_manual_free(location_manual_state_t *state);
+
void location_manual_print_help(FILE *f);
+int location_manual_set_option(location_manual_state_t *state,
+ const char *key, const char *value);
+
int location_manual_get_location(location_manual_state_t *state, float *lat,
float *lon);
diff --git a/src/redshift.c b/src/redshift.c
index e2d8964..dc8fae6 100644
--- a/src/redshift.c
+++ b/src/redshift.c
@@ -96,8 +96,10 @@ static const gamma_method_t gamma_methods[] = {
{
"RANDR",
(gamma_method_init_func *)randr_init,
+ (gamma_method_start_func *)randr_start,
(gamma_method_free_func *)randr_free,
(gamma_method_print_help_func *)randr_print_help,
+ (gamma_method_set_option_func *)randr_set_option,
(gamma_method_restore_func *)randr_restore,
(gamma_method_set_temperature_func *)randr_set_temperature
},
@@ -106,8 +108,10 @@ static const gamma_method_t gamma_methods[] = {
{
"VidMode",
(gamma_method_init_func *)vidmode_init,
+ (gamma_method_start_func *)vidmode_start,
(gamma_method_free_func *)vidmode_free,
(gamma_method_print_help_func *)vidmode_print_help,
+ (gamma_method_set_option_func *)vidmode_set_option,
(gamma_method_restore_func *)vidmode_restore,
(gamma_method_set_temperature_func *)vidmode_set_temperature
},
@@ -116,8 +120,10 @@ static const gamma_method_t gamma_methods[] = {
{
"WinGDI",
(gamma_method_init_func *)w32gdi_init,
+ (gamma_method_start_func *)w32gdi_start,
(gamma_method_free_func *)w32gdi_free,
(gamma_method_print_help_func *)w32gdi_print_help,
+ (gamma_method_set_option_func *)w32gdi_set_option,
(gamma_method_restore_func *)w32gdi_restore,
(gamma_method_set_temperature_func *)w32gdi_set_temperature
},
@@ -141,9 +147,12 @@ static const location_provider_t location_providers[] = {
{
"GNOME-Clock",
(location_provider_init_func *)location_gnome_clock_init,
+ (location_provider_start_func *)location_gnome_clock_start,
(location_provider_free_func *)location_gnome_clock_free,
(location_provider_print_help_func *)
location_gnome_clock_print_help,
+ (location_provider_set_option_func *)
+ location_gnome_clock_set_option,
(location_provider_get_location_func *)
location_gnome_clock_get_location
},
@@ -151,9 +160,12 @@ static const location_provider_t location_providers[] = {
{
"Manual",
(location_provider_init_func *)location_manual_init,
+ (location_provider_start_func *)location_manual_start,
(location_provider_free_func *)location_manual_free,
(location_provider_print_help_func *)
location_manual_print_help,
+ (location_provider_set_option_func *)
+ location_manual_set_option,
(location_provider_get_location_func *)
location_manual_get_location
},
@@ -317,6 +329,111 @@ print_provider_list()
}
+static int
+provider_try_start(const location_provider_t *provider,
+ location_state_t *state, char *args)
+{
+ int r;
+
+ r = provider->init(state);
+ if (r < 0) {
+ fprintf(stderr, _("Initialization of %s failed.\n"),
+ provider->name);
+ return -1;
+ }
+
+ /* Set provider options. */
+ while (args != NULL) {
+ char *next_arg = strchr(args, ':');
+ if (next_arg != NULL) *(next_arg++) = '\0';
+
+ char *key = NULL;
+ char *value = strchr(args, '=');
+ if (value != NULL) {
+ key = args;
+ *(value++) = '\0';
+ } else {
+ value = args;
+ }
+
+ r = provider->set_option(state, key, value);
+ if (r < 0) {
+ provider->free(state);
+ fprintf(stderr, _("Failed to set %s option.\n"),
+ provider->name);
+ fprintf(stderr, _("Try `-p %s:help' for more"
+ " information.\n"), provider->name);
+ return -1;
+ }
+
+ args = next_arg;
+ }
+
+ /* Start provider. */
+ r = provider->start(state);
+ if (r < 0) {
+ provider->free(state);
+ fprintf(stderr, _("Failed to start provider %s.\n"),
+ provider->name);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int
+method_try_start(const gamma_method_t *method,
+ gamma_state_t *state, char *args)
+{
+ int r;
+
+ r = method->init(state);
+ if (r < 0) {
+ fprintf(stderr, _("Initialization of %s failed.\n"),
+ method->name);
+ return -1;
+ }
+
+ /* Set method options. */
+ while (args != NULL) {
+ char *next_arg = strchr(args, ':');
+ if (next_arg != NULL) *(next_arg++) = '\0';
+
+ char *key = NULL;
+ char *value = strchr(args, '=');
+ if (value != NULL) {
+ key = args;
+ *(value++) = '\0';
+ } else {
+ value = args;
+ }
+
+ r = method->set_option(state, key, value);
+ if (r < 0) {
+ method->free(state);
+ fprintf(stderr, _("Failed to set %s option.\n"),
+ method->name);
+ fprintf(stderr, _("Try `-p %s:help' for more"
+ " information.\n"), method->name);
+ return -1;
+ }
+
+ args = next_arg;
+ }
+
+ /* Start method. */
+ r = method->start(state);
+ if (r < 0) {
+ method->free(state);
+ fprintf(stderr, _("Failed to start adjustment method %s.\n"),
+ method->name);
+ return -1;
+ }
+
+ return 0;
+}
+
+
int
main(int argc, char *argv[])
{
@@ -395,6 +512,7 @@ main(int argc, char *argv[])
char *end;
float v = strtof(optarg, &end);
if (errno == 0 && *end == ':') {
+ /* Use instead as arguments to `manual'. */
provider_name = "Manual";
provider_args = optarg;
} else {
@@ -504,25 +622,21 @@ main(int argc, char *argv[])
if (provider != NULL) {
/* Use provider specified on command line. */
- r = provider->init(&location_state, provider_args);
- if (r < 0) {
- fprintf(stderr, _("Initialization of %s failed.\n"),
- provider->name);
- exit(EXIT_FAILURE);
- }
+ r = provider_try_start(provider, &location_state,
+ provider_args);
+ if (r < 0) exit(EXIT_FAILURE);
} else {
/* Try all providers, use the first that works. */
for (int i = 0; location_providers[i].name != NULL; i++) {
const location_provider_t *p = &location_providers[i];
- r = p->init(&location_state, provider_args);
+ r = provider_try_start(p, &location_state, NULL);
if (r < 0) {
- fprintf(stderr, _("Initialization of %s"
- " failed.\n"), p->name);
fputs(_("Trying other provider...\n"), stderr);
- } else {
- provider = p;
- break;
+ continue;
}
+
+ provider = p;
+ break;
}
/* Failure if no providers were successful at this point. */
@@ -605,25 +719,20 @@ main(int argc, char *argv[])
if (method != NULL) {
/* Use method specified on command line. */
- r = method->init(&state, method_args);
- if (r < 0) {
- fprintf(stderr, _("Initialization of %s failed.\n"),
- method->name);
- exit(EXIT_FAILURE);
- }
+ r = method_try_start(method, &state, method_args);
+ if (r < 0) exit(EXIT_FAILURE);
} else {
/* Try all methods, use the first that works. */
for (int i = 0; gamma_methods[i].name != NULL; i++) {
const gamma_method_t *m = &gamma_methods[i];
- r = m->init(&state, method_args);
+ r = method_try_start(m, &state, NULL);
if (r < 0) {
- fprintf(stderr, _("Initialization of %s"
- " failed.\n"), m->name);
fputs(_("Trying other method...\n"), stderr);
- } else {
- method = m;
- break;
+ continue;
}
+
+ method = m;
+ break;
}
/* Failure if no methods were successful at this point. */
diff --git a/src/redshift.h b/src/redshift.h
index 18b087e..8f488f6 100644
--- a/src/redshift.h
+++ b/src/redshift.h
@@ -25,35 +25,64 @@
/* Gamma adjustment method */
-typedef int gamma_method_init_func(void *state, char *args);
+typedef int gamma_method_init_func(void *state);
+typedef int gamma_method_start_func(void *state);
typedef void gamma_method_free_func(void *state);
typedef void gamma_method_print_help_func(FILE *f);
+typedef int gamma_method_set_option_func(void *state, const char *key,
+ const char *value);
typedef void gamma_method_restore_func(void *state);
typedef int gamma_method_set_temperature_func(void *state, int temp,
float gamma[3]);
typedef struct {
char *name;
+
+ /* Initialize state. Options can be set between init and start. */
gamma_method_init_func *init;
+ /* Allocate storage and make connections that depend on options. */
+ gamma_method_start_func *start;
+ /* Free all allocated storage and close connections. */
gamma_method_free_func *free;
+
+ /* Print help on options for this adjustment method. */
gamma_method_print_help_func *print_help;
+ /* Set an option key, value-pair */
+ gamma_method_set_option_func *set_option;
+
+ /* Restore the adjustment to the state before start was called. */
gamma_method_restore_func *restore;
+ /* Set a specific color temperature. */
gamma_method_set_temperature_func *set_temperature;
} gamma_method_t;
/* Location provider */
-typedef int location_provider_init_func(void *state, char *args);
+typedef int location_provider_init_func(void *state);
+typedef int location_provider_start_func(void *state);
typedef void location_provider_free_func(void *state);
typedef void location_provider_print_help_func(FILE *f);
+typedef int location_provider_set_option_func(void *state, const char *key,
+ const char *value);
typedef int location_provider_get_location_func(void *state, float *lat,
float *lon);
typedef struct {
char *name;
+
+ /* Initialize state. Options can be set between init and start. */
location_provider_init_func *init;
+ /* Allocate storage and make connections that depend on options. */
+ location_provider_start_func *start;
+ /* Free all allocated storage and close connections. */
location_provider_free_func *free;
+
+ /* Print help on options for this location provider. */
location_provider_print_help_func *print_help;
+ /* Set an option key, value-pair. */
+ location_provider_set_option_func *set_option;
+
+ /* Get current location. */
location_provider_get_location_func *get_location;
} location_provider_t;