From: Andy Spencer Date: Fri, 12 Oct 2012 03:50:36 +0000 (+0000) Subject: Start on events and calendars X-Git-Url: http://pileus.org/git/?p=lackey;a=commitdiff_plain;h=c222556b46998f2eb2466ded089c1b01091c88a6 Start on events and calendars --- diff --git a/cal/dummy.c b/cal/dummy.c new file mode 100644 index 0000000..da4b8ee --- /dev/null +++ b/cal/dummy.c @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2012 Andy Spencer + * + * 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 . + */ + +#include + +#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]; +} diff --git a/makefile b/makefile index 46df956..08012b6 100644 --- 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 index 0000000..ed3a343 --- /dev/null +++ b/src/event.c @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2012 Andy Spencer + * + * 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 . + */ + +#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 index 0000000..501741d --- /dev/null +++ b/src/event.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2012 Andy Spencer + * + * 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 . + */ + +#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 diff --git a/src/main.c b/src/main.c index 20bea55..6daa8f8 100644 --- a/src/main.c +++ b/src/main.c @@ -26,9 +26,11 @@ #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; diff --git a/src/main.h b/src/main.h index d1d4511..cdc5c82 100644 --- a/src/main.h +++ b/src/main.h @@ -22,11 +22,14 @@ #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, ...); diff --git a/src/screen.c b/src/screen.c index feb23c6..c1c655a 100644 --- a/src/screen.c +++ b/src/screen.c @@ -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) diff --git a/src/util.h b/src/util.h index f073205..9b27551 100644 --- a/src/util.h +++ b/src/util.h @@ -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, diff --git a/view/week.c b/view/week.c index bde7608..40d5b08 100644 --- a/view/week.c +++ b/view/week.c @@ -26,6 +26,29 @@ /* 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; diff --git a/view/year.c b/view/year.c index 9746261..16522c8 100644 --- a/view/year.c +++ b/view/year.c @@ -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;