]> Pileus Git - lackey/commitdiff
Start on events and calendars
authorAndy Spencer <andy753421@gmail.com>
Fri, 12 Oct 2012 03:50:36 +0000 (03:50 +0000)
committerAndy Spencer <andy753421@gmail.com>
Fri, 12 Oct 2012 04:47:46 +0000 (04:47 +0000)
cal/dummy.c [new file with mode: 0644]
makefile
src/event.c [new file with mode: 0644]
src/event.h [new file with mode: 0644]
src/main.c
src/main.h
src/screen.c
src/util.h
view/week.c
view/year.c

diff --git a/cal/dummy.c b/cal/dummy.c
new file mode 100644 (file)
index 0000000..da4b8ee
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
+ * 
+ * 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, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+
+#include "main.h"
+#include "event.h"
+
+/* Test data */
+static cal_t cal = {
+       .name  = "dummy",
+       .desc  = "dummy calendar",
+       .data  = NULL,
+};
+
+static event_t event = {
+       .cal   = &cal,
+       .start = {2012, OCT, 0, 12, 0},
+       .end   = {2012, OCT, 0, 13, 0},
+       .name  = "dummy event",
+       .desc  = "this event is random and does not exist",
+       .next  = NULL,
+};
+
+static event_t events[8];
+
+/* Event functions */
+event_t *dummy_get(cal_t *cal, year_t year, month_t month, day_t day, int days)
+{
+       for (int i = 0; i < N_ELEMENTS(events); i++) {
+               datetime_t *s = &events[i].start;
+               datetime_t *e = &events[i].end;
+               events[i] = event;
+               add_days(&s->year, &s->month, &s->day, 7*i);
+               add_days(&e->year, &e->month, &e->day, 7*i);
+               if (i+1 < N_ELEMENTS(events))
+                       events[i].next = &events[i+1];
+       }
+       return &events[0];
+}
index 46df9566fb8537951519d743e17a498ad9d4fe26..08012b62af8ce77ec0b9d175d79b2593aee50cd2 100644 (file)
--- a/makefile
+++ b/makefile
@@ -9,9 +9,10 @@ LDFLAGS  ?= -lncursesw
 # Sources
 PROG      = lackey
 TEST      = test
-SOURCES   = main screen util
+SOURCES   = main screen event util
 TESTS     = test util
 VIEWS     = day week month year todo notes settings help
+CALS      = dummy
 
 # Targets
 all: $(PROG) $(TEST)
@@ -27,10 +28,10 @@ clean:
        rm -f src/*.o view/*.o $(PROG) $(TEST) 
 
 # Rules
-$(PROG): $(SOURCES:%=src/%.o) $(VIEWS:%=view/%.o)
+$(PROG): $(SOURCES:%=src/%.o) $(VIEWS:%=view/%.o) $(CALS:%=cal/%.o)
        $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS)
 
-$(TEST): $(TESTS:%=src/%.o) $(VIEWS:%=view/%.o)
+$(TEST): $(TESTS:%=src/%.o) $(VIEWS:%=view/%.o) $(CALS:%=cal/%.o)
        $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS)
 
 %.o: %.c $(SOURCES:%=src/%.h) makefile
diff --git a/src/event.c b/src/event.c
new file mode 100644 (file)
index 0000000..ed3a343
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
+ * 
+ * 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, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "util.h"
+#include "event.h"
+
+/* Event get */
+event_t *event_get(year_t year, month_t month, day_t day, int days)
+{
+       return dummy_get(0, year, month, day, days);
+}
diff --git a/src/event.h b/src/event.h
new file mode 100644 (file)
index 0000000..501741d
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
+ * 
+ * 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, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef EVENT_H
+#define EVENT_H
+
+#include "util.h"
+
+/* Calendar types */
+typedef struct {
+       char *name;
+       char *desc;
+       void *data;
+} cal_t;
+
+/* Event types */
+typedef struct {
+       year_t  year;
+       month_t month;
+       day_t   day;
+       hour_t  hour;
+       min_t   min;
+} datetime_t;
+
+typedef struct event_t {
+       const cal_t    *cal;
+       datetime_t      start;
+       datetime_t      end;
+       const char     *name;
+       const char     *desc;
+       struct event_t *next;
+} event_t;
+
+/* Event functions */
+event_t *event_get(year_t year, month_t month, day_t day, int days);
+
+/* Calendar implementation functions */
+event_t *dummy_get(cal_t *cal, year_t year, month_t month, day_t day, int days);
+
+#endif
index 20bea55e766185ee350f0ab0e1982231b4ab5b74..6daa8f8cfcfcdf9770065c6948758d397ce1ffb5 100644 (file)
 #include "screen.h"
 
 /* Debugging */
-year_t  YEAR  = 2012;
-month_t MONTH = 8;
-day_t   DAY   = 29;
+year_t   YEAR   = 2012;
+month_t  MONTH  = 8;
+day_t    DAY    = 29;
+
+event_t *EVENTS = NULL;
 
 /* Static data */
 static FILE *debug_fd = NULL;
@@ -74,9 +76,11 @@ int main(int argc, char **argv)
        /* Time setup */
        time_t sec = time(NULL);
        struct tm *tm = localtime(&sec);
-       YEAR  = tm->tm_year+1900;
-       MONTH = tm->tm_mon;
-       DAY   = tm->tm_mday-1;
+       YEAR   = tm->tm_year+1900;
+       MONTH  = tm->tm_mon;
+       DAY    = tm->tm_mday-1;
+
+       EVENTS = event_get(2012, JAN, 0, 366);
 
        /* Curses setup */
        setlocale(LC_ALL, "");
@@ -92,6 +96,12 @@ int main(int argc, char **argv)
        screen_init();
        screen_draw();
 
+       /* Debug */
+       for (event_t *e = EVENTS; e; e = e->next)
+               debug("event: %04d-%02d-%02d %02d:%02d: %s - %s\n",
+                               e->start.year, e->start.month, e->start.day,
+                               e->start.hour, e->start.min, e->name, e->desc);
+
        /* Run */
        while (1) {
                MEVENT btn;
index d1d4511e82f89094282c788a8929876e1f50bf4d..cdc5c8276ea8d5170a6da939f15383c9046bbf8e 100644 (file)
 #define COLOR_ERROR 2
 
 #include "util.h"
+#include "event.h"
 
 /* Debugging */
-extern year_t  YEAR;
-extern month_t MONTH;
-extern day_t   DAY;
+extern year_t   YEAR;
+extern month_t  MONTH;
+extern day_t    DAY;
+
+extern event_t *EVENTS;
 
 /* Debug functions */
 int debug(char *fmt, ...);
index feb23c6c7bd213dbc2429942ca2aa158545e4ace..c1c655aa455816cbb766cfa2049afe2dd9d00b80 100644 (file)
@@ -45,7 +45,7 @@ view_t views[] = {
        { "Help",     help_init,     help_draw,     help_run,     {KEY_F(8), '8', '?'} },
 };
 
-int active = 0;
+int active = 1;
 
 /* Local functions */
 void draw_header(void)
index f073205352529f7cf0977a27261c8ea9aad87e65..9b2755162379e9e3160c5d554c6b6ba50295e039 100644 (file)
@@ -27,6 +27,8 @@
 /* Time types */
 typedef int year_t;
 typedef int day_t;
+typedef int hour_t;
+typedef int min_t;
 
 typedef enum {
        JAN =  0,
index bde7608ff031bf41d90c8262e632938bae46d0d3..40d5b086406620d1c1e506f5dbbc894188b90e99 100644 (file)
 /* Static data */
 static WINDOW *win;
 
+/* Local functions */
+static void print_event(event_t *event, wday_t day, hour_t hour, min_t min, float hstep)
+{
+       int x = 6+ROUND(day*hstep);
+       int y = 3+hour*4;
+       int l = (event->end.min - event->start.min)/15;
+       mvwprintw(win, y, x, "%s", event->name);
+       debug("event: %s\n", event->name);
+}
+
+static int before(datetime_t *start, int year, int month, int day, int hour, int min)
+{
+       int rval = start->year  < year  ? 1 : start->year  > year ? 0 :
+                  start->month < month ? 1 : start->month > month? 0 :
+                  start->day   < day   ? 1 : start->day   > day  ? 0 :
+                  start->hour  < hour  ? 1 : start->hour  > hour ? 0 :
+                  start->min   < min   ? 1 : start->min   > min  ? 0 : 0;
+       debug("%04d-%02d-%02d %02d:%02d < %04d-%02d-%02d %02d:%02d == %d\n",
+                       start->year, start->month, start->day, start->hour, start->min,
+                       year, month, day, hour, min, rval);
+       return rval;
+}
+
 /* Week init */
 void week_init(WINDOW *_win)
 {
@@ -61,7 +84,7 @@ void week_draw(void)
        }
 
        /* Print times */
-       int start = 8;
+       hour_t start = 8;
        for (int h = 0; h < (LINES-6)/4+1; h++)
                mvwprintw(win, 3+h*4, 0,"%02d:%02d", (start+h)%12, 0);
 
@@ -70,6 +93,22 @@ void week_draw(void)
        for (int d = 0; d < 7; d++)
                mvwvline(win, y, x+ROUND(d*hstep)-1, ACS_VLINE, LINES-y-2);
 
+       /* Print events */
+       event_t *event = EVENTS;
+       add_days(&year, &month, &day, -7);
+       for (int d = 0; d <  7; d++) {
+               for (int h = 0; h < (LINES-6)/4+1; h++) {
+                       for (int m = 0; m < 60; m+=15) {
+                               while (event && before(&event->start, year, month, day, start+h+(m+15)/60, (m+15)%60)) {
+                                       if (!before(&event->start, year, month, day, start+h, m))
+                                               print_event(event, d, h, m, hstep);
+                                       event = event->next;
+                               }
+                       }
+               }
+               add_days(&year, &month, &day, 1);
+       }
+
        /* Draw today */
        int l = x+ROUND((shift+0)*hstep)-1;
        int r = x+ROUND((shift+1)*hstep)-1;
index 9746261809623bfaf5d6a58a9fe05f4bf29870e9..16522c836534d22e158a330c380df38f36c30b05 100644 (file)
@@ -100,7 +100,9 @@ void year_draw(void)
 /* Year run */
 int year_run(int key, mmask_t btn, int row, int col)
 {
-       int d = DAY, m = MONTH, y = YEAR;
+       day_t d = DAY;
+       month_t m = MONTH;
+       year_t y = YEAR;
        wday_t day = day_of_week(YEAR, MONTH, DAY);
        int week = (start_of_month(y, m) + d) / 7;
        int dir = 0;