aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 4265d3f04823f8a9878963dad9e465594923a029 (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
/* 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 "configuration.h"
#include "sun.h"

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

static GClueAccuracyLevel accuracy_level = GCLUE_ACCURACY_LEVEL_EXACT;

GClueSimple *simple = NULL;
GClueClient *client = NULL;
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;

static void
get_gclue_location (GClueSimple *simple)
{
    GClueLocation *location;
    location = gclue_simple_get_location (simple);
    lat = gclue_location_get_latitude (location);
    lng = gclue_location_get_longitude (location);

    return;
}

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[])
{

    dict = load_config ();
    loctype = config_get_location_type (dict);
    g_print("%d\n", loctype);

    if (loctype == MANUAL) {
        lat = config_get_latitude (dict);
        lng = config_get_longitude (dict);
    } else {
        get_gclue_location (gclue_simple_new_sync ("autodarkmode",
                            accuracy_level,
                            NULL,
                            NULL));
        g_print ("lat: %f\n", lat);
        g_print ("lng: %f\n", lng);
    }

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

    iniparser_freedict (dict);

	return EXIT_SUCCESS;
}