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