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

Annotation of src/usr.bin/tmux/cmd-display-menu.c, Revision 1.6

1.6     ! nicm        1: /* $OpenBSD: cmd-display-menu.c,v 1.5 2020/03/18 09:13:49 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2019 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:
                     24: #include "tmux.h"
                     25:
                     26: /*
                     27:  * Display a menu on a client.
                     28:  */
                     29:
                     30: static enum cmd_retval cmd_display_menu_exec(struct cmd *,
                     31:                            struct cmdq_item *);
                     32:
                     33: const struct cmd_entry cmd_display_menu_entry = {
                     34:        .name = "display-menu",
                     35:        .alias = "menu",
                     36:
1.4       nicm       37:        .args = { "c:t:T:x:y:", 1, -1 },
                     38:        .usage = "[-c target-client] " CMD_TARGET_PANE_USAGE " [-T title] "
                     39:                 "[-x position] [-y position] name key command ...",
1.1       nicm       40:
                     41:        .target = { 't', CMD_FIND_PANE, 0 },
                     42:
                     43:        .flags = CMD_AFTERHOOK,
                     44:        .exec = cmd_display_menu_exec
                     45: };
                     46:
1.5       nicm       47: static void
                     48: cmd_display_menu_get_position(struct client *c, struct cmdq_item *item,
                     49:     struct args *args, u_int *px, u_int *py, u_int w, u_int h)
                     50: {
                     51:        struct winlink          *wl = item->target.wl;
                     52:        struct window_pane      *wp = item->target.wp;
                     53:        struct style_range      *sr;
                     54:        const char              *xp, *yp;
                     55:        int                      at = status_at_line(c);
                     56:        u_int                    ox, oy, sx, sy;
                     57:
                     58:        xp = args_get(args, 'x');
                     59:        if (xp == NULL)
                     60:                *px = 0;
                     61:        else if (strcmp(xp, "R") == 0)
                     62:                *px = c->tty.sx - 1;
1.6     ! nicm       63:        else if (strcmp(xp, "C") == 0)
        !            64:                *px = (c->tty.sx - 1) / 2 - w / 2;
1.5       nicm       65:        else if (strcmp(xp, "P") == 0) {
                     66:                tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
                     67:                if (wp->xoff >= ox)
                     68:                        *px = wp->xoff - ox;
                     69:                else
                     70:                        *px = 0;
                     71:        } else if (strcmp(xp, "M") == 0 && item->shared->mouse.valid) {
                     72:                if (item->shared->mouse.x > w / 2)
                     73:                        *px = item->shared->mouse.x - w / 2;
                     74:                else
                     75:                        *px = 0;
                     76:        } else if (strcmp(xp, "W") == 0) {
                     77:                if (at == -1)
                     78:                        *px = 0;
                     79:                else {
                     80:                        TAILQ_FOREACH(sr, &c->status.entries[0].ranges, entry) {
                     81:                                if (sr->type != STYLE_RANGE_WINDOW)
                     82:                                        continue;
                     83:                                if (sr->argument == (u_int)wl->idx)
                     84:                                        break;
                     85:                        }
                     86:                        if (sr != NULL)
                     87:                                *px = sr->start;
                     88:                        else
                     89:                                *px = 0;
                     90:                }
                     91:        } else
                     92:                *px = strtoul(xp, NULL, 10);
                     93:        if ((*px) + w >= c->tty.sx)
                     94:                *px = c->tty.sx - w;
                     95:
                     96:        yp = args_get(args, 'y');
                     97:        if (yp == NULL)
                     98:                *py = 0;
1.6     ! nicm       99:        else if (strcmp(yp, "C") == 0)
        !           100:                *py = (c->tty.sy - 1) / 2 + h / 2;
1.5       nicm      101:        else if (strcmp(yp, "P") == 0) {
                    102:                tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
                    103:                if (wp->yoff + wp->sy >= oy)
                    104:                        *py = wp->yoff + wp->sy - oy;
                    105:                else
                    106:                        *py = 0;
                    107:        } else if (strcmp(yp, "M") == 0 && item->shared->mouse.valid)
                    108:                *py = item->shared->mouse.y + h;
                    109:        else if (strcmp(yp, "S") == 0) {
                    110:                if (at == -1)
                    111:                        *py = c->tty.sy;
                    112:                else if (at == 0)
                    113:                        *py = status_line_size(c) + h;
                    114:                else
                    115:                        *py = at;
                    116:        } else
                    117:                *py = strtoul(yp, NULL, 10);
                    118:        if (*py < h)
                    119:                *py = 0;
                    120:        else
                    121:                *py -= h;
                    122:        if ((*py) + h >= c->tty.sy)
                    123:                *py = c->tty.sy - h;
                    124: }
                    125:
1.1       nicm      126: static enum cmd_retval
                    127: cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
                    128: {
                    129:        struct args             *args = self->args;
                    130:        struct client           *c;
                    131:        struct session          *s = item->target.s;
                    132:        struct winlink          *wl = item->target.wl;
                    133:        struct window_pane      *wp = item->target.wp;
                    134:        struct cmd_find_state   *fs = &item->target;
                    135:        struct menu             *menu = NULL;
1.4       nicm      136:        struct menu_item         menu_item;
1.5       nicm      137:        const char              *key;
1.4       nicm      138:        char                    *title, *name;
1.6     ! nicm      139:        int                      flags = 0, i;
1.5       nicm      140:        u_int                    px, py;
1.1       nicm      141:
                    142:        if ((c = cmd_find_client(item, args_get(args, 'c'), 0)) == NULL)
                    143:                return (CMD_RETURN_ERROR);
                    144:        if (c->overlay_draw != NULL)
                    145:                return (CMD_RETURN_NORMAL);
                    146:
                    147:        if (args_has(args, 'T'))
                    148:                title = format_single(NULL, args_get(args, 'T'), c, s, wl, wp);
                    149:        else
                    150:                title = xstrdup("");
1.4       nicm      151:
                    152:        menu = menu_create(title);
                    153:
                    154:        for (i = 0; i != args->argc; /* nothing */) {
                    155:                name = args->argv[i++];
                    156:                if (*name == '\0') {
                    157:                        menu_add_item(menu, NULL, item, c, fs);
                    158:                        continue;
                    159:                }
                    160:
                    161:                if (args->argc - i < 2) {
                    162:                        cmdq_error(item, "not enough arguments");
                    163:                        free(title);
                    164:                        menu_free(menu);
                    165:                        return (CMD_RETURN_ERROR);
                    166:                }
                    167:                key = args->argv[i++];
                    168:
                    169:                menu_item.name = name;
                    170:                menu_item.key = key_string_lookup_string(key);
                    171:                menu_item.command = args->argv[i++];
                    172:
                    173:                menu_add_item(menu, &menu_item, item, c, fs);
                    174:        }
1.1       nicm      175:        free(title);
                    176:        if (menu == NULL) {
1.4       nicm      177:                cmdq_error(item, "invalid menu arguments");
1.1       nicm      178:                return (CMD_RETURN_ERROR);
                    179:        }
                    180:        if (menu->count == 0) {
                    181:                menu_free(menu);
                    182:                return (CMD_RETURN_NORMAL);
                    183:        }
1.5       nicm      184:        cmd_display_menu_get_position(c, item, args, &px, &py, menu->width + 4,
                    185:            menu->count + 2);
1.1       nicm      186:
                    187:        if (!item->shared->mouse.valid)
                    188:                flags |= MENU_NOMOUSE;
                    189:        if (menu_display(menu, flags, item, px, py, c, fs, NULL, NULL) != 0)
                    190:                return (CMD_RETURN_NORMAL);
                    191:        return (CMD_RETURN_WAIT);
                    192: }