From ad8ba2db29c0f3abecb292b1059dad554bf7f3b5 Mon Sep 17 00:00:00 2001 From: axtloss Date: Tue, 28 May 2024 14:30:34 +0200 Subject: add geoclue and glib date --- meson.build | 5 +- src/main.c | 119 +++++++++++++++++++++++++++++++++++++++----- src/meson.build | 1 + src/sun.c | 133 ++++++++------------------------------------------ src/sun.h | 7 ++- xen.nya.autodark.json | 7 +-- 6 files changed, 138 insertions(+), 134 deletions(-) diff --git a/meson.build b/meson.build index ae62c25..0af50b3 100644 --- a/meson.build +++ b/meson.build @@ -1,7 +1,7 @@ project('autodarkmode', 'c', version: '0.1.0', meson_version: '>= 0.62.0', - default_options: [ 'warning_level=2', 'werror=false', 'c_std=gnu11', ], + default_options: [ 'warning_level=2', 'werror=false', 'c_std=gnu99', ], ) cc = meson.get_compiler('c') @@ -14,7 +14,6 @@ add_project_arguments(['-I' + meson.project_build_root()], language: 'c') project_c_args = ['-lm',] test_c_args = [ '-Wcast-align', - '-Wdeclaration-after-statement', '-Werror=address', '-Werror=array-bounds', '-Werror=empty-body', @@ -36,7 +35,6 @@ test_c_args = [ '-Werror=redundant-decls', '-Werror=return-type', '-Werror=sequence-point', - '-Werror=shadow', '-Werror=strict-prototypes', '-Werror=trigraphs', '-Werror=undef', @@ -78,7 +76,6 @@ foreach arg: test_c_args endforeach add_project_arguments(project_c_args, language: 'c') add_project_arguments('-lm', language: 'c') -# add_project_arguments('-fsanitize=undefined,address', language: 'c') subdir('src') diff --git a/src/main.c b/src/main.c index 34f528a..4e459bf 100644 --- a/src/main.c +++ b/src/main.c @@ -23,6 +23,102 @@ #include #include +#include +#include +#include + +static gint timeout = 30; /* seconds */ +static GClueAccuracyLevel accuracy_level = GCLUE_ACCURACY_LEVEL_EXACT; +static gint time_threshold; + GClueSimple *simple = NULL; + GClueClient *client = NULL; + GMainLoop *main_loop; + +static gboolean +on_location_timeout (gpointer user_data) +{ + g_clear_object (&client); + g_clear_object (&simple); + g_main_loop_quit (main_loop); + + return FALSE; +} + +static void +print_location (GClueSimple *simple) +{ + GClueLocation *location; + + GDateTime *current_time = g_date_time_new_now_local(); + + location = gclue_simple_get_location (simple); + g_print ("\nNew location:\n"); + g_print ("Latitude: %f°\nLongitude: %f°\nAccuracy: %f meters\n", + gclue_location_get_latitude (location), + gclue_location_get_longitude (location), + gclue_location_get_accuracy (location)); + + int year, month, day; + g_date_time_get_ymd (current_time, &year, &month, &day); + g_print ("%d - %d - %d\n", year, month, day); + + int utc_offset = g_date_time_get_utc_offset (current_time)*2.7778*pow(10.0, -10.0); + + float localT = calculateSun(year, month, day, gclue_location_get_latitude (location), gclue_location_get_longitude (location), utc_offset, 0); + double hours; + float minutes = modf(localT,&hours)*60; + g_print("Sunrise: %.0f:%.0f\n",hours,minutes); + + float sunsetT = calculateSun(year, month, day, gclue_location_get_latitude (location), gclue_location_get_longitude (location), utc_offset, 1); + double sunHours; + float sunMinutes = modf(sunsetT,&sunHours)*60; + g_print("Sunset: %.0f:%.0f\n",sunHours,sunMinutes); + + g_free (current_time); +} + +static void +on_client_active_notify (GClueClient *client, + GParamSpec *pspec, + gpointer user_data) +{ + if (gclue_client_get_active (client)) + return; + + g_print ("Geolocation disabled. Quitting..\n"); + on_location_timeout (NULL); +} + +static void +on_simple_ready (GObject *source_object, + GAsyncResult *res, + gpointer user_data) +{ + GError *error = NULL; + + simple = gclue_simple_new_with_thresholds_finish (res, &error); + if (error != NULL) { + g_critical ("Failed to connect to GeoClue2 service: %s", error->message); + exit (-1); + } + client = gclue_simple_get_client (simple); + if (client) { + g_object_ref (client); + g_print ("Client object: %s\n", + g_dbus_proxy_get_object_path (G_DBUS_PROXY (client))); + + g_signal_connect (client, + "notify::active", + G_CALLBACK (on_client_active_notify), + NULL); + } + print_location (simple); + + g_signal_connect (simple, + "notify::location", + G_CALLBACK (print_location), + NULL); +} gint main (gint argc, @@ -51,21 +147,18 @@ main (gint argc, return EXIT_SUCCESS; } - int year = 2024, month = 5, day = 28; - double latitude = 51.01; - double longitude = 7.563; - - float localT = calculateSunrise(year, month, day, latitude, longitude, 2); - double hours; - float minutes = modf(localT,&hours)*60; - g_print("Sunrise: %.0f:%.0f\n",hours,minutes); + g_timeout_add_seconds (timeout, on_location_timeout, NULL); - float sunsetT = calculateSunset(year, month, day, latitude, longitude, 2); - double sunHours; - float sunMinutes = modf(sunsetT,&sunHours)*60; - g_print("Sunset: %.0f:%.0f\n",sunHours,sunMinutes); + gclue_simple_new_with_thresholds ("autodarkmode", + accuracy_level, + time_threshold, + 0, + NULL, + on_simple_ready, + NULL); - return 0; + main_loop = g_main_loop_new (NULL, FALSE); + g_main_loop_run (main_loop); return EXIT_SUCCESS; } diff --git a/src/meson.build b/src/meson.build index 4848423..7668291 100644 --- a/src/meson.build +++ b/src/meson.build @@ -5,6 +5,7 @@ autodarkmode_sources = [ autodarkmode_deps = [ dependency('glib-2.0'), + dependency('libgeoclue-2.0'), ] executable('autodarkmode', autodarkmode_sources, diff --git a/src/sun.c b/src/sun.c index f08dfd8..8d5a7e6 100644 --- a/src/sun.c +++ b/src/sun.c @@ -1,148 +1,59 @@ #include #include -#define PI 3.1415926 +#include "sun.h" #define ZENITH -.83 -float calculateSunrise(int year,int month,int day,float lat, float lng,int localOffset) { - /* - localOffset will be <0 for western hemisphere and >0 for eastern hemisphere - daylightSavings should be 1 if it is in effect during the summer otherwise it should be 0 - */ - //1. first calculate the day of the year - float N1 = floor(275 * month / 9); - float N2 = floor((month + 9) / 12); - float N3 = (1 + floor((year - 4 * floor(year / 4) + 2) / 3)); - float N = N1 - (N2 * N3) + day - 30; - - //2. convert the longitude to hour value and calculate an approximate time - float lngHour = lng / 15.0; - float t = N + ((6 - lngHour) / 24); - - //3. calculate the Sun's mean anomaly - float M = (0.9856 * t) - 3.289; - - //4. calculate the Sun's true longitude - float L = fmod(M + (1.916 * sin((PI/180)*M)) + (0.020 * sin(2 *(PI/180) * M)) + 282.634,360.0); - - if ( L < 0 ) - L = L+360; - else if ( L > 360) - L = L-360; - - //5a. calculate the Sun's right ascension - float RA = fmod(180/PI*atan(0.91764 * tan((PI/180)*L)),360.0); - - if ( RA < 0 ) - RA = RA+360; - else if ( RA > 360) - RA = RA-360; - - //5b. right ascension value needs to be in the same quadrant as L - float Lquadrant = floor( L/90) * 90; - float RAquadrant = floor(RA/90) * 90; - RA = RA + (Lquadrant - RAquadrant); - - //5c. right ascension value needs to be converted into hours - RA = RA / 15; - - //6. calculate the Sun's declination - float sinDec = 0.39782 * sin((PI/180)*L); - float cosDec = cos(asin(sinDec)); - - //7a. calculate the Sun's local hour angle - float cosH = (sin((PI/180)*ZENITH) - (sinDec * sin((PI/180)*lat))) / (cosDec * cos((PI/180)*lat)); - - if (cosH > 1) - g_print("Never rising\n"); - else if (cosH < -1) - g_print("Never setting\n"); - - //7b. finish calculating H and convert into hours - float H = 360 - (180/PI)*acos(cosH); // if if rising time is desired: - - H = H / 15; - - //8. calculate local mean time of rising/setting - float T = H + RA - (0.06571 * t) - 6.622; - - //9. adjust back to UTC - float UT = fmod(T - lngHour,24.0); +float to_rad (float n) { + return (M_PI/180) * n; +} - if ( UT < 0 ) - UT = UT+24; - else if (UT > 24) - UT = UT-24; +float to_deg (float n) { + return (180/M_PI) * n; +} - //10. convert UT value to local time zone of latitude/longitude - return UT + localOffset; +// http://edwilliams.org/sunrise_sunset_algorithm.htm +// if the sunset is desired, set sunset to >= 1 +// returns -1 if the sun never rises/sets in the specified location +float calculateSun(int year, int month, int day, float lat, float lng, int localOffset, int sunset) { - } + if (sunset > 1) + sunset = 1; -float calculateSunset(int year,int month,int day,float lat, float lng,int localOffset) { - /* - localOffset will be <0 for western hemisphere and >0 for eastern hemisphere - daylightSavings should be 1 if it is in effect during the summer otherwise it should be 0 - */ - //1. first calculate the day of the year float N1 = floor(275 * month / 9); float N2 = floor((month + 9) / 12); float N3 = (1 + floor((year - 4 * floor(year / 4) + 2) / 3)); float N = N1 - (N2 * N3) + day - 30; - //2. convert the longitude to hour value and calculate an approximate time float lngHour = lng / 15.0; - float t = N + ((18 - lngHour) / 24); //if setting time is desired: + float t = N + ((sunset == 0 ? 6 : 18 - lngHour) / 24); - //3. calculate the Sun's mean anomaly float M = (0.9856 * t) - 3.289; - //4. calculate the Sun's true longitude - float L = fmod(M + (1.916 * sin((PI/180)*M)) + (0.020 * sin(2 *(PI/180) * M)) + 282.634,360.0); - - if ( L < 0 ) - L = L+360; - else if ( L > 360) - L = L-360; + float L = fmod(M + (1.916 * sin(to_rad(M))) + (0.020 * sin(2 *to_rad(M))) + 282.634,360.0); - //5a. calculate the Sun's right ascension - float RA = fmod(180/PI*atan(0.91764 * tan((PI/180)*L)),360.0); + float RA = fmod(to_deg(atan(0.91764 * tan(to_rad(L)))),360.0); - if ( RA < 0 ) - RA = RA+360; - else if ( RA > 360) - RA = RA-360; - - //5b. right ascension value needs to be in the same quadrant as L float Lquadrant = floor( L/90) * 90; float RAquadrant = floor(RA/90) * 90; RA = RA + (Lquadrant - RAquadrant); - - //5c. right ascension value needs to be converted into hours RA = RA / 15; - //6. calculate the Sun's declination - float sinDec = 0.39782 * sin((PI/180)*L); + float sinDec = 0.39782 * sin(to_rad(L)); float cosDec = cos(asin(sinDec)); - //7a. calculate the Sun's local hour angle - float cosH = (sin((PI/180)*ZENITH) - (sinDec * sin((PI/180)*lat))) / (cosDec * cos((PI/180)*lat)); + float cosH = (sin(to_rad(ZENITH)) - (sinDec * sin(to_rad(lat)))) / (cosDec * cos(to_rad(lat))); if (cosH > 1) - g_print("Never rising\n"); + return -1; else if (cosH < -1) - g_print("Never setting\n"); - + return -1; - //7b. finish calculating H and convert into hours - - float H = (180/PI)*acos(cosH); // if setting time is desired: + float H = sunset == 0 ? (360 - to_deg(acos(cosH))) : to_deg(acos(cosH)); H = H / 15; - //8. calculate local mean time of rising/setting float T = H + RA - (0.06571 * t) - 6.622; - //9. adjust back to UTC float UT = fmod(T - lngHour,24.0); if ( UT < 0 ) @@ -150,7 +61,5 @@ float calculateSunset(int year,int month,int day,float lat, float lng,int localO else if (UT > 24) UT = UT-24; - //10. convert UT value to local time zone of latitude/longitude return UT + localOffset; - } diff --git a/src/sun.h b/src/sun.h index 0eeb187..9a571e8 100644 --- a/src/sun.h +++ b/src/sun.h @@ -1,2 +1,5 @@ -float calculateSunrise(int year,int month,int day,float lat, float lng,int localOffset); -float calculateSunset(int year,int month,int day,float lat, float lng,int localOffset); +float to_rad (float n); +float to_deg (float n); + +float calculateSun(int year,int month,int day,float lat, float lng,int localOffset, int sunset); + diff --git a/xen.nya.autodark.json b/xen.nya.autodark.json index 1ef1a21..5fadee4 100644 --- a/xen.nya.autodark.json +++ b/xen.nya.autodark.json @@ -1,10 +1,11 @@ { "id" : "xen.nya.autodark", - "runtime" : "org.freedesktop.Platform", - "runtime-version" : "23.08", - "sdk" : "org.freedesktop.Sdk", + "runtime" : "org.gnome.Platform", + "runtime-version" : "46", + "sdk" : "org.gnome.Sdk", "command" : "autodarkmode", "finish-args" : [ + "--system-talk-name=org.freedesktop.GeoClue2.*" ], "cleanup" : [ "/include", -- cgit v1.2.3