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-geofile.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/location-geofile.c (limited to 'src/location-geofile.c') diff --git a/src/location-geofile.c b/src/location-geofile.c new file mode 100644 index 0000000..62d6b26 --- /dev/null +++ b/src/location-geofile.c @@ -0,0 +1,114 @@ +/*- + * 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; + + /** + * File to read location from, `NULL` for default + */ + char *file; +}; + + +static int +geofile_create(struct location_state **state_out) +{ + *state_out = emalloc(sizeof(**state_out)); + (*state_out)->file = NULL; + return 0; +} + + +GCC_ONLY(__attribute__((__pure__))) +static int +geofile_start(struct location_state *state) +{ + struct libgeome_data data = {.requested_data = LIBGEOME_DATUM_LATITUDE | LIBGEOME_DATUM_LONGITUDE}; + struct libgeome_context ctx; + int r; + libgeome_basic_context(&ctx, argv0); + r = libgeome_get_from_file(&ctx, &data, state->file); + free(state->file); + state->file = NULL; + if (r || data.requested_data != (LIBGEOME_DATUM_LATITUDE | LIBGEOME_DATUM_LONGITUDE)) + return -1; + state->location.latitude = data.latitude; + state->location.longitude = data.longitude; + return 0; +} + + +static void +geofile_free(struct location_state *state) +{ + free(state->file); + free(state); +} + + +static void +geofile_print_help(void) +{ + printf(_("Specify location via file.\n")); + printf("\n"); + + printf(" file=%s %s\n", _("FILE "), _("File to read location from (empty for default)")); + printf("\n"); +} + + +static int +geofile_set_option(struct location_state *state, const char *key, const char *value) +{ + if (!strcasecmp(key, "file")) { + free(state->file); + state->file = *value ? estrdup(value) : NULL; + return 0; + } else { + weprintf(_("Unknown provider parameter: `%s'."), key); + return -1; + } +} + + +static int +geofile_get_fd(struct location_state *state) +{ + (void) state; + return -1; +} + + +static int +geofile_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 geofile_location_provider = LOCATION_PROVIDER_INIT("geofile", geofile); -- cgit v1.2.3-70-g09d2