summaryrefslogtreecommitdiff
path: root/opplysning.c
blob: 05943d5f05e5b91e7d1b910f8e35fdd4b1e9fac2 (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
#define _POSIX_C_SOURCE 199309L

#include <err.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <libical/ical.h>
#include <raylib.h>

// Unfortunately, 0 = Sunday.
int
weekday(int year, int month, int day)
{
	struct tm tm = {
	    .tm_year = year - 1900,
	    .tm_mon = month - 1,
	    .tm_mday = day,
	};
	mktime(&tm);
	return tm.tm_wday;
}

const char *maaned[] = {
    "",	    "JANUAR", "FEBRUAR",   "MARS",    "APRIL",	  "MAI",      "JUNI",
    "JULI", "AUGUST", "SEPTEMBER", "OKTOBER", "NOVEMBER", "DESEMBER",
};
const char *ukedag[] = {
    "SØNDAG", "MANDAG", "TIRSDAG", "ONSDAG", "TORSDAG", "FREDAG", "LØRDAG",
};

enum {
	MAX_EVENTS = 64,
	SIZEOF_TITLE = 256,
};
struct event {
	int year;
	int month;
	int day;
	int wday;
	int hour;
	int minute;
	char title[SIZEOF_TITLE];
};
struct event the_events[MAX_EVENTS];
int num_events;
char *
read_stream(char *s, size_t size, void *d)
{
	return fgets(s, (int)size, (FILE *)d);
}
void
load_the_events(void)
{
	icalparser *p = icalparser_new();
	assert(p);

	FILE *f = fopen("events.ical", "r");
	assert(f);

	icalparser_set_gen_data(p, f);
	for (;;) {
		char *line = icalparser_get_line(p, read_stream);
		if (!line) {
			break;
		}

		icalcomponent *comp = icalparser_add_line(p, line);
		if (!comp) {
			continue;
		}

		for (icalcompiter i = icalcomponent_begin_component(
			 comp, ICAL_VEVENT_COMPONENT);
		     icalcompiter_deref(&i); icalcompiter_next(&i)) {
			if (MAX_EVENTS <= num_events) {
				warnx("MAX_EVENTS exceeded at %u", num_events);
				break;
			}
			icalcomponent *c = icalcompiter_deref(&i);

			struct icaltimetype dts = icalcomponent_get_dtstart(c);
			const char *sum = icalcomponent_get_summary(c);

			the_events[num_events].year = dts.year;
			the_events[num_events].month = dts.month;
			the_events[num_events].day = dts.day;
			the_events[num_events].hour = dts.hour;
			the_events[num_events].minute = dts.minute;
			strncpy(the_events[num_events].title, sum,
				SIZEOF_TITLE);
			the_events[num_events].wday =
			    weekday(dts.year, dts.month, dts.day);
			++num_events;
		}

		icalcomponent_free(comp);
	}

	icalparser_free(p);
}

const char *the_alphabet = "0123456789"
			   "ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ"
			   "abcdefghijklmnopqrstuvwxyzæøå"
			   "!@#$%^&*()[];'\\:\"|,./<>?";

void
line(int x, int y, Font f, Color c, char *s)
{
	DrawTextEx(f, s, (Vector2){x, y}, (float)f.baseSize, 2, c);
}

void
draw_hd(int x, int y, Font f, Color c, char *s)
{
	DrawTextEx(f, s, (Vector2){x, y}, (float)f.baseSize, 2, c);
}

int
main(void)
{
	load_the_events();
	for (int i = 0; i < num_events; ++i) {
		struct event *e = the_events + i;
		printf("\t%2d %s %u. %s %02u:%02u %s\n", i, ukedag[e->wday],
		       e->day, maaned[e->month], e->hour, e->minute, e->title);
	}

	const int sw = 1920;
	const int sh = 1080;

	InitWindow(sw, sh, "opplysning");

	SetTargetFPS(60);

	int ncp = 0;
	int *cp = LoadCodepoints(the_alphabet, &ncp);
	Font font_h = LoadFontEx("font/adventpro-bold.ttf", 60, cp, ncp);
	Font font_p = LoadFontEx("font/adventpro-semibold.ttf", 40, cp, ncp);
	UnloadCodepoints(cp);

	Color bg = RAYWHITE;
	Color fg = BLACK;
	Color hd = {0xf0, 0x4a, 0x00, 0xff};

	while (!WindowShouldClose()) {
		struct timespec now = {0};
		clock_gettime(CLOCK_REALTIME, &now);
		struct tm *ti = localtime(&now.tv_sec);
		char ts[64] = {0};
		snprintf(ts, sizeof(ts), "%02d:%02d", ti->tm_hour, ti->tm_min);

		BeginDrawing();
		ClearBackground(bg);
		line(sw / 2 - 60, 0, font_h, fg, ts);

		int y = 0;
		int year = 0, month = 0, day = 0;
		for (int i = 0; i < num_events; ++i) {
			struct event *e = the_events + i;
			if (year != e->year || month != e->month ||
			    day != e->day) {
				year = e->year;
				month = e->month;
				day = e->day;

				char s[64] = {0};
				snprintf(s, sizeof(s), "%s %u. %s",
					 ukedag[e->wday], e->day,
					 maaned[e->month]);
				line(20, y += 50, font_h, hd, s);
				y += 20;
			}
			{
				char s[64] = {0};
				snprintf(s, sizeof(s), "%02u:%02u %s", e->hour,
					 e->minute, e->title);
				line(25, y += 40, font_p, fg, s);
			}
		}
		EndDrawing();
	}

	CloseWindow();
}