From 8fed0248b8c26a4135bd230ec9dea84c4ee23233 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 25 Mar 2025 23:06:51 +0100 Subject: Add support for /etc/geolocation and guessing by timezone MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/location-timezone.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/location-timezone.c (limited to 'src/location-timezone.c') diff --git a/src/location-timezone.c b/src/location-timezone.c new file mode 100644 index 0000000..58bf0cf --- /dev/null +++ b/src/location-timezone.c @@ -0,0 +1,108 @@ +/*- + * redshift-ng - Automatically adjust display colour temperature according the Sun + * + * Copyright (c) 2009-2018 Jon Lund Steffensen + * Copyright (c) 2014-2016, 2025 Mattias Andrée + * + * redshift-ng is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * redshift-ng is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with redshift-ng. If not, see . + */ +#include "common.h" + + +struct location_state { + /** + * The loaded location + */ + struct location location; +}; + + +static int +timezone_create(struct location_state **state_out) +{ + *state_out = emalloc(sizeof(**state_out)); + return 0; +} + + +GCC_ONLY(__attribute__((__pure__))) +static int +timezone_start(struct location_state *state) +{ + struct libgeome_data data; + struct libgeome_context ctx; + int r; + libgeome_basic_context(&ctx, argv0); + + data.requested_data = LIBGEOME_DATUM_LATITUDE | LIBGEOME_DATUM_LONGITUDE; + r = libgeome_get_from_timezone(&ctx, &data); + if (r) { + data.requested_data = LIBGEOME_DATUM_LATITUDE | LIBGEOME_DATUM_LONGITUDE; + r = libgeome_get_from_time(&ctx, &data); + } + + if (r || !(data.requested_data & LIBGEOME_DATUM_LONGITUDE)) + return -1; + if (data.requested_data & LIBGEOME_DATUM_LATITUDE) + state->location.latitude = data.latitude; + else + state->location.latitude = 0; + state->location.longitude = data.longitude; + return 0; +} + + +static void +timezone_free(struct location_state *state) +{ + free(state); +} + + +static void +timezone_print_help(void) +{ + printf(_("Get rough location from timezone.\n")); + printf("\n"); +} + + +static int +timezone_set_option(struct location_state *state, const char *key, const char *value) +{ + (void) state; + (void) value; + weprintf(_("Unknown provider parameter: `%s'."), key); + return -1; +} + + +static int +timezone_get_fd(struct location_state *state) +{ + (void) state; + return -1; +} + + +static int +timezone_fetch(struct location_state *state, struct location *location_out, int *available_out) +{ + *location_out = state->location; + *available_out = 1; + return 0; +} + + +const struct location_provider timezone_location_provider = LOCATION_PROVIDER_INIT("timezone", timezone); -- cgit v1.2.3-70-g09d2