]> Pileus Git - lackey/blob - src/main.c
Add copyright
[lackey] / src / main.c
1 /*
2  * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #define _POSIX_C_SOURCE 1
19 #include <stdarg.h>
20 #include <stdlib.h>
21 #include <signal.h>
22 #include <ncurses.h>
23
24 #include "main.h"
25 #include "screen.h"
26
27 /* Static data */
28 static FILE *debug_fd = NULL;
29
30 /* Control-C handler, so we don't hose the therminal */
31 static void on_sigint(int signum)
32 {
33         endwin();
34         exit(0);
35 }
36
37 /* Window change */
38 static void on_sigwinch(int signum)
39 {
40         endwin();
41         refresh();
42         screen_resize();
43         screen_draw();
44 }
45
46 /* Debugging functions */
47 int debug(char *fmt, ...)
48 {
49         int rval;
50         va_list ap;
51
52         /* Open log file */
53         if (!debug_fd)
54                 debug_fd = fopen("acal.log", "w+");
55
56         /* Log to debug file */
57         va_start(ap, fmt);
58         vfprintf(debug_fd, "debug: ", ap);
59         rval = vfprintf(debug_fd, fmt, ap);
60
61         /* Log to status bar */
62         va_start(ap, fmt);
63         mvhline(LINES-2, 0, ACS_HLINE, COLS);
64         move(LINES-1, 0);
65         attron(COLOR_PAIR(COLOR_ERROR));
66         vwprintw(stdscr, fmt, ap);
67         attroff(COLOR_PAIR(COLOR_ERROR));
68         clrtoeol();
69
70         va_end(ap);
71         return rval;
72 }
73
74 /* Main */
75 int main(int argc, char **argv)
76 {
77         /* Misc setup */
78         struct sigaction act;
79         sigemptyset(&act.sa_mask);
80         act.sa_flags   = 0;
81         act.sa_handler = on_sigint;
82         sigaction(SIGINT, &act, NULL);
83         act.sa_handler = on_sigwinch;
84         sigaction(SIGWINCH, &act, NULL);
85
86         /* Curses setup */
87         initscr();
88         cbreak();
89         noecho();
90         keypad(stdscr, TRUE);
91         start_color();
92         curs_set(false);
93         mousemask(ALL_MOUSE_EVENTS, NULL);
94         init_pair(COLOR_TITLE, COLOR_GREEN, COLOR_BLACK);
95         init_pair(COLOR_ERROR, COLOR_RED,   COLOR_BLACK);
96         screen_init();
97         screen_draw();
98
99         /* Run */
100         while (1) {
101                 MEVENT btn;
102                 int chr = getch();
103                 if (chr == KEY_MOUSE)
104                         if (getmouse(&btn) != OK)
105                                 continue;
106                 if (chr == 'q')
107                         break;
108                 if (KEY_MOUSE)
109                         //debug("mouse xyz=%d,%d,%d id=%hd state=%lx\n",
110                         //      btn.x, btn.y, btn.z, btn.id, btn.bstate);
111                 switch (chr) {
112                         case 'L':
113                                 clear();
114                         case 'l':
115                                 screen_draw();
116                                 break;
117                         default:
118                                 screen_run(chr, btn.bstate, btn.y, btn.x);
119                                 break;
120                 }
121         }
122
123         /* Cleanup, see also on_sigint */
124         endwin();
125         return 0;
126 }