1 /* $Id: main.c,v 1.9 2007/02/28 12:47:35 tamentis Exp $
2 *
3 * Copyright (c) 2007 Bertrand Janin <tamentis@neopulsar.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28
29 #include <time.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <err.h>
34 #include <curses.h>
35
36 #include "tbclock.h"
37
38
39 struct tbclock_data tbc;
40
41
42 /* tbc_next_help_value - rotate through different reading helps */
43 void
tbc_next_help_value()44 tbc_next_help_value()
45 {
46 unsigned int tm, lm;
47
48 tm = tbc.format.top_margin + tbc.format.dot_h / 2;
49 lm = tbc.format.width - tbc.format.left_margin / 2 - 1;
50
51 if (tbc.options.helper > 2)
52 tbc.options.helper = 0;
53 else
54 tbc.options.helper++;
55
56 }
57
58
59 /* tbc_set_default - set the starting vars, default options */
60 void
tbc_set_default()61 tbc_set_default()
62 {
63 tbc.bigbang = time(NULL);
64
65 tbc.format.width = 80;
66 tbc.format.height = 24;
67
68 tbc.options.frame = tbc.options.frame_default = 1;
69 tbc.options.border = tbc.options.border_default = 1;
70 tbc.options.dots = 1;
71 tbc.options.vertical = 0;
72 tbc.options.helper = 0;
73 tbc.options.ampm = 0;
74
75 tbc.colors.hour = COLOR_BLUE;
76 tbc.colors.minute = COLOR_RED;
77 tbc.colors.second = COLOR_YELLOW;
78 tbc.colors.tenth = COLOR_GREEN;
79 }
80
81
82
83 /* tbc_configure - setup layout and all those things, could be called from
84 * anywhere, when term is resized, at the beginning of modules... it
85 * deals with weither something is tolerated or not depending of
86 * a size. */
87 void
tbc_configure()88 tbc_configure()
89 {
90
91 /* terminal is too small, removing frame & border */
92 if (tbc.format.height < 10 || tbc.format.width < 19)
93 tbc.options.frame = tbc.options.border = 0;
94 else {
95 tbc.options.frame = tbc.options.frame_default;
96 tbc.options.border = tbc.options.border_default;
97 }
98
99 /* term not wide enough for horizontal helper */
100 if (!tbc.options.vertical && tbc.format.width < 8
101 && tbc.options.helper & 1)
102 tbc.options.helper --;
103
104 /* term is not tall enough for bottom help in vertical mode */
105 if (tbc.options.vertical && tbc.format.height < 5
106 && tbc.options.helper > 0)
107 tbc.options.helper = 0;
108
109 /* term is not tall enough for bottom help */
110 if (tbc.format.height < 4 && tbc.options.helper > 1)
111 tbc.options.helper -= 2;
112
113 /* height of a dot (term height - frame - spaces - helper) */
114 tbc.format.dot_h =
115 ( tbc.format.height
116 - tbc.options.frame * 2
117 - (tbc.format.res_y - 1) * tbc.options.border
118 - (!tbc.options.vertical && tbc.options.helper > 1 ? 1 : 0)
119 - (tbc.options.vertical && tbc.options.helper ? 1 : 0)
120 ) / tbc.format.res_y;
121
122 /* width of a dot */
123 tbc.format.dot_w =
124 ( tbc.format.width
125 - tbc.options.frame * 2
126 - (tbc.format.res_x - 1) * tbc.options.border
127 - (!tbc.options.vertical && tbc.options.helper & 1 ? 2 : 0)
128 ) / tbc.format.res_x;
129
130 /* top margin */
131 tbc.format.top_margin = tbc.options.frame +
132 ( tbc.format.height
133 - tbc.options.frame * 2
134 - tbc.format.res_y * tbc.format.dot_h
135 - (tbc.format.res_y - 1) * tbc.options.border
136 - (!tbc.options.vertical && tbc.options.helper & 1 ? 2 : 0)
137 ) / 2;
138
139 /* left margin */
140 tbc.format.left_margin = tbc.options.frame +
141 ( tbc.format.width
142 - tbc.options.frame * 2
143 - tbc.format.res_x * tbc.format.dot_w
144 - (tbc.format.res_x - 1) * tbc.options.border
145 - (!tbc.options.vertical && tbc.options.helper & 1 ? 2 : 0)
146 ) / 2;
147
148 tbc_clear();
149
150 }
151
152
153 /* this doesn't do anything, I was thinking about removing it. */
154 #define SET_COLOR(x) i = atoi(optarg); if (i >= 0 && i < 8) x = i;
155 int
main(int ac,char ** av)156 main(int ac, char **av)
157 {
158 int ch, i;
159 char modulename[9] = "clock";
160
161 tbc_set_default();
162
163 while ((ch = getopt(ac, av, "abdefg:hvm:pH:M:S:T:")) != -1) {
164 switch (ch) {
165 case 'a':
166 tbc.options.vertical = 1;
167 break;
168 case 'b':
169 tbc.options.border = tbc.options.border_default = 0;
170 break;
171 case 'd':
172 tbc.options.dots = 0;
173 break;
174 case 'e':
175 tbc.options.helper++;
176 break;
177 case 'f':
178 tbc.options.frame = tbc.options.frame_default = 0;
179 break;
180 case 'g':
181 case 'm':
182 strncpy(modulename, optarg, 9);
183 modulename[8] = 0;
184 break;
185 case 'p':
186 tbc.options.ampm = 1;
187 break;
188 case 'H':
189 SET_COLOR(tbc.colors.hour);
190 break;
191 case 'M':
192 SET_COLOR(tbc.colors.minute);
193 break;
194 case 'S':
195 SET_COLOR(tbc.colors.second);
196 break;
197 case 'T':
198 SET_COLOR(tbc.colors.tenth);
199 break;
200 case 'v':
201 printf(TBCCOPY);
202 exit(-1);
203 case 'h':
204 default:
205 printf(USAGE_FMT, av[0]);
206 exit(-1);
207 }
208 }
209 ac -= optind;
210 av += optind;
211
212 tbc_display_init();
213
214 /* module selection */
215 if (strncmp(modulename, "guessbin", 9) == 0) {
216 tbc.options.frame = 1;
217 tbc.options.border = 1;
218 mod_guessbin();
219 } else if (strncmp(modulename, "chrono", 7) == 0) {
220 mod_chrono();
221 } else {
222 mod_clock();
223 }
224
225 endwin();
226 printf("Thank you for using tbclock!\n");
227
228 return (0);
229 }
230
231