#include <stdlib.h>
-#include "main.h"
+#include "util.h"
+#include "date.h"
#include "event.h"
/* Test data */
-default: run-lackey
+CFLAGS = -g -Wall -Werror --std=c99
+default: all run-lackey
# Sources
PROG = lackey
+PROG_SRC = main screen date event util
TEST = test
-SOURCES = main screen event util
-TESTS = test util
+TEST_SRC = test date
VIEWS = day week month year todo notes settings help
CALS = dummy
./$<
clean:
- rm -f src/*.o view/*.o $(PROG) $(TEST)
+ rm -f src/*.o view/*.o cal/*.o $(PROG) $(TEST)
# Rules
-$(PROG): $(SOURCES:%=src/%.o) $(VIEWS:%=view/%.o) $(CALS:%=cal/%.o)
+$(PROG): $(PROG_SRC:%=src/%.o) $(VIEWS:%=view/%.o) $(CALS:%=cal/%.o)
$(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS)
-$(TEST): $(TESTS:%=src/%.o) $(VIEWS:%=view/%.o) $(CALS:%=cal/%.o)
+$(TEST): $(TEST_SRC:%=src/%.o) $(CALS:%=cal/%.o)
$(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS)
-%.o: %.c $(SOURCES:%=src/%.h) makefile
+%.o: %.c $(wildcard src/*.h) makefile
$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
--- /dev/null
+/*
+ * 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/>.
+ */
+
+/* Time Keeping Bugs Abound! */
+
+#include <stdio.h>
+#include <time.h>
+
+#include "date.h"
+
+/* Global data */
+year_t YEAR;
+month_t MONTH;
+day_t DAY;
+
+/* Initialize */
+void date_init(void)
+{
+ time_t sec = time(NULL);
+ struct tm *tm = localtime(&sec);
+
+ YEAR = tm->tm_year+1900;
+ MONTH = tm->tm_mon;
+ DAY = tm->tm_mday-1;
+}
+
+/* Time functions */
+int is_leap_year(year_t year)
+{
+ return (year % 400 == 0) ? 1 :
+ (year % 100 == 0) ? 0 :
+ (year % 4 == 0) ? 1 : 0;
+}
+
+int days_in_year(year_t year)
+{
+ return 365 + is_leap_year(year);
+}
+
+int days_in_month(year_t year, month_t month)
+{
+ static int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
+ int days = mdays[month];
+ if (month == FEB)
+ days += is_leap_year(year);
+ return days;
+}
+
+int weeks_in_month(year_t year, month_t month)
+{
+ int start = start_of_month(year, month);
+ int days = days_in_month(year, month);
+ return ((start + days)-1) / 7 + 1;
+}
+
+wday_t day_of_week(year_t year, month_t month, day_t day)
+{
+ static int tmp[] = {0, 3, 2, 5, 0, 3,
+ 5, 1, 4, 6, 2, 4};
+ if (month < MAR)
+ year--;
+ int start = year + year / 4
+ - year / 100
+ + year / 400
+ + tmp[month];
+ return (start + day + 1) % 7;
+}
+
+wday_t start_of_month(year_t year, month_t month)
+{
+ return day_of_week(year, month, 0);
+}
+
+void add_days(year_t *year, month_t *month, day_t *day, int days)
+{
+ time_t time = mktime(&(struct tm){
+ .tm_year = *year-1900,
+ .tm_mon = *month,
+ .tm_mday = *day+1,
+ .tm_hour = 12});
+ time += days*24*60*60;
+ struct tm *tm = localtime(&time);
+ *year = tm->tm_year+1900;
+ *month = tm->tm_mon;
+ *day = tm->tm_mday-1;
+}
+
+void add_months(year_t *year, month_t *month, int months)
+{
+ int total = *year*12 + *month + months;
+ *year = total / 12;
+ *month = total % 12;
+}
+
+/* Debug functions */
+const char *month_to_str(month_t month)
+{
+ static const char *map[] =
+ { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", };
+ return map[month%12];
+}
+const char *month_to_string(month_t month)
+{
+ static const char *map[] =
+ { "January", "February", "March", "April",
+ "May", "June", "July", "August",
+ "September", "October", "November", "December" };
+ return map[month%12];
+}
+
+const char *day_to_st(wday_t day)
+{
+ static const char *map[] =
+ { "Su","Mo", "Tu", "We", "Th", "Fr", "Sa" };
+ return map[day%7];
+}
+const char *day_to_str(wday_t day)
+{
+ static const char *map[] =
+ { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
+ return map[day%7];
+}
+const char *day_to_string(wday_t day)
+{
+ static const char *map[] =
+ { "Sunday", "Monday", "Tuesday", "Wednesday",
+ "Thursday", "Friday", "Saturday" };
+ return map[day%7];
+}
+
+/* Test functions */
+void test_time(void)
+{
+ printf("Info\n");
+ printf(" Year Month Start Weeks Days\n");
+ for (int y = 2012; y <= 2012; y++)
+ for (int m = JAN; m <= DEC; m++) {
+ printf(" %-5d", y);
+ printf(" %-10s", month_to_string(m));
+ printf(" %-6s", day_to_str(start_of_month(y,m)));
+ printf(" %-6d", weeks_in_month(y,m));
+ printf(" %-2d", days_in_month(y,m));
+ printf("\n");
+ }
+}
--- /dev/null
+/*
+ * 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/>.
+ */
+
+/* Time types */
+typedef int year_t;
+typedef int day_t;
+typedef int hour_t;
+typedef int min_t;
+
+typedef enum {
+ JAN = 0,
+ FEB = 1,
+ MAR = 2,
+ APR = 3,
+ MAY = 4,
+ JUN = 5,
+ JUL = 6,
+ AUG = 7,
+ SEP = 8,
+ OCT = 9,
+ NOV = 10,
+ DEC = 11,
+} month_t;
+
+typedef enum {
+ SUN = 0,
+ MON = 1,
+ TUE = 2,
+ WED = 3,
+ THU = 4,
+ FRI = 5,
+ SAT = 6,
+} wday_t;
+
+/* Global data */
+extern year_t YEAR;
+extern month_t MONTH;
+extern day_t DAY;
+
+/* Initialize */
+void date_init(void);
+
+/* Time functions */
+int is_leap_year(year_t year);
+int days_in_year(year_t year);
+int days_in_month(year_t year, month_t month);
+int weeks_in_month(year_t year, month_t month);
+wday_t day_of_week(year_t year, month_t month, day_t day);
+wday_t start_of_month(year_t year, month_t month);
+day_t start_of_week(year_t year, month_t month, day_t day);
+void add_days(year_t *year, month_t *month, day_t *day, int days);
+void add_months(year_t *year, month_t *month, int months);
+
+/* Time to string functions */
+const char *month_to_str(month_t month);
+const char *month_to_string(month_t month);
+const char *day_to_st(wday_t day);
+const char *day_to_str(wday_t day);
+const char *day_to_string(wday_t day);
+
+/* Tests */
+void test_time(void);
*/
#include "util.h"
+#include "date.h"
#include "event.h"
+/* Global data */
+event_t *EVENTS;
+
+/* Initialize */
+void event_init(void)
+{
+ EVENTS = event_get(2012, JAN, 0, 366);
+
+ /* 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);
+}
+
/* Event get */
event_t *event_get(year_t year, month_t month, day_t day, int days)
{
* 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;
struct event_t *next;
} event_t;
+/* Global data */
+extern event_t *EVENTS;
+
/* Event functions */
+void event_init(void);
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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <stdarg.h>
#include <stdlib.h>
#include <signal.h>
-#include <time.h>
#include <locale.h>
#include <ncurses.h>
-#include "main.h"
+#include "util.h"
+#include "date.h"
+#include "event.h"
#include "screen.h"
-/* Debugging */
-year_t YEAR = 2012;
-month_t MONTH = 8;
-day_t DAY = 29;
-
-event_t *EVENTS = NULL;
-
-/* Static data */
-static FILE *debug_fd = NULL;
-
/* Control-C handler, so we don't hose the therminal */
static void on_sigint(int signum)
{
exit(0);
}
-/* Debugging functions */
-int debug(char *fmt, ...)
-{
- int rval;
- va_list ap;
-
- /* Log to debug file */
- va_start(ap, fmt);
- vfprintf(debug_fd, "debug: ", ap);
- rval = vfprintf(debug_fd, fmt, ap);
-
- /* Log to status bar */
- va_start(ap, fmt);
- mvhline(LINES-2, 0, ACS_HLINE, COLS);
- move(LINES-1, 0);
- attron(COLOR_PAIR(COLOR_ERROR));
- vwprintw(stdscr, fmt, ap);
- attroff(COLOR_PAIR(COLOR_ERROR));
- clrtoeol();
-
- va_end(ap);
- return rval;
-}
-
/* Main */
int main(int argc, char **argv)
{
/* Misc setup */
signal(SIGINT, on_sigint);
- debug_fd = fopen("/tmp/lackey.log", "w+");
-
- /* 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;
-
- EVENTS = event_get(2012, JAN, 0, 366);
- /* Curses setup */
+ /* Setup Curses */
setlocale(LC_ALL, "");
initscr();
cbreak();
mousemask(ALL_MOUSE_EVENTS, NULL);
init_pair(COLOR_TITLE, COLOR_GREEN, COLOR_BLACK);
init_pair(COLOR_ERROR, COLOR_RED, COLOR_BLACK);
+
+ /* Initialize */
+ util_init();
+ date_init();
+ event_init();
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);
+ /* Draw initial screen */
+ screen_draw();
/* Run */
while (1) {
}
if (screen_run(chr, btn.bstate, btn.y, btn.x))
continue;
- //debug("Unhandled key: Dec %3d, Hex %02x, Oct %03o, Chr <%c>\n",
- // chr, chr, chr, chr);
+ debug("main: Unhandled key - Dec %3d, Hex %02x, Oct %03o, Chr <%c>\n",
+ chr, chr, chr, chr);
}
/* Cleanup, see also on_sigint */
+++ /dev/null
-/*
- * 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 MAIN_H
-#define MAIN_H
-
-#define COLOR_TITLE 1
-#define COLOR_ERROR 2
-
-#include "util.h"
-#include "event.h"
-
-/* Debugging */
-extern year_t YEAR;
-extern month_t MONTH;
-extern day_t DAY;
-
-extern event_t *EVENTS;
-
-/* Debug functions */
-int debug(char *fmt, ...);
-
-#endif
#include <string.h>
#include <ncurses.h>
-#include "main.h"
-#include "screen.h"
+
#include "util.h"
+#include "date.h"
+#include "screen.h"
/* Types */
typedef struct {
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef SCREEN_H
-#define SCREEN_H
+/* Configuration */
+#define COLOR_TITLE 1
+#define COLOR_ERROR 2
/* Screen functions */
void screen_init(void);
int notes_run(int,mmask_t,int,int);
int settings_run(int,mmask_t,int,int);
int help_run(int,mmask_t,int,int);
-
-#endif
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "util.h"
-
-int YEAR = 2012;
-int MONTH = 8;
-int DAY = 29;
+#include "date.h"
int main(int argc, char **argv)
{
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-/* Time Keeping Bugs Abound! */
-
#include <stdio.h>
-#include <time.h>
+#include <ncurses.h>
-#include "util.h"
+#include "screen.h"
-/* Time functions */
-int is_leap_year(year_t year)
-{
- return (year % 400 == 0) ? 1 :
- (year % 100 == 0) ? 0 :
- (year % 4 == 0) ? 1 : 0;
-}
+/* Static data */
+static FILE *debug_fd = NULL;
-int days_in_year(year_t year)
+/* Initialize */
+void util_init(void)
{
- return 365 + is_leap_year(year);
+ debug_fd = fopen("/tmp/lackey.log", "w+");
}
-int days_in_month(year_t year, month_t month)
+/* Debugging functions */
+int debug(char *fmt, ...)
{
- static int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- int days = mdays[month];
- if (month == FEB)
- days += is_leap_year(year);
- return days;
-}
+ int rval;
+ va_list ap;
-int weeks_in_month(year_t year, month_t month)
-{
- int start = start_of_month(year, month);
- int days = days_in_month(year, month);
- return ((start + days)-1) / 7 + 1;
-}
-
-wday_t day_of_week(year_t year, month_t month, day_t day)
-{
- static int tmp[] = {0, 3, 2, 5, 0, 3,
- 5, 1, 4, 6, 2, 4};
- if (month < MAR)
- year--;
- int start = year + year / 4
- - year / 100
- + year / 400
- + tmp[month];
- return (start + day + 1) % 7;
-}
-
-wday_t start_of_month(year_t year, month_t month)
-{
- return day_of_week(year, month, 0);
-}
-
-void add_days(year_t *year, month_t *month, day_t *day, int days)
-{
- time_t time = mktime(&(struct tm){
- .tm_year = *year-1900,
- .tm_mon = *month,
- .tm_mday = *day+1,
- .tm_hour = 12});
- time += days*24*60*60;
- struct tm *tm = localtime(&time);
- *year = tm->tm_year+1900;
- *month = tm->tm_mon;
- *day = tm->tm_mday-1;
-}
-
-void add_months(year_t *year, month_t *month, int months)
-{
- int total = *year*12 + *month + months;
- *year = total / 12;
- *month = total % 12;
-}
-
-/* Debug functions */
-const char *month_to_str(month_t month)
-{
- static const char *map[] =
- { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", };
- return map[month%12];
-}
-const char *month_to_string(month_t month)
-{
- static const char *map[] =
- { "January", "February", "March", "April",
- "May", "June", "July", "August",
- "September", "October", "November", "December" };
- return map[month%12];
-}
-
-const char *day_to_st(wday_t day)
-{
- static const char *map[] =
- { "Su","Mo", "Tu", "We", "Th", "Fr", "Sa" };
- return map[day%7];
-}
-const char *day_to_str(wday_t day)
-{
- static const char *map[] =
- { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
- return map[day%7];
-}
-const char *day_to_string(wday_t day)
-{
- static const char *map[] =
- { "Sunday", "Monday", "Tuesday", "Wednesday",
- "Thursday", "Friday", "Saturday" };
- return map[day%7];
-}
+ /* Log to debug file */
+ if (debug_fd) {
+ va_start(ap, fmt);
+ vfprintf(debug_fd, "debug: ", ap);
+ rval = vfprintf(debug_fd, fmt, ap);
+ }
-/* Test functions */
-void test_time(void)
-{
- printf("Info\n");
- printf(" Year Month Start Weeks Days\n");
- for (int y = 2012; y <= 2012; y++)
- for (int m = JAN; m <= DEC; m++) {
- printf(" %-5d", y);
- printf(" %-10s", month_to_string(m));
- printf(" %-6s", day_to_str(start_of_month(y,m)));
- printf(" %-6d", weeks_in_month(y,m));
- printf(" %-2d", days_in_month(y,m));
- printf("\n");
+ /* Log to status bar */
+ if (stdscr) {
+ va_start(ap, fmt);
+ mvhline(LINES-2, 0, ACS_HLINE, COLS);
+ move(LINES-1, 0);
+ attron(COLOR_PAIR(COLOR_ERROR));
+ vwprintw(stdscr, fmt, ap);
+ attroff(COLOR_PAIR(COLOR_ERROR));
+ clrtoeol();
}
+
+ va_end(ap);
+ return rval;
}
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef UTIL_H
-#define UTIL_H
-
/* Macros */
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define ROUND(x) ((int)((x)+0.5))
#define N_ELEMENTS(x) (sizeof(x)/sizeof((x)[0]))
-/* Time types */
-typedef int year_t;
-typedef int day_t;
-typedef int hour_t;
-typedef int min_t;
-
-typedef enum {
- JAN = 0,
- FEB = 1,
- MAR = 2,
- APR = 3,
- MAY = 4,
- JUN = 5,
- JUL = 6,
- AUG = 7,
- SEP = 8,
- OCT = 9,
- NOV = 10,
- DEC = 11,
-} month_t;
-
-typedef enum {
- SUN = 0,
- MON = 1,
- TUE = 2,
- WED = 3,
- THU = 4,
- FRI = 5,
- SAT = 6,
-} wday_t;
-
-/* Time functions */
-int is_leap_year(year_t year);
-int days_in_year(year_t year);
-int days_in_month(year_t year, month_t month);
-int weeks_in_month(year_t year, month_t month);
-wday_t day_of_week(year_t year, month_t month, day_t day);
-wday_t start_of_month(year_t year, month_t month);
-day_t start_of_week(year_t year, month_t month, day_t day);
-void add_days(year_t *year, month_t *month, day_t *day, int days);
-void add_months(year_t *year, month_t *month, int months);
-
-/* Time to string functions */
-const char *month_to_str(month_t month);
-const char *month_to_string(month_t month);
-const char *day_to_st(wday_t day);
-const char *day_to_str(wday_t day);
-const char *day_to_string(wday_t day);
-
-/* Tests */
-void test_time(void);
+/* Debug functions */
+void util_init(void);
+/* Debug functions */
+#ifdef DEBUG
+int debug(char *fmt, ...);
+#else
+#define debug(...)
#endif
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <string.h>
#include <ncurses.h>
-#include "main.h"
-#include "util.h"
+#include "date.h"
/* Static data */
static WINDOW *win;
#include <string.h>
#include <ncurses.h>
-#include "main.h"
#include "util.h"
+#include "date.h"
/* Static data */
static WINDOW *win;
#define _XOPEN_SOURCE_EXTENDED
-#include <string.h>
#include <ncurses.h>
-#include "main.h"
#include "util.h"
+#include "date.h"
+#include "event.h"
/* Static data */
static WINDOW *win;
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);
+ debug("week: event = %s\n", event->name);
+ (void)l;
}
static int before(datetime_t *start, int year, int month, int day, int hour, int min)
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",
+ debug("week: %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;
#include <string.h>
#include <ncurses.h>
-#include "main.h"
#include "util.h"
+#include "date.h"
/* Constants */
#define MW (2*7+6)