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