[BACK]Return to window-clock.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/window-clock.c, Revision 1.13

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