]> Pileus Git - lackey/blob - src/main.c
Some organization
[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 #include <stdlib.h>
19 #include <signal.h>
20 #include <locale.h>
21 #include <ncurses.h>
22
23 #include "util.h"
24 #include "date.h"
25 #include "event.h"
26 #include "screen.h"
27
28 /* Control-C handler, so we don't hose the therminal */
29 static void on_sigint(int signum)
30 {
31         endwin();
32         exit(0);
33 }
34
35 /* Main */
36 int main(int argc, char **argv)
37 {
38         /* Misc setup */
39         signal(SIGINT, on_sigint);
40
41         /* Setup Curses */
42         setlocale(LC_ALL, "");
43         initscr();
44         cbreak();
45         noecho();
46         keypad(stdscr, TRUE);
47         start_color();
48         curs_set(false);
49         mousemask(ALL_MOUSE_EVENTS, NULL);
50         init_pair(COLOR_TITLE, COLOR_GREEN, COLOR_BLACK);
51         init_pair(COLOR_ERROR, COLOR_RED,   COLOR_BLACK);
52
53         /* Initialize */
54         util_init();
55         date_init();
56         event_init();
57         screen_init();
58
59         /* Draw initial screen */
60         screen_draw();
61
62         /* Run */
63         while (1) {
64                 MEVENT btn;
65                 int chr = getch();
66                 if (chr == 'q')
67                         break;
68                 if (chr == KEY_MOUSE)
69                         if (getmouse(&btn) != OK)
70                                 continue;
71                 switch (chr) {
72                         case ERR:
73                                 continue;
74                         case KEY_RESIZE:
75                                 endwin();
76                                 refresh();
77                                 screen_resize();
78                                 screen_draw();
79                                 continue;
80                         case '\14':
81                                 clear();
82                         case '\7':
83                                 screen_draw();
84                                 continue;
85                 }
86                 if (screen_run(chr, btn.bstate, btn.y, btn.x))
87                         continue;
88                 debug("main: Unhandled key - Dec %3d,  Hex %02x,  Oct %03o,  Chr <%c>\n",
89                                 chr, chr, chr, chr);
90         }
91
92         /* Cleanup, see also on_sigint */
93         endwin();
94         return 0;
95 }