aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 4e459bfec592a1f1c76b449d8b5c317f641395f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* main.c
 *
 * Copyright 2024 Unknown
 *
 * This program 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, version 3 of the License only.
 *
 * This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "config.h"
#include "sun.h"
#include<math.h>

#include <glib.h>
#include <stdlib.h>
#include <locale.h>
#include <glib/gi18n.h>
#include <geoclue.h>

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,
      gchar *argv[])
{
	g_autoptr(GOptionContext) context = NULL;
	g_autoptr(GError) error = NULL;
	gboolean version = FALSE;
	GOptionEntry main_entries[] = {
		{ "version", 0, 0, G_OPTION_ARG_NONE, &version, "Show program version" },
		{ NULL }
	};

	context = g_option_context_new ("- my command line tool");
	g_option_context_add_main_entries (context, main_entries, NULL);

	if (!g_option_context_parse (context, &argc, &argv, &error))
	{
		g_printerr ("%s\n", error->message);
		return EXIT_FAILURE;
	}

	if (version)
	{
		g_printerr ("%s\n", PACKAGE_VERSION);
		return EXIT_SUCCESS;
	}

    g_timeout_add_seconds (timeout, on_location_timeout, NULL);

    gclue_simple_new_with_thresholds ("autodarkmode",
                                      accuracy_level,
                                      time_threshold,
                                      0,
                                      NULL,
                                      on_simple_ready,
                                      NULL);

    main_loop = g_main_loop_new (NULL, FALSE);
    g_main_loop_run (main_loop);

	return EXIT_SUCCESS;
}