xref: /openbsd-src/usr.bin/tmux/window-clock.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /* $OpenBSD: window-clock.c,v 1.2 2009/06/27 14:40:22 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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 <string.h>
22 #include <time.h>
23 
24 #include "tmux.h"
25 
26 struct screen *window_clock_init(struct window_pane *);
27 void	window_clock_free(struct window_pane *);
28 void	window_clock_resize(struct window_pane *, u_int, u_int);
29 void	window_clock_key(struct window_pane *, struct client *, int);
30 void	window_clock_timer(struct window_pane *);
31 
32 void	window_clock_draw_screen(struct window_pane *);
33 
34 const struct window_mode window_clock_mode = {
35 	window_clock_init,
36 	window_clock_free,
37 	window_clock_resize,
38 	window_clock_key,
39 	NULL,
40 	window_clock_timer,
41 };
42 
43 struct window_clock_mode_data {
44 	struct screen	        screen;
45 	time_t			tim;
46 };
47 
48 struct screen *
49 window_clock_init(struct window_pane *wp)
50 {
51 	struct window_clock_mode_data	*data;
52 	struct screen			*s;
53 
54 	wp->modedata = data = xmalloc(sizeof *data);
55 	data->tim = time(NULL);
56 
57 	s = &data->screen;
58 	screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0);
59 	s->mode &= ~MODE_CURSOR;
60 
61 	window_clock_draw_screen(wp);
62 
63 	return (s);
64 }
65 
66 void
67 window_clock_free(struct window_pane *wp)
68 {
69 	struct window_clock_mode_data	*data = wp->modedata;
70 
71 	screen_free(&data->screen);
72 	xfree(data);
73 }
74 
75 void
76 window_clock_resize(struct window_pane *wp, u_int sx, u_int sy)
77 {
78 	struct window_clock_mode_data	*data = wp->modedata;
79 	struct screen			*s = &data->screen;
80 
81  	screen_resize(s, sx, sy);
82 	window_clock_draw_screen(wp);
83 }
84 
85 void
86 window_clock_key(
87     struct window_pane *wp, unused struct client *c, unused int key)
88 {
89 	window_pane_reset_mode(wp);
90 }
91 
92 void
93 window_clock_timer(struct window_pane *wp)
94 {
95 	struct window_clock_mode_data	*data = wp->modedata;
96 	struct tm			 now, then;
97 	time_t				 t;
98 
99 	t = time(NULL);
100 	gmtime_r(&t, &now);
101 	gmtime_r(&data->tim, &then);
102 	if (now.tm_min == then.tm_min)
103 		return;
104 	data->tim = t;
105 
106 	window_clock_draw_screen(wp);
107 	server_redraw_window(wp->window);
108 }
109 
110 void
111 window_clock_draw_screen(struct window_pane *wp)
112 {
113 	struct window_clock_mode_data	*data = wp->modedata;
114 	struct screen_write_ctx	 	 ctx;
115 	u_int				 colour;
116 	int				 style;
117 
118 	colour = options_get_number(&wp->window->options, "clock-mode-colour");
119 	style = options_get_number(&wp->window->options, "clock-mode-style");
120 
121 	screen_write_start(&ctx, NULL, &data->screen);
122 	clock_draw(&ctx, colour, style);
123 	screen_write_stop(&ctx);
124 }
125