]> Pileus Git - lackey/blob - src/main.c
Add simple config file parser
[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 "conf.h"
25 #include "date.h"
26 #include "cal.h"
27 #include "view.h"
28
29 /* Config parser */
30 static void on_config(const char *group, const char *name, const char *key, const char *value)
31 {
32         view_config(group, name, key, value);
33         cal_config(group, name, key, value);
34 }
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 /* Main */
44 int main(int argc, char **argv)
45 {
46         /* Misc setup */
47         signal(SIGINT, on_sigint);
48
49         /* Setup Curses */
50         setlocale(LC_ALL, "");
51         initscr();
52         cbreak();
53         noecho();
54         keypad(stdscr, TRUE);
55         start_color();
56         curs_set(false);
57         use_default_colors();
58         mousemask(ALL_MOUSE_EVENTS, NULL);
59
60         init_pair(COLOR_TITLE, COLOR_GREEN,   -1);
61         init_pair(COLOR_ERROR, COLOR_RED,     -1);
62
63         init_pair(COLOR_NEW,   COLOR_RED,     -1);
64         init_pair(COLOR_WIP,   COLOR_YELLOW,  -1);
65         init_pair(COLOR_DONE,  COLOR_GREEN,   -1);
66
67         init_pair(COLOR_CLASS, COLOR_BLUE,    -1);
68         init_pair(COLOR_EC,    COLOR_GREEN,   -1);
69         init_pair(COLOR_WORK,  COLOR_MAGENTA, -1);
70         init_pair(COLOR_OTHER, COLOR_RED,     -1);
71
72         /* Configuration */
73         conf_setup(argc, argv, ".lackeyrc", on_config);
74
75         /* Initialize */
76         util_init();
77         conf_init();
78         date_init();
79         cal_init();
80         view_init();
81
82         /* Draw initial view */
83         view_draw();
84
85         /* Run */
86         while (1) {
87                 MEVENT btn;
88                 conf_sync();
89                 int chr = getch();
90                 if (chr == 'q')
91                         break;
92                 if (chr == KEY_MOUSE)
93                         if (getmouse(&btn) != OK)
94                                 continue;
95                 switch (chr) {
96                         case ERR:
97                                 continue;
98                         case KEY_RESIZE:
99                                 endwin();
100                                 refresh();
101                                 view_resize();
102                                 view_draw();
103                                 continue;
104                         case '\14': // Ctrl-L
105                                 clear();
106                         case '\7':  // Ctrl-G
107                                 view_resize();
108                                 view_draw();
109                                 continue;
110                 }
111                 if (view_run(chr, btn.bstate, btn.y, btn.x))
112                         continue;
113                 debug("main: Unhandled key - Dec %3d,  Hex %02x,  Oct %03o,  Chr <%c>",
114                                 chr, chr, chr, chr);
115         }
116
117         /* Cleanup, see also on_sigint, error */
118         endwin();
119         return 0;
120 }