aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 8dc859f4a85e4c158838fa0d02cb1479481fdd21 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* main.c
 *
 * Copyright 2025 Rose <rose@pinkro.se>
 *
 * 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 "configuration.h"
#include "sun.h"

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <signal.h>
#include <unistd.h>
#include <glib.h>

#ifdef USE_GCLUE
#include <geoclue.h>

static GClueAccuracyLevel accuracy_level = GCLUE_ACCURACY_LEVEL_EXACT;

GClueSimple *simple = NULL;
GClueClient *client = NULL;
#endif

GMainLoop *main_loop;

enum LocationType loctype = 0;
dictionary *dict = NULL;
bool quit = false;
bool firstrun = true;
bool isdark = false;
float lat = 0;
float lng = 0;

#ifdef USE_GCLUE
static void
get_gclue_location (GClueSimple *simple)
{
    GClueLocation *location;
    location = gclue_simple_get_location (simple);

    if (!location) {
      g_printerr ("Failed to get location through geoclue");
      exit (1);
    }

    lat = gclue_location_get_latitude (location);
    lng = gclue_location_get_longitude (location);

    return;
}
#endif

void
on_dark (void)
{
    char *home = getenv ("HOME");
    char *darklock = malloc(strlen(home)*sizeof(char)+strlen("/.cache/darkmode")*sizeof(char)+1);
    sprintf (darklock, "%s/.cache/darkmode", home);
    if (firstrun) {
      if (home != NULL) {
          if (access(darklock, F_OK) == 0) {
              free (darklock);
              isdark = true;
              return;
          }
      }
    } else if (isdark) {
      return;
    }

    const char *dark_command = iniparser_getstring (dict, "dark:cmd", "");
    if (sizeof (dark_command) == sizeof ("")) {
        g_printerr ("No command for dark mode specified!");
        return;
    }
    int rtrn = system (dark_command);
    if (rtrn != 0) {
        g_printerr("Dark command failed with non-zero exit code!");
        free (darklock);
        return;
    }
    FILE *dark = fopen (darklock, "w");
    fclose (dark);
    free (darklock);
    isdark = true;
}

void
on_light (void)
{
    char *home = getenv("HOME");
    char *darklock = malloc(strlen(home)*sizeof(char)+strlen("/.cache/darkmode")*sizeof(char)+1);
    sprintf (darklock, "%s/.cache/darkmode", home);
    if (firstrun) {
      if (home != NULL) {
          if (access(darklock, F_OK) != 0) {
              free (darklock);
              isdark = true;
              return;
          }
      }
    } else if (!isdark) {
      return;
    }

    const char *light_command = iniparser_getstring (dict, "light:cmd", "");
    if (sizeof (light_command) == sizeof ("")) {
        g_printerr ("No command for light mode specified!");
        return;
    }
    int rtrn = system (light_command);
    if (rtrn != 0) {
        g_printerr("Light command failed with non-zero exit code!");
        free (darklock);
        return;
    }

    remove (darklock);
    free (darklock);
    isdark = false;
}

void
loop (void)
{
        float sunrise = getSunrise (lat, lng);
        double hours;
        float minutes = modf (sunrise, &hours)*60;
        g_print ("Sunrise: %.0f:%.0f\n", hours, minutes);

        float sunset = getSunset (lat, lng);
        minutes = modf (sunset, &hours)*60;
        g_print ("Sunset: %.0f:%.0f\n", hours, minutes);

        if (isDark (sunrise, sunset))
            on_dark ();
        else
            on_light ();
        firstrun = false;
}

void
handleExit(int sig) {
    quit = true;
    signal(sig, SIG_IGN);
    return;
}

gint
main (gint   argc,
      gchar *argv[])
{

    g_autoptr(GError) error = NULL;
    g_autoptr(GOptionContext) context = NULL;

    gdouble locLat = 100;
    gdouble locLng = 100;

    GOptionEntry entries[] = {
        { "latitude", 'l', 0, G_OPTION_ARG_DOUBLE, &locLat, "Override the latitude", "lat" },
        { "longitude", 'o', 0, G_OPTION_ARG_DOUBLE, &locLng, "Override the longitude", "long" },
        G_OPTION_ENTRY_NULL
    };

    context = g_option_context_new ("- automatically switch between dark and light mode");
    g_option_context_add_main_entries (context, entries, NULL);
    if (!g_option_context_parse (context, &argc, &argv, &error)) {
        g_print ("option parsing failed: %s\n", error->message);
        exit (1);
    }

    dict = load_config ();
    loctype = config_get_location_type (dict);

    if (loctype == MANUAL) {
        lat = config_get_latitude (dict);
        lng = config_get_longitude (dict);
    } else {
#if defined USE_GCLUE
        get_gclue_location (gclue_simple_new_sync ("autodarkmode",
                            accuracy_level,
                            NULL,
                            NULL));
        g_print ("lat: %f\n", lat);
        g_print ("lng: %f\n", lng);
#else
        fprintf (stderr, "ERROR: autodarkmode was not compiled with gclue support.\n");
        iniparser_freedict (dict);
        return 1;
#endif
    }

    lat = locLat == 100 ? lat : locLat;
    lng = locLng == 100 ? lng : locLng;

    signal(SIGINT, handleExit);
    while (!quit) {
        loop();
        sleep (60);
    }

    iniparser_freedict (dict);

    return EXIT_SUCCESS;
}