GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/window-clock.c Lines: 0 80 0.0 %
Date: 2016-12-06 Branches: 0 32 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: window-clock.c,v 1.18 2016/07/15 00:42:56 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <sys/types.h>
20
21
#include <stdlib.h>
22
#include <string.h>
23
#include <time.h>
24
25
#include "tmux.h"
26
27
struct screen *window_clock_init(struct window_pane *);
28
void	window_clock_free(struct window_pane *);
29
void	window_clock_resize(struct window_pane *, u_int, u_int);
30
void	window_clock_key(struct window_pane *, struct client *,
31
	    struct session *, key_code, struct mouse_event *);
32
33
void	window_clock_timer_callback(int, short, void *);
34
void	window_clock_draw_screen(struct window_pane *);
35
36
const struct window_mode window_clock_mode = {
37
	window_clock_init,
38
	window_clock_free,
39
	window_clock_resize,
40
	window_clock_key,
41
};
42
43
struct window_clock_mode_data {
44
	struct screen	        screen;
45
	time_t			tim;
46
	struct event		timer;
47
};
48
49
const char window_clock_table[14][5][5] = {
50
	{ { 1,1,1,1,1 }, /* 0 */
51
	  { 1,0,0,0,1 },
52
	  { 1,0,0,0,1 },
53
	  { 1,0,0,0,1 },
54
	  { 1,1,1,1,1 } },
55
	{ { 0,0,0,0,1 }, /* 1 */
56
	  { 0,0,0,0,1 },
57
	  { 0,0,0,0,1 },
58
	  { 0,0,0,0,1 },
59
	  { 0,0,0,0,1 } },
60
	{ { 1,1,1,1,1 }, /* 2 */
61
	  { 0,0,0,0,1 },
62
	  { 1,1,1,1,1 },
63
	  { 1,0,0,0,0 },
64
	  { 1,1,1,1,1 } },
65
	{ { 1,1,1,1,1 }, /* 3 */
66
	  { 0,0,0,0,1 },
67
	  { 1,1,1,1,1 },
68
	  { 0,0,0,0,1 },
69
	  { 1,1,1,1,1 } },
70
	{ { 1,0,0,0,1 }, /* 4 */
71
	  { 1,0,0,0,1 },
72
	  { 1,1,1,1,1 },
73
	  { 0,0,0,0,1 },
74
	  { 0,0,0,0,1 } },
75
	{ { 1,1,1,1,1 }, /* 5 */
76
	  { 1,0,0,0,0 },
77
	  { 1,1,1,1,1 },
78
	  { 0,0,0,0,1 },
79
	  { 1,1,1,1,1 } },
80
	{ { 1,1,1,1,1 }, /* 6 */
81
	  { 1,0,0,0,0 },
82
	  { 1,1,1,1,1 },
83
	  { 1,0,0,0,1 },
84
	  { 1,1,1,1,1 } },
85
	{ { 1,1,1,1,1 }, /* 7 */
86
	  { 0,0,0,0,1 },
87
	  { 0,0,0,0,1 },
88
	  { 0,0,0,0,1 },
89
	  { 0,0,0,0,1 } },
90
	{ { 1,1,1,1,1 }, /* 8 */
91
	  { 1,0,0,0,1 },
92
	  { 1,1,1,1,1 },
93
	  { 1,0,0,0,1 },
94
	  { 1,1,1,1,1 } },
95
	{ { 1,1,1,1,1 }, /* 9 */
96
	  { 1,0,0,0,1 },
97
	  { 1,1,1,1,1 },
98
	  { 0,0,0,0,1 },
99
	  { 1,1,1,1,1 } },
100
	{ { 0,0,0,0,0 }, /* : */
101
	  { 0,0,1,0,0 },
102
	  { 0,0,0,0,0 },
103
	  { 0,0,1,0,0 },
104
	  { 0,0,0,0,0 } },
105
	{ { 1,1,1,1,1 }, /* A */
106
	  { 1,0,0,0,1 },
107
	  { 1,1,1,1,1 },
108
	  { 1,0,0,0,1 },
109
	  { 1,0,0,0,1 } },
110
	{ { 1,1,1,1,1 }, /* P */
111
	  { 1,0,0,0,1 },
112
	  { 1,1,1,1,1 },
113
	  { 1,0,0,0,0 },
114
	  { 1,0,0,0,0 } },
115
	{ { 1,0,0,0,1 }, /* M */
116
	  { 1,1,0,1,1 },
117
	  { 1,0,1,0,1 },
118
	  { 1,0,0,0,1 },
119
	  { 1,0,0,0,1 } },
120
};
121
122
void
123
window_clock_timer_callback(__unused int fd, __unused short events, void *arg)
124
{
125
	struct window_pane		*wp = arg;
126
	struct window_clock_mode_data	*data = wp->modedata;
127
	struct tm			 now, then;
128
	time_t				 t;
129
	struct timeval			 tv = { .tv_sec = 1 };
130
131
	evtimer_del(&data->timer);
132
	evtimer_add(&data->timer, &tv);
133
134
	t = time(NULL);
135
	gmtime_r(&t, &now);
136
	gmtime_r(&data->tim, &then);
137
	if (now.tm_min == then.tm_min)
138
		return;
139
	data->tim = t;
140
141
	window_clock_draw_screen(wp);
142
	server_redraw_window(wp->window);
143
}
144
145
struct screen *
146
window_clock_init(struct window_pane *wp)
147
{
148
	struct window_clock_mode_data	*data;
149
	struct screen			*s;
150
	struct timeval			 tv = { .tv_sec = 1 };
151
152
	wp->modedata = data = xmalloc(sizeof *data);
153
	data->tim = time(NULL);
154
155
	evtimer_set(&data->timer, window_clock_timer_callback, wp);
156
	evtimer_add(&data->timer, &tv);
157
158
	s = &data->screen;
159
	screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0);
160
	s->mode &= ~MODE_CURSOR;
161
162
	window_clock_draw_screen(wp);
163
164
	return (s);
165
}
166
167
void
168
window_clock_free(struct window_pane *wp)
169
{
170
	struct window_clock_mode_data	*data = wp->modedata;
171
172
	evtimer_del(&data->timer);
173
	screen_free(&data->screen);
174
	free(data);
175
}
176
177
void
178
window_clock_resize(struct window_pane *wp, u_int sx, u_int sy)
179
{
180
	struct window_clock_mode_data	*data = wp->modedata;
181
	struct screen			*s = &data->screen;
182
183
	screen_resize(s, sx, sy, 0);
184
	window_clock_draw_screen(wp);
185
}
186
187
void
188
window_clock_key(struct window_pane *wp, __unused struct client *c,
189
    __unused struct session *sess, __unused key_code key,
190
    __unused struct mouse_event *m)
191
{
192
	window_pane_reset_mode(wp);
193
}
194
195
void
196
window_clock_draw_screen(struct window_pane *wp)
197
{
198
	struct window_clock_mode_data	*data = wp->modedata;
199
	struct screen_write_ctx	 	 ctx;
200
	int				 colour, style;
201
	struct screen			*s = &data->screen;
202
	struct grid_cell		 gc;
203
	char				 tim[64], *ptr;
204
	time_t				 t;
205
	struct tm			*tm;
206
	u_int				 i, j, x, y, idx;
207
208
	colour = options_get_number(wp->window->options, "clock-mode-colour");
209
	style = options_get_number(wp->window->options, "clock-mode-style");
210
211
	screen_write_start(&ctx, NULL, s);
212
213
	t = time(NULL);
214
	tm = localtime(&t);
215
	if (style == 0) {
216
		strftime(tim, sizeof tim, "%l:%M ", localtime(&t));
217
		if (tm->tm_hour >= 12)
218
			strlcat(tim, "PM", sizeof tim);
219
		else
220
			strlcat(tim, "AM", sizeof tim);
221
	} else
222
		strftime(tim, sizeof tim, "%H:%M", tm);
223
224
	screen_write_clearscreen(&ctx);
225
226
	if (screen_size_x(s) < 6 * strlen(tim) || screen_size_y(s) < 6) {
227
		if (screen_size_x(s) >= strlen(tim) && screen_size_y(s) != 0) {
228
			x = (screen_size_x(s) / 2) - (strlen(tim) / 2);
229
			y = screen_size_y(s) / 2;
230
			screen_write_cursormove(&ctx, x, y);
231
232
			memcpy(&gc, &grid_default_cell, sizeof gc);
233
			gc.fg = colour;
234
			screen_write_puts(&ctx, &gc, "%s", tim);
235
		}
236
237
		screen_write_stop(&ctx);
238
		return;
239
	}
240
241
	x = (screen_size_x(s) / 2) - 3 * strlen(tim);
242
	y = (screen_size_y(s) / 2) - 3;
243
244
	memcpy(&gc, &grid_default_cell, sizeof gc);
245
	gc.bg = colour;
246
	for (ptr = tim; *ptr != '\0'; ptr++) {
247
		if (*ptr >= '0' && *ptr <= '9')
248
			idx = *ptr - '0';
249
		else if (*ptr == ':')
250
			idx = 10;
251
		else if (*ptr == 'A')
252
			idx = 11;
253
		else if (*ptr == 'P')
254
			idx = 12;
255
		else if (*ptr == 'M')
256
			idx = 13;
257
		else {
258
			x += 6;
259
			continue;
260
		}
261
262
		for (j = 0; j < 5; j++) {
263
			for (i = 0; i < 5; i++) {
264
				screen_write_cursormove(&ctx, x + i, y + j);
265
				if (window_clock_table[idx][j][i])
266
					screen_write_putc(&ctx, &gc, ' ');
267
			}
268
		}
269
		x += 6;
270
	}
271
272
	screen_write_stop(&ctx);
273
}