aboutsummaryrefslogtreecommitdiffstats
path: root/src/location.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/location.c')
-rw-r--r--src/location.c63
1 files changed, 40 insertions, 23 deletions
diff --git a/src/location.c b/src/location.c
index 60a1ca0..a70be8c 100644
--- a/src/location.c
+++ b/src/location.c
@@ -1,4 +1,5 @@
-/* redshift-ng - Automatically adjust display colour temperature according the Sun
+/*-
+ * redshift-ng - Automatically adjust display colour temperature according the Sun
*
* Copyright (c) 2009-2018 Jon Lund Steffensen <jonlst@gmail.com>
* Copyright (c) 2014-2016, 2025 Mattias Andrée <m@maandree.se>
@@ -31,6 +32,34 @@ const struct location_provider *location_providers[] = {
};
+/**
+ * Get the current monotonic time in milliseconds
+ *
+ * @return The number of milliseconds elapsed since some arbitrary fixed time
+ */
+static long long int
+get_monotonic_millis(void)
+{
+#if defined(WINDOWS)
+ return (long long int)GetTickCount64();
+#else
+ struct timespec now;
+ if (clock_gettime(CLOCK_MONOTONIC, &now))
+ eprintf("clock_gettime CLOCK_MONOTONIC:");
+ return (long long int)now.tv_sec * 1000LL + (long long int)now.tv_nsec / 1000000LL;
+#endif
+}
+
+
+/**
+ * Attempt to start a specific location provider
+ *
+ * @param provider The location provider
+ * @param state_out Output parameter for the location provider state
+ * @param config Loaded information file
+ * @param args `NULL` or option part of the command line argument for the location provider
+ * @return 0 on success, -1 on failure
+ */
static int
try_start(const struct location_provider *provider, LOCATION_STATE **state_out, struct config_ini_state *config, char *args)
{
@@ -65,7 +94,7 @@ try_start(const struct location_provider *provider, LOCATION_STATE **state_out,
* 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") && i < ELEMSOF(manual_keys)) {
+ if (!strcmp(provider->name, "manual") && i < (int)ELEMSOF(manual_keys)) {
key = manual_keys[i];
value = args;
} else {
@@ -103,28 +132,15 @@ fail:
}
-static long long int
-get_monotonic_millis(void)
-{
-#if defined(WINDOWS)
- return (long long int)GetTickCount64();
-#else
- struct timespec now;
- if (clock_gettime(CLOCK_MONOTONIC, &now))
- eprintf("clock_gettime CLOCK_MONOTONIC:");
- return (long long int)now.tv_sec * 1000LL + (long long int)now.tv_nsec / 1000000LL;
-#endif
-}
-
-
-/* Wait for location to become available from provider.
- Waits until timeout (milliseconds) has elapsed or forever if timeout
- is -1. Writes location to loc. Returns -1 on error,
- 0 if timeout was reached, 1 if location became available. */
int
-get_location(const struct location_provider *provider, LOCATION_STATE *state, int timeout, struct location *loc)
+get_location(const struct location_provider *provider, LOCATION_STATE *state, int timeout, struct location *location_out)
{
- int r, available;
+#ifdef WINDOWS /* we don't have poll on Windows, but neither do with have any dynamic location providers */
+ int available;
+ return provider->fetch(state, location_out, &available) < 0 ? -1 : available;
+
+#else
+ int r, available = 0;
struct pollfd pollfds[1];
long long int now = get_monotonic_millis();
long long int end = now + (long long int)timeout;
@@ -150,11 +166,12 @@ get_location(const struct location_provider *provider, LOCATION_STATE *state, in
}
}
- if (provider->fetch(state, loc, &available) < 0)
+ if (provider->fetch(state, location_out, &available) < 0)
return -1;
} while (!available);
return 1;
+#endif
}