]> Pileus Git - lackey/blob - src/main.c
Add day and week view
[lackey] / src / main.c
1 #define _POSIX_C_SOURCE 1
2 #include <stdarg.h>
3 #include <stdlib.h>
4 #include <signal.h>
5 #include <ncurses.h>
6
7 #include "main.h"
8 #include "screen.h"
9
10 /* Static data */
11 static FILE *debug_fd = NULL;
12
13 /* Control-C handler, so we don't hose the therminal */
14 static void on_sigint(int signum)
15 {
16         endwin();
17         exit(0);
18 }
19
20 /* Window change */
21 static void on_sigwinch(int signum)
22 {
23         endwin();
24         refresh();
25         screen_resize();
26         screen_draw();
27 }
28
29 /* Debugging functions */
30 int debug(char *fmt, ...)
31 {
32         int rval;
33         va_list ap;
34
35         /* Open log file */
36         if (!debug_fd)
37                 debug_fd = fopen("acal.log", "w+");
38
39         /* Log to debug file */
40         va_start(ap, fmt);
41         vfprintf(debug_fd, "debug: ", ap);
42         rval = vfprintf(debug_fd, fmt, ap);
43
44         /* Log to status bar */
45         va_start(ap, fmt);
46         mvhline(LINES-2, 0, ACS_HLINE, COLS);
47         move(LINES-1, 0);
48         attron(COLOR_PAIR(COLOR_ERROR));
49         vwprintw(stdscr, fmt, ap);
50         attroff(COLOR_PAIR(COLOR_ERROR));
51         clrtoeol();
52
53         va_end(ap);
54         return rval;
55 }
56
57 /* Main */
58 int main(int argc, char **argv)
59 {
60         /* Misc setup */
61         struct sigaction act;
62         sigemptyset(&act.sa_mask);
63         act.sa_flags   = 0;
64         act.sa_handler = on_sigint;
65         sigaction(SIGINT, &act, NULL);
66         act.sa_handler = on_sigwinch;
67         sigaction(SIGWINCH, &act, NULL);
68
69         /* Curses setup */
70         initscr();
71         cbreak();
72         noecho();
73         keypad(stdscr, TRUE);
74         start_color();
75         curs_set(false);
76         mousemask(ALL_MOUSE_EVENTS, NULL);
77         init_pair(COLOR_TITLE, COLOR_GREEN, COLOR_BLACK);
78         init_pair(COLOR_ERROR, COLOR_RED,   COLOR_BLACK);
79         screen_init();
80         screen_draw();
81
82         /* Run */
83         while (1) {
84                 MEVENT btn;
85                 int chr = getch();
86                 if (chr == KEY_MOUSE)
87                         if (getmouse(&btn) != OK)
88                                 continue;
89                 if (chr == 'q')
90                         break;
91                 if (KEY_MOUSE)
92                         //debug("mouse xyz=%d,%d,%d id=%hd state=%lx\n",
93                         //      btn.x, btn.y, btn.z, btn.id, btn.bstate);
94                 switch (chr) {
95                         case 'L':
96                                 clear();
97                         case 'l':
98                                 screen_draw();
99                                 break;
100                         default:
101                                 screen_run(chr, btn.bstate, btn.y, btn.x);
102                                 break;
103                 }
104         }
105
106         /* Cleanup, see also on_sigint */
107         endwin();
108         return 0;
109 }