1 /* $OpenBSD: window-clock.c,v 1.11 2015/04/19 21:34:21 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 <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 *, int, struct mouse_event *); 32 void window_clock_timer(struct window_pane *); 33 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 window_clock_timer, 42 }; 43 44 struct window_clock_mode_data { 45 struct screen screen; 46 time_t tim; 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 struct screen * 123 window_clock_init(struct window_pane *wp) 124 { 125 struct window_clock_mode_data *data; 126 struct screen *s; 127 128 wp->modedata = data = xmalloc(sizeof *data); 129 data->tim = time(NULL); 130 131 s = &data->screen; 132 screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0); 133 s->mode &= ~MODE_CURSOR; 134 135 window_clock_draw_screen(wp); 136 137 return (s); 138 } 139 140 void 141 window_clock_free(struct window_pane *wp) 142 { 143 struct window_clock_mode_data *data = wp->modedata; 144 145 screen_free(&data->screen); 146 free(data); 147 } 148 149 void 150 window_clock_resize(struct window_pane *wp, u_int sx, u_int sy) 151 { 152 struct window_clock_mode_data *data = wp->modedata; 153 struct screen *s = &data->screen; 154 155 screen_resize(s, sx, sy, 0); 156 window_clock_draw_screen(wp); 157 } 158 159 void 160 window_clock_key(struct window_pane *wp, unused struct client *c, 161 unused struct session *sess, unused int key, unused struct mouse_event *m) 162 { 163 window_pane_reset_mode(wp); 164 } 165 166 void 167 window_clock_timer(struct window_pane *wp) 168 { 169 struct window_clock_mode_data *data = wp->modedata; 170 struct tm now, then; 171 time_t t; 172 173 t = time(NULL); 174 gmtime_r(&t, &now); 175 gmtime_r(&data->tim, &then); 176 if (now.tm_min == then.tm_min) 177 return; 178 data->tim = t; 179 180 window_clock_draw_screen(wp); 181 server_redraw_window(wp->window); 182 } 183 184 void 185 window_clock_draw_screen(struct window_pane *wp) 186 { 187 struct window_clock_mode_data *data = wp->modedata; 188 struct screen_write_ctx ctx; 189 int colour, style; 190 struct screen *s = &data->screen; 191 struct grid_cell gc; 192 char tim[64], *ptr; 193 time_t t; 194 struct tm *tm; 195 u_int i, j, x, y, idx; 196 197 colour = options_get_number(&wp->window->options, "clock-mode-colour"); 198 style = options_get_number(&wp->window->options, "clock-mode-style"); 199 200 screen_write_start(&ctx, NULL, s); 201 202 t = time(NULL); 203 tm = localtime(&t); 204 if (style == 0) { 205 strftime(tim, sizeof tim, "%l:%M ", localtime(&t)); 206 if (tm->tm_hour >= 12) 207 strlcat(tim, "PM", sizeof tim); 208 else 209 strlcat(tim, "AM", sizeof tim); 210 } else 211 strftime(tim, sizeof tim, "%H:%M", tm); 212 213 screen_write_clearscreen(&ctx); 214 215 if (screen_size_x(s) < 6 * strlen(tim) || screen_size_y(s) < 6) { 216 if (screen_size_x(s) >= strlen(tim) && screen_size_y(s) != 0) { 217 x = (screen_size_x(s) / 2) - (strlen(tim) / 2); 218 y = screen_size_y(s) / 2; 219 screen_write_cursormove(&ctx, x, y); 220 221 memcpy(&gc, &grid_default_cell, sizeof gc); 222 colour_set_fg(&gc, colour); 223 screen_write_puts(&ctx, &gc, "%s", tim); 224 } 225 226 227 screen_write_stop(&ctx); 228 return; 229 } 230 231 x = (screen_size_x(s) / 2) - 3 * strlen(tim); 232 y = (screen_size_y(s) / 2) - 3; 233 234 memcpy(&gc, &grid_default_cell, sizeof gc); 235 colour_set_bg(&gc, colour); 236 for (ptr = tim; *ptr != '\0'; ptr++) { 237 if (*ptr >= '0' && *ptr <= '9') 238 idx = *ptr - '0'; 239 else if (*ptr == ':') 240 idx = 10; 241 else if (*ptr == 'A') 242 idx = 11; 243 else if (*ptr == 'P') 244 idx = 12; 245 else if (*ptr == 'M') 246 idx = 13; 247 else { 248 x += 6; 249 continue; 250 } 251 252 for (j = 0; j < 5; j++) { 253 for (i = 0; i < 5; i++) { 254 screen_write_cursormove(&ctx, x + i, y + j); 255 if (window_clock_table[idx][j][i]) 256 screen_write_putc(&ctx, &gc, ' '); 257 } 258 } 259 x += 6; 260 } 261 262 screen_write_stop(&ctx); 263 } 264