summaryrefslogtreecommitdiffstats
path: root/src/clock.c
blob: 0e6085cc248ecb73bfb1345433f5dc01bb44e89f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Clock screen functions
 *
 * Copyright (C) 2019  Patrick McDermott
 *
 * This file is part of Timeteller.
 *
 * Timeteller is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Timeteller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Timeteller.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "clock.h"

#include <assert.h>
#include <curses.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>

#include "screen.h"

#define MIN(a, b) (((a) < (b)) ? (a) : (b))

#define NUM_DIGITS   6
#define NUM_COLONS   2
#define DIGIT_HEIGHT 5
#define DIGIT_WIDTH  3
#define COLON_WIDTH  1
#define SPACING      1

static const uint16_t _clock_font[] = {
	075557,  /* 0 */
	011111,  /* 1 */
	071747,  /* 2 */
	071717,  /* 3 */
	055711,  /* 4 */
	074717,  /* 5 */
	074757,  /* 6 */
	071111,  /* 7 */
	075757,  /* 8 */
	075717,  /* 9 */
	004040,  /* : */
};
#define COLON_DIGIT 10

struct clock {
	WINDOW *win;
	int     begy;
	int     begx;
	int     font_size;
};

struct clock *
clock_new(WINDOW *win)
{
	struct clock *clock;

	clock = calloc(1, sizeof(*clock));
	if (clock == NULL) {
		return NULL;
	}

	if (clock_size(clock) == false) {
		free(clock);
		return NULL;
	}

	clock->win = win;

	return clock;
}

bool
clock_size(struct clock *clock)
{
	int win_h;
	int win_w;
	int clock_h;
	int clock_w;

	assert(clock);

	getmaxyx(clock->win, win_h, win_w);
	if (win_h < 0 && win_w < 0) {
		win_h = LINES;
		win_w = COLS;
	}
	clock_h = DIGIT_HEIGHT + SPACING * 2;
	clock_w = NUM_DIGITS * (DIGIT_WIDTH + SPACING)
	        + NUM_COLONS * (COLON_WIDTH + SPACING)
	        + SPACING;
	clock_w *= 2;  /* A "dot" is two columns wide.  */
	if (win_h < clock_h || win_w < clock_w) {
		return false;
	}
	clock->font_size = MIN(win_h / clock_h, win_w / clock_w);
	clock_h *= clock->font_size;
	clock_w *= clock->font_size;
	clock->begy = (win_h - clock_h    ) / 2;
	clock->begx = (win_w - clock_w + 1) / 2;

	return true;
}

/* begy and begx are measured in dots from the top left corner of clock area. */
static void
_clock_draw_dot(struct clock *clock, int begy, int begx, int dot)
{
	chtype ch;
	int y;
	int x;

	assert(clock);

	ch = ' ' | (dot ? A_REVERSE : 0);

	begy = clock->begy +  begy * clock->font_size;
	begx = clock->begx + (begx * clock->font_size) * 2;
	for (y = 0; y < clock->font_size; ++y) {
		for (x = 0; x < clock->font_size; ++x) {
			mvwaddch(clock->win, begy + y, begx + x * 2    , ch);
			mvwaddch(clock->win, begy + y, begx + x * 2 + 1, ch);
		}
	}
}

/* begx is measured in dots from the left side of clock area.  */
static void
_clock_draw_digit(struct clock *clock, int begx, int d)
{
	int i;
	int y;
	int x;
	int dot;

	assert(clock);

	for (i = 0; i < DIGIT_HEIGHT * DIGIT_WIDTH; ++i) {
		y =    1 + i / DIGIT_WIDTH;
		x = begx + i % DIGIT_WIDTH;
		dot = _clock_font[d] & (1 << (DIGIT_HEIGHT*DIGIT_WIDTH-1-i));
		_clock_draw_dot(clock, y, x, dot);
	}
}

void
clock_draw(struct clock *clock)
{
	time_t     t = 0;
	struct tm *tm;
	int        x;

	assert(clock);

	time(&t);
	if (t < (time_t) 0) {
		return;
	}

	tm = localtime(&t);
	if (tm == NULL) {
		return;
	}

	x = 0;
	_clock_draw_digit(clock, x += SPACING              , tm->tm_hour / 10);
	_clock_draw_digit(clock, x += SPACING + DIGIT_WIDTH, tm->tm_hour % 10);
	_clock_draw_digit(clock, x += SPACING + DIGIT_WIDTH, COLON_DIGIT);
	_clock_draw_digit(clock, x += SPACING + COLON_WIDTH, tm->tm_min / 10);
	_clock_draw_digit(clock, x += SPACING + DIGIT_WIDTH, tm->tm_min % 10);
	_clock_draw_digit(clock, x += SPACING + DIGIT_WIDTH, COLON_DIGIT);
	_clock_draw_digit(clock, x += SPACING + COLON_WIDTH, tm->tm_sec / 10);
	_clock_draw_digit(clock, x += SPACING + DIGIT_WIDTH, tm->tm_sec % 10);
}

struct clock *
clock_destroy(struct clock **clock_p)
{
	struct clock *clock;

	assert(clock_p && *clock_p);

	clock = *clock_p;

	free(clock);
	return clock = NULL;
}