]> Pileus Git - lackey/blob - src/main.c
Fix some time keeping bugs
[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 <time.h>
23 #include <ncurses.h>
24
25 #include "main.h"
26 #include "screen.h"
27
28 /* Debugging */
29 int YEAR  = 2012;
30 int MONTH = 8;
31 int DAY   = 29;
32
33 /* Static data */
34 static FILE *debug_fd = NULL;
35
36 /* Control-C handler, so we don't hose the therminal */
37 static void on_sigint(int signum)
38 {
39         endwin();
40         exit(0);
41 }
42
43 /* Window change */
44 static void on_sigwinch(int signum)
45 {
46         endwin();
47         refresh();
48         screen_resize();
49         screen_draw();
50 }
51
52 /* Debugging functions */
53 int debug(char *fmt, ...)
54 {
55         int rval;
56         va_list ap;
57
58         /* Open log file */
59         if (!debug_fd)
60                 debug_fd = fopen("lackey.log", "w+");
61
62         /* Log to debug file */
63         va_start(ap, fmt);
64         vfprintf(debug_fd, "debug: ", ap);
65         rval = vfprintf(debug_fd, fmt, ap);
66
67         /* Log to status bar */
68         va_start(ap, fmt);
69         mvhline(LINES-2, 0, ACS_HLINE, COLS);
70         move(LINES-1, 0);
71         attron(COLOR_PAIR(COLOR_ERROR));
72         vwprintw(stdscr, fmt, ap);
73         attroff(COLOR_PAIR(COLOR_ERROR));
74         clrtoeol();
75
76         va_end(ap);
77         return rval;
78 }
79
80 /* Main */
81 int main(int argc, char **argv)
82 {
83         /* Misc setup */
84         struct sigaction act;
85         sigemptyset(&act.sa_mask);
86         act.sa_flags   = 0;
87         act.sa_handler = on_sigint;
88         sigaction(SIGINT, &act, NULL);
89         act.sa_handler = on_sigwinch;
90         sigaction(SIGWINCH, &act, NULL);
91
92         /* Time setup */
93         time_t sec = time(NULL);
94         struct tm *tm = localtime(&sec);
95         YEAR  = tm->tm_year+1900;
96         MONTH = tm->tm_mon;
97         DAY   = tm->tm_mday-1;
98
99         /* Curses setup */
100         initscr();
101         cbreak();
102         noecho();
103         keypad(stdscr, TRUE);
104         start_color();
105         curs_set(false);
106         mousemask(ALL_MOUSE_EVENTS, NULL);
107         init_pair(COLOR_TITLE, COLOR_GREEN, COLOR_BLACK);
108         init_pair(COLOR_ERROR, COLOR_RED,   COLOR_BLACK);
109         screen_init();
110         screen_draw();
111
112         /* Run */
113         while (1) {
114                 MEVENT btn;
115                 int chr = getch();
116                 if (chr == KEY_MOUSE)
117                         if (getmouse(&btn) != OK)
118                                 continue;
119                 if (chr == 'q')
120                         break;
121                 if (KEY_MOUSE)
122                         //debug("mouse xyz=%d,%d,%d id=%hd state=%lx\n",
123                         //      btn.x, btn.y, btn.z, btn.id, btn.bstate);
124                 switch (chr) {
125                         case 'L':
126                                 clear();
127                         case 'l':
128                                 screen_draw();
129                                 break;
130                         default:
131                                 screen_run(chr, btn.bstate, btn.y, btn.x);
132                                 break;
133                 }
134         }
135
136         /* Cleanup, see also on_sigint */
137         endwin();
138         return 0;
139 }