]> Pileus Git - lackey/blob - src/main.c
Start using windows and views
[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_draw();
26 }
27
28 /* Debugging functions */
29 int debug(char *fmt, ...)
30 {
31         int rval;
32         va_list ap;
33
34         /* Log to debug file */
35         va_start(ap, fmt);
36         vfprintf(debug_fd, "debug: ", ap);
37         rval = vfprintf(debug_fd, fmt, ap);
38
39         /* Log to status bar */
40         va_start(ap, fmt);
41         mvhline(LINES-2, 0, ACS_HLINE, COLS);
42         move(LINES-1, 0);
43         attron(COLOR_PAIR(COLOR_ERROR));
44         vwprintw(stdscr, fmt, ap);
45         attroff(COLOR_PAIR(COLOR_ERROR));
46         clrtoeol();
47
48         va_end(ap);
49         return rval;
50 }
51
52 /* Main */
53 int main(int argc, char **argv)
54 {
55         /* Misc setup */
56         debug_fd = fopen("acal.log", "w+");
57         struct sigaction act;
58         sigemptyset(&act.sa_mask);
59         act.sa_flags   = 0;
60         act.sa_handler = on_sigint;
61         sigaction(SIGINT, &act, NULL);
62         act.sa_handler = on_sigwinch;
63         sigaction(SIGWINCH, &act, NULL);
64
65         /* Curses setup */
66         initscr();
67         cbreak();
68         noecho();
69         keypad(stdscr, TRUE);
70         start_color();
71         curs_set(false);
72         mousemask(ALL_MOUSE_EVENTS, NULL);
73         init_pair(COLOR_TITLE, COLOR_GREEN, COLOR_BLACK);
74         init_pair(COLOR_ERROR, COLOR_RED,   COLOR_BLACK);
75         screen_init();
76         screen_draw();
77
78         /* Run */
79         while (1) {
80                 MEVENT btn;
81                 int chr = getch();
82                 if (chr == KEY_MOUSE)
83                         if (getmouse(&btn) != OK)
84                                 continue;
85                 if (chr == 'q')
86                         break;
87                 if (KEY_MOUSE)
88                         //debug("mouse xyz=%d,%d,%d id=%hd state=%lx\n",
89                         //      btn.x, btn.y, btn.z, btn.id, btn.bstate);
90                 switch (chr) {
91                         case 'L':
92                                 clear();
93                         case 'l':
94                                 screen_draw();
95                                 break;
96                         default:
97                                 screen_run(chr, btn.bstate, btn.y, btn.x);
98                                 break;
99                 }
100         }
101
102         /* Cleanup, see also on_sigint */
103         endwin();
104         return 0;
105 }