GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/window-clock.c Lines: 0 87 0.0 %
Date: 2017-11-13 Branches: 0 34 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: window-clock.c,v 1.24 2017/05/30 21:44:59 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
static struct screen *window_clock_init(struct window_pane *,
28
		    struct cmd_find_state *, struct args *);
29
static void	window_clock_free(struct window_pane *);
30
static void	window_clock_resize(struct window_pane *, u_int, u_int);
31
static void	window_clock_key(struct window_pane *, struct client *,
32
		    struct session *, key_code, struct mouse_event *);
33
34
static void	window_clock_timer_callback(int, short, void *);
35
static void	window_clock_draw_screen(struct window_pane *);
36
37
const struct window_mode window_clock_mode = {
38
	.name = "clock-mode",
39
40
	.init = window_clock_init,
41
	.free = window_clock_free,
42
	.resize = window_clock_resize,
43
	.key = window_clock_key,
44
};
45
46
struct window_clock_mode_data {
47
	struct screen	        screen;
48
	time_t			tim;
49
	struct event		timer;
50
};
51
52
const char window_clock_table[14][5][5] = {
53
	{ { 1,1,1,1,1 }, /* 0 */
54
	  { 1,0,0,0,1 },
55
	  { 1,0,0,0,1 },
56
	  { 1,0,0,0,1 },
57
	  { 1,1,1,1,1 } },
58
	{ { 0,0,0,0,1 }, /* 1 */
59
	  { 0,0,0,0,1 },
60
	  { 0,0,0,0,1 },
61
	  { 0,0,0,0,1 },
62
	  { 0,0,0,0,1 } },
63
	{ { 1,1,1,1,1 }, /* 2 */
64
	  { 0,0,0,0,1 },
65
	  { 1,1,1,1,1 },
66
	  { 1,0,0,0,0 },
67
	  { 1,1,1,1,1 } },
68
	{ { 1,1,1,1,1 }, /* 3 */
69
	  { 0,0,0,0,1 },
70
	  { 1,1,1,1,1 },
71
	  { 0,0,0,0,1 },
72
	  { 1,1,1,1,1 } },
73
	{ { 1,0,0,0,1 }, /* 4 */
74
	  { 1,0,0,0,1 },
75
	  { 1,1,1,1,1 },
76
	  { 0,0,0,0,1 },
77
	  { 0,0,0,0,1 } },
78
	{ { 1,1,1,1,1 }, /* 5 */
79
	  { 1,0,0,0,0 },
80
	  { 1,1,1,1,1 },
81
	  { 0,0,0,0,1 },
82
	  { 1,1,1,1,1 } },
83
	{ { 1,1,1,1,1 }, /* 6 */
84
	  { 1,0,0,0,0 },
85
	  { 1,1,1,1,1 },
86
	  { 1,0,0,0,1 },
87
	  { 1,1,1,1,1 } },
88
	{ { 1,1,1,1,1 }, /* 7 */
89
	  { 0,0,0,0,1 },
90
	  { 0,0,0,0,1 },
91
	  { 0,0,0,0,1 },
92
	  { 0,0,0,0,1 } },
93
	{ { 1,1,1,1,1 }, /* 8 */
94
	  { 1,0,0,0,1 },
95
	  { 1,1,1,1,1 },
96
	  { 1,0,0,0,1 },
97
	  { 1,1,1,1,1 } },
98
	{ { 1,1,1,1,1 }, /* 9 */
99
	  { 1,0,0,0,1 },
100
	  { 1,1,1,1,1 },
101
	  { 0,0,0,0,1 },
102
	  { 1,1,1,1,1 } },
103
	{ { 0,0,0,0,0 }, /* : */
104
	  { 0,0,1,0,0 },
105
	  { 0,0,0,0,0 },
106
	  { 0,0,1,0,0 },
107
	  { 0,0,0,0,0 } },
108
	{ { 1,1,1,1,1 }, /* A */
109
	  { 1,0,0,0,1 },
110
	  { 1,1,1,1,1 },
111
	  { 1,0,0,0,1 },
112
	  { 1,0,0,0,1 } },
113
	{ { 1,1,1,1,1 }, /* P */
114
	  { 1,0,0,0,1 },
115
	  { 1,1,1,1,1 },
116
	  { 1,0,0,0,0 },
117
	  { 1,0,0,0,0 } },
118
	{ { 1,0,0,0,1 }, /* M */
119
	  { 1,1,0,1,1 },
120
	  { 1,0,1,0,1 },
121
	  { 1,0,0,0,1 },
122
	  { 1,0,0,0,1 } },
123
};
124
125
static void
126
window_clock_timer_callback(__unused int fd, __unused short events, void *arg)
127
{
128
	struct window_pane		*wp = arg;
129
	struct window_clock_mode_data	*data = wp->modedata;
130
	struct tm			 now, then;
131
	time_t				 t;
132
	struct timeval			 tv = { .tv_sec = 1 };
133
134
	evtimer_del(&data->timer);
135
	evtimer_add(&data->timer, &tv);
136
137
	t = time(NULL);
138
	gmtime_r(&t, &now);
139
	gmtime_r(&data->tim, &then);
140
	if (now.tm_min == then.tm_min)
141
		return;
142
	data->tim = t;
143
144
	window_clock_draw_screen(wp);
145
	server_redraw_window(wp->window);
146
}
147
148
static struct screen *
149
window_clock_init(struct window_pane *wp, __unused struct cmd_find_state *fs,
150
    __unused struct args *args)
151
{
152
	struct window_clock_mode_data	*data;
153
	struct screen			*s;
154
	struct timeval			 tv = { .tv_sec = 1 };
155
156
	wp->modedata = data = xmalloc(sizeof *data);
157
	data->tim = time(NULL);
158
159
	evtimer_set(&data->timer, window_clock_timer_callback, wp);
160
	evtimer_add(&data->timer, &tv);
161
162
	s = &data->screen;
163
	screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0);
164
	s->mode &= ~MODE_CURSOR;
165
166
	window_clock_draw_screen(wp);
167
168
	return (s);
169
}
170
171
static void
172
window_clock_free(struct window_pane *wp)
173
{
174
	struct window_clock_mode_data	*data = wp->modedata;
175
176
	evtimer_del(&data->timer);
177
	screen_free(&data->screen);
178
	free(data);
179
}
180
181
static void
182
window_clock_resize(struct window_pane *wp, u_int sx, u_int sy)
183
{
184
	struct window_clock_mode_data	*data = wp->modedata;
185
	struct screen			*s = &data->screen;
186
187
	screen_resize(s, sx, sy, 0);
188
	window_clock_draw_screen(wp);
189
}
190
191
static void
192
window_clock_key(struct window_pane *wp, __unused struct client *c,
193
    __unused struct session *sess, __unused key_code key,
194
    __unused struct mouse_event *m)
195
{
196
	window_pane_reset_mode(wp);
197
}
198
199
static void
200
window_clock_draw_screen(struct window_pane *wp)
201
{
202
	struct window_clock_mode_data	*data = wp->modedata;
203
	struct screen_write_ctx	 	 ctx;
204
	int				 colour, style;
205
	struct screen			*s = &data->screen;
206
	struct grid_cell		 gc;
207
	char				 tim[64], *ptr;
208
	time_t				 t;
209
	struct tm			*tm;
210
	u_int				 i, j, x, y, idx;
211
212
	colour = options_get_number(wp->window->options, "clock-mode-colour");
213
	style = options_get_number(wp->window->options, "clock-mode-style");
214
215
	screen_write_start(&ctx, NULL, s);
216
217
	t = time(NULL);
218
	tm = localtime(&t);
219
	if (style == 0) {
220
		strftime(tim, sizeof tim, "%l:%M ", localtime(&t));
221
		if (tm->tm_hour >= 12)
222
			strlcat(tim, "PM", sizeof tim);
223
		else
224
			strlcat(tim, "AM", sizeof tim);
225
	} else
226
		strftime(tim, sizeof tim, "%H:%M", tm);
227
228
	screen_write_clearscreen(&ctx, 8);
229
230
	if (screen_size_x(s) < 6 * strlen(tim) || screen_size_y(s) < 6) {
231
		if (screen_size_x(s) >= strlen(tim) && screen_size_y(s) != 0) {
232
			x = (screen_size_x(s) / 2) - (strlen(tim) / 2);
233
			y = screen_size_y(s) / 2;
234
			screen_write_cursormove(&ctx, x, y);
235
236
			memcpy(&gc, &grid_default_cell, sizeof gc);
237
			gc.flags |= GRID_FLAG_NOPALETTE;
238
			gc.fg = colour;
239
			screen_write_puts(&ctx, &gc, "%s", tim);
240
		}
241
242
		screen_write_stop(&ctx);
243
		return;
244
	}
245
246
	x = (screen_size_x(s) / 2) - 3 * strlen(tim);
247
	y = (screen_size_y(s) / 2) - 3;
248
249
	memcpy(&gc, &grid_default_cell, sizeof gc);
250
	gc.flags |= GRID_FLAG_NOPALETTE;
251
	gc.bg = colour;
252
	for (ptr = tim; *ptr != '\0'; ptr++) {
253
		if (*ptr >= '0' && *ptr <= '9')
254
			idx = *ptr - '0';
255
		else if (*ptr == ':')
256
			idx = 10;
257
		else if (*ptr == 'A')
258
			idx = 11;
259
		else if (*ptr == 'P')
260
			idx = 12;
261
		else if (*ptr == 'M')
262
			idx = 13;
263
		else {
264
			x += 6;
265
			continue;
266
		}
267
268
		for (j = 0; j < 5; j++) {
269
			for (i = 0; i < 5; i++) {
270
				screen_write_cursormove(&ctx, x + i, y + j);
271
				if (window_clock_table[idx][j][i])
272
					screen_write_putc(&ctx, &gc, ' ');
273
			}
274
		}
275
		x += 6;
276
	}
277
278
	screen_write_stop(&ctx);
279
}