Annotation of embedaddon/tmux/window-clock.c, revision 1.1.1.1

1.1       misho       1: /* $OpenBSD$ */
                      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:        .init = window_clock_init,
                     38:        .free = window_clock_free,
                     39:        .resize = window_clock_resize,
                     40:        .key = window_clock_key,
                     41: };
                     42: 
                     43: struct window_clock_mode_data {
                     44:        struct screen           screen;
                     45:        time_t                  tim;
                     46:        struct event            timer;
                     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: static 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: 
                    145: static struct screen *
                    146: window_clock_init(struct window_pane *wp)
                    147: {
                    148:        struct window_clock_mode_data   *data;
                    149:        struct screen                   *s;
                    150:        struct timeval                   tv = { .tv_sec = 1 };
                    151: 
                    152:        wp->modedata = data = xmalloc(sizeof *data);
                    153:        data->tim = time(NULL);
                    154: 
                    155:        evtimer_set(&data->timer, window_clock_timer_callback, wp);
                    156:        evtimer_add(&data->timer, &tv);
                    157: 
                    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: static void
                    168: window_clock_free(struct window_pane *wp)
                    169: {
                    170:        struct window_clock_mode_data   *data = wp->modedata;
                    171: 
                    172:        evtimer_del(&data->timer);
                    173:        screen_free(&data->screen);
                    174:        free(data);
                    175: }
                    176: 
                    177: static 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: 
                    183:        screen_resize(s, sx, sy, 0);
                    184:        window_clock_draw_screen(wp);
                    185: }
                    186: 
                    187: static void
                    188: window_clock_key(struct window_pane *wp, __unused struct client *c,
                    189:     __unused struct session *sess, __unused key_code key,
                    190:     __unused struct mouse_event *m)
                    191: {
                    192:        window_pane_reset_mode(wp);
                    193: }
                    194: 
                    195: static void
                    196: window_clock_draw_screen(struct window_pane *wp)
                    197: {
                    198:        struct window_clock_mode_data   *data = wp->modedata;
                    199:        struct screen_write_ctx          ctx;
                    200:        int                              colour, style;
                    201:        struct screen                   *s = &data->screen;
                    202:        struct grid_cell                 gc;
                    203:        char                             tim[64], *ptr;
                    204:        time_t                           t;
                    205:        struct tm                       *tm;
                    206:        u_int                            i, j, x, y, idx;
                    207: 
                    208:        colour = options_get_number(wp->window->options, "clock-mode-colour");
                    209:        style = options_get_number(wp->window->options, "clock-mode-style");
                    210: 
                    211:        screen_write_start(&ctx, NULL, s);
                    212: 
                    213:        t = time(NULL);
                    214:        tm = localtime(&t);
                    215:        if (style == 0) {
                    216:                strftime(tim, sizeof tim, "%l:%M ", localtime(&t));
                    217:                if (tm->tm_hour >= 12)
                    218:                        strlcat(tim, "PM", sizeof tim);
                    219:                else
                    220:                        strlcat(tim, "AM", sizeof tim);
                    221:        } else
                    222:                strftime(tim, sizeof tim, "%H:%M", tm);
                    223: 
                    224:        screen_write_clearscreen(&ctx, 8);
                    225: 
                    226:        if (screen_size_x(s) < 6 * strlen(tim) || screen_size_y(s) < 6) {
                    227:                if (screen_size_x(s) >= strlen(tim) && screen_size_y(s) != 0) {
                    228:                        x = (screen_size_x(s) / 2) - (strlen(tim) / 2);
                    229:                        y = screen_size_y(s) / 2;
                    230:                        screen_write_cursormove(&ctx, x, y);
                    231: 
                    232:                        memcpy(&gc, &grid_default_cell, sizeof gc);
                    233:                        gc.flags |= GRID_FLAG_NOPALETTE;
                    234:                        gc.fg = colour;
                    235:                        screen_write_puts(&ctx, &gc, "%s", tim);
                    236:                }
                    237: 
                    238:                screen_write_stop(&ctx);
                    239:                return;
                    240:        }
                    241: 
                    242:        x = (screen_size_x(s) / 2) - 3 * strlen(tim);
                    243:        y = (screen_size_y(s) / 2) - 3;
                    244: 
                    245:        memcpy(&gc, &grid_default_cell, sizeof gc);
                    246:        gc.flags |= GRID_FLAG_NOPALETTE;
                    247:        gc.bg = colour;
                    248:        for (ptr = tim; *ptr != '\0'; ptr++) {
                    249:                if (*ptr >= '0' && *ptr <= '9')
                    250:                        idx = *ptr - '0';
                    251:                else if (*ptr == ':')
                    252:                        idx = 10;
                    253:                else if (*ptr == 'A')
                    254:                        idx = 11;
                    255:                else if (*ptr == 'P')
                    256:                        idx = 12;
                    257:                else if (*ptr == 'M')
                    258:                        idx = 13;
                    259:                else {
                    260:                        x += 6;
                    261:                        continue;
                    262:                }
                    263: 
                    264:                for (j = 0; j < 5; j++) {
                    265:                        for (i = 0; i < 5; i++) {
                    266:                                screen_write_cursormove(&ctx, x + i, y + j);
                    267:                                if (window_clock_table[idx][j][i])
                    268:                                        screen_write_putc(&ctx, &gc, ' ');
                    269:                        }
                    270:                }
                    271:                x += 6;
                    272:        }
                    273: 
                    274:        screen_write_stop(&ctx);
                    275: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>