4 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
27 static struct screen
*window_clock_init(struct window_mode_entry
*,
28 struct cmd_find_state
*, struct args
*);
29 static void window_clock_free(struct window_mode_entry
*);
30 static void window_clock_resize(struct window_mode_entry
*, u_int
, u_int
);
31 static void window_clock_key(struct window_mode_entry
*, struct client
*,
32 struct session
*, struct winlink
*, key_code
,
33 struct mouse_event
*);
35 static void window_clock_timer_callback(int, short, void *);
36 static void window_clock_draw_screen(struct window_mode_entry
*);
38 const struct window_mode window_clock_mode
= {
41 .init
= window_clock_init
,
42 .free
= window_clock_free
,
43 .resize
= window_clock_resize
,
44 .key
= window_clock_key
,
47 struct window_clock_mode_data
{
53 const char window_clock_table
[14][5][5] = {
54 { { 1,1,1,1,1 }, /* 0 */
59 { { 0,0,0,0,1 }, /* 1 */
64 { { 1,1,1,1,1 }, /* 2 */
69 { { 1,1,1,1,1 }, /* 3 */
74 { { 1,0,0,0,1 }, /* 4 */
79 { { 1,1,1,1,1 }, /* 5 */
84 { { 1,1,1,1,1 }, /* 6 */
89 { { 1,1,1,1,1 }, /* 7 */
94 { { 1,1,1,1,1 }, /* 8 */
99 { { 1,1,1,1,1 }, /* 9 */
104 { { 0,0,0,0,0 }, /* : */
109 { { 1,1,1,1,1 }, /* A */
114 { { 1,1,1,1,1 }, /* P */
119 { { 1,0,0,0,1 }, /* M */
127 window_clock_timer_callback(__unused
int fd
, __unused
short events
, void *arg
)
129 struct window_mode_entry
*wme
= arg
;
130 struct window_pane
*wp
= wme
->wp
;
131 struct window_clock_mode_data
*data
= wme
->data
;
134 struct timeval tv
= { .tv_sec
= 1 };
136 evtimer_del(&data
->timer
);
137 evtimer_add(&data
->timer
, &tv
);
139 if (TAILQ_FIRST(&wp
->modes
) != wme
)
144 gmtime_r(&data
->tim
, &then
);
145 if (now
.tm_min
== then
.tm_min
)
149 window_clock_draw_screen(wme
);
150 wp
->flags
|= PANE_REDRAW
;
153 static struct screen
*
154 window_clock_init(struct window_mode_entry
*wme
,
155 __unused
struct cmd_find_state
*fs
, __unused
struct args
*args
)
157 struct window_pane
*wp
= wme
->wp
;
158 struct window_clock_mode_data
*data
;
160 struct timeval tv
= { .tv_sec
= 1 };
162 wme
->data
= data
= xmalloc(sizeof *data
);
163 data
->tim
= time(NULL
);
165 evtimer_set(&data
->timer
, window_clock_timer_callback
, wme
);
166 evtimer_add(&data
->timer
, &tv
);
169 screen_init(s
, screen_size_x(&wp
->base
), screen_size_y(&wp
->base
), 0);
170 s
->mode
&= ~MODE_CURSOR
;
172 window_clock_draw_screen(wme
);
178 window_clock_free(struct window_mode_entry
*wme
)
180 struct window_clock_mode_data
*data
= wme
->data
;
182 evtimer_del(&data
->timer
);
183 screen_free(&data
->screen
);
188 window_clock_resize(struct window_mode_entry
*wme
, u_int sx
, u_int sy
)
190 struct window_clock_mode_data
*data
= wme
->data
;
191 struct screen
*s
= &data
->screen
;
193 screen_resize(s
, sx
, sy
, 0);
194 window_clock_draw_screen(wme
);
198 window_clock_key(struct window_mode_entry
*wme
, __unused
struct client
*c
,
199 __unused
struct session
*s
, __unused
struct winlink
*wl
,
200 __unused key_code key
, __unused
struct mouse_event
*m
)
202 window_pane_reset_mode(wme
->wp
);
206 window_clock_draw_screen(struct window_mode_entry
*wme
)
208 struct window_pane
*wp
= wme
->wp
;
209 struct window_clock_mode_data
*data
= wme
->data
;
210 struct screen_write_ctx ctx
;
212 struct screen
*s
= &data
->screen
;
217 u_int i
, j
, x
, y
, idx
;
219 colour
= options_get_number(wp
->window
->options
, "clock-mode-colour");
220 style
= options_get_number(wp
->window
->options
, "clock-mode-style");
222 screen_write_start(&ctx
, s
);
227 strftime(tim
, sizeof tim
, "%l:%M ", localtime(&t
));
228 if (tm
->tm_hour
>= 12)
229 strlcat(tim
, "PM", sizeof tim
);
231 strlcat(tim
, "AM", sizeof tim
);
233 strftime(tim
, sizeof tim
, "%H:%M", tm
);
235 screen_write_clearscreen(&ctx
, 8);
237 if (screen_size_x(s
) < 6 * strlen(tim
) || screen_size_y(s
) < 6) {
238 if (screen_size_x(s
) >= strlen(tim
) && screen_size_y(s
) != 0) {
239 x
= (screen_size_x(s
) / 2) - (strlen(tim
) / 2);
240 y
= screen_size_y(s
) / 2;
241 screen_write_cursormove(&ctx
, x
, y
, 0);
243 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
244 gc
.flags
|= GRID_FLAG_NOPALETTE
;
246 screen_write_puts(&ctx
, &gc
, "%s", tim
);
249 screen_write_stop(&ctx
);
253 x
= (screen_size_x(s
) / 2) - 3 * strlen(tim
);
254 y
= (screen_size_y(s
) / 2) - 3;
256 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
257 gc
.flags
|= GRID_FLAG_NOPALETTE
;
259 for (ptr
= tim
; *ptr
!= '\0'; ptr
++) {
260 if (*ptr
>= '0' && *ptr
<= '9')
262 else if (*ptr
== ':')
264 else if (*ptr
== 'A')
266 else if (*ptr
== 'P')
268 else if (*ptr
== 'M')
275 for (j
= 0; j
< 5; j
++) {
276 for (i
= 0; i
< 5; i
++) {
277 screen_write_cursormove(&ctx
, x
+ i
, y
+ j
, 0);
278 if (window_clock_table
[idx
][j
][i
])
279 screen_write_putc(&ctx
, &gc
, ' ');
285 screen_write_stop(&ctx
);