Skip to content

Commit 9aff2c7

Browse files
committed
Use a static tz->coords list for geolocation.
1 parent 48886ec commit 9aff2c7

File tree

3 files changed

+505
-106
lines changed

3 files changed

+505
-106
lines changed

plugins/color/csd-night-light.c

Lines changed: 25 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020

2121
#include "config.h"
2222

23-
// #include <geoclue.h>
24-
2523
#define GNOME_DESKTOP_USE_UNSTABLE_API
2624
#include "gnome-datetime-source.h"
2725

2826
#include "csd-color-state.h"
2927

3028
#include "csd-night-light.h"
3129
#include "csd-night-light-common.h"
30+
#include "tz-coords.h"
3231

3332
struct _CsdNightLight {
3433
GObject parent;
@@ -39,8 +38,6 @@ struct _CsdNightLight {
3938
gboolean geoclue_enabled;
4039
GSource *source;
4140
guint validate_id;
42-
// GClueClient *geoclue_client;
43-
// GClueSimple *geoclue_simple;
4441
GSettings *location_settings;
4542
gdouble cached_sunrise;
4643
gdouble cached_sunset;
@@ -378,27 +375,6 @@ night_light_recheck (CsdNightLight *self)
378375
csd_night_light_set_temperature (self, temp_smeared);
379376
}
380377

381-
static gboolean
382-
night_light_recheck_schedule_cb (gpointer user_data)
383-
{
384-
CsdNightLight *self = CSD_NIGHT_LIGHT (user_data);
385-
night_light_recheck (self);
386-
self->validate_id = 0;
387-
return G_SOURCE_REMOVE;
388-
}
389-
390-
/* called when something changed */
391-
static void
392-
night_light_recheck_schedule (CsdNightLight *self)
393-
{
394-
if (self->validate_id != 0)
395-
g_source_remove (self->validate_id);
396-
self->validate_id =
397-
g_timeout_add_seconds (CSD_NIGHT_LIGHT_SCHEDULE_TIMEOUT,
398-
night_light_recheck_schedule_cb,
399-
self);
400-
}
401-
402378
/* called when the time may have changed */
403379
static gboolean
404380
night_light_recheck_cb (gpointer user_data)
@@ -454,89 +430,34 @@ settings_changed_cb (GSettings *settings, gchar *key, gpointer user_data)
454430
night_light_recheck (self);
455431
}
456432

457-
// static void
458-
// on_location_notify (GClueSimple *simple,
459-
// GParamSpec *pspec,
460-
// gpointer user_data)
461-
// {
462-
// CsdNightLight *self = CSD_NIGHT_LIGHT (user_data);
463-
// GClueLocation *location;
464-
// gdouble latitude, longitude;
465-
466-
// location = gclue_simple_get_location (simple);
467-
// latitude = gclue_location_get_latitude (location);
468-
// longitude = gclue_location_get_longitude (location);
469-
470-
// g_settings_set_value (self->settings,
471-
// "night-light-last-coordinates",
472-
// g_variant_new ("(dd)", latitude, longitude));
473-
474-
// g_debug ("got geoclue latitude %f, longitude %f", latitude, longitude);
475-
476-
// /* recheck the levels if the location changed significantly */
477-
// if (update_cached_sunrise_sunset (self))
478-
// night_light_recheck_schedule (self);
479-
// }
480-
481-
// static void
482-
// on_geoclue_simple_ready (GObject *source_object,
483-
// GAsyncResult *res,
484-
// gpointer user_data)
485-
// {
486-
// CsdNightLight *self = CSD_NIGHT_LIGHT (user_data);
487-
// GClueSimple *geoclue_simple;
488-
// g_autoptr(GError) error = NULL;
489-
490-
// geoclue_simple = gclue_simple_new_finish (res, &error);
491-
// if (geoclue_simple == NULL) {
492-
// if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
493-
// g_warning ("Failed to connect to GeoClue2 service: %s", error->message);
494-
// return;
495-
// }
496-
497-
// self->geoclue_simple = geoclue_simple;
498-
// self->geoclue_client = gclue_simple_get_client (self->geoclue_simple);
499-
// g_object_set (G_OBJECT (self->geoclue_client),
500-
// "time-threshold", 60*60, NULL); /* 1 hour */
501-
502-
// g_signal_connect (self->geoclue_simple, "notify::location",
503-
// G_CALLBACK (on_location_notify), user_data);
504-
505-
// on_location_notify (self->geoclue_simple, NULL, user_data);
506-
// }
507-
508-
// static void
509-
// start_geoclue (CsdNightLight *self)
510-
// {
511-
// self->cancellable = g_cancellable_new ();
512-
// gclue_simple_new (DESKTOP_ID,
513-
// GCLUE_ACCURACY_LEVEL_CITY,
514-
// self->cancellable,
515-
// on_geoclue_simple_ready,
516-
// self);
517-
518-
// }
519-
520-
// static void
521-
// stop_geoclue (CsdNightLight *self)
522-
// {
523-
// g_cancellable_cancel (self->cancellable);
524-
// g_clear_object (&self->cancellable);
525-
526-
// if (self->geoclue_client != NULL) {
527-
// gclue_client_call_stop (self->geoclue_client, NULL, NULL, NULL);
528-
// self->geoclue_client = NULL;
529-
// }
530-
// g_clear_object (&self->geoclue_simple);
531-
// }
433+
static void
434+
update_location_from_timezone (CsdNightLight *self)
435+
{
436+
GTimeZone *tz = g_time_zone_new_local ();
437+
const gchar *id = g_time_zone_get_identifier (tz);
438+
439+
for (int i = 0; i < G_N_ELEMENTS (tz_coord_list); i++)
440+
{
441+
const TZCoords *coords = &tz_coord_list[i];
442+
if (g_strcmp0 (coords->timezone, id) == 0)
443+
{
444+
g_settings_set_value (self->settings,
445+
"night-light-last-coordinates",
446+
g_variant_new ("(dd)", coords->latitude, coords->longitude));
447+
break;
448+
}
449+
}
450+
451+
g_time_zone_unref (tz);
452+
}
532453

533454
static void
534455
check_location_settings (CsdNightLight *self)
535456
{
536-
// if (g_settings_get_boolean (self->location_settings, "enabled") && self->geoclue_enabled)
537-
// start_geoclue (self);
538-
// else
539-
// stop_geoclue (self);
457+
if (g_settings_get_boolean (self->location_settings, "enabled") && self->geoclue_enabled)
458+
{
459+
update_location_from_timezone (self);
460+
}
540461
}
541462

542463
void
@@ -636,8 +557,6 @@ csd_night_light_finalize (GObject *object)
636557
{
637558
CsdNightLight *self = CSD_NIGHT_LIGHT (object);
638559

639-
// stop_geoclue (self);
640-
641560
poll_timeout_destroy (self);
642561
poll_smooth_destroy (self);
643562

plugins/color/generate-tz-header.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/python3
2+
3+
import re
4+
5+
d = {}
6+
7+
8+
with open("/usr/share/zoneinfo/zone.tab", "r") as f:
9+
for line in f:
10+
if line.startswith("#"):
11+
continue
12+
13+
res = re.search(r"([A-Z]{2})\s([0-9-+]+)\s([\w/_\-]+)\s", line)
14+
code, coords, tz = res.groups()
15+
16+
res = re.search(r"([+-]{1})([0-9]+)([+-]{1})([0-9]+)", coords)
17+
lat_sign, lat_val, long_sign, long_val = res.groups()
18+
19+
lat_str = lat_sign + lat_val[0:2] + "." + lat_val[2:]
20+
long_str = long_sign + long_val[0:3] + "." + long_val[3:]
21+
22+
lat = float(lat_str)
23+
long = float(long_str)
24+
25+
d[tz] = [lat, long]
26+
27+
header = """
28+
// Generated from /usr/share/zoneinfo/zone.tab, used by csd-nightlight.c to calculate sunrise and sunset based on the system timezone
29+
30+
typedef struct
31+
{
32+
const gchar *timezone;
33+
double latitude;
34+
double longitude;
35+
} TZCoords;
36+
37+
static TZCoords tz_coord_list[] = {
38+
"""
39+
40+
for zone in d.keys():
41+
latitude, longitude = d[zone]
42+
43+
header += " { \"%s\", %f, %f },\n" % (zone, latitude, longitude)
44+
45+
header += "};"
46+
47+
with open("tz-coords.h", "w") as f:
48+
f.write(header)
49+
50+
quit()

0 commit comments

Comments
 (0)