[BACK]Return to cmd-display-panes.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-display-panes.c, Revision 1.34

1.34    ! nicm        1: /* $OpenBSD: cmd-display-panes.c,v 1.33 2020/04/13 18:59:41 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.12      nicm        4:  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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.13      nicm       21: #include <stdlib.h>
1.24      nicm       22: #include <string.h>
1.13      nicm       23:
1.1       nicm       24: #include "tmux.h"
                     25:
                     26: /*
                     27:  * Display panes on a client.
                     28:  */
                     29:
1.16      nicm       30: static enum cmd_retval cmd_display_panes_exec(struct cmd *,
                     31:                            struct cmdq_item *);
1.13      nicm       32:
1.1       nicm       33: const struct cmd_entry cmd_display_panes_entry = {
1.10      nicm       34:        .name = "display-panes",
                     35:        .alias = "displayp",
                     36:
1.23      nicm       37:        .args = { "bd:t:", 0, 1 },
                     38:        .usage = "[-b] [-d duration] " CMD_TARGET_CLIENT_USAGE " [template]",
1.10      nicm       39:
1.34    ! nicm       40:        .flags = CMD_AFTERHOOK|CMD_CLIENT_TFLAG,
1.10      nicm       41:        .exec = cmd_display_panes_exec
1.1       nicm       42: };
                     43:
1.24      nicm       44: struct cmd_display_panes_data {
                     45:        struct cmdq_item        *item;
                     46:        char                    *command;
                     47: };
                     48:
                     49: static void
                     50: cmd_display_panes_draw_pane(struct screen_redraw_ctx *ctx,
                     51:     struct window_pane *wp)
1.1       nicm       52: {
1.24      nicm       53:        struct client           *c = ctx->c;
                     54:        struct tty              *tty = &c->tty;
                     55:        struct session          *s = c->session;
                     56:        struct options          *oo = s->options;
                     57:        struct window           *w = wp->window;
                     58:        struct grid_cell         gc;
                     59:        u_int                    idx, px, py, i, j, xoff, yoff, sx, sy;
                     60:        int                      colour, active_colour;
                     61:        char                     buf[16], *ptr;
                     62:        size_t                   len;
                     63:
                     64:        if (wp->xoff + wp->sx <= ctx->ox ||
                     65:            wp->xoff >= ctx->ox + ctx->sx ||
                     66:            wp->yoff + wp->sy <= ctx->oy ||
                     67:            wp->yoff >= ctx->oy + ctx->sy)
                     68:                return;
                     69:
                     70:        if (wp->xoff >= ctx->ox && wp->xoff + wp->sx <= ctx->ox + ctx->sx) {
                     71:                /* All visible. */
                     72:                xoff = wp->xoff - ctx->ox;
                     73:                sx = wp->sx;
                     74:        } else if (wp->xoff < ctx->ox &&
                     75:            wp->xoff + wp->sx > ctx->ox + ctx->sx) {
                     76:                /* Both left and right not visible. */
                     77:                xoff = 0;
                     78:                sx = ctx->sx;
                     79:        } else if (wp->xoff < ctx->ox) {
                     80:                /* Left not visible. */
                     81:                xoff = 0;
                     82:                sx = wp->sx - (ctx->ox - wp->xoff);
                     83:        } else {
                     84:                /* Right not visible. */
                     85:                xoff = wp->xoff - ctx->ox;
                     86:                sx = wp->sx - xoff;
                     87:        }
                     88:        if (wp->yoff >= ctx->oy && wp->yoff + wp->sy <= ctx->oy + ctx->sy) {
                     89:                /* All visible. */
                     90:                yoff = wp->yoff - ctx->oy;
                     91:                sy = wp->sy;
                     92:        } else if (wp->yoff < ctx->oy &&
                     93:            wp->yoff + wp->sy > ctx->oy + ctx->sy) {
                     94:                /* Both top and bottom not visible. */
                     95:                yoff = 0;
                     96:                sy = ctx->sy;
                     97:        } else if (wp->yoff < ctx->oy) {
                     98:                /* Top not visible. */
                     99:                yoff = 0;
                    100:                sy = wp->sy - (ctx->oy - wp->yoff);
                    101:        } else {
                    102:                /* Bottom not visible. */
                    103:                yoff = wp->yoff - ctx->oy;
                    104:                sy = wp->sy - yoff;
                    105:        }
1.19      nicm      106:
1.24      nicm      107:        if (ctx->statustop)
                    108:                yoff += ctx->statuslines;
                    109:        px = sx / 2;
                    110:        py = sy / 2;
                    111:
                    112:        if (window_pane_index(wp, &idx) != 0)
                    113:                fatalx("index not found");
                    114:        len = xsnprintf(buf, sizeof buf, "%u", idx);
                    115:
                    116:        if (sx < len)
                    117:                return;
                    118:        colour = options_get_number(oo, "display-panes-colour");
                    119:        active_colour = options_get_number(oo, "display-panes-active-colour");
                    120:
                    121:        if (sx < len * 6 || sy < 5) {
                    122:                tty_cursor(tty, xoff + px - len / 2, yoff + py);
                    123:                goto draw_text;
                    124:        }
1.13      nicm      125:
1.24      nicm      126:        px -= len * 3;
                    127:        py -= 2;
1.13      nicm      128:
1.24      nicm      129:        memcpy(&gc, &grid_default_cell, sizeof gc);
                    130:        if (w->active == wp)
                    131:                gc.bg = active_colour;
1.13      nicm      132:        else
1.24      nicm      133:                gc.bg = colour;
                    134:        gc.flags |= GRID_FLAG_NOPALETTE;
                    135:
                    136:        tty_attributes(tty, &gc, wp);
                    137:        for (ptr = buf; *ptr != '\0'; ptr++) {
                    138:                if (*ptr < '0' || *ptr > '9')
                    139:                        continue;
                    140:                idx = *ptr - '0';
                    141:
                    142:                for (j = 0; j < 5; j++) {
                    143:                        for (i = px; i < px + 5; i++) {
                    144:                                tty_cursor(tty, xoff + i, yoff + py + j);
                    145:                                if (window_clock_table[idx][j][i - px])
                    146:                                        tty_putc(tty, ' ');
                    147:                        }
                    148:                }
                    149:                px += 6;
                    150:        }
                    151:
                    152:        len = xsnprintf(buf, sizeof buf, "%ux%u", wp->sx, wp->sy);
                    153:        if (sx < len || sy < 6)
                    154:                return;
                    155:        tty_cursor(tty, xoff + sx - len, yoff);
                    156:
                    157: draw_text:
                    158:        memcpy(&gc, &grid_default_cell, sizeof gc);
                    159:        if (w->active == wp)
                    160:                gc.fg = active_colour;
1.23      nicm      161:        else
1.24      nicm      162:                gc.fg = colour;
                    163:        gc.flags |= GRID_FLAG_NOPALETTE;
1.13      nicm      164:
1.24      nicm      165:        tty_attributes(tty, &gc, wp);
                    166:        tty_puts(tty, buf);
                    167:
                    168:        tty_cursor(tty, 0, 0);
                    169: }
                    170:
                    171: static void
                    172: cmd_display_panes_draw(struct client *c, struct screen_redraw_ctx *ctx)
                    173: {
                    174:        struct window           *w = c->session->curw->window;
                    175:        struct window_pane      *wp;
                    176:
                    177:        log_debug("%s: %s @%u", __func__, c->name, w->id);
1.1       nicm      178:
1.24      nicm      179:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    180:                if (window_pane_visible(wp))
                    181:                        cmd_display_panes_draw_pane(ctx, wp);
                    182:        }
1.13      nicm      183: }
                    184:
                    185: static void
1.24      nicm      186: cmd_display_panes_free(struct client *c)
                    187: {
                    188:        struct cmd_display_panes_data   *cdata = c->overlay_data;
                    189:
                    190:        if (cdata->item != NULL)
1.28      nicm      191:                cmdq_continue(cdata->item);
1.24      nicm      192:        free(cdata->command);
                    193:        free(cdata);
                    194: }
                    195:
                    196: static int
                    197: cmd_display_panes_key(struct client *c, struct key_event *event)
1.13      nicm      198: {
1.24      nicm      199:        struct cmd_display_panes_data   *cdata = c->overlay_data;
1.33      nicm      200:        char                            *cmd, *expanded, *error;
1.24      nicm      201:        struct window                   *w = c->session->curw->window;
                    202:        struct window_pane              *wp;
1.33      nicm      203:        enum cmd_parse_status            status;
1.13      nicm      204:
1.24      nicm      205:        if (event->key < '0' || event->key > '9')
1.29      nicm      206:                return (-1);
1.24      nicm      207:
                    208:        wp = window_pane_at_index(w, event->key - '0');
1.15      nicm      209:        if (wp == NULL)
1.24      nicm      210:                return (1);
                    211:        window_unzoom(w);
1.21      nicm      212:
1.15      nicm      213:        xasprintf(&expanded, "%%%u", wp->id);
1.24      nicm      214:        cmd = cmd_template_replace(cdata->command, expanded, 1);
1.15      nicm      215:
1.33      nicm      216:        status = cmd_parse_and_append(cmd, NULL, c, NULL, &error);
                    217:        if (status == CMD_PARSE_ERROR) {
                    218:                cmdq_append(c, cmdq_get_error(error));
                    219:                free(error);
1.23      nicm      220:        }
1.15      nicm      221:
                    222:        free(cmd);
                    223:        free(expanded);
1.24      nicm      224:        return (1);
                    225: }
                    226:
                    227: static enum cmd_retval
                    228: cmd_display_panes_exec(struct cmd *self, struct cmdq_item *item)
                    229: {
1.31      nicm      230:        struct args                     *args = cmd_get_args(self);
1.34    ! nicm      231:        struct client                   *tc = cmdq_get_target_client(item);
        !           232:        struct session                  *s = tc->session;
1.24      nicm      233:        u_int                            delay;
                    234:        char                            *cause;
                    235:        struct cmd_display_panes_data   *cdata;
                    236:
1.34    ! nicm      237:        if (tc->overlay_draw != NULL)
1.24      nicm      238:                return (CMD_RETURN_NORMAL);
1.13      nicm      239:
1.24      nicm      240:        if (args_has(args, 'd')) {
                    241:                delay = args_strtonum(args, 'd', 0, UINT_MAX, &cause);
                    242:                if (cause != NULL) {
                    243:                        cmdq_error(item, "delay %s", cause);
                    244:                        free(cause);
                    245:                        return (CMD_RETURN_ERROR);
                    246:                }
                    247:        } else
                    248:                delay = options_get_number(s->options, "display-panes-time");
                    249:
                    250:        cdata = xmalloc(sizeof *cdata);
                    251:        if (args->argc != 0)
                    252:                cdata->command = xstrdup(args->argv[0]);
                    253:        else
                    254:                cdata->command = xstrdup("select-pane -t '%%'");
                    255:        if (args_has(args, 'b'))
                    256:                cdata->item = NULL;
                    257:        else
                    258:                cdata->item = item;
1.21      nicm      259:
1.34    ! nicm      260:        server_client_set_overlay(tc, delay, NULL, NULL, cmd_display_panes_draw,
1.24      nicm      261:            cmd_display_panes_key, cmd_display_panes_free, cdata);
1.21      nicm      262:
1.24      nicm      263:        if (args_has(args, 'b'))
                    264:                return (CMD_RETURN_NORMAL);
                    265:        return (CMD_RETURN_WAIT);
1.1       nicm      266: }