[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.3

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