diff options
author | Jon Lund Steffensen <jonlst@gmail.com> | 2017-10-11 20:36:41 -0700 |
---|---|---|
committer | Jon Lund Steffensen <jonlst@gmail.com> | 2017-10-13 18:09:28 -0700 |
commit | b27acd687800499324381648036edd098c996524 (patch) | |
tree | 3197172546126fff94e8918ad2244f0788f60ee7 /src | |
parent | Move module struct definitions to separate files (diff) | |
download | redshift-ng-b27acd687800499324381648036edd098c996524.tar.gz redshift-ng-b27acd687800499324381648036edd098c996524.tar.bz2 redshift-ng-b27acd687800499324381648036edd098c996524.tar.xz |
Allocate module data in init functions
Diffstat (limited to 'src')
-rw-r--r-- | src/gamma-drm.c | 19 | ||||
-rw-r--r-- | src/gamma-dummy.c | 3 | ||||
-rw-r--r-- | src/gamma-quartz.c | 12 | ||||
-rw-r--r-- | src/gamma-randr.c | 32 | ||||
-rw-r--r-- | src/gamma-vidmode.c | 21 | ||||
-rw-r--r-- | src/gamma-w32gdi.c | 12 | ||||
-rw-r--r-- | src/location-corelocation.m | 5 | ||||
-rw-r--r-- | src/location-geoclue2.c | 6 | ||||
-rw-r--r-- | src/location-manual.c | 11 | ||||
-rw-r--r-- | src/redshift.c | 106 | ||||
-rw-r--r-- | src/redshift.h | 32 |
11 files changed, 138 insertions, 121 deletions
diff --git a/src/gamma-drm.c b/src/gamma-drm.c index 19f66fd..e706d46 100644 --- a/src/gamma-drm.c +++ b/src/gamma-drm.c @@ -15,6 +15,7 @@ along with Redshift. If not, see <http://www.gnu.org/licenses/>. Copyright (c) 2014 Mattias Andrée <maandree@member.fsf.org> + Copyright (c) 2017 Jon Lund Steffensen <jonlst@gmail.com> */ #include <stdio.h> @@ -42,14 +43,18 @@ static int -drm_init(drm_state_t *state) +drm_init(drm_state_t **state) { /* Initialize state. */ - state->card_num = 0; - state->crtc_num = -1; - state->fd = -1; - state->res = NULL; - state->crtcs = NULL; + *state = malloc(sizeof(drm_state_t)); + if (*state == NULL) return -1; + + drm_state_t *s = *state; + s->card_num = 0; + s->crtc_num = -1; + s->fd = -1; + s->res = NULL; + s->crtcs = NULL; return 0; } @@ -206,6 +211,8 @@ drm_free(drm_state_t *state) close(state->fd); state->fd = -1; } + + free(state); } static void diff --git a/src/gamma-dummy.c b/src/gamma-dummy.c index dadc014..25e723f 100644 --- a/src/gamma-dummy.c +++ b/src/gamma-dummy.c @@ -31,8 +31,9 @@ static int -gamma_dummy_init(void *state) +gamma_dummy_init(void **state) { + *state = NULL; return 0; } diff --git a/src/gamma-quartz.c b/src/gamma-quartz.c index 6bacff4..6b7d1ec 100644 --- a/src/gamma-quartz.c +++ b/src/gamma-quartz.c @@ -38,10 +38,14 @@ static int -quartz_init(quartz_state_t *state) +quartz_init(quartz_state_t **state) { - state->preserve = 1; - state->displays = NULL; + *state = malloc(sizeof(quartz_state_t)); + if (*state == NULL) return -1; + + quartz_state_t *s = *state; + s->preserve = 1; + s->displays = NULL; return 0; } @@ -49,7 +53,6 @@ quartz_init(quartz_state_t *state) static int quartz_start(quartz_state_t *state) { - int r; CGError error; uint32_t display_count; @@ -147,6 +150,7 @@ quartz_free(quartz_state_t *state) } } free(state->displays); + free(state); } static void diff --git a/src/gamma-randr.c b/src/gamma-randr.c index ef240b2..44bd497 100644 --- a/src/gamma-randr.c +++ b/src/gamma-randr.c @@ -43,29 +43,33 @@ static int -randr_init(randr_state_t *state) +randr_init(randr_state_t **state) { /* Initialize state. */ - state->screen_num = -1; - state->crtc_num = NULL; + *state = malloc(sizeof(randr_state_t)); + if (*state == NULL) return -1; - state->crtc_num_count = 0; - state->crtc_count = 0; - state->crtcs = NULL; + randr_state_t *s = *state; + s->screen_num = -1; + s->crtc_num = NULL; - state->preserve = 1; + s->crtc_num_count = 0; + s->crtc_count = 0; + s->crtcs = NULL; + + s->preserve = 1; xcb_generic_error_t *error; /* Open X server connection */ - state->conn = xcb_connect(NULL, &state->preferred_screen); + s->conn = xcb_connect(NULL, &s->preferred_screen); /* Query RandR version */ xcb_randr_query_version_cookie_t ver_cookie = - xcb_randr_query_version(state->conn, RANDR_VERSION_MAJOR, + xcb_randr_query_version(s->conn, RANDR_VERSION_MAJOR, RANDR_VERSION_MINOR); xcb_randr_query_version_reply_t *ver_reply = - xcb_randr_query_version_reply(state->conn, ver_cookie, &error); + xcb_randr_query_version_reply(s->conn, ver_cookie, &error); /* TODO What does it mean when both error and ver_reply is NULL? Apparently, we have to check both to avoid seg faults. */ @@ -73,7 +77,8 @@ randr_init(randr_state_t *state) int ec = (error != 0) ? error->error_code : -1; fprintf(stderr, _("`%s' returned error %d\n"), "RANDR Query Version", ec); - xcb_disconnect(state->conn); + xcb_disconnect(s->conn); + free(s); return -1; } @@ -82,7 +87,8 @@ randr_init(randr_state_t *state) fprintf(stderr, _("Unsupported RANDR version (%u.%u)\n"), ver_reply->major_version, ver_reply->minor_version); free(ver_reply); - xcb_disconnect(state->conn); + xcb_disconnect(s->conn); + free(s); return -1; } @@ -269,6 +275,8 @@ randr_free(randr_state_t *state) /* Close connection */ xcb_disconnect(state->conn); + + free(state); } static void diff --git a/src/gamma-vidmode.c b/src/gamma-vidmode.c index 8d49363..8d1791a 100644 --- a/src/gamma-vidmode.c +++ b/src/gamma-vidmode.c @@ -38,18 +38,21 @@ static int -vidmode_init(vidmode_state_t *state) +vidmode_init(vidmode_state_t **state) { - state->screen_num = -1; - state->saved_ramps = NULL; + *state = malloc(sizeof(vidmode_state_t)); + if (*state == NULL) return -1; - state->preserve = 1; + vidmode_state_t *s = *state; + s->screen_num = -1; + s->saved_ramps = NULL; + + s->preserve = 1; /* Open display */ - state->display = XOpenDisplay(NULL); - if (state->display == NULL) { - fprintf(stderr, _("X request failed: %s\n"), - "XOpenDisplay"); + s->display = XOpenDisplay(NULL); + if (s->display == NULL) { + fprintf(stderr, _("X request failed: %s\n"), "XOpenDisplay"); return -1; } @@ -121,6 +124,8 @@ vidmode_free(vidmode_state_t *state) /* Close display connection */ XCloseDisplay(state->display); + + free(state); } static void diff --git a/src/gamma-w32gdi.c b/src/gamma-w32gdi.c index 9896dce..be39786 100644 --- a/src/gamma-w32gdi.c +++ b/src/gamma-w32gdi.c @@ -41,10 +41,14 @@ static int -w32gdi_init(w32gdi_state_t *state) +w32gdi_init(w32gdi_state_t **state) { - state->saved_ramps = NULL; - state->preserve = 1; + *state = malloc(sizeof(w32gdi_state_t)); + if (state == NULL) return -1; + + w32gdi_state_t *s = *state; + s->saved_ramps = NULL; + s->preserve = 1; return 0; } @@ -96,6 +100,8 @@ w32gdi_free(w32gdi_state_t *state) { /* Free saved ramps */ free(state->saved_ramps); + + free(state); } diff --git a/src/location-corelocation.m b/src/location-corelocation.m index 17e0054..bf60507 100644 --- a/src/location-corelocation.m +++ b/src/location-corelocation.m @@ -173,8 +173,10 @@ pipe_close_callback( static int -location_corelocation_init(location_corelocation_state_t *state) +location_corelocation_init(location_corelocation_state_t **state) { + *state = malloc(sizeof(location_corelocation_state_t)); + if (*state == NULL) return -1; return 0; } @@ -223,6 +225,7 @@ location_corelocation_free(location_corelocation_state_t *state) } free(state->private); + free(state); } static void diff --git a/src/location-geoclue2.c b/src/location-geoclue2.c index b012da4..0bb1024 100644 --- a/src/location-geoclue2.c +++ b/src/location-geoclue2.c @@ -325,11 +325,13 @@ run_geoclue2_loop(void *state_) } static int -location_geoclue2_init(location_geoclue2_state_t *state) +location_geoclue2_init(location_geoclue2_state_t **state) { #if !GLIB_CHECK_VERSION(2, 35, 0) g_type_init(); #endif + *state = malloc(sizeof(location_geoclue2_state_t)); + if (*state == NULL) return -1; return 0; } @@ -374,6 +376,8 @@ location_geoclue2_free(location_geoclue2_state_t *state) state->thread = NULL; g_mutex_clear(&state->lock); + + free(state); } static void diff --git a/src/location-manual.c b/src/location-manual.c index d7ba37a..f459b27 100644 --- a/src/location-manual.c +++ b/src/location-manual.c @@ -34,10 +34,14 @@ static int -location_manual_init(location_manual_state_t *state) +location_manual_init(location_manual_state_t **state) { - state->loc.lat = NAN; - state->loc.lon = NAN; + *state = malloc(sizeof(location_manual_state_t)); + if (*state == NULL) return -1; + + location_manual_state_t *s = *state; + s->loc.lat = NAN; + s->loc.lon = NAN; return 0; } @@ -57,6 +61,7 @@ location_manual_start(location_manual_state_t *state) static void location_manual_free(location_manual_state_t *state) { + free(state); } static void diff --git a/src/redshift.c b/src/redshift.c index 3c7dcec..6e63b2c 100644 --- a/src/redshift.c +++ b/src/redshift.c @@ -107,37 +107,6 @@ int poll(struct pollfd *fds, int nfds, int timeout) { abort(); return -1; } #undef CLAMP #define CLAMP(lo,mid,up) (((lo) > (mid)) ? (lo) : (((mid) < (up)) ? (mid) : (up))) -/* Union of state data for gamma adjustment methods */ -typedef union { -#ifdef ENABLE_DRM - drm_state_t drm; -#endif -#ifdef ENABLE_RANDR - randr_state_t randr; -#endif -#ifdef ENABLE_VIDMODE - vidmode_state_t vidmode; -#endif -#ifdef ENABLE_QUARTZ - quartz_state_t quartz; -#endif -#ifdef ENABLE_WINGDI - w32gdi_state_t w32gdi; -#endif -} gamma_state_t; - - -/* Union of state data for location providers */ -typedef union { - location_manual_state_t manual; -#ifdef ENABLE_GEOCLUE2 - location_geoclue2_state_t geoclue2; -#endif -#ifdef ENABLE_CORELOCATION - location_corelocation_state_t corelocation; -#endif -} location_state_t; - /* Bounds for parameters. */ #define MIN_LAT -90.0 @@ -477,8 +446,8 @@ print_provider_list(const location_provider_t location_providers[]) static int provider_try_start(const location_provider_t *provider, - location_state_t *state, - config_ini_state_t *config, char *args) + location_state_t **state, config_ini_state_t *config, + char *args) { int r; @@ -495,10 +464,10 @@ provider_try_start(const location_provider_t *provider, if (section != NULL) { config_ini_setting_t *setting = section->settings; while (setting != NULL) { - r = provider->set_option(state, setting->name, + r = provider->set_option(*state, setting->name, setting->value); if (r < 0) { - provider->free(state); + provider->free(*state); fprintf(stderr, _("Failed to set %s" " option.\n"), provider->name); @@ -540,9 +509,9 @@ provider_try_start(const location_provider_t *provider, *(value++) = '\0'; } - r = provider->set_option(state, key, value); + r = provider->set_option(*state, key, value); if (r < 0) { - provider->free(state); + provider->free(*state); fprintf(stderr, _("Failed to set %s option.\n"), provider->name); /* TRANSLATORS: `help' must not be translated. */ @@ -556,9 +525,9 @@ provider_try_start(const location_provider_t *provider, } /* Start provider. */ - r = provider->start(state); + r = provider->start(*state); if (r < 0) { - provider->free(state); + provider->free(*state); fprintf(stderr, _("Failed to start provider %s.\n"), provider->name); return -1; @@ -569,8 +538,7 @@ provider_try_start(const location_provider_t *provider, static int method_try_start(const gamma_method_t *method, - gamma_state_t *state, - config_ini_state_t *config, char *args) + gamma_state_t **state, config_ini_state_t *config, char *args) { int r; @@ -587,10 +555,10 @@ method_try_start(const gamma_method_t *method, if (section != NULL) { config_ini_setting_t *setting = section->settings; while (setting != NULL) { - r = method->set_option(state, setting->name, - setting->value); + r = method->set_option( + *state, setting->name, setting->value); if (r < 0) { - method->free(state); + method->free(*state); fprintf(stderr, _("Failed to set %s" " option.\n"), method->name); @@ -620,9 +588,9 @@ method_try_start(const gamma_method_t *method, *(value++) = '\0'; } - r = method->set_option(state, key, value); + r = method->set_option(*state, key, value); if (r < 0) { - method->free(state); + method->free(*state); fprintf(stderr, _("Failed to set %s option.\n"), method->name); /* TRANSLATORS: `help' must not be translated. */ @@ -635,9 +603,9 @@ method_try_start(const gamma_method_t *method, } /* Start method. */ - r = method->start(state); + r = method->start(*state); if (r < 0) { - method->free(state); + method->free(*state); fprintf(stderr, _("Failed to start adjustment method %s.\n"), method->name); return -1; @@ -888,7 +856,7 @@ run_continual_mode(const location_provider_t *provider, location_state_t *location_state, const transition_scheme_t *scheme, const gamma_method_t *method, - gamma_state_t *state, + gamma_state_t *method_state, int use_fade, int verbose) { int r; @@ -1086,7 +1054,7 @@ run_continual_mode(const location_provider_t *provider, } /* Adjust temperature */ - r = method->set_temperature(state, &interp); + r = method->set_temperature(method_state, &interp); if (r < 0) { fputs(_("Temperature adjustment failed.\n"), stderr); @@ -1167,7 +1135,7 @@ run_continual_mode(const location_provider_t *provider, } /* Restore saved gamma ramps */ - method->restore(state); + method->restore(method_state); return 0; } @@ -1630,7 +1598,7 @@ main(int argc, char *argv[]) /* Initialize location provider if needed. If provider is NULL try all providers until one that works is found. */ - location_state_t location_state; + location_state_t *location_state; /* Location is not needed for reset mode and manual mode. */ int need_location = @@ -1758,14 +1726,15 @@ main(int argc, char *argv[]) /* Initialize gamma adjustment method. If method is NULL try all methods until one that works is found. */ - gamma_state_t state; + gamma_state_t *method_state; /* Gamma adjustment not needed for print mode */ if (mode != PROGRAM_MODE_PRINT) { if (method != NULL) { /* Use method specified on command line. */ - r = method_try_start(method, &state, &config_state, - method_args); + r = method_try_start( + method, &method_state, &config_state, + method_args); if (r < 0) exit(EXIT_FAILURE); } else { /* Try all methods, use the first that works. */ @@ -1773,7 +1742,8 @@ main(int argc, char *argv[]) const gamma_method_t *m = &gamma_methods[i]; if (!m->autostart) continue; - r = method_try_start(m, &state, &config_state, NULL); + r = method_try_start( + m, &method_state, &config_state, NULL); if (r < 0) { fputs(_("Trying next method...\n"), stderr); continue; @@ -1806,7 +1776,7 @@ main(int argc, char *argv[]) /* Wait for location provider. */ int r = provider_get_location( - provider, &location_state, -1, &loc); + provider, location_state, -1, &loc); if (r < 0) { fputs(_("Unable to get location" " from provider.\n"), stderr); @@ -1824,7 +1794,7 @@ main(int argc, char *argv[]) r = systemtime_get_time(&now); if (r < 0) { fputs(_("Unable to read system time.\n"), stderr); - method->free(&state); + method->free(method_state); exit(EXIT_FAILURE); } @@ -1866,11 +1836,11 @@ main(int argc, char *argv[]) if (mode != PROGRAM_MODE_PRINT) { /* Adjust temperature */ - r = method->set_temperature(&state, &interp); + r = method->set_temperature(method_state, &interp); if (r < 0) { fputs(_("Temperature adjustment failed.\n"), stderr); - method->free(&state); + method->free(method_state); exit(EXIT_FAILURE); } @@ -1892,10 +1862,10 @@ main(int argc, char *argv[]) /* Adjust temperature */ color_setting_t manual = scheme.day; manual.temperature = temp_set; - r = method->set_temperature(&state, &manual); + r = method->set_temperature(method_state, &manual); if (r < 0) { fputs(_("Temperature adjustment failed.\n"), stderr); - method->free(&state); + method->free(method_state); exit(EXIT_FAILURE); } @@ -1912,10 +1882,10 @@ main(int argc, char *argv[]) { /* Reset screen */ color_setting_t reset = { NEUTRAL_TEMP, { 1.0, 1.0, 1.0 }, 1.0 }; - r = method->set_temperature(&state, &reset); + r = method->set_temperature(method_state, &reset); if (r < 0) { fputs(_("Temperature adjustment failed.\n"), stderr); - method->free(&state); + method->free(method_state); exit(EXIT_FAILURE); } @@ -1930,8 +1900,8 @@ main(int argc, char *argv[]) break; case PROGRAM_MODE_CONTINUAL: { - r = run_continual_mode(provider, &location_state, &scheme, - method, &state, + r = run_continual_mode(provider, location_state, &scheme, + method, method_state, use_fade, verbose); if (r < 0) exit(EXIT_FAILURE); } @@ -1940,12 +1910,12 @@ main(int argc, char *argv[]) /* Clean up gamma adjustment state */ if (mode != PROGRAM_MODE_PRINT) { - method->free(&state); + method->free(method_state); } /* Clean up location provider state */ if (need_location) { - provider->free(&location_state); + provider->free(location_state); } return EXIT_SUCCESS; diff --git a/src/redshift.h b/src/redshift.h index c659502..98f9f37 100644 --- a/src/redshift.h +++ b/src/redshift.h @@ -14,7 +14,7 @@ You should have received a copy of the GNU General Public License along with Redshift. If not, see <http://www.gnu.org/licenses/>. - Copyright (c) 2013-2014 Jon Lund Steffensen <jonlst@gmail.com> + Copyright (c) 2013-2017 Jon Lund Steffensen <jonlst@gmail.com> */ #ifndef REDSHIFT_REDSHIFT_H @@ -47,14 +47,16 @@ typedef struct { /* Gamma adjustment method */ -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 struct gamma_state gamma_state_t; + +typedef int gamma_method_init_func(gamma_state_t **state); +typedef int gamma_method_start_func(gamma_state_t *state); +typedef void gamma_method_free_func(gamma_state_t *state); typedef void gamma_method_print_help_func(FILE *f); -typedef int gamma_method_set_option_func(void *state, const char *key, +typedef int gamma_method_set_option_func(gamma_state_t *state, const char *key, const char *value); -typedef void gamma_method_restore_func(void *state); -typedef int gamma_method_set_temperature_func(void *state, +typedef void gamma_method_restore_func(gamma_state_t *state); +typedef int gamma_method_set_temperature_func(gamma_state_t *state, const color_setting_t *setting); typedef struct { @@ -83,15 +85,17 @@ typedef struct { /* Location provider */ -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 struct location_state location_state_t; + +typedef int location_provider_init_func(location_state_t **state); +typedef int location_provider_start_func(location_state_t *state); +typedef void location_provider_free_func(location_state_t *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_fd_func(void *state); +typedef int location_provider_set_option_func( + location_state_t *state, const char *key, const char *value); +typedef int location_provider_get_fd_func(location_state_t *state); typedef int location_provider_handle_func( - void *state, location_t *location, int *available); + location_state_t *state, location_t *location, int *available); typedef struct { char *name; |