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

Annotation of src/usr.bin/tmux/menu.c, Revision 1.19

1.19    ! nicm        1: /* $OpenBSD: menu.c,v 1.18 2020/04/13 14:04:25 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: struct menu_data {
                     27:        struct cmdq_item        *item;
                     28:        int                      flags;
                     29:
                     30:        struct cmd_find_state    fs;
                     31:        struct screen            s;
                     32:
                     33:        u_int                    px;
                     34:        u_int                    py;
                     35:
                     36:        struct menu             *menu;
                     37:        int                      choice;
                     38:
                     39:        menu_choice_cb           cb;
                     40:        void                    *data;
                     41: };
                     42:
1.8       nicm       43: void
                     44: menu_add_items(struct menu *menu, const struct menu_item *items,
                     45:     struct cmdq_item *qitem, struct client *c, struct cmd_find_state *fs)
                     46: {
                     47:        const struct menu_item  *loop;
                     48:
                     49:        for (loop = items; loop->name != NULL; loop++)
                     50:                menu_add_item(menu, loop, qitem, c, fs);
                     51: }
                     52:
                     53: void
                     54: menu_add_item(struct menu *menu, const struct menu_item *item,
1.6       nicm       55:     struct cmdq_item *qitem, struct client *c, struct cmd_find_state *fs)
1.1       nicm       56: {
                     57:        struct menu_item        *new_item;
1.8       nicm       58:        const char              *key, *cmd;
                     59:        char                    *s, *name;
1.1       nicm       60:        u_int                    width;
1.8       nicm       61:        int                      line;
                     62:
                     63:        line = (item == NULL || item->name == NULL || *item->name == '\0');
                     64:        if (line && menu->count == 0)
                     65:                return;
1.1       nicm       66:
                     67:        menu->items = xreallocarray(menu->items, menu->count + 1,
                     68:            sizeof *menu->items);
                     69:        new_item = &menu->items[menu->count++];
                     70:        memset(new_item, 0, sizeof *new_item);
                     71:
1.8       nicm       72:        if (line)
1.1       nicm       73:                return;
1.8       nicm       74:
                     75:        if (fs != NULL)
                     76:                s = format_single(qitem, item->name, c, fs->s, fs->wl, fs->wp);
                     77:        else
                     78:                s = format_single(qitem, item->name, c, NULL, NULL, NULL);
                     79:        if (*s == '\0') { /* no item if empty after format expanded */
1.1       nicm       80:                menu->count--;
                     81:                return;
                     82:        }
1.9       nicm       83:        if (*s != '-' && item->key != KEYC_UNKNOWN && item->key != KEYC_NONE) {
1.1       nicm       84:                key = key_string_lookup_key(item->key);
1.8       nicm       85:                xasprintf(&name, "%s#[default] #[align=right](%s)", s, key);
1.1       nicm       86:        } else
1.8       nicm       87:                xasprintf(&name, "%s", s);
                     88:        new_item->name = name;
                     89:        free(s);
                     90:
                     91:        cmd = item->command;
                     92:        if (cmd != NULL) {
                     93:                if (fs != NULL)
                     94:                        s = format_single(qitem, cmd, c, fs->s, fs->wl, fs->wp);
                     95:                else
                     96:                        s = format_single(qitem, cmd, c, NULL, NULL, NULL);
1.6       nicm       97:        } else
1.8       nicm       98:                s = NULL;
                     99:        new_item->command = s;
1.1       nicm      100:        new_item->key = item->key;
                    101:
                    102:        width = format_width(new_item->name);
                    103:        if (width > menu->width)
                    104:                menu->width = width;
                    105: }
                    106:
                    107: struct menu *
1.8       nicm      108: menu_create(const char *title)
1.1       nicm      109: {
                    110:        struct menu     *menu;
                    111:
                    112:        menu = xcalloc(1, sizeof *menu);
                    113:        menu->title = xstrdup(title);
                    114:
                    115:        return (menu);
                    116: }
                    117:
                    118: void
                    119: menu_free(struct menu *menu)
                    120: {
                    121:        u_int   i;
                    122:
                    123:        for (i = 0; i < menu->count; i++) {
1.8       nicm      124:                free((void *)menu->items[i].name);
                    125:                free((void *)menu->items[i].command);
1.1       nicm      126:        }
                    127:        free(menu->items);
                    128:
1.8       nicm      129:        free((void *)menu->title);
1.1       nicm      130:        free(menu);
                    131: }
                    132:
1.15      nicm      133: static int
                    134: menu_mode_cb(struct client *c, __unused u_int *cx, __unused u_int *cy)
                    135: {
                    136:        struct menu_data        *md = c->overlay_data;
                    137:
                    138:        if (~md->flags & MENU_NOMOUSE)
                    139:                return (MODE_MOUSE_ALL);
                    140:        return (0);
                    141: }
                    142:
1.1       nicm      143: static void
                    144: menu_draw_cb(struct client *c, __unused struct screen_redraw_ctx *ctx0)
                    145: {
                    146:        struct menu_data        *md = c->overlay_data;
                    147:        struct tty              *tty = &c->tty;
                    148:        struct screen           *s = &md->s;
                    149:        struct menu             *menu = md->menu;
                    150:        struct screen_write_ctx  ctx;
1.13      nicm      151:        u_int                    i, px = md->px, py = md->py;
1.1       nicm      152:
                    153:        screen_write_start(&ctx, NULL, s);
                    154:        screen_write_clearscreen(&ctx, 8);
                    155:        screen_write_menu(&ctx, menu, md->choice);
                    156:        screen_write_stop(&ctx);
                    157:
                    158:        for (i = 0; i < screen_size_y(&md->s); i++)
                    159:                tty_draw_line(tty, NULL, s, 0, i, menu->width + 4, px, py + i);
                    160: }
                    161:
                    162: static void
                    163: menu_free_cb(struct client *c)
                    164: {
                    165:        struct menu_data        *md = c->overlay_data;
                    166:
                    167:        if (md->item != NULL)
1.10      nicm      168:                cmdq_continue(md->item);
1.1       nicm      169:
1.3       nicm      170:        if (md->cb != NULL)
                    171:                md->cb(md->menu, UINT_MAX, KEYC_NONE, md->data);
                    172:
1.1       nicm      173:        screen_free(&md->s);
                    174:        menu_free(md->menu);
                    175:        free(md);
                    176: }
                    177:
                    178: static int
                    179: menu_key_cb(struct client *c, struct key_event *event)
                    180: {
                    181:        struct menu_data                *md = c->overlay_data;
                    182:        struct menu                     *menu = md->menu;
                    183:        struct mouse_event              *m = &event->m;
                    184:        u_int                            i;
                    185:        int                              count = menu->count, old = md->choice;
                    186:        const struct menu_item          *item;
                    187:        struct cmdq_item                *new_item;
1.5       nicm      188:        struct cmd_parse_result         *pr;
1.9       nicm      189:        const char                      *name;
1.1       nicm      190:
                    191:        if (KEYC_IS_MOUSE(event->key)) {
1.12      nicm      192:                if (md->flags & MENU_NOMOUSE) {
                    193:                        if (MOUSE_BUTTONS(m->b) != 0)
                    194:                                return (1);
1.1       nicm      195:                        return (0);
1.12      nicm      196:                }
1.1       nicm      197:                if (m->x < md->px ||
                    198:                    m->x > md->px + 4 + menu->width ||
                    199:                    m->y < md->py + 1 ||
                    200:                    m->y > md->py + 1 + count - 1) {
                    201:                        if (MOUSE_RELEASE(m->b))
                    202:                                return (1);
                    203:                        if (md->choice != -1) {
                    204:                                md->choice = -1;
                    205:                                c->flags |= CLIENT_REDRAWOVERLAY;
                    206:                        }
                    207:                        return (0);
                    208:                }
                    209:                if (MOUSE_RELEASE(m->b))
                    210:                        goto chosen;
1.7       nicm      211:                md->choice = m->y - (md->py + 1);
1.1       nicm      212:                if (md->choice != old)
                    213:                        c->flags |= CLIENT_REDRAWOVERLAY;
                    214:                return (0);
                    215:        }
1.11      nicm      216:        for (i = 0; i < (u_int)count; i++) {
                    217:                name = menu->items[i].name;
                    218:                if (name == NULL || *name == '-')
                    219:                        continue;
                    220:                if (event->key == menu->items[i].key) {
                    221:                        md->choice = i;
                    222:                        goto chosen;
                    223:                }
                    224:        }
1.1       nicm      225:        switch (event->key) {
                    226:        case KEYC_UP:
1.11      nicm      227:        case 'k':
1.9       nicm      228:                if (old == -1)
                    229:                        old = 0;
1.1       nicm      230:                do {
                    231:                        if (md->choice == -1 || md->choice == 0)
                    232:                                md->choice = count - 1;
                    233:                        else
                    234:                                md->choice--;
1.9       nicm      235:                        name = menu->items[md->choice].name;
                    236:                } while ((name == NULL || *name == '-') && md->choice != old);
1.1       nicm      237:                c->flags |= CLIENT_REDRAWOVERLAY;
                    238:                return (0);
                    239:        case KEYC_DOWN:
1.11      nicm      240:        case 'j':
1.9       nicm      241:                if (old == -1)
                    242:                        old = 0;
1.1       nicm      243:                do {
                    244:                        if (md->choice == -1 || md->choice == count - 1)
                    245:                                md->choice = 0;
1.9       nicm      246:                        else
                    247:                                md->choice++;
                    248:                        name = menu->items[md->choice].name;
                    249:                } while ((name == NULL || *name == '-') && md->choice != old);
1.1       nicm      250:                c->flags |= CLIENT_REDRAWOVERLAY;
                    251:                return (0);
                    252:        case '\r':
                    253:                goto chosen;
                    254:        case '\033': /* Escape */
                    255:        case '\003': /* C-c */
                    256:        case '\007': /* C-g */
                    257:        case 'q':
                    258:                return (1);
                    259:        }
                    260:        return (0);
                    261:
                    262: chosen:
                    263:        if (md->choice == -1)
                    264:                return (1);
                    265:        item = &menu->items[md->choice];
1.9       nicm      266:        if (item->name == NULL || *item->name == '-')
1.1       nicm      267:                return (1);
                    268:        if (md->cb != NULL) {
                    269:            md->cb(md->menu, md->choice, item->key, md->data);
1.3       nicm      270:            md->cb = NULL;
1.1       nicm      271:            return (1);
                    272:        }
1.5       nicm      273:
                    274:        pr = cmd_parse_from_string(item->command, NULL);
                    275:        switch (pr->status) {
                    276:        case CMD_PARSE_EMPTY:
                    277:                break;
                    278:        case CMD_PARSE_ERROR:
                    279:                new_item = cmdq_get_error(pr->error);
                    280:                free(pr->error);
                    281:                cmdq_append(c, new_item);
                    282:                break;
                    283:        case CMD_PARSE_SUCCESS:
1.6       nicm      284:                if (md->item != NULL)
1.19    ! nicm      285:                        event = cmdq_get_event(md->item);
1.6       nicm      286:                else
1.19    ! nicm      287:                        event = NULL;
        !           288:                new_item = cmdq_get_command(pr->cmdlist, &md->fs, event, 0);
1.5       nicm      289:                cmd_list_free(pr->cmdlist);
                    290:                cmdq_append(c, new_item);
                    291:                break;
1.1       nicm      292:        }
                    293:        return (1);
                    294: }
                    295:
                    296: int
                    297: menu_display(struct menu *menu, int flags, struct cmdq_item *item, u_int px,
                    298:     u_int py, struct client *c, struct cmd_find_state *fs, menu_choice_cb cb,
                    299:     void *data)
                    300: {
                    301:        struct menu_data        *md;
                    302:
                    303:        if (c->tty.sx < menu->width + 4 || c->tty.sy < menu->count + 2)
                    304:                return (-1);
1.14      nicm      305:        if (px + menu->width + 4 > c->tty.sx)
                    306:                px = c->tty.sx - menu->width - 4;
                    307:        if (py + menu->count + 2 > c->tty.sy)
                    308:                py = c->tty.sy - menu->count - 2;
1.1       nicm      309:
                    310:        md = xcalloc(1, sizeof *md);
                    311:        md->item = item;
                    312:        md->flags = flags;
                    313:
1.3       nicm      314:        if (fs != NULL)
                    315:                cmd_find_copy_state(&md->fs, fs);
1.1       nicm      316:        screen_init(&md->s, menu->width + 4, menu->count + 2, 0);
                    317:
                    318:        md->px = px;
                    319:        md->py = py;
                    320:
                    321:        md->menu = menu;
                    322:        md->choice = -1;
                    323:
                    324:        md->cb = cb;
                    325:        md->data = data;
                    326:
1.15      nicm      327:        server_client_set_overlay(c, 0, NULL, menu_mode_cb, menu_draw_cb,
                    328:            menu_key_cb, menu_free_cb, md);
1.1       nicm      329:        return (0);
                    330: }