summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Stensgård <mastensg@mastensg.net>2025-04-18 20:02:44 +0200
committerMartin Stensgård <mastensg@mastensg.net>2025-04-18 20:02:44 +0200
commite3e1751aaaaa39d1c6453d138aeceabf55fcfed9 (patch)
treec20f32a75d9fbcc10f56042f7c87091e7cbb9024
parent6888113d42ba2e1e4c0690e82e61092872cdffb3 (diff)
load the events
-rw-r--r--.gitignore1
-rw-r--r--opplysning.c103
2 files changed, 103 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 4c3ad24..f9c165e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
*.swp
/check_ical
/check_ical_out.txt
+/events.ical
/opplysning
core
diff --git a/opplysning.c b/opplysning.c
index ab87995..163db47 100644
--- a/opplysning.c
+++ b/opplysning.c
@@ -1,5 +1,6 @@
#define _POSIX_C_SOURCE 199309L
+#include <err.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
@@ -7,8 +8,101 @@
#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 {
+ uint32_t year;
+ uint32_t month;
+ uint32_t day;
+ uint32_t wday;
+ uint32_t hour;
+ uint32_t 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æøå"
@@ -23,6 +117,13 @@ line(int x, int y, Font f, Color c, char *s)
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;
@@ -49,7 +150,7 @@ main(void)
BeginDrawing();
ClearBackground(bg);
- // DrawFPS(1920-100, 20);
+ // DrawFPS(1920 - 100, 20);
int y = 0;
line(sw / 2 - 60, 0, font_h, fg, ts);
line(20, y += 60, font_h, hd, "FREDAG 18. APRIL");