]> Pileus Git - lackey/blob - src/main.c
Add some mouse support
[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         debug("got sigint\n");
18         exit(0);
19 }
20
21 /* Window change */
22 static void on_sigwinch(int signum)
23 {
24         endwin();
25         refresh();
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         if (sigaction(SIGINT, &act, NULL) < 0)
63                 debug("sigint error\n");
64         act.sa_handler = on_sigwinch;
65         if (sigaction(SIGWINCH, &act, NULL) < 0)
66                 debug("sigwinch error\n");
67
68         /* Curses setup */
69         initscr();
70         cbreak();
71         noecho();
72         keypad(stdscr, TRUE);
73         start_color();
74         curs_set(false);
75         mousemask(ALL_MOUSE_EVENTS, NULL);
76         init_pair(COLOR_TITLE, COLOR_GREEN, COLOR_BLACK);
77         init_pair(COLOR_ERROR, COLOR_RED,   COLOR_BLACK);
78         screen_init();
79
80         /* Run */
81         while (1) {
82                 MEVENT btn;
83                 int chr = getch();
84                 if (chr == KEY_MOUSE)
85                         if (getmouse(&btn) != OK)
86                                 continue;
87                 if (chr == 'q')
88                         break;
89                 if (KEY_MOUSE)
90                         //debug("mouse xyz=%d,%d,%d id=%hd state=%lx\n",
91                         //      btn.x, btn.y, btn.z, btn.id, btn.bstate);
92                 switch (chr) {
93                         case 'L':
94                                 clear();
95                         case 'l':
96                                 screen_draw();
97                                 break;
98                         default:
99                                 screen_run(chr, btn.bstate, btn.y, btn.x);
100                                 break;
101                 }
102         }
103
104         /* Cleanup, see also on_sigint */
105         endwin();
106         debug("cleanup");
107         return 0;
108 }