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

1.13    ! nicm        1: /* $OpenBSD: popup.c,v 1.12 2020/04/13 15:55:51 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:
1.8       nicm       67:        ft = format_create(c, item, FORMAT_NONE, 0);
1.1       nicm       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 &&
1.8       nicm      155:                    cmdq_get_client(item) != NULL &&
                    156:                    cmdq_get_client(item)->session == NULL)
                    157:                        cmdq_get_client(item)->retval = pd->status;
1.1       nicm      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) {
1.7       nicm      203:                if (m->x < pd->px + 3)
1.1       nicm      204:                        return;
1.7       nicm      205:                if (m->y < pd->py + 3)
1.1       nicm      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 format_tree      *ft;
1.6       nicm      226:        const char              *cmd, *buf;
                    227:        size_t                   len;
1.13    ! nicm      228:        struct cmdq_state       *state;
        !           229:        enum cmd_parse_status    status;
        !           230:        char                    *error;
1.1       nicm      231:
                    232:        if (KEYC_IS_MOUSE(event->key)) {
                    233:                if (pd->dragging != OFF) {
                    234:                        popup_handle_drag(c, pd, m);
                    235:                        goto out;
                    236:                }
                    237:                if (m->x < pd->px ||
                    238:                    m->x > pd->px + pd->sx - 1 ||
                    239:                    m->y < pd->py ||
                    240:                    m->y > pd->py + pd->sy - 1) {
                    241:                        if (MOUSE_BUTTONS (m->b) == 1)
                    242:                                return (1);
                    243:                        return (0);
                    244:                }
                    245:                if ((m->b & MOUSE_MASK_META) ||
                    246:                    m->x == pd->px ||
                    247:                    m->x == pd->px + pd->sx - 1 ||
                    248:                    m->y == pd->py ||
                    249:                    m->y == pd->py + pd->sy - 1) {
                    250:                        if (!MOUSE_DRAG(m->b))
                    251:                                goto out;
                    252:                        if (MOUSE_BUTTONS(m->lb) == 0)
                    253:                                pd->dragging = MOVE;
                    254:                        else if (MOUSE_BUTTONS(m->lb) == 2)
                    255:                                pd->dragging = SIZE;
                    256:                        pd->dx = m->lx - pd->px;
                    257:                        pd->dy = m->ly - pd->py;
                    258:                        goto out;
                    259:                }
                    260:        }
                    261:
                    262:        if (pd->ictx != NULL && (pd->flags & POPUP_WRITEKEYS)) {
1.3       nicm      263:                if (((pd->flags & (POPUP_CLOSEEXIT|POPUP_CLOSEEXITZERO)) == 0 ||
                    264:                    pd->job == NULL) &&
1.1       nicm      265:                    (event->key == '\033' || event->key == '\003'))
                    266:                        return (1);
                    267:                if (pd->job == NULL)
                    268:                        return (0);
1.6       nicm      269:                if (KEYC_IS_MOUSE(event->key)) {
                    270:                        /* Must be inside, checked already. */
                    271:                        if (!input_key_get_mouse(&pd->s, m, m->x - pd->px,
                    272:                            m->y - pd->py, &buf, &len))
                    273:                                return (0);
                    274:                        bufferevent_write(job_get_event(pd->job), buf, len);
                    275:                        return (0);
                    276:                }
1.1       nicm      277:                input_key(NULL, &pd->s, job_get_event(pd->job), event->key);
                    278:                return (0);
                    279:        }
                    280:
                    281:        if (pd->cmd == NULL)
                    282:                return (1);
                    283:
                    284:        ft = format_create(NULL, pd->item, FORMAT_NONE, 0);
                    285:        if (cmd_find_valid_state(fs))
                    286:                format_defaults(ft, c, fs->s, fs->wl, fs->wp);
                    287:        else
                    288:                format_defaults(ft, c, NULL, NULL, NULL);
                    289:        format_add(ft, "popup_key", "%s", key_string_lookup_key(event->key));
                    290:        if (KEYC_IS_MOUSE(event->key)) {
                    291:                format_add(ft, "popup_mouse", "1");
                    292:                format_add(ft, "popup_mouse_x", "%u", m->x - pd->px);
                    293:                format_add(ft, "popup_mouse_y", "%u", m->y - pd->py);
                    294:        }
                    295:        cmd = format_expand(ft, pd->cmd);
                    296:        format_free(ft);
                    297:
1.13    ! nicm      298:        if (pd->item != NULL)
        !           299:                event = cmdq_get_event(pd->item);
        !           300:        else
        !           301:                event = NULL;
        !           302:        state = cmdq_new_state(&pd->fs, event, 0);
        !           303:
        !           304:        status = cmd_parse_and_append(cmd, NULL, c, state, &error);
        !           305:        if (status == CMD_PARSE_ERROR) {
        !           306:                cmdq_append(c, cmdq_get_error(error));
        !           307:                free(error);
1.1       nicm      308:        }
1.13    ! nicm      309:        cmdq_free_state(state);
        !           310:
1.1       nicm      311:        return (1);
                    312:
                    313: out:
                    314:        pd->lx = m->x;
                    315:        pd->ly = m->y;
                    316:        pd->lb = m->b;
                    317:        return (0);
                    318: }
                    319:
                    320: static void
                    321: popup_job_update_cb(struct job *job)
                    322: {
                    323:        struct popup_data       *pd = job_get_data(job);
                    324:        struct evbuffer         *evb = job_get_event(job)->input;
                    325:        struct screen           *s = &pd->s;
                    326:        void                    *data = EVBUFFER_DATA(evb);
                    327:        size_t                   size = EVBUFFER_LENGTH(evb);
                    328:
                    329:        if (size != 0) {
                    330:                input_parse_screen(pd->ictx, s, data, size);
                    331:                evbuffer_drain(evb, size);
                    332:                pd->c->flags |= CLIENT_REDRAWOVERLAY;
                    333:        }
                    334: }
                    335:
                    336: static void
                    337: popup_job_complete_cb(struct job *job)
                    338: {
                    339:        struct popup_data       *pd = job_get_data(job);
                    340:        int                      status;
                    341:
                    342:        status = job_get_status(pd->job);
                    343:        if (WIFEXITED(status))
                    344:                pd->status = WEXITSTATUS(status);
                    345:        else if (WIFSIGNALED(status))
                    346:                pd->status = WTERMSIG(status);
                    347:        else
                    348:                pd->status = 0;
                    349:        pd->job = NULL;
                    350:
1.4       nicm      351:        if ((pd->flags & POPUP_CLOSEEXIT) ||
                    352:            ((pd->flags & POPUP_CLOSEEXITZERO) && pd->status == 0))
1.1       nicm      353:                server_client_clear_overlay(pd->c);
                    354: }
                    355:
                    356: u_int
1.2       nicm      357: popup_height(u_int nlines, const char **lines)
                    358: {
                    359:        char    *copy, *next, *loop;
                    360:        u_int    i, height = 0;
                    361:
                    362:        for (i = 0; i < nlines; i++) {
                    363:                copy = next = xstrdup(lines[i]);
                    364:                while ((loop = strsep(&next, "\n")) != NULL)
                    365:                        height++;
                    366:                free(copy);
                    367:        }
                    368:
                    369:        return (height);
                    370: }
                    371:
                    372: u_int
1.1       nicm      373: popup_width(struct cmdq_item *item, u_int nlines, const char **lines,
                    374:     struct client *c, struct cmd_find_state *fs)
                    375: {
                    376:        char                    *copy, *next, *loop, *tmp;
                    377:        struct format_tree      *ft;
                    378:        u_int                    i, width = 0, tmpwidth;
                    379:
1.8       nicm      380:        ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
1.1       nicm      381:        if (fs != NULL && cmd_find_valid_state(fs))
                    382:                format_defaults(ft, c, fs->s, fs->wl, fs->wp);
                    383:        else
                    384:                format_defaults(ft, c, NULL, NULL, NULL);
                    385:
                    386:        for (i = 0; i < nlines; i++) {
                    387:                copy = next = xstrdup(lines[i]);
                    388:                while ((loop = strsep(&next, "\n")) != NULL) {
                    389:                        tmp = format_expand(ft, loop);
                    390:                        tmpwidth = format_width(tmp);
                    391:                        if (tmpwidth > width)
                    392:                                width = tmpwidth;
                    393:                        free(tmp);
                    394:                }
1.2       nicm      395:                free(copy);
1.1       nicm      396:        }
                    397:
                    398:        format_free(ft);
                    399:        return (width);
                    400: }
                    401:
                    402: int
                    403: popup_display(int flags, struct cmdq_item *item, u_int px, u_int py, u_int sx,
                    404:     u_int sy, u_int nlines, const char **lines, const char *shellcmd,
                    405:     const char *cmd, const char *cwd, struct client *c,
                    406:     struct cmd_find_state *fs)
                    407: {
                    408:        struct popup_data       *pd;
                    409:        u_int                    i;
                    410:        struct session          *s;
                    411:        int                      jobflags;
                    412:
                    413:        if (sx < 3 || sy < 3)
                    414:                return (-1);
                    415:        if (c->tty.sx < sx || c->tty.sy < sy)
                    416:                return (-1);
                    417:
                    418:        pd = xcalloc(1, sizeof *pd);
                    419:        pd->item = item;
                    420:        pd->flags = flags;
                    421:
                    422:        pd->c = c;
                    423:        pd->c->references++;
                    424:
                    425:        pd->status = 128 + SIGHUP;
                    426:
                    427:        if (fs != NULL)
                    428:                cmd_find_copy_state(&pd->fs, fs);
                    429:        screen_init(&pd->s, sx - 2, sy - 2, 0);
                    430:
                    431:        if (cmd != NULL)
                    432:                pd->cmd = xstrdup(cmd);
                    433:
                    434:        pd->px = px;
                    435:        pd->py = py;
                    436:        pd->sx = sx;
                    437:        pd->sy = sy;
                    438:
                    439:        pd->nlines = nlines;
                    440:        if (pd->nlines != 0)
                    441:                pd->lines = xreallocarray(NULL, pd->nlines, sizeof *pd->lines);
                    442:
                    443:        for (i = 0; i < pd->nlines; i++)
                    444:                pd->lines[i] = xstrdup(lines[i]);
                    445:        popup_write_screen(c, pd);
                    446:
                    447:        if (shellcmd != NULL) {
                    448:                if (fs != NULL)
                    449:                        s = fs->s;
                    450:                else
                    451:                        s = NULL;
                    452:                jobflags = JOB_NOWAIT|JOB_PTY;
                    453:                if (flags & POPUP_WRITEKEYS)
                    454:                    jobflags |= JOB_KEEPWRITE;
                    455:                pd->job = job_run(shellcmd, s, cwd, popup_job_update_cb,
                    456:                    popup_job_complete_cb, NULL, pd, jobflags, pd->sx - 2,
                    457:                    pd->sy - 2);
1.5       nicm      458:                pd->ictx = input_init(NULL, job_get_event(pd->job));
1.1       nicm      459:        }
                    460:
                    461:        server_client_set_overlay(c, 0, popup_check_cb, popup_mode_cb,
                    462:            popup_draw_cb, popup_key_cb, popup_free_cb, pd);
                    463:        return (0);
                    464: }