]> Pileus Git - lackey/blob - src/main.c
Add basic screen layout stuff
[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 /* Global data */
11 int win_rows = 0;
12 int win_cols = 0;
13
14 /* Static data */
15 static WINDOW *win = NULL;
16 static FILE *debug_fd = NULL;
17
18 /* Control-C handler, so we don't hose the therminal */
19 static void on_sigint(int signum)
20 {
21         endwin();
22         debug("got sigint\n");
23         exit(0);
24 }
25
26 /* Window change */
27 static void update(void)
28 {
29         getmaxyx(win, win_rows, win_cols);
30         win_rows++;
31         win_cols++;
32         screen_draw();
33 }
34
35 /* Window change */
36 static void on_sigwinch(int signum)
37 {
38         endwin();
39         refresh();
40         update();
41 }
42
43 /* Debugging functions */
44 int debug(char *fmt, ...)
45 {
46         int rval;
47         va_list ap;
48         va_start(ap, fmt);
49         vfprintf(debug_fd, "debug: ", ap);
50         rval = vfprintf(debug_fd, fmt, ap);
51         va_end(ap);
52         return rval;
53 }
54
55 /* Main */
56 int main(int argc, char **argv)
57 {
58         /* Misc setup */
59         debug_fd = fopen("acal.log", "w+");
60         struct sigaction act;
61         sigemptyset(&act.sa_mask);
62         act.sa_flags   = 0;
63         act.sa_handler = on_sigint;
64         if (sigaction(SIGINT, &act, NULL) < 0)
65                 debug("sigint error\n");
66         act.sa_handler = on_sigwinch;
67         if (sigaction(SIGWINCH, &act, NULL) < 0)
68                 debug("sigwinch error\n");
69
70         /* Curses setup */
71         win = initscr();
72         cbreak();
73         noecho();
74         start_color();
75         curs_set(false);
76         screen_init();
77
78         /* Run */
79         while (1) {
80                 int chr = getch();
81                 if (chr == 'q')
82                         break;
83                 switch (chr) {
84                         case 'L':
85                                 clear();
86                         case 'l':
87                                 update();
88                                 break;
89                         default:
90                                 screen_run(chr);
91                                 break;
92                 }
93         }
94
95         /* Cleanup, see also on_sigint */
96         endwin();
97         debug("cleanup");
98         return 0;
99 }