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

Annotation of src/usr.bin/tmux/popup.c, Revision 1.6

1.6     ! nicm        1: /* $OpenBSD: popup.c,v 1.5 2020/03/31 06:35:38 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2020 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: #include <sys/wait.h>
                     21:
                     22: #include <signal.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
                     25:
                     26: #include "tmux.h"
                     27:
                     28: struct popup_data {
                     29:        struct client            *c;
                     30:        struct cmdq_item         *item;
                     31:        int                       flags;
                     32:
                     33:        char                    **lines;
                     34:        u_int                     nlines;
                     35:
                     36:        char                     *cmd;
                     37:        struct cmd_find_state     fs;
                     38:        struct screen             s;
                     39:
                     40:        struct job               *job;
                     41:        struct input_ctx         *ictx;
                     42:        int                       status;
                     43:
                     44:        u_int                     px;
                     45:        u_int                     py;
                     46:        u_int                     sx;
                     47:        u_int                     sy;
                     48:
                     49:        enum { OFF, MOVE, SIZE }  dragging;
                     50:        u_int                     dx;
                     51:        u_int                     dy;
                     52:
                     53:        u_int                     lx;
                     54:        u_int                     ly;
                     55:        u_int                     lb;
                     56: };
                     57:
                     58: static void
                     59: popup_write_screen(struct client *c, struct popup_data *pd)
                     60: {
                     61:        struct cmdq_item        *item = pd->item;
                     62:        struct screen_write_ctx  ctx;
                     63:        char                    *copy, *next, *loop, *tmp;
                     64:        struct format_tree      *ft;
                     65:        u_int                    i, y;
                     66:
                     67:        ft = format_create(item->client, item, FORMAT_NONE, 0);
                     68:        if (cmd_find_valid_state(&pd->fs))
                     69:                format_defaults(ft, c, pd->fs.s, pd->fs.wl, pd->fs.wp);
                     70:        else
                     71:                format_defaults(ft, c, NULL, NULL, NULL);
                     72:
                     73:        screen_write_start(&ctx, NULL, &pd->s);
                     74:        screen_write_clearscreen(&ctx, 8);
                     75:
                     76:        y = 0;
                     77:        for (i = 0; i < pd->nlines; i++) {
                     78:                if (y == pd->sy - 2)
                     79:                        break;
                     80:                copy = next = xstrdup(pd->lines[i]);
                     81:                while ((loop = strsep(&next, "\n")) != NULL) {
                     82:                        if (y == pd->sy - 2)
                     83:                                break;
                     84:                        tmp = format_expand(ft, loop);
                     85:                        screen_write_cursormove(&ctx, 0, y, 0);
                     86:                        format_draw(&ctx, &grid_default_cell, pd->sx - 2, tmp,
                     87:                            NULL);
                     88:                        free(tmp);
                     89:                        y++;
                     90:                }
                     91:                free(copy);
                     92:        }
                     93:
                     94:        format_free(ft);
                     95:        screen_write_cursormove(&ctx, 0, y, 0);
                     96:        screen_write_stop(&ctx);
                     97: }
                     98:
                     99: static int
                    100: popup_mode_cb(struct client *c, u_int *cx, u_int *cy)
                    101: {
                    102:        struct popup_data       *pd = c->overlay_data;
                    103:
                    104:        if (pd->ictx == NULL)
                    105:                return (0);
                    106:        *cx = pd->px + 1 + pd->s.cx;
                    107:        *cy = pd->py + 1 + pd->s.cy;
                    108:        return (pd->s.mode);
                    109: }
                    110:
                    111: static int
                    112: popup_check_cb(struct client *c, u_int px, u_int py)
                    113: {
                    114:        struct popup_data       *pd = c->overlay_data;
                    115:
                    116:        if (px < pd->px || px > pd->px + pd->sx - 1)
                    117:                return (1);
                    118:        if (py < pd->py || py > pd->py + pd->sy - 1)
                    119:                return (1);
                    120:        return (0);
                    121: }
                    122:
                    123: static void
                    124: popup_draw_cb(struct client *c, __unused struct screen_redraw_ctx *ctx0)
                    125: {
                    126:        struct popup_data       *pd = c->overlay_data;
                    127:        struct tty              *tty = &c->tty;
                    128:        struct screen            s;
                    129:        struct screen_write_ctx  ctx;
                    130:        u_int                    i, px = pd->px, py = pd->py;
                    131:
                    132:        screen_init(&s, pd->sx, pd->sy, 0);
                    133:        screen_write_start(&ctx, NULL, &s);
                    134:        screen_write_clearscreen(&ctx, 8);
                    135:        screen_write_box(&ctx, pd->sx, pd->sy);
                    136:        screen_write_cursormove(&ctx, 1, 1, 0);
                    137:        screen_write_fast_copy(&ctx, &pd->s, 0, 0, pd->sx - 2, pd->sy - 2);
                    138:        screen_write_stop(&ctx);
                    139:
                    140:        c->overlay_check = NULL;
                    141:        for (i = 0; i < pd->sy; i++)
                    142:                tty_draw_line(tty, NULL, &s, 0, i, pd->sx, px, py + i);
                    143:        c->overlay_check = popup_check_cb;
                    144: }
                    145:
                    146: static void
                    147: popup_free_cb(struct client *c)
                    148: {
                    149:        struct popup_data       *pd = c->overlay_data;
                    150:        struct cmdq_item        *item = pd->item;
                    151:        u_int                    i;
                    152:
                    153:        if (item != NULL) {
                    154:                if (pd->ictx != NULL &&
                    155:                    item->client != NULL &&
                    156:                    item->client->session == NULL)
                    157:                        item->client->retval = pd->status;
                    158:                cmdq_continue(item);
                    159:        }
                    160:        server_client_unref(pd->c);
                    161:
                    162:        if (pd->job != NULL)
                    163:                job_free(pd->job);
                    164:        if (pd->ictx != NULL)
                    165:                input_free(pd->ictx);
                    166:
                    167:        for (i = 0; i < pd->nlines; i++)
                    168:                free(pd->lines[i]);
                    169:        free(pd->lines);
                    170:
                    171:        screen_free(&pd->s);
                    172:        free(pd->cmd);
                    173:        free(pd);
                    174: }
                    175:
                    176: static void
                    177: popup_handle_drag(struct client *c, struct popup_data *pd,
                    178:     struct mouse_event *m)
                    179: {
                    180:        u_int   px, py;
                    181:
                    182:        if (!MOUSE_DRAG(m->b))
                    183:                pd->dragging = OFF;
                    184:        else if (pd->dragging == MOVE) {
                    185:                if (m->x < pd->dx)
                    186:                        px = 0;
                    187:                else if (m->x - pd->dx + pd->sx > c->tty.sx)
                    188:                        px = c->tty.sx - pd->sx;
                    189:                else
                    190:                        px = m->x - pd->dx;
                    191:                if (m->y < pd->dy)
                    192:                        py = 0;
                    193:                else if (m->y - pd->dy + pd->sy > c->tty.sy)
                    194:                        py = c->tty.sy - pd->sy;
                    195:                else
                    196:                        py = m->y - pd->dy;
                    197:                pd->px = px;
                    198:                pd->py = py;
                    199:                pd->dx = m->x - pd->px;
                    200:                pd->dy = m->y - pd->py;
                    201:                server_redraw_client(c);
                    202:        } else if (pd->dragging == SIZE) {
                    203:                if (m->x < pd->px + 2)
                    204:                        return;
                    205:                if (m->y < pd->py + 2)
                    206:                        return;
                    207:                pd->sx = m->x - pd->px;
                    208:                pd->sy = m->y - pd->py;
                    209:
                    210:                screen_resize(&pd->s, pd->sx, pd->sy, 0);
                    211:                if (pd->ictx == NULL)
                    212:                        popup_write_screen(c, pd);
                    213:                else if (pd->job != NULL)
                    214:                        job_resize(pd->job, pd->sx - 2, pd->sy - 2);
                    215:                server_redraw_client(c);
                    216:        }
                    217: }
                    218:
                    219: static int
                    220: popup_key_cb(struct client *c, struct key_event *event)
                    221: {
                    222:        struct popup_data       *pd = c->overlay_data;
                    223:        struct mouse_event      *m = &event->m;
                    224:        struct cmd_find_state   *fs = &pd->fs;
                    225:        struct cmdq_item        *new_item;
                    226:        struct cmd_parse_result *pr;
                    227:        struct format_tree      *ft;
1.6     ! nicm      228:        const char              *cmd, *buf;
        !           229:        size_t                   len;
1.1       nicm      230:
                    231:        if (KEYC_IS_MOUSE(event->key)) {
                    232:                if (pd->dragging != OFF) {
                    233:                        popup_handle_drag(c, pd, m);
                    234:                        goto out;
                    235:                }
                    236:                if (m->x < pd->px ||
                    237:                    m->x > pd->px + pd->sx - 1 ||
                    238:                    m->y < pd->py ||
                    239:                    m->y > pd->py + pd->sy - 1) {
                    240:                        if (MOUSE_BUTTONS (m->b) == 1)
                    241:                                return (1);
                    242:                        return (0);
                    243:                }
                    244:                if ((m->b & MOUSE_MASK_META) ||
                    245:                    m->x == pd->px ||
                    246:                    m->x == pd->px + pd->sx - 1 ||
                    247:                    m->y == pd->py ||
                    248:                    m->y == pd->py + pd->sy - 1) {
                    249:                        if (!MOUSE_DRAG(m->b))
                    250:                                goto out;
                    251:                        if (MOUSE_BUTTONS(m->lb) == 0)
                    252:                                pd->dragging = MOVE;
                    253:                        else if (MOUSE_BUTTONS(m->lb) == 2)
                    254:                                pd->dragging = SIZE;
                    255:                        pd->dx = m->lx - pd->px;
                    256:                        pd->dy = m->ly - pd->py;
                    257:                        goto out;
                    258:                }
                    259:        }
                    260:
                    261:        if (pd->ictx != NULL && (pd->flags & POPUP_WRITEKEYS)) {
1.3       nicm      262:                if (((pd->flags & (POPUP_CLOSEEXIT|POPUP_CLOSEEXITZERO)) == 0 ||
                    263:                    pd->job == NULL) &&
1.1       nicm      264:                    (event->key == '\033' || event->key == '\003'))
                    265:                        return (1);
                    266:                if (pd->job == NULL)
                    267:                        return (0);
1.6     ! nicm      268:                if (KEYC_IS_MOUSE(event->key)) {
        !           269:                        /* Must be inside, checked already. */
        !           270:                        if (!input_key_get_mouse(&pd->s, m, m->x - pd->px,
        !           271:                            m->y - pd->py, &buf, &len))
        !           272:                                return (0);
        !           273:                        bufferevent_write(job_get_event(pd->job), buf, len);
        !           274:                        return (0);
        !           275:                }
1.1       nicm      276:                input_key(NULL, &pd->s, job_get_event(pd->job), event->key);
                    277:                return (0);
                    278:        }
                    279:
                    280:        if (pd->cmd == NULL)
                    281:                return (1);
                    282:
                    283:        ft = format_create(NULL, pd->item, FORMAT_NONE, 0);
                    284:        if (cmd_find_valid_state(fs))
                    285:                format_defaults(ft, c, fs->s, fs->wl, fs->wp);
                    286:        else
                    287:                format_defaults(ft, c, NULL, NULL, NULL);
                    288:        format_add(ft, "popup_key", "%s", key_string_lookup_key(event->key));
                    289:        if (KEYC_IS_MOUSE(event->key)) {
                    290:                format_add(ft, "popup_mouse", "1");
                    291:                format_add(ft, "popup_mouse_x", "%u", m->x - pd->px);
                    292:                format_add(ft, "popup_mouse_y", "%u", m->y - pd->py);
                    293:        }
                    294:        cmd = format_expand(ft, pd->cmd);
                    295:        format_free(ft);
                    296:
                    297:        pr = cmd_parse_from_string(cmd, NULL);
                    298:        switch (pr->status) {
                    299:        case CMD_PARSE_EMPTY:
                    300:                break;
                    301:        case CMD_PARSE_ERROR:
                    302:                new_item = cmdq_get_error(pr->error);
                    303:                free(pr->error);
                    304:                cmdq_append(c, new_item);
                    305:                break;
                    306:        case CMD_PARSE_SUCCESS:
                    307:                if (pd->item != NULL)
                    308:                        m = &pd->item->shared->mouse;
                    309:                else
                    310:                        m = NULL;
                    311:                new_item = cmdq_get_command(pr->cmdlist, fs, m, 0);
                    312:                cmd_list_free(pr->cmdlist);
                    313:                cmdq_append(c, new_item);
                    314:                break;
                    315:        }
                    316:        return (1);
                    317:
                    318: out:
                    319:        pd->lx = m->x;
                    320:        pd->ly = m->y;
                    321:        pd->lb = m->b;
                    322:        return (0);
                    323: }
                    324:
                    325: static void
                    326: popup_job_update_cb(struct job *job)
                    327: {
                    328:        struct popup_data       *pd = job_get_data(job);
                    329:        struct evbuffer         *evb = job_get_event(job)->input;
                    330:        struct screen           *s = &pd->s;
                    331:        void                    *data = EVBUFFER_DATA(evb);
                    332:        size_t                   size = EVBUFFER_LENGTH(evb);
                    333:
                    334:        if (size != 0) {
                    335:                input_parse_screen(pd->ictx, s, data, size);
                    336:                evbuffer_drain(evb, size);
                    337:                pd->c->flags |= CLIENT_REDRAWOVERLAY;
                    338:        }
                    339: }
                    340:
                    341: static void
                    342: popup_job_complete_cb(struct job *job)
                    343: {
                    344:        struct popup_data       *pd = job_get_data(job);
                    345:        int                      status;
                    346:
                    347:        status = job_get_status(pd->job);
                    348:        if (WIFEXITED(status))
                    349:                pd->status = WEXITSTATUS(status);
                    350:        else if (WIFSIGNALED(status))
                    351:                pd->status = WTERMSIG(status);
                    352:        else
                    353:                pd->status = 0;
                    354:        pd->job = NULL;
                    355:
1.4       nicm      356:        if ((pd->flags & POPUP_CLOSEEXIT) ||
                    357:            ((pd->flags & POPUP_CLOSEEXITZERO) && pd->status == 0))
1.1       nicm      358:                server_client_clear_overlay(pd->c);
                    359: }
                    360:
                    361: u_int
1.2       nicm      362: popup_height(u_int nlines, const char **lines)
                    363: {
                    364:        char    *copy, *next, *loop;
                    365:        u_int    i, height = 0;
                    366:
                    367:        for (i = 0; i < nlines; i++) {
                    368:                copy = next = xstrdup(lines[i]);
                    369:                while ((loop = strsep(&next, "\n")) != NULL)
                    370:                        height++;
                    371:                free(copy);
                    372:        }
                    373:
                    374:        return (height);
                    375: }
                    376:
                    377: u_int
1.1       nicm      378: popup_width(struct cmdq_item *item, u_int nlines, const char **lines,
                    379:     struct client *c, struct cmd_find_state *fs)
                    380: {
                    381:        char                    *copy, *next, *loop, *tmp;
                    382:        struct format_tree      *ft;
                    383:        u_int                    i, width = 0, tmpwidth;
                    384:
                    385:        ft = format_create(item->client, item, FORMAT_NONE, 0);
                    386:        if (fs != NULL && cmd_find_valid_state(fs))
                    387:                format_defaults(ft, c, fs->s, fs->wl, fs->wp);
                    388:        else
                    389:                format_defaults(ft, c, NULL, NULL, NULL);
                    390:
                    391:        for (i = 0; i < nlines; i++) {
                    392:                copy = next = xstrdup(lines[i]);
                    393:                while ((loop = strsep(&next, "\n")) != NULL) {
                    394:                        tmp = format_expand(ft, loop);
                    395:                        tmpwidth = format_width(tmp);
                    396:                        if (tmpwidth > width)
                    397:                                width = tmpwidth;
                    398:                        free(tmp);
                    399:                }
1.2       nicm      400:                free(copy);
1.1       nicm      401:        }
                    402:
                    403:        format_free(ft);
                    404:        return (width);
                    405: }
                    406:
                    407: int
                    408: popup_display(int flags, struct cmdq_item *item, u_int px, u_int py, u_int sx,
                    409:     u_int sy, u_int nlines, const char **lines, const char *shellcmd,
                    410:     const char *cmd, const char *cwd, struct client *c,
                    411:     struct cmd_find_state *fs)
                    412: {
                    413:        struct popup_data       *pd;
                    414:        u_int                    i;
                    415:        struct session          *s;
                    416:        int                      jobflags;
                    417:
                    418:        if (sx < 3 || sy < 3)
                    419:                return (-1);
                    420:        if (c->tty.sx < sx || c->tty.sy < sy)
                    421:                return (-1);
                    422:
                    423:        pd = xcalloc(1, sizeof *pd);
                    424:        pd->item = item;
                    425:        pd->flags = flags;
                    426:
                    427:        pd->c = c;
                    428:        pd->c->references++;
                    429:
                    430:        pd->status = 128 + SIGHUP;
                    431:
                    432:        if (fs != NULL)
                    433:                cmd_find_copy_state(&pd->fs, fs);
                    434:        screen_init(&pd->s, sx - 2, sy - 2, 0);
                    435:
                    436:        if (cmd != NULL)
                    437:                pd->cmd = xstrdup(cmd);
                    438:
                    439:        pd->px = px;
                    440:        pd->py = py;
                    441:        pd->sx = sx;
                    442:        pd->sy = sy;
                    443:
                    444:        pd->nlines = nlines;
                    445:        if (pd->nlines != 0)
                    446:                pd->lines = xreallocarray(NULL, pd->nlines, sizeof *pd->lines);
                    447:
                    448:        for (i = 0; i < pd->nlines; i++)
                    449:                pd->lines[i] = xstrdup(lines[i]);
                    450:        popup_write_screen(c, pd);
                    451:
                    452:        if (shellcmd != NULL) {
                    453:                if (fs != NULL)
                    454:                        s = fs->s;
                    455:                else
                    456:                        s = NULL;
                    457:                jobflags = JOB_NOWAIT|JOB_PTY;
                    458:                if (flags & POPUP_WRITEKEYS)
                    459:                    jobflags |= JOB_KEEPWRITE;
                    460:                pd->job = job_run(shellcmd, s, cwd, popup_job_update_cb,
                    461:                    popup_job_complete_cb, NULL, pd, jobflags, pd->sx - 2,
                    462:                    pd->sy - 2);
1.5       nicm      463:                pd->ictx = input_init(NULL, job_get_event(pd->job));
1.1       nicm      464:        }
                    465:
                    466:        server_client_set_overlay(c, 0, popup_check_cb, popup_mode_cb,
                    467:            popup_draw_cb, popup_key_cb, popup_free_cb, pd);
                    468:        return (0);
                    469: }