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

Annotation of src/usr.bin/tmux/server-client.c, Revision 1.400

1.400   ! nicm        1: /* $OpenBSD: server-client.c,v 1.399 2023/01/16 11:26:14 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.181     nicm        4:  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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>
1.92      nicm       20: #include <sys/ioctl.h>
1.162     nicm       21: #include <sys/uio.h>
1.1       nicm       22:
1.114     benno      23: #include <errno.h>
1.12      nicm       24: #include <event.h>
1.1       nicm       25: #include <fcntl.h>
1.162     nicm       26: #include <imsg.h>
1.98      nicm       27: #include <paths.h>
                     28: #include <stdlib.h>
1.1       nicm       29: #include <string.h>
1.4       nicm       30: #include <time.h>
1.1       nicm       31: #include <unistd.h>
1.400   ! nicm       32: #include <vis.h>
1.1       nicm       33:
                     34: #include "tmux.h"
                     35:
1.190     nicm       36: static void    server_client_free(int, short, void *);
1.344     nicm       37: static void    server_client_check_pane_resize(struct window_pane *);
1.345     nicm       38: static void    server_client_check_pane_buffer(struct window_pane *);
1.344     nicm       39: static void    server_client_check_window_resize(struct window *);
1.276     nicm       40: static key_code        server_client_check_mouse(struct client *, struct key_event *);
1.190     nicm       41: static void    server_client_repeat_timer(int, short, void *);
1.192     nicm       42: static void    server_client_click_timer(int, short, void *);
1.190     nicm       43: static void    server_client_check_exit(struct client *);
                     44: static void    server_client_check_redraw(struct client *);
1.366     nicm       45: static void    server_client_check_modes(struct client *);
1.190     nicm       46: static void    server_client_set_title(struct client *);
1.393     nicm       47: static void    server_client_set_path(struct client *);
1.190     nicm       48: static void    server_client_reset_state(struct client *);
1.398     nicm       49: static int     server_client_is_bracket_pasting(struct client *, key_code);
1.190     nicm       50: static int     server_client_assume_paste(struct session *);
1.373     nicm       51: static void    server_client_update_latest(struct client *);
1.190     nicm       52:
                     53: static void    server_client_dispatch(struct imsg *, void *);
                     54: static void    server_client_dispatch_command(struct client *, struct imsg *);
                     55: static void    server_client_dispatch_identify(struct client *, struct imsg *);
                     56: static void    server_client_dispatch_shell(struct client *);
1.233     nicm       57:
1.341     nicm       58: /* Compare client windows. */
                     59: static int
                     60: server_client_window_cmp(struct client_window *cw1,
                     61:     struct client_window *cw2)
                     62: {
                     63:        if (cw1->window < cw2->window)
                     64:                return (-1);
                     65:        if (cw1->window > cw2->window)
                     66:                return (1);
                     67:        return (0);
                     68: }
                     69: RB_GENERATE(client_windows, client_window, entry, server_client_window_cmp);
                     70:
1.233     nicm       71: /* Number of attached clients. */
                     72: u_int
                     73: server_client_how_many(void)
                     74: {
                     75:        struct client   *c;
                     76:        u_int            n;
                     77:
                     78:        n = 0;
                     79:        TAILQ_FOREACH(c, &clients, entry) {
1.352     nicm       80:                if (c->session != NULL && (~c->flags & CLIENT_UNATTACHEDFLAGS))
1.233     nicm       81:                        n++;
                     82:        }
                     83:        return (n);
                     84: }
1.140     nicm       85:
1.281     nicm       86: /* Overlay timer callback. */
1.214     nicm       87: static void
1.281     nicm       88: server_client_overlay_timer(__unused int fd, __unused short events, void *data)
1.214     nicm       89: {
1.281     nicm       90:        server_client_clear_overlay(data);
1.214     nicm       91: }
                     92:
1.281     nicm       93: /* Set an overlay on client. */
1.214     nicm       94: void
1.309     nicm       95: server_client_set_overlay(struct client *c, u_int delay,
                     96:     overlay_check_cb checkcb, overlay_mode_cb modecb,
                     97:     overlay_draw_cb drawcb, overlay_key_cb keycb, overlay_free_cb freecb,
1.376     nicm       98:     overlay_resize_cb resizecb, void *data)
1.214     nicm       99: {
                    100:        struct timeval  tv;
                    101:
1.282     nicm      102:        if (c->overlay_draw != NULL)
                    103:                server_client_clear_overlay(c);
                    104:
1.214     nicm      105:        tv.tv_sec = delay / 1000;
                    106:        tv.tv_usec = (delay % 1000) * 1000L;
                    107:
1.281     nicm      108:        if (event_initialized(&c->overlay_timer))
                    109:                evtimer_del(&c->overlay_timer);
                    110:        evtimer_set(&c->overlay_timer, server_client_overlay_timer, c);
1.242     nicm      111:        if (delay != 0)
1.281     nicm      112:                evtimer_add(&c->overlay_timer, &tv);
                    113:
1.309     nicm      114:        c->overlay_check = checkcb;
                    115:        c->overlay_mode = modecb;
1.281     nicm      116:        c->overlay_draw = drawcb;
                    117:        c->overlay_key = keycb;
                    118:        c->overlay_free = freecb;
1.376     nicm      119:        c->overlay_resize = resizecb;
1.281     nicm      120:        c->overlay_data = data;
1.214     nicm      121:
1.378     nicm      122:        if (c->overlay_check == NULL)
                    123:                c->tty.flags |= TTY_FREEZE;
1.309     nicm      124:        if (c->overlay_mode == NULL)
                    125:                c->tty.flags |= TTY_NOCURSOR;
1.214     nicm      126:        server_redraw_client(c);
                    127: }
                    128:
1.281     nicm      129: /* Clear overlay mode on client. */
1.309     nicm      130: void
1.281     nicm      131: server_client_clear_overlay(struct client *c)
1.214     nicm      132: {
1.281     nicm      133:        if (c->overlay_draw == NULL)
1.214     nicm      134:                return;
                    135:
1.281     nicm      136:        if (event_initialized(&c->overlay_timer))
                    137:                evtimer_del(&c->overlay_timer);
                    138:
                    139:        if (c->overlay_free != NULL)
1.380     nicm      140:                c->overlay_free(c, c->overlay_data);
1.281     nicm      141:
1.309     nicm      142:        c->overlay_check = NULL;
                    143:        c->overlay_mode = NULL;
1.281     nicm      144:        c->overlay_draw = NULL;
                    145:        c->overlay_key = NULL;
1.309     nicm      146:        c->overlay_free = NULL;
                    147:        c->overlay_data = NULL;
1.214     nicm      148:
                    149:        c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
                    150:        server_redraw_client(c);
1.388     nicm      151: }
                    152:
                    153: /*
                    154:  * Given overlay position and dimensions, return parts of the input range which
                    155:  * are visible.
                    156:  */
                    157: void
                    158: server_client_overlay_range(u_int x, u_int y, u_int sx, u_int sy, u_int px,
                    159:     u_int py, u_int nx, struct overlay_ranges *r)
                    160: {
                    161:        u_int   ox, onx;
                    162:
                    163:        /* Return up to 2 ranges. */
                    164:        r->px[2] = 0;
                    165:        r->nx[2] = 0;
                    166:
                    167:        /* Trivial case of no overlap in the y direction. */
                    168:        if (py < y || py > y + sy - 1) {
                    169:                r->px[0] = px;
                    170:                r->nx[0] = nx;
                    171:                r->px[1] = 0;
                    172:                r->nx[1] = 0;
                    173:                return;
                    174:        }
                    175:
                    176:        /* Visible bit to the left of the popup. */
                    177:        if (px < x) {
                    178:                r->px[0] = px;
                    179:                r->nx[0] = x - px;
                    180:                if (r->nx[0] > nx)
                    181:                        r->nx[0] = nx;
                    182:        } else {
                    183:                r->px[0] = 0;
                    184:                r->nx[0] = 0;
                    185:        }
                    186:
                    187:        /* Visible bit to the right of the popup. */
                    188:        ox = x + sx;
                    189:        if (px > ox)
                    190:                ox = px;
                    191:        onx = px + nx;
                    192:        if (onx > ox) {
                    193:                r->px[1] = ox;
                    194:                r->nx[1] = onx - ox;
                    195:        } else {
                    196:                r->px[1] = 0;
                    197:                r->nx[1] = 0;
                    198:        }
1.214     nicm      199: }
                    200:
1.140     nicm      201: /* Check if this client is inside this server. */
                    202: int
                    203: server_client_check_nested(struct client *c)
                    204: {
                    205:        struct environ_entry    *envent;
                    206:        struct window_pane      *wp;
                    207:
1.164     nicm      208:        envent = environ_find(c->environ, "TMUX");
1.140     nicm      209:        if (envent == NULL || *envent->value == '\0')
                    210:                return (0);
                    211:
                    212:        RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
1.216     nicm      213:                if (strcmp(wp->tty, c->ttyname) == 0)
1.140     nicm      214:                        return (1);
                    215:        }
                    216:        return (0);
                    217: }
1.1       nicm      218:
1.132     nicm      219: /* Set client key table. */
                    220: void
1.178     nicm      221: server_client_set_key_table(struct client *c, const char *name)
1.132     nicm      222: {
1.178     nicm      223:        if (name == NULL)
                    224:                name = server_client_get_key_table(c);
                    225:
1.132     nicm      226:        key_bindings_unref_table(c->keytable);
                    227:        c->keytable = key_bindings_get_table(name, 1);
                    228:        c->keytable->references++;
                    229: }
                    230:
1.178     nicm      231: /* Get default key table. */
                    232: const char *
                    233: server_client_get_key_table(struct client *c)
                    234: {
                    235:        struct session  *s = c->session;
                    236:        const char      *name;
                    237:
                    238:        if (s == NULL)
                    239:                return ("root");
                    240:
                    241:        name = options_get_string(s->options, "key-table");
                    242:        if (*name == '\0')
                    243:                return ("root");
                    244:        return (name);
                    245: }
                    246:
1.223     nicm      247: /* Is this table the default key table? */
                    248: static int
                    249: server_client_is_default_key_table(struct client *c, struct key_table *table)
1.191     nicm      250: {
1.223     nicm      251:        return (strcmp(table->name, server_client_get_key_table(c)) == 0);
1.191     nicm      252: }
                    253:
1.1       nicm      254: /* Create a new client. */
1.246     nicm      255: struct client *
1.1       nicm      256: server_client_create(int fd)
                    257: {
                    258:        struct client   *c;
                    259:
1.49      nicm      260:        setblocking(fd, 0);
1.1       nicm      261:
                    262:        c = xcalloc(1, sizeof *c);
1.141     nicm      263:        c->references = 1;
1.162     nicm      264:        c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c);
1.25      nicm      265:
1.10      nicm      266:        if (gettimeofday(&c->creation_time, NULL) != 0)
1.1       nicm      267:                fatal("gettimeofday failed");
1.11      nicm      268:        memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.111     nicm      269:
1.164     nicm      270:        c->environ = environ_create();
1.1       nicm      271:
1.146     nicm      272:        c->fd = -1;
1.351     nicm      273:        c->out_fd = -1;
1.145     nicm      274:
1.316     nicm      275:        c->queue = cmdq_new();
1.341     nicm      276:        RB_INIT(&c->windows);
1.345     nicm      277:        RB_INIT(&c->files);
1.94      nicm      278:
1.1       nicm      279:        c->tty.sx = 80;
                    280:        c->tty.sy = 24;
                    281:
1.271     nicm      282:        status_init(c);
1.93      nicm      283:        c->flags |= CLIENT_FOCUSED;
                    284:
1.132     nicm      285:        c->keytable = key_bindings_get_table("root", 1);
                    286:        c->keytable->references++;
                    287:
1.17      nicm      288:        evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
1.192     nicm      289:        evtimer_set(&c->click_timer, server_client_click_timer, c);
1.17      nicm      290:
1.135     nicm      291:        TAILQ_INSERT_TAIL(&clients, c, entry);
1.157     nicm      292:        log_debug("new client %p", c);
1.246     nicm      293:        return (c);
1.72      nicm      294: }
                    295:
                    296: /* Open client terminal if needed. */
                    297: int
1.119     nicm      298: server_client_open(struct client *c, char **cause)
1.72      nicm      299: {
1.339     nicm      300:        const char      *ttynam = _PATH_TTY;
                    301:
1.75      nicm      302:        if (c->flags & CLIENT_CONTROL)
                    303:                return (0);
1.120     nicm      304:
1.339     nicm      305:        if (strcmp(c->ttyname, ttynam) == 0||
                    306:            ((isatty(STDIN_FILENO) &&
                    307:            (ttynam = ttyname(STDIN_FILENO)) != NULL &&
                    308:            strcmp(c->ttyname, ttynam) == 0) ||
                    309:            (isatty(STDOUT_FILENO) &&
                    310:            (ttynam = ttyname(STDOUT_FILENO)) != NULL &&
                    311:            strcmp(c->ttyname, ttynam) == 0) ||
                    312:            (isatty(STDERR_FILENO) &&
                    313:            (ttynam = ttyname(STDERR_FILENO)) != NULL &&
                    314:            strcmp(c->ttyname, ttynam) == 0))) {
                    315:                xasprintf(cause, "can't use %s", c->ttyname);
1.120     nicm      316:                return (-1);
                    317:        }
1.75      nicm      318:
1.72      nicm      319:        if (!(c->flags & CLIENT_TERMINAL)) {
1.116     nicm      320:                *cause = xstrdup("not a terminal");
1.72      nicm      321:                return (-1);
                    322:        }
                    323:
1.119     nicm      324:        if (tty_open(&c->tty, cause) != 0)
1.72      nicm      325:                return (-1);
                    326:
                    327:        return (0);
1.1       nicm      328: }
                    329:
1.373     nicm      330: /* Lost an attached client. */
                    331: static void
                    332: server_client_attached_lost(struct client *c)
                    333: {
1.384     nicm      334:        struct session  *s;
1.373     nicm      335:        struct window   *w;
                    336:        struct client   *loop;
                    337:        struct client   *found;
                    338:
                    339:        log_debug("lost attached client %p", c);
                    340:
                    341:        /*
                    342:         * By this point the session in the client has been cleared so walk all
                    343:         * windows to find any with this client as the latest.
                    344:         */
                    345:        RB_FOREACH(w, windows, &windows) {
                    346:                if (w->latest != c)
                    347:                        continue;
                    348:
                    349:                found = NULL;
                    350:                TAILQ_FOREACH(loop, &clients, entry) {
                    351:                        s = loop->session;
                    352:                        if (loop == c || s == NULL || s->curw->window != w)
                    353:                                continue;
1.379     nicm      354:                        if (found == NULL || timercmp(&loop->activity_time,
                    355:                            &found->activity_time, >))
1.373     nicm      356:                                found = loop;
                    357:                }
                    358:                if (found != NULL)
                    359:                        server_client_update_latest(found);
                    360:        }
                    361: }
                    362:
1.379     nicm      363: /* Set client session. */
                    364: void
                    365: server_client_set_session(struct client *c, struct session *s)
                    366: {
                    367:        struct session  *old = c->session;
                    368:
                    369:        if (s != NULL && c->session != NULL && c->session != s)
                    370:                c->last_session = c->session;
                    371:        else if (s == NULL)
                    372:                c->last_session = NULL;
                    373:        c->session = s;
                    374:        c->flags |= CLIENT_FOCUSED;
                    375:
                    376:        if (old != NULL && old->curw != NULL)
                    377:                window_update_focus(old->curw->window);
                    378:        if (s != NULL) {
1.387     nicm      379:                recalculate_sizes();
1.379     nicm      380:                window_update_focus(s->curw->window);
                    381:                session_update_activity(s, NULL);
                    382:                gettimeofday(&s->last_attached_time, NULL);
                    383:                s->curw->flags &= ~WINLINK_ALERTFLAGS;
                    384:                s->curw->window->latest = c;
                    385:                alerts_check_session(s);
                    386:                tty_update_client_offset(c);
                    387:                status_timer_start(c);
                    388:                notify_client("client-session-changed", c);
                    389:                server_redraw_client(c);
                    390:        }
                    391:
                    392:        server_check_unattached();
                    393:        server_update_socket();
                    394: }
                    395:
1.1       nicm      396: /* Lost a client. */
                    397: void
                    398: server_client_lost(struct client *c)
                    399: {
1.340     nicm      400:        struct client_file      *cf, *cf1;
1.341     nicm      401:        struct client_window    *cw, *cw1;
1.1       nicm      402:
1.141     nicm      403:        c->flags |= CLIENT_DEAD;
                    404:
1.281     nicm      405:        server_client_clear_overlay(c);
1.141     nicm      406:        status_prompt_clear(c);
                    407:        status_message_clear(c);
                    408:
1.340     nicm      409:        RB_FOREACH_SAFE(cf, client_files, &c->files, cf1) {
1.300     nicm      410:                cf->error = EINTR;
                    411:                file_fire_done(cf);
                    412:        }
1.341     nicm      413:        RB_FOREACH_SAFE(cw, client_windows, &c->windows, cw1) {
                    414:                RB_REMOVE(client_windows, &c->windows, cw);
                    415:                free(cw);
                    416:        }
1.141     nicm      417:
1.135     nicm      418:        TAILQ_REMOVE(&clients, c, entry);
1.157     nicm      419:        log_debug("lost client %p", c);
1.1       nicm      420:
1.373     nicm      421:        if (c->flags & CLIENT_ATTACHED) {
                    422:                server_client_attached_lost(c);
1.371     nicm      423:                notify_client("client-detached", c);
1.373     nicm      424:        }
1.371     nicm      425:
1.349     nicm      426:        if (c->flags & CLIENT_CONTROL)
                    427:                control_stop(c);
1.1       nicm      428:        if (c->flags & CLIENT_TERMINAL)
                    429:                tty_free(&c->tty);
1.109     nicm      430:        free(c->ttyname);
1.392     nicm      431:        free(c->clipboard_panes);
1.333     nicm      432:
1.329     nicm      433:        free(c->term_name);
1.333     nicm      434:        free(c->term_type);
1.370     nicm      435:        tty_term_free_list(c->term_caps, c->term_ncaps);
1.1       nicm      436:
1.270     nicm      437:        status_free(c);
1.1       nicm      438:
1.77      nicm      439:        free(c->title);
1.165     nicm      440:        free((void *)c->cwd);
1.1       nicm      441:
1.17      nicm      442:        evtimer_del(&c->repeat_timer);
1.192     nicm      443:        evtimer_del(&c->click_timer);
1.17      nicm      444:
1.132     nicm      445:        key_bindings_unref_table(c->keytable);
                    446:
1.77      nicm      447:        free(c->message_string);
1.116     nicm      448:        if (event_initialized(&c->message_timer))
1.69      nicm      449:                evtimer_del(&c->message_timer);
1.1       nicm      450:
1.259     nicm      451:        free(c->prompt_saved);
1.77      nicm      452:        free(c->prompt_string);
                    453:        free(c->prompt_buffer);
1.61      nicm      454:
1.228     nicm      455:        format_lost_client(c);
1.164     nicm      456:        environ_free(c->environ);
1.1       nicm      457:
1.162     nicm      458:        proc_remove_peer(c->peer);
                    459:        c->peer = NULL;
1.13      nicm      460:
1.351     nicm      461:        if (c->out_fd != -1)
                    462:                close(c->out_fd);
1.348     nicm      463:        if (c->fd != -1) {
                    464:                close(c->fd);
                    465:                c->fd = -1;
                    466:        }
1.142     nicm      467:        server_client_unref(c);
1.71      nicm      468:
                    469:        server_add_accept(0); /* may be more file descriptors now */
1.1       nicm      470:
                    471:        recalculate_sizes();
1.41      nicm      472:        server_check_unattached();
1.19      nicm      473:        server_update_socket();
1.141     nicm      474: }
                    475:
                    476: /* Remove reference from a client. */
                    477: void
1.142     nicm      478: server_client_unref(struct client *c)
1.141     nicm      479: {
1.157     nicm      480:        log_debug("unref client %p (%d references)", c, c->references);
1.141     nicm      481:
                    482:        c->references--;
                    483:        if (c->references == 0)
                    484:                event_once(-1, EV_TIMEOUT, server_client_free, c, NULL);
                    485: }
                    486:
                    487: /* Free dead client. */
1.190     nicm      488: static void
1.170     nicm      489: server_client_free(__unused int fd, __unused short events, void *arg)
1.141     nicm      490: {
                    491:        struct client   *c = arg;
                    492:
1.157     nicm      493:        log_debug("free client %p (%d references)", c, c->references);
1.141     nicm      494:
1.316     nicm      495:        cmdq_free(c->queue);
1.194     nicm      496:
1.216     nicm      497:        if (c->references == 0) {
                    498:                free((void *)c->name);
1.141     nicm      499:                free(c);
1.216     nicm      500:        }
1.9       nicm      501: }
                    502:
1.220     nicm      503: /* Suspend a client. */
                    504: void
                    505: server_client_suspend(struct client *c)
                    506: {
1.232     nicm      507:        struct session  *s = c->session;
1.220     nicm      508:
1.352     nicm      509:        if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1.220     nicm      510:                return;
                    511:
                    512:        tty_stop_tty(&c->tty);
                    513:        c->flags |= CLIENT_SUSPENDED;
                    514:        proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0);
                    515: }
                    516:
1.174     nicm      517: /* Detach a client. */
                    518: void
                    519: server_client_detach(struct client *c, enum msgtype msgtype)
                    520: {
1.232     nicm      521:        struct session  *s = c->session;
1.174     nicm      522:
1.389     nicm      523:        if (s == NULL || (c->flags & CLIENT_NODETACHFLAGS))
1.174     nicm      524:                return;
                    525:
1.352     nicm      526:        c->flags |= CLIENT_EXIT;
                    527:
                    528:        c->exit_type = CLIENT_EXIT_DETACH;
                    529:        c->exit_msgtype = msgtype;
                    530:        c->exit_session = xstrdup(s->name);
1.207     nicm      531: }
                    532:
1.208     nicm      533: /* Execute command to replace a client. */
1.207     nicm      534: void
                    535: server_client_exec(struct client *c, const char *cmd)
                    536: {
                    537:        struct session  *s = c->session;
1.208     nicm      538:        char            *msg;
                    539:        const char      *shell;
1.207     nicm      540:        size_t           cmdsize, shellsize;
                    541:
                    542:        if (*cmd == '\0')
                    543:                return;
                    544:        cmdsize = strlen(cmd) + 1;
                    545:
                    546:        if (s != NULL)
                    547:                shell = options_get_string(s->options, "default-shell");
                    548:        else
                    549:                shell = options_get_string(global_s_options, "default-shell");
1.308     nicm      550:        if (!checkshell(shell))
                    551:                shell = _PATH_BSHELL;
1.207     nicm      552:        shellsize = strlen(shell) + 1;
                    553:
                    554:        msg = xmalloc(cmdsize + shellsize);
                    555:        memcpy(msg, cmd, cmdsize);
                    556:        memcpy(msg + cmdsize, shell, shellsize);
                    557:
                    558:        proc_send(c->peer, MSG_EXEC, -1, msg, cmdsize + shellsize);
                    559:        free(msg);
1.174     nicm      560: }
                    561:
1.66      nicm      562: /* Check for mouse keys. */
1.190     nicm      563: static key_code
1.276     nicm      564: server_client_check_mouse(struct client *c, struct key_event *event)
1.66      nicm      565: {
1.276     nicm      566:        struct mouse_event      *m = &event->m;
1.192     nicm      567:        struct session          *s = c->session;
1.272     nicm      568:        struct winlink          *wl;
1.192     nicm      569:        struct window_pane      *wp;
1.261     nicm      570:        u_int                    x, y, b, sx, sy, px, py;
1.307     nicm      571:        int                      ignore = 0;
1.192     nicm      572:        key_code                 key;
                    573:        struct timeval           tv;
1.272     nicm      574:        struct style_range      *sr;
1.283     nicm      575:        enum { NOTYPE,
                    576:               MOVE,
                    577:               DOWN,
                    578:               UP,
                    579:               DRAG,
                    580:               WHEEL,
1.311     nicm      581:               SECOND,
1.283     nicm      582:               DOUBLE,
                    583:               TRIPLE } type = NOTYPE;
1.276     nicm      584:        enum { NOWHERE,
                    585:               PANE,
                    586:               STATUS,
                    587:               STATUS_LEFT,
                    588:               STATUS_RIGHT,
                    589:               STATUS_DEFAULT,
1.283     nicm      590:               BORDER } where = NOWHERE;
1.131     nicm      591:
1.261     nicm      592:        log_debug("%s mouse %02x at %u,%u (last %u,%u) (%d)", c->name, m->b,
                    593:            m->x, m->y, m->lx, m->ly, c->tty.mouse_drag_flag);
1.131     nicm      594:
                    595:        /* What type of event is this? */
1.306     nicm      596:        if (event->key == KEYC_DOUBLECLICK) {
                    597:                type = DOUBLE;
                    598:                x = m->x, y = m->y, b = m->b;
1.307     nicm      599:                ignore = 1;
1.306     nicm      600:                log_debug("double-click at %u,%u", x, y);
                    601:        } else if ((m->sgr_type != ' ' &&
1.209     nicm      602:            MOUSE_DRAG(m->sgr_b) &&
1.391     nicm      603:            MOUSE_RELEASE(m->sgr_b)) ||
1.209     nicm      604:            (m->sgr_type == ' ' &&
                    605:            MOUSE_DRAG(m->b) &&
1.391     nicm      606:            MOUSE_RELEASE(m->b) &&
                    607:            MOUSE_RELEASE(m->lb))) {
1.209     nicm      608:                type = MOVE;
                    609:                x = m->x, y = m->y, b = 0;
                    610:                log_debug("move at %u,%u", x, y);
                    611:        } else if (MOUSE_DRAG(m->b)) {
1.131     nicm      612:                type = DRAG;
                    613:                if (c->tty.mouse_drag_flag) {
                    614:                        x = m->x, y = m->y, b = m->b;
1.278     nicm      615:                        if (x == m->lx && y == m->ly)
                    616:                                return (KEYC_UNKNOWN);
1.131     nicm      617:                        log_debug("drag update at %u,%u", x, y);
                    618:                } else {
1.277     nicm      619:                        x = m->lx, y = m->ly, b = m->lb;
1.131     nicm      620:                        log_debug("drag start at %u,%u", x, y);
                    621:                }
                    622:        } else if (MOUSE_WHEEL(m->b)) {
                    623:                type = WHEEL;
                    624:                x = m->x, y = m->y, b = m->b;
                    625:                log_debug("wheel at %u,%u", x, y);
1.200     nicm      626:        } else if (MOUSE_RELEASE(m->b)) {
1.131     nicm      627:                type = UP;
                    628:                x = m->x, y = m->y, b = m->lb;
                    629:                log_debug("up at %u,%u", x, y);
                    630:        } else {
1.192     nicm      631:                if (c->flags & CLIENT_DOUBLECLICK) {
                    632:                        evtimer_del(&c->click_timer);
                    633:                        c->flags &= ~CLIENT_DOUBLECLICK;
                    634:                        if (m->b == c->click_button) {
1.311     nicm      635:                                type = SECOND;
                    636:                                x = m->x, y = m->y, b = m->b;
                    637:                                log_debug("second-click at %u,%u", x, y);
1.306     nicm      638:                                c->flags |= CLIENT_TRIPLECLICK;
1.192     nicm      639:                        }
                    640:                } else if (c->flags & CLIENT_TRIPLECLICK) {
                    641:                        evtimer_del(&c->click_timer);
                    642:                        c->flags &= ~CLIENT_TRIPLECLICK;
                    643:                        if (m->b == c->click_button) {
                    644:                                type = TRIPLE;
                    645:                                x = m->x, y = m->y, b = m->b;
                    646:                                log_debug("triple-click at %u,%u", x, y);
                    647:                                goto have_event;
                    648:                        }
1.311     nicm      649:                } else {
                    650:                        type = DOWN;
                    651:                        x = m->x, y = m->y, b = m->b;
                    652:                        log_debug("down at %u,%u", x, y);
1.307     nicm      653:                        c->flags |= CLIENT_DOUBLECLICK;
1.311     nicm      654:                }
1.192     nicm      655:
                    656:                if (KEYC_CLICK_TIMEOUT != 0) {
1.306     nicm      657:                        memcpy(&c->click_event, m, sizeof c->click_event);
1.192     nicm      658:                        c->click_button = m->b;
                    659:
1.312     nicm      660:                        log_debug("click timer started");
1.192     nicm      661:                        tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000;
                    662:                        tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L;
                    663:                        evtimer_del(&c->click_timer);
                    664:                        evtimer_add(&c->click_timer, &tv);
                    665:                }
1.131     nicm      666:        }
1.192     nicm      667:
                    668: have_event:
1.131     nicm      669:        if (type == NOTYPE)
1.177     nicm      670:                return (KEYC_UNKNOWN);
1.131     nicm      671:
1.258     nicm      672:        /* Save the session. */
1.131     nicm      673:        m->s = s->id;
1.258     nicm      674:        m->w = -1;
1.307     nicm      675:        m->ignore = ignore;
1.131     nicm      676:
                    677:        /* Is this on the status line? */
                    678:        m->statusat = status_at_line(c);
1.292     nicm      679:        m->statuslines = status_line_size(c);
1.272     nicm      680:        if (m->statusat != -1 &&
                    681:            y >= (u_int)m->statusat &&
1.292     nicm      682:            y < m->statusat + m->statuslines) {
1.272     nicm      683:                sr = status_get_range(c, x, y - m->statusat);
1.274     nicm      684:                if (sr == NULL) {
                    685:                        where = STATUS_DEFAULT;
                    686:                } else {
                    687:                        switch (sr->type) {
                    688:                        case STYLE_RANGE_NONE:
1.273     nicm      689:                                return (KEYC_UNKNOWN);
1.274     nicm      690:                        case STYLE_RANGE_LEFT:
                    691:                                where = STATUS_LEFT;
                    692:                                break;
                    693:                        case STYLE_RANGE_RIGHT:
                    694:                                where = STATUS_RIGHT;
                    695:                                break;
                    696:                        case STYLE_RANGE_WINDOW:
1.298     nicm      697:                                wl = winlink_find_by_index(&s->windows,
                    698:                                    sr->argument);
1.274     nicm      699:                                if (wl == NULL)
                    700:                                        return (KEYC_UNKNOWN);
                    701:                                m->w = wl->window->id;
1.273     nicm      702:
1.274     nicm      703:                                where = STATUS;
                    704:                                break;
                    705:                        }
1.258     nicm      706:                }
                    707:        }
1.131     nicm      708:
                    709:        /* Not on status line. Adjust position and check for border or pane. */
                    710:        if (where == NOWHERE) {
1.261     nicm      711:                px = x;
1.292     nicm      712:                if (m->statusat == 0 && y >= m->statuslines)
                    713:                        py = y - m->statuslines;
1.131     nicm      714:                else if (m->statusat > 0 && y >= (u_int)m->statusat)
1.261     nicm      715:                        py = m->statusat - 1;
                    716:                else
                    717:                        py = y;
                    718:
                    719:                tty_window_offset(&c->tty, &m->ox, &m->oy, &sx, &sy);
                    720:                log_debug("mouse window @%u at %u,%u (%ux%u)",
                    721:                    s->curw->window->id, m->ox, m->oy, sx, sy);
                    722:                if (px > sx || py > sy)
                    723:                        return (KEYC_UNKNOWN);
                    724:                px = px + m->ox;
                    725:                py = py + m->oy;
1.131     nicm      726:
1.260     nicm      727:                /* Try the pane borders if not zoomed. */
                    728:                if (~s->curw->window->flags & WINDOW_ZOOMED) {
                    729:                        TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
1.261     nicm      730:                                if ((wp->xoff + wp->sx == px &&
                    731:                                    wp->yoff <= 1 + py &&
                    732:                                    wp->yoff + wp->sy >= py) ||
                    733:                                    (wp->yoff + wp->sy == py &&
                    734:                                    wp->xoff <= 1 + px &&
                    735:                                    wp->xoff + wp->sx >= px))
1.260     nicm      736:                                        break;
                    737:                        }
                    738:                        if (wp != NULL)
                    739:                                where = BORDER;
1.131     nicm      740:                }
1.260     nicm      741:
                    742:                /* Otherwise try inside the pane. */
                    743:                if (where == NOWHERE) {
1.261     nicm      744:                        wp = window_get_active_at(s->curw->window, px, py);
1.260     nicm      745:                        if (wp != NULL)
1.131     nicm      746:                                where = PANE;
1.315     nicm      747:                        else
                    748:                                return (KEYC_UNKNOWN);
1.131     nicm      749:                }
1.260     nicm      750:                if (where == PANE)
                    751:                        log_debug("mouse %u,%u on pane %%%u", x, y, wp->id);
                    752:                else if (where == BORDER)
                    753:                        log_debug("mouse on pane %%%u border", wp->id);
1.131     nicm      754:                m->wp = wp->id;
                    755:                m->w = wp->window->id;
                    756:        } else
                    757:                m->wp = -1;
                    758:
                    759:        /* Stop dragging if needed. */
1.391     nicm      760:        if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag != 0) {
1.131     nicm      761:                if (c->tty.mouse_drag_release != NULL)
                    762:                        c->tty.mouse_drag_release(c, m);
                    763:
                    764:                c->tty.mouse_drag_update = NULL;
                    765:                c->tty.mouse_drag_release = NULL;
                    766:
1.182     nicm      767:                /*
1.183     nicm      768:                 * End a mouse drag by passing a MouseDragEnd key corresponding
                    769:                 * to the button that started the drag.
1.182     nicm      770:                 */
1.391     nicm      771:                switch (c->tty.mouse_drag_flag - 1) {
                    772:                case MOUSE_BUTTON_1:
1.182     nicm      773:                        if (where == PANE)
1.183     nicm      774:                                key = KEYC_MOUSEDRAGEND1_PANE;
1.182     nicm      775:                        if (where == STATUS)
1.183     nicm      776:                                key = KEYC_MOUSEDRAGEND1_STATUS;
1.258     nicm      777:                        if (where == STATUS_LEFT)
                    778:                                key = KEYC_MOUSEDRAGEND1_STATUS_LEFT;
                    779:                        if (where == STATUS_RIGHT)
                    780:                                key = KEYC_MOUSEDRAGEND1_STATUS_RIGHT;
1.274     nicm      781:                        if (where == STATUS_DEFAULT)
                    782:                                key = KEYC_MOUSEDRAGEND1_STATUS_DEFAULT;
1.182     nicm      783:                        if (where == BORDER)
1.183     nicm      784:                                key = KEYC_MOUSEDRAGEND1_BORDER;
1.182     nicm      785:                        break;
1.391     nicm      786:                case MOUSE_BUTTON_2:
1.182     nicm      787:                        if (where == PANE)
1.183     nicm      788:                                key = KEYC_MOUSEDRAGEND2_PANE;
1.182     nicm      789:                        if (where == STATUS)
1.183     nicm      790:                                key = KEYC_MOUSEDRAGEND2_STATUS;
1.258     nicm      791:                        if (where == STATUS_LEFT)
                    792:                                key = KEYC_MOUSEDRAGEND2_STATUS_LEFT;
                    793:                        if (where == STATUS_RIGHT)
                    794:                                key = KEYC_MOUSEDRAGEND2_STATUS_RIGHT;
1.274     nicm      795:                        if (where == STATUS_DEFAULT)
                    796:                                key = KEYC_MOUSEDRAGEND2_STATUS_DEFAULT;
1.182     nicm      797:                        if (where == BORDER)
1.183     nicm      798:                                key = KEYC_MOUSEDRAGEND2_BORDER;
1.182     nicm      799:                        break;
1.391     nicm      800:                case MOUSE_BUTTON_3:
1.182     nicm      801:                        if (where == PANE)
1.183     nicm      802:                                key = KEYC_MOUSEDRAGEND3_PANE;
1.182     nicm      803:                        if (where == STATUS)
1.183     nicm      804:                                key = KEYC_MOUSEDRAGEND3_STATUS;
1.258     nicm      805:                        if (where == STATUS_LEFT)
                    806:                                key = KEYC_MOUSEDRAGEND3_STATUS_LEFT;
                    807:                        if (where == STATUS_RIGHT)
                    808:                                key = KEYC_MOUSEDRAGEND3_STATUS_RIGHT;
1.274     nicm      809:                        if (where == STATUS_DEFAULT)
                    810:                                key = KEYC_MOUSEDRAGEND3_STATUS_DEFAULT;
1.182     nicm      811:                        if (where == BORDER)
1.183     nicm      812:                                key = KEYC_MOUSEDRAGEND3_BORDER;
1.182     nicm      813:                        break;
1.391     nicm      814:                case MOUSE_BUTTON_6:
                    815:                        if (where == PANE)
                    816:                                key = KEYC_MOUSEDRAGEND6_PANE;
                    817:                        if (where == STATUS)
                    818:                                key = KEYC_MOUSEDRAGEND6_STATUS;
                    819:                        if (where == STATUS_LEFT)
                    820:                                key = KEYC_MOUSEDRAGEND6_STATUS_LEFT;
                    821:                        if (where == STATUS_RIGHT)
                    822:                                key = KEYC_MOUSEDRAGEND6_STATUS_RIGHT;
                    823:                        if (where == STATUS_DEFAULT)
                    824:                                key = KEYC_MOUSEDRAGEND6_STATUS_DEFAULT;
                    825:                        if (where == BORDER)
                    826:                                key = KEYC_MOUSEDRAGEND6_BORDER;
                    827:                        break;
                    828:                case MOUSE_BUTTON_7:
                    829:                        if (where == PANE)
                    830:                                key = KEYC_MOUSEDRAGEND7_PANE;
                    831:                        if (where == STATUS)
                    832:                                key = KEYC_MOUSEDRAGEND7_STATUS;
                    833:                        if (where == STATUS_LEFT)
                    834:                                key = KEYC_MOUSEDRAGEND7_STATUS_LEFT;
                    835:                        if (where == STATUS_RIGHT)
                    836:                                key = KEYC_MOUSEDRAGEND7_STATUS_RIGHT;
                    837:                        if (where == STATUS_DEFAULT)
                    838:                                key = KEYC_MOUSEDRAGEND7_STATUS_DEFAULT;
                    839:                        if (where == BORDER)
                    840:                                key = KEYC_MOUSEDRAGEND7_BORDER;
                    841:                        break;
                    842:                case MOUSE_BUTTON_8:
                    843:                        if (where == PANE)
                    844:                                key = KEYC_MOUSEDRAGEND8_PANE;
                    845:                        if (where == STATUS)
                    846:                                key = KEYC_MOUSEDRAGEND8_STATUS;
                    847:                        if (where == STATUS_LEFT)
                    848:                                key = KEYC_MOUSEDRAGEND8_STATUS_LEFT;
                    849:                        if (where == STATUS_RIGHT)
                    850:                                key = KEYC_MOUSEDRAGEND8_STATUS_RIGHT;
                    851:                        if (where == STATUS_DEFAULT)
                    852:                                key = KEYC_MOUSEDRAGEND8_STATUS_DEFAULT;
                    853:                        if (where == BORDER)
                    854:                                key = KEYC_MOUSEDRAGEND8_BORDER;
                    855:                        break;
                    856:                case MOUSE_BUTTON_9:
                    857:                        if (where == PANE)
                    858:                                key = KEYC_MOUSEDRAGEND9_PANE;
                    859:                        if (where == STATUS)
                    860:                                key = KEYC_MOUSEDRAGEND9_STATUS;
                    861:                        if (where == STATUS_LEFT)
                    862:                                key = KEYC_MOUSEDRAGEND9_STATUS_LEFT;
                    863:                        if (where == STATUS_RIGHT)
                    864:                                key = KEYC_MOUSEDRAGEND9_STATUS_RIGHT;
                    865:                        if (where == STATUS_DEFAULT)
                    866:                                key = KEYC_MOUSEDRAGEND9_STATUS_DEFAULT;
                    867:                        if (where == BORDER)
                    868:                                key = KEYC_MOUSEDRAGEND9_BORDER;
                    869:                        break;
                    870:                case MOUSE_BUTTON_10:
                    871:                        if (where == PANE)
                    872:                                key = KEYC_MOUSEDRAGEND10_PANE;
                    873:                        if (where == STATUS)
                    874:                                key = KEYC_MOUSEDRAGEND10_STATUS;
                    875:                        if (where == STATUS_LEFT)
                    876:                                key = KEYC_MOUSEDRAGEND10_STATUS_LEFT;
                    877:                        if (where == STATUS_RIGHT)
                    878:                                key = KEYC_MOUSEDRAGEND10_STATUS_RIGHT;
                    879:                        if (where == STATUS_DEFAULT)
                    880:                                key = KEYC_MOUSEDRAGEND10_STATUS_DEFAULT;
                    881:                        if (where == BORDER)
                    882:                                key = KEYC_MOUSEDRAGEND10_BORDER;
                    883:                        break;
                    884:                case MOUSE_BUTTON_11:
                    885:                        if (where == PANE)
                    886:                                key = KEYC_MOUSEDRAGEND11_PANE;
                    887:                        if (where == STATUS)
                    888:                                key = KEYC_MOUSEDRAGEND11_STATUS;
                    889:                        if (where == STATUS_LEFT)
                    890:                                key = KEYC_MOUSEDRAGEND11_STATUS_LEFT;
                    891:                        if (where == STATUS_RIGHT)
                    892:                                key = KEYC_MOUSEDRAGEND11_STATUS_RIGHT;
                    893:                        if (where == STATUS_DEFAULT)
                    894:                                key = KEYC_MOUSEDRAGEND11_STATUS_DEFAULT;
                    895:                        if (where == BORDER)
                    896:                                key = KEYC_MOUSEDRAGEND11_BORDER;
                    897:                        break;
1.182     nicm      898:                default:
                    899:                        key = KEYC_MOUSE;
                    900:                        break;
                    901:                }
1.131     nicm      902:                c->tty.mouse_drag_flag = 0;
1.305     nicm      903:                goto out;
1.131     nicm      904:        }
                    905:
                    906:        /* Convert to a key binding. */
1.177     nicm      907:        key = KEYC_UNKNOWN;
1.131     nicm      908:        switch (type) {
                    909:        case NOTYPE:
                    910:                break;
1.209     nicm      911:        case MOVE:
                    912:                if (where == PANE)
                    913:                        key = KEYC_MOUSEMOVE_PANE;
                    914:                if (where == STATUS)
                    915:                        key = KEYC_MOUSEMOVE_STATUS;
1.274     nicm      916:                if (where == STATUS_LEFT)
                    917:                        key = KEYC_MOUSEMOVE_STATUS_LEFT;
                    918:                if (where == STATUS_RIGHT)
                    919:                        key = KEYC_MOUSEMOVE_STATUS_RIGHT;
                    920:                if (where == STATUS_DEFAULT)
                    921:                        key = KEYC_MOUSEMOVE_STATUS_DEFAULT;
1.209     nicm      922:                if (where == BORDER)
                    923:                        key = KEYC_MOUSEMOVE_BORDER;
                    924:                break;
1.131     nicm      925:        case DRAG:
1.204     nicm      926:                if (c->tty.mouse_drag_update != NULL)
                    927:                        key = KEYC_DRAGGING;
                    928:                else {
1.131     nicm      929:                        switch (MOUSE_BUTTONS(b)) {
1.391     nicm      930:                        case MOUSE_BUTTON_1:
1.131     nicm      931:                                if (where == PANE)
                    932:                                        key = KEYC_MOUSEDRAG1_PANE;
                    933:                                if (where == STATUS)
                    934:                                        key = KEYC_MOUSEDRAG1_STATUS;
1.258     nicm      935:                                if (where == STATUS_LEFT)
                    936:                                        key = KEYC_MOUSEDRAG1_STATUS_LEFT;
                    937:                                if (where == STATUS_RIGHT)
                    938:                                        key = KEYC_MOUSEDRAG1_STATUS_RIGHT;
1.274     nicm      939:                                if (where == STATUS_DEFAULT)
                    940:                                        key = KEYC_MOUSEDRAG1_STATUS_DEFAULT;
1.131     nicm      941:                                if (where == BORDER)
                    942:                                        key = KEYC_MOUSEDRAG1_BORDER;
                    943:                                break;
1.391     nicm      944:                        case MOUSE_BUTTON_2:
1.131     nicm      945:                                if (where == PANE)
                    946:                                        key = KEYC_MOUSEDRAG2_PANE;
                    947:                                if (where == STATUS)
                    948:                                        key = KEYC_MOUSEDRAG2_STATUS;
1.258     nicm      949:                                if (where == STATUS_LEFT)
                    950:                                        key = KEYC_MOUSEDRAG2_STATUS_LEFT;
                    951:                                if (where == STATUS_RIGHT)
                    952:                                        key = KEYC_MOUSEDRAG2_STATUS_RIGHT;
1.274     nicm      953:                                if (where == STATUS_DEFAULT)
                    954:                                        key = KEYC_MOUSEDRAG2_STATUS_DEFAULT;
1.131     nicm      955:                                if (where == BORDER)
                    956:                                        key = KEYC_MOUSEDRAG2_BORDER;
                    957:                                break;
1.391     nicm      958:                        case MOUSE_BUTTON_3:
1.131     nicm      959:                                if (where == PANE)
                    960:                                        key = KEYC_MOUSEDRAG3_PANE;
                    961:                                if (where == STATUS)
                    962:                                        key = KEYC_MOUSEDRAG3_STATUS;
1.258     nicm      963:                                if (where == STATUS_LEFT)
                    964:                                        key = KEYC_MOUSEDRAG3_STATUS_LEFT;
                    965:                                if (where == STATUS_RIGHT)
                    966:                                        key = KEYC_MOUSEDRAG3_STATUS_RIGHT;
1.274     nicm      967:                                if (where == STATUS_DEFAULT)
                    968:                                        key = KEYC_MOUSEDRAG3_STATUS_DEFAULT;
1.131     nicm      969:                                if (where == BORDER)
                    970:                                        key = KEYC_MOUSEDRAG3_BORDER;
                    971:                                break;
1.391     nicm      972:                        case MOUSE_BUTTON_6:
                    973:                                if (where == PANE)
                    974:                                        key = KEYC_MOUSEDRAG6_PANE;
                    975:                                if (where == STATUS)
                    976:                                        key = KEYC_MOUSEDRAG6_STATUS;
                    977:                                if (where == STATUS_LEFT)
                    978:                                        key = KEYC_MOUSEDRAG6_STATUS_LEFT;
                    979:                                if (where == STATUS_RIGHT)
                    980:                                        key = KEYC_MOUSEDRAG6_STATUS_RIGHT;
                    981:                                if (where == STATUS_DEFAULT)
                    982:                                        key = KEYC_MOUSEDRAG6_STATUS_DEFAULT;
                    983:                                if (where == BORDER)
                    984:                                        key = KEYC_MOUSEDRAG6_BORDER;
                    985:                                break;
                    986:                        case MOUSE_BUTTON_7:
                    987:                                if (where == PANE)
                    988:                                        key = KEYC_MOUSEDRAG7_PANE;
                    989:                                if (where == STATUS)
                    990:                                        key = KEYC_MOUSEDRAG7_STATUS;
                    991:                                if (where == STATUS_LEFT)
                    992:                                        key = KEYC_MOUSEDRAG7_STATUS_LEFT;
                    993:                                if (where == STATUS_RIGHT)
                    994:                                        key = KEYC_MOUSEDRAG7_STATUS_RIGHT;
                    995:                                if (where == STATUS_DEFAULT)
                    996:                                        key = KEYC_MOUSEDRAG7_STATUS_DEFAULT;
                    997:                                if (where == BORDER)
                    998:                                        key = KEYC_MOUSEDRAG7_BORDER;
                    999:                                break;
                   1000:                        case MOUSE_BUTTON_8:
                   1001:                                if (where == PANE)
                   1002:                                        key = KEYC_MOUSEDRAG8_PANE;
                   1003:                                if (where == STATUS)
                   1004:                                        key = KEYC_MOUSEDRAG8_STATUS;
                   1005:                                if (where == STATUS_LEFT)
                   1006:                                        key = KEYC_MOUSEDRAG8_STATUS_LEFT;
                   1007:                                if (where == STATUS_RIGHT)
                   1008:                                        key = KEYC_MOUSEDRAG8_STATUS_RIGHT;
                   1009:                                if (where == STATUS_DEFAULT)
                   1010:                                        key = KEYC_MOUSEDRAG8_STATUS_DEFAULT;
                   1011:                                if (where == BORDER)
                   1012:                                        key = KEYC_MOUSEDRAG8_BORDER;
                   1013:                                break;
                   1014:                        case MOUSE_BUTTON_9:
                   1015:                                if (where == PANE)
                   1016:                                        key = KEYC_MOUSEDRAG9_PANE;
                   1017:                                if (where == STATUS)
                   1018:                                        key = KEYC_MOUSEDRAG9_STATUS;
                   1019:                                if (where == STATUS_LEFT)
                   1020:                                        key = KEYC_MOUSEDRAG9_STATUS_LEFT;
                   1021:                                if (where == STATUS_RIGHT)
                   1022:                                        key = KEYC_MOUSEDRAG9_STATUS_RIGHT;
                   1023:                                if (where == STATUS_DEFAULT)
                   1024:                                        key = KEYC_MOUSEDRAG9_STATUS_DEFAULT;
                   1025:                                if (where == BORDER)
                   1026:                                        key = KEYC_MOUSEDRAG9_BORDER;
                   1027:                                break;
                   1028:                        case MOUSE_BUTTON_10:
                   1029:                                if (where == PANE)
                   1030:                                        key = KEYC_MOUSEDRAG10_PANE;
                   1031:                                if (where == STATUS)
                   1032:                                        key = KEYC_MOUSEDRAG10_STATUS;
                   1033:                                if (where == STATUS_LEFT)
                   1034:                                        key = KEYC_MOUSEDRAG10_STATUS_LEFT;
                   1035:                                if (where == STATUS_RIGHT)
                   1036:                                        key = KEYC_MOUSEDRAG10_STATUS_RIGHT;
                   1037:                                if (where == STATUS_DEFAULT)
                   1038:                                        key = KEYC_MOUSEDRAG10_STATUS_DEFAULT;
                   1039:                                if (where == BORDER)
                   1040:                                        key = KEYC_MOUSEDRAG10_BORDER;
                   1041:                                break;
                   1042:                        case MOUSE_BUTTON_11:
                   1043:                                if (where == PANE)
                   1044:                                        key = KEYC_MOUSEDRAG11_PANE;
                   1045:                                if (where == STATUS)
                   1046:                                        key = KEYC_MOUSEDRAG11_STATUS;
                   1047:                                if (where == STATUS_LEFT)
                   1048:                                        key = KEYC_MOUSEDRAG11_STATUS_LEFT;
                   1049:                                if (where == STATUS_RIGHT)
                   1050:                                        key = KEYC_MOUSEDRAG11_STATUS_RIGHT;
                   1051:                                if (where == STATUS_DEFAULT)
                   1052:                                        key = KEYC_MOUSEDRAG11_STATUS_DEFAULT;
                   1053:                                if (where == BORDER)
                   1054:                                        key = KEYC_MOUSEDRAG11_BORDER;
                   1055:                                break;
1.131     nicm     1056:                        }
                   1057:                }
1.66      nicm     1058:
1.182     nicm     1059:                /*
                   1060:                 * Begin a drag by setting the flag to a non-zero value that
                   1061:                 * corresponds to the mouse button in use.
                   1062:                 */
                   1063:                c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1;
1.131     nicm     1064:                break;
                   1065:        case WHEEL:
                   1066:                if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
                   1067:                        if (where == PANE)
                   1068:                                key = KEYC_WHEELUP_PANE;
                   1069:                        if (where == STATUS)
                   1070:                                key = KEYC_WHEELUP_STATUS;
1.258     nicm     1071:                        if (where == STATUS_LEFT)
                   1072:                                key = KEYC_WHEELUP_STATUS_LEFT;
                   1073:                        if (where == STATUS_RIGHT)
                   1074:                                key = KEYC_WHEELUP_STATUS_RIGHT;
1.274     nicm     1075:                        if (where == STATUS_DEFAULT)
                   1076:                                key = KEYC_WHEELUP_STATUS_DEFAULT;
1.131     nicm     1077:                        if (where == BORDER)
                   1078:                                key = KEYC_WHEELUP_BORDER;
                   1079:                } else {
                   1080:                        if (where == PANE)
                   1081:                                key = KEYC_WHEELDOWN_PANE;
                   1082:                        if (where == STATUS)
                   1083:                                key = KEYC_WHEELDOWN_STATUS;
1.274     nicm     1084:                        if (where == STATUS_LEFT)
                   1085:                                key = KEYC_WHEELDOWN_STATUS_LEFT;
                   1086:                        if (where == STATUS_RIGHT)
                   1087:                                key = KEYC_WHEELDOWN_STATUS_RIGHT;
                   1088:                        if (where == STATUS_DEFAULT)
                   1089:                                key = KEYC_WHEELDOWN_STATUS_DEFAULT;
1.131     nicm     1090:                        if (where == BORDER)
                   1091:                                key = KEYC_WHEELDOWN_BORDER;
                   1092:                }
                   1093:                break;
                   1094:        case UP:
                   1095:                switch (MOUSE_BUTTONS(b)) {
1.391     nicm     1096:                case MOUSE_BUTTON_1:
1.131     nicm     1097:                        if (where == PANE)
                   1098:                                key = KEYC_MOUSEUP1_PANE;
                   1099:                        if (where == STATUS)
                   1100:                                key = KEYC_MOUSEUP1_STATUS;
1.258     nicm     1101:                        if (where == STATUS_LEFT)
                   1102:                                key = KEYC_MOUSEUP1_STATUS_LEFT;
                   1103:                        if (where == STATUS_RIGHT)
                   1104:                                key = KEYC_MOUSEUP1_STATUS_RIGHT;
1.274     nicm     1105:                        if (where == STATUS_DEFAULT)
                   1106:                                key = KEYC_MOUSEUP1_STATUS_DEFAULT;
1.131     nicm     1107:                        if (where == BORDER)
                   1108:                                key = KEYC_MOUSEUP1_BORDER;
                   1109:                        break;
1.391     nicm     1110:                case MOUSE_BUTTON_2:
1.131     nicm     1111:                        if (where == PANE)
                   1112:                                key = KEYC_MOUSEUP2_PANE;
                   1113:                        if (where == STATUS)
                   1114:                                key = KEYC_MOUSEUP2_STATUS;
1.258     nicm     1115:                        if (where == STATUS_LEFT)
                   1116:                                key = KEYC_MOUSEUP2_STATUS_LEFT;
                   1117:                        if (where == STATUS_RIGHT)
                   1118:                                key = KEYC_MOUSEUP2_STATUS_RIGHT;
1.274     nicm     1119:                        if (where == STATUS_DEFAULT)
                   1120:                                key = KEYC_MOUSEUP2_STATUS_DEFAULT;
1.131     nicm     1121:                        if (where == BORDER)
                   1122:                                key = KEYC_MOUSEUP2_BORDER;
                   1123:                        break;
1.391     nicm     1124:                case MOUSE_BUTTON_3:
1.131     nicm     1125:                        if (where == PANE)
                   1126:                                key = KEYC_MOUSEUP3_PANE;
                   1127:                        if (where == STATUS)
                   1128:                                key = KEYC_MOUSEUP3_STATUS;
1.258     nicm     1129:                        if (where == STATUS_LEFT)
                   1130:                                key = KEYC_MOUSEUP3_STATUS_LEFT;
                   1131:                        if (where == STATUS_RIGHT)
                   1132:                                key = KEYC_MOUSEUP3_STATUS_RIGHT;
1.274     nicm     1133:                        if (where == STATUS_DEFAULT)
                   1134:                                key = KEYC_MOUSEUP3_STATUS_DEFAULT;
1.131     nicm     1135:                        if (where == BORDER)
                   1136:                                key = KEYC_MOUSEUP3_BORDER;
                   1137:                        break;
1.391     nicm     1138:                case MOUSE_BUTTON_6:
                   1139:                        if (where == PANE)
                   1140:                                key = KEYC_MOUSEUP6_PANE;
                   1141:                        if (where == STATUS)
                   1142:                                key = KEYC_MOUSEUP6_STATUS;
                   1143:                        if (where == STATUS_LEFT)
                   1144:                                key = KEYC_MOUSEUP6_STATUS_LEFT;
                   1145:                        if (where == STATUS_RIGHT)
                   1146:                                key = KEYC_MOUSEUP6_STATUS_RIGHT;
                   1147:                        if (where == STATUS_DEFAULT)
                   1148:                                key = KEYC_MOUSEUP6_STATUS_DEFAULT;
                   1149:                        if (where == BORDER)
                   1150:                                key = KEYC_MOUSEUP6_BORDER;
                   1151:                        break;
                   1152:                case MOUSE_BUTTON_7:
                   1153:                        if (where == PANE)
                   1154:                                key = KEYC_MOUSEUP7_PANE;
                   1155:                        if (where == STATUS)
                   1156:                                key = KEYC_MOUSEUP7_STATUS;
                   1157:                        if (where == STATUS_LEFT)
                   1158:                                key = KEYC_MOUSEUP7_STATUS_LEFT;
                   1159:                        if (where == STATUS_RIGHT)
                   1160:                                key = KEYC_MOUSEUP7_STATUS_RIGHT;
                   1161:                        if (where == STATUS_DEFAULT)
                   1162:                                key = KEYC_MOUSEUP7_STATUS_DEFAULT;
                   1163:                        if (where == BORDER)
                   1164:                                key = KEYC_MOUSEUP7_BORDER;
                   1165:                        break;
                   1166:                case MOUSE_BUTTON_8:
                   1167:                        if (where == PANE)
                   1168:                                key = KEYC_MOUSEUP8_PANE;
                   1169:                        if (where == STATUS)
                   1170:                                key = KEYC_MOUSEUP8_STATUS;
                   1171:                        if (where == STATUS_LEFT)
                   1172:                                key = KEYC_MOUSEUP8_STATUS_LEFT;
                   1173:                        if (where == STATUS_RIGHT)
                   1174:                                key = KEYC_MOUSEUP8_STATUS_RIGHT;
                   1175:                        if (where == STATUS_DEFAULT)
                   1176:                                key = KEYC_MOUSEUP8_STATUS_DEFAULT;
                   1177:                        if (where == BORDER)
                   1178:                                key = KEYC_MOUSEUP8_BORDER;
                   1179:                        break;
                   1180:                case MOUSE_BUTTON_9:
                   1181:                        if (where == PANE)
                   1182:                                key = KEYC_MOUSEUP9_PANE;
                   1183:                        if (where == STATUS)
                   1184:                                key = KEYC_MOUSEUP9_STATUS;
                   1185:                        if (where == STATUS_LEFT)
                   1186:                                key = KEYC_MOUSEUP9_STATUS_LEFT;
                   1187:                        if (where == STATUS_RIGHT)
                   1188:                                key = KEYC_MOUSEUP9_STATUS_RIGHT;
                   1189:                        if (where == STATUS_DEFAULT)
                   1190:                                key = KEYC_MOUSEUP9_STATUS_DEFAULT;
                   1191:                        if (where == BORDER)
                   1192:                                key = KEYC_MOUSEUP9_BORDER;
                   1193:                        break;
                   1194:                case MOUSE_BUTTON_10:
                   1195:                        if (where == PANE)
                   1196:                                key = KEYC_MOUSEUP1_PANE;
                   1197:                        if (where == STATUS)
                   1198:                                key = KEYC_MOUSEUP1_STATUS;
                   1199:                        if (where == STATUS_LEFT)
                   1200:                                key = KEYC_MOUSEUP1_STATUS_LEFT;
                   1201:                        if (where == STATUS_RIGHT)
                   1202:                                key = KEYC_MOUSEUP1_STATUS_RIGHT;
                   1203:                        if (where == STATUS_DEFAULT)
                   1204:                                key = KEYC_MOUSEUP1_STATUS_DEFAULT;
                   1205:                        if (where == BORDER)
                   1206:                                key = KEYC_MOUSEUP1_BORDER;
                   1207:                        break;
                   1208:                case MOUSE_BUTTON_11:
                   1209:                        if (where == PANE)
                   1210:                                key = KEYC_MOUSEUP11_PANE;
                   1211:                        if (where == STATUS)
                   1212:                                key = KEYC_MOUSEUP11_STATUS;
                   1213:                        if (where == STATUS_LEFT)
                   1214:                                key = KEYC_MOUSEUP11_STATUS_LEFT;
                   1215:                        if (where == STATUS_RIGHT)
                   1216:                                key = KEYC_MOUSEUP11_STATUS_RIGHT;
                   1217:                        if (where == STATUS_DEFAULT)
                   1218:                                key = KEYC_MOUSEUP11_STATUS_DEFAULT;
                   1219:                        if (where == BORDER)
                   1220:                                key = KEYC_MOUSEUP11_BORDER;
                   1221:                        break;
1.131     nicm     1222:                }
                   1223:                break;
                   1224:        case DOWN:
                   1225:                switch (MOUSE_BUTTONS(b)) {
1.391     nicm     1226:                case MOUSE_BUTTON_1:
1.131     nicm     1227:                        if (where == PANE)
                   1228:                                key = KEYC_MOUSEDOWN1_PANE;
                   1229:                        if (where == STATUS)
                   1230:                                key = KEYC_MOUSEDOWN1_STATUS;
1.258     nicm     1231:                        if (where == STATUS_LEFT)
                   1232:                                key = KEYC_MOUSEDOWN1_STATUS_LEFT;
                   1233:                        if (where == STATUS_RIGHT)
                   1234:                                key = KEYC_MOUSEDOWN1_STATUS_RIGHT;
1.274     nicm     1235:                        if (where == STATUS_DEFAULT)
                   1236:                                key = KEYC_MOUSEDOWN1_STATUS_DEFAULT;
1.131     nicm     1237:                        if (where == BORDER)
                   1238:                                key = KEYC_MOUSEDOWN1_BORDER;
                   1239:                        break;
1.391     nicm     1240:                case MOUSE_BUTTON_2:
1.131     nicm     1241:                        if (where == PANE)
                   1242:                                key = KEYC_MOUSEDOWN2_PANE;
                   1243:                        if (where == STATUS)
                   1244:                                key = KEYC_MOUSEDOWN2_STATUS;
1.258     nicm     1245:                        if (where == STATUS_LEFT)
                   1246:                                key = KEYC_MOUSEDOWN2_STATUS_LEFT;
                   1247:                        if (where == STATUS_RIGHT)
                   1248:                                key = KEYC_MOUSEDOWN2_STATUS_RIGHT;
1.274     nicm     1249:                        if (where == STATUS_DEFAULT)
                   1250:                                key = KEYC_MOUSEDOWN2_STATUS_DEFAULT;
1.131     nicm     1251:                        if (where == BORDER)
                   1252:                                key = KEYC_MOUSEDOWN2_BORDER;
                   1253:                        break;
1.391     nicm     1254:                case MOUSE_BUTTON_3:
1.131     nicm     1255:                        if (where == PANE)
                   1256:                                key = KEYC_MOUSEDOWN3_PANE;
                   1257:                        if (where == STATUS)
                   1258:                                key = KEYC_MOUSEDOWN3_STATUS;
1.258     nicm     1259:                        if (where == STATUS_LEFT)
                   1260:                                key = KEYC_MOUSEDOWN3_STATUS_LEFT;
                   1261:                        if (where == STATUS_RIGHT)
                   1262:                                key = KEYC_MOUSEDOWN3_STATUS_RIGHT;
1.274     nicm     1263:                        if (where == STATUS_DEFAULT)
                   1264:                                key = KEYC_MOUSEDOWN3_STATUS_DEFAULT;
1.131     nicm     1265:                        if (where == BORDER)
                   1266:                                key = KEYC_MOUSEDOWN3_BORDER;
1.311     nicm     1267:                        break;
1.391     nicm     1268:                case MOUSE_BUTTON_6:
                   1269:                        if (where == PANE)
                   1270:                                key = KEYC_MOUSEDOWN6_PANE;
                   1271:                        if (where == STATUS)
                   1272:                                key = KEYC_MOUSEDOWN6_STATUS;
                   1273:                        if (where == STATUS_LEFT)
                   1274:                                key = KEYC_MOUSEDOWN6_STATUS_LEFT;
                   1275:                        if (where == STATUS_RIGHT)
                   1276:                                key = KEYC_MOUSEDOWN6_STATUS_RIGHT;
                   1277:                        if (where == STATUS_DEFAULT)
                   1278:                                key = KEYC_MOUSEDOWN6_STATUS_DEFAULT;
                   1279:                        if (where == BORDER)
                   1280:                                key = KEYC_MOUSEDOWN6_BORDER;
                   1281:                        break;
                   1282:                case MOUSE_BUTTON_7:
                   1283:                        if (where == PANE)
                   1284:                                key = KEYC_MOUSEDOWN7_PANE;
                   1285:                        if (where == STATUS)
                   1286:                                key = KEYC_MOUSEDOWN7_STATUS;
                   1287:                        if (where == STATUS_LEFT)
                   1288:                                key = KEYC_MOUSEDOWN7_STATUS_LEFT;
                   1289:                        if (where == STATUS_RIGHT)
                   1290:                                key = KEYC_MOUSEDOWN7_STATUS_RIGHT;
                   1291:                        if (where == STATUS_DEFAULT)
                   1292:                                key = KEYC_MOUSEDOWN7_STATUS_DEFAULT;
                   1293:                        if (where == BORDER)
                   1294:                                key = KEYC_MOUSEDOWN7_BORDER;
                   1295:                        break;
                   1296:                case MOUSE_BUTTON_8:
                   1297:                        if (where == PANE)
                   1298:                                key = KEYC_MOUSEDOWN8_PANE;
                   1299:                        if (where == STATUS)
                   1300:                                key = KEYC_MOUSEDOWN8_STATUS;
                   1301:                        if (where == STATUS_LEFT)
                   1302:                                key = KEYC_MOUSEDOWN8_STATUS_LEFT;
                   1303:                        if (where == STATUS_RIGHT)
                   1304:                                key = KEYC_MOUSEDOWN8_STATUS_RIGHT;
                   1305:                        if (where == STATUS_DEFAULT)
                   1306:                                key = KEYC_MOUSEDOWN8_STATUS_DEFAULT;
                   1307:                        if (where == BORDER)
                   1308:                                key = KEYC_MOUSEDOWN8_BORDER;
                   1309:                        break;
                   1310:                case MOUSE_BUTTON_9:
                   1311:                        if (where == PANE)
                   1312:                                key = KEYC_MOUSEDOWN9_PANE;
                   1313:                        if (where == STATUS)
                   1314:                                key = KEYC_MOUSEDOWN9_STATUS;
                   1315:                        if (where == STATUS_LEFT)
                   1316:                                key = KEYC_MOUSEDOWN9_STATUS_LEFT;
                   1317:                        if (where == STATUS_RIGHT)
                   1318:                                key = KEYC_MOUSEDOWN9_STATUS_RIGHT;
                   1319:                        if (where == STATUS_DEFAULT)
                   1320:                                key = KEYC_MOUSEDOWN9_STATUS_DEFAULT;
                   1321:                        if (where == BORDER)
                   1322:                                key = KEYC_MOUSEDOWN9_BORDER;
                   1323:                        break;
                   1324:                case MOUSE_BUTTON_10:
                   1325:                        if (where == PANE)
                   1326:                                key = KEYC_MOUSEDOWN10_PANE;
                   1327:                        if (where == STATUS)
                   1328:                                key = KEYC_MOUSEDOWN10_STATUS;
                   1329:                        if (where == STATUS_LEFT)
                   1330:                                key = KEYC_MOUSEDOWN10_STATUS_LEFT;
                   1331:                        if (where == STATUS_RIGHT)
                   1332:                                key = KEYC_MOUSEDOWN10_STATUS_RIGHT;
                   1333:                        if (where == STATUS_DEFAULT)
                   1334:                                key = KEYC_MOUSEDOWN10_STATUS_DEFAULT;
                   1335:                        if (where == BORDER)
                   1336:                                key = KEYC_MOUSEDOWN10_BORDER;
                   1337:                        break;
                   1338:                case MOUSE_BUTTON_11:
                   1339:                        if (where == PANE)
                   1340:                                key = KEYC_MOUSEDOWN11_PANE;
                   1341:                        if (where == STATUS)
                   1342:                                key = KEYC_MOUSEDOWN11_STATUS;
                   1343:                        if (where == STATUS_LEFT)
                   1344:                                key = KEYC_MOUSEDOWN11_STATUS_LEFT;
                   1345:                        if (where == STATUS_RIGHT)
                   1346:                                key = KEYC_MOUSEDOWN11_STATUS_RIGHT;
                   1347:                        if (where == STATUS_DEFAULT)
                   1348:                                key = KEYC_MOUSEDOWN11_STATUS_DEFAULT;
                   1349:                        if (where == BORDER)
                   1350:                                key = KEYC_MOUSEDOWN11_BORDER;
                   1351:                        break;
1.311     nicm     1352:                }
                   1353:                break;
                   1354:        case SECOND:
                   1355:                switch (MOUSE_BUTTONS(b)) {
1.391     nicm     1356:                case MOUSE_BUTTON_1:
1.311     nicm     1357:                        if (where == PANE)
                   1358:                                key = KEYC_SECONDCLICK1_PANE;
                   1359:                        if (where == STATUS)
                   1360:                                key = KEYC_SECONDCLICK1_STATUS;
                   1361:                        if (where == STATUS_LEFT)
                   1362:                                key = KEYC_SECONDCLICK1_STATUS_LEFT;
                   1363:                        if (where == STATUS_RIGHT)
                   1364:                                key = KEYC_SECONDCLICK1_STATUS_RIGHT;
                   1365:                        if (where == STATUS_DEFAULT)
                   1366:                                key = KEYC_SECONDCLICK1_STATUS_DEFAULT;
                   1367:                        if (where == BORDER)
                   1368:                                key = KEYC_SECONDCLICK1_BORDER;
                   1369:                        break;
1.391     nicm     1370:                case MOUSE_BUTTON_2:
1.311     nicm     1371:                        if (where == PANE)
                   1372:                                key = KEYC_SECONDCLICK2_PANE;
                   1373:                        if (where == STATUS)
                   1374:                                key = KEYC_SECONDCLICK2_STATUS;
                   1375:                        if (where == STATUS_LEFT)
                   1376:                                key = KEYC_SECONDCLICK2_STATUS_LEFT;
                   1377:                        if (where == STATUS_RIGHT)
                   1378:                                key = KEYC_SECONDCLICK2_STATUS_RIGHT;
                   1379:                        if (where == STATUS_DEFAULT)
                   1380:                                key = KEYC_SECONDCLICK2_STATUS_DEFAULT;
                   1381:                        if (where == BORDER)
                   1382:                                key = KEYC_SECONDCLICK2_BORDER;
                   1383:                        break;
1.391     nicm     1384:                case MOUSE_BUTTON_3:
1.311     nicm     1385:                        if (where == PANE)
                   1386:                                key = KEYC_SECONDCLICK3_PANE;
                   1387:                        if (where == STATUS)
                   1388:                                key = KEYC_SECONDCLICK3_STATUS;
                   1389:                        if (where == STATUS_LEFT)
                   1390:                                key = KEYC_SECONDCLICK3_STATUS_LEFT;
                   1391:                        if (where == STATUS_RIGHT)
                   1392:                                key = KEYC_SECONDCLICK3_STATUS_RIGHT;
                   1393:                        if (where == STATUS_DEFAULT)
                   1394:                                key = KEYC_SECONDCLICK3_STATUS_DEFAULT;
                   1395:                        if (where == BORDER)
                   1396:                                key = KEYC_SECONDCLICK3_BORDER;
1.131     nicm     1397:                        break;
1.391     nicm     1398:                case MOUSE_BUTTON_6:
                   1399:                        if (where == PANE)
                   1400:                                key = KEYC_SECONDCLICK6_PANE;
                   1401:                        if (where == STATUS)
                   1402:                                key = KEYC_SECONDCLICK6_STATUS;
                   1403:                        if (where == STATUS_LEFT)
                   1404:                                key = KEYC_SECONDCLICK6_STATUS_LEFT;
                   1405:                        if (where == STATUS_RIGHT)
                   1406:                                key = KEYC_SECONDCLICK6_STATUS_RIGHT;
                   1407:                        if (where == STATUS_DEFAULT)
                   1408:                                key = KEYC_SECONDCLICK6_STATUS_DEFAULT;
                   1409:                        if (where == BORDER)
                   1410:                                key = KEYC_SECONDCLICK6_BORDER;
                   1411:                        break;
                   1412:                case MOUSE_BUTTON_7:
                   1413:                        if (where == PANE)
                   1414:                                key = KEYC_SECONDCLICK7_PANE;
                   1415:                        if (where == STATUS)
                   1416:                                key = KEYC_SECONDCLICK7_STATUS;
                   1417:                        if (where == STATUS_LEFT)
                   1418:                                key = KEYC_SECONDCLICK7_STATUS_LEFT;
                   1419:                        if (where == STATUS_RIGHT)
                   1420:                                key = KEYC_SECONDCLICK7_STATUS_RIGHT;
                   1421:                        if (where == STATUS_DEFAULT)
                   1422:                                key = KEYC_SECONDCLICK7_STATUS_DEFAULT;
                   1423:                        if (where == BORDER)
                   1424:                                key = KEYC_SECONDCLICK7_BORDER;
                   1425:                        break;
                   1426:                case MOUSE_BUTTON_8:
                   1427:                        if (where == PANE)
                   1428:                                key = KEYC_SECONDCLICK8_PANE;
                   1429:                        if (where == STATUS)
                   1430:                                key = KEYC_SECONDCLICK8_STATUS;
                   1431:                        if (where == STATUS_LEFT)
                   1432:                                key = KEYC_SECONDCLICK8_STATUS_LEFT;
                   1433:                        if (where == STATUS_RIGHT)
                   1434:                                key = KEYC_SECONDCLICK8_STATUS_RIGHT;
                   1435:                        if (where == STATUS_DEFAULT)
                   1436:                                key = KEYC_SECONDCLICK8_STATUS_DEFAULT;
                   1437:                        if (where == BORDER)
                   1438:                                key = KEYC_SECONDCLICK8_BORDER;
                   1439:                        break;
                   1440:                case MOUSE_BUTTON_9:
                   1441:                        if (where == PANE)
                   1442:                                key = KEYC_SECONDCLICK9_PANE;
                   1443:                        if (where == STATUS)
                   1444:                                key = KEYC_SECONDCLICK9_STATUS;
                   1445:                        if (where == STATUS_LEFT)
                   1446:                                key = KEYC_SECONDCLICK9_STATUS_LEFT;
                   1447:                        if (where == STATUS_RIGHT)
                   1448:                                key = KEYC_SECONDCLICK9_STATUS_RIGHT;
                   1449:                        if (where == STATUS_DEFAULT)
                   1450:                                key = KEYC_SECONDCLICK9_STATUS_DEFAULT;
                   1451:                        if (where == BORDER)
                   1452:                                key = KEYC_SECONDCLICK9_BORDER;
                   1453:                        break;
                   1454:                case MOUSE_BUTTON_10:
                   1455:                        if (where == PANE)
                   1456:                                key = KEYC_SECONDCLICK10_PANE;
                   1457:                        if (where == STATUS)
                   1458:                                key = KEYC_SECONDCLICK10_STATUS;
                   1459:                        if (where == STATUS_LEFT)
                   1460:                                key = KEYC_SECONDCLICK10_STATUS_LEFT;
                   1461:                        if (where == STATUS_RIGHT)
                   1462:                                key = KEYC_SECONDCLICK10_STATUS_RIGHT;
                   1463:                        if (where == STATUS_DEFAULT)
                   1464:                                key = KEYC_SECONDCLICK10_STATUS_DEFAULT;
                   1465:                        if (where == BORDER)
                   1466:                                key = KEYC_SECONDCLICK10_BORDER;
                   1467:                        break;
                   1468:                case MOUSE_BUTTON_11:
                   1469:                        if (where == PANE)
                   1470:                                key = KEYC_SECONDCLICK11_PANE;
                   1471:                        if (where == STATUS)
                   1472:                                key = KEYC_SECONDCLICK11_STATUS;
                   1473:                        if (where == STATUS_LEFT)
                   1474:                                key = KEYC_SECONDCLICK11_STATUS_LEFT;
                   1475:                        if (where == STATUS_RIGHT)
                   1476:                                key = KEYC_SECONDCLICK11_STATUS_RIGHT;
                   1477:                        if (where == STATUS_DEFAULT)
                   1478:                                key = KEYC_SECONDCLICK11_STATUS_DEFAULT;
                   1479:                        if (where == BORDER)
                   1480:                                key = KEYC_SECONDCLICK11_BORDER;
                   1481:                        break;
1.66      nicm     1482:                }
1.131     nicm     1483:                break;
1.192     nicm     1484:        case DOUBLE:
                   1485:                switch (MOUSE_BUTTONS(b)) {
1.391     nicm     1486:                case MOUSE_BUTTON_1:
1.192     nicm     1487:                        if (where == PANE)
                   1488:                                key = KEYC_DOUBLECLICK1_PANE;
                   1489:                        if (where == STATUS)
                   1490:                                key = KEYC_DOUBLECLICK1_STATUS;
1.258     nicm     1491:                        if (where == STATUS_LEFT)
                   1492:                                key = KEYC_DOUBLECLICK1_STATUS_LEFT;
                   1493:                        if (where == STATUS_RIGHT)
                   1494:                                key = KEYC_DOUBLECLICK1_STATUS_RIGHT;
1.274     nicm     1495:                        if (where == STATUS_DEFAULT)
                   1496:                                key = KEYC_DOUBLECLICK1_STATUS_DEFAULT;
1.192     nicm     1497:                        if (where == BORDER)
                   1498:                                key = KEYC_DOUBLECLICK1_BORDER;
                   1499:                        break;
1.391     nicm     1500:                case MOUSE_BUTTON_2:
1.192     nicm     1501:                        if (where == PANE)
                   1502:                                key = KEYC_DOUBLECLICK2_PANE;
                   1503:                        if (where == STATUS)
                   1504:                                key = KEYC_DOUBLECLICK2_STATUS;
1.258     nicm     1505:                        if (where == STATUS_LEFT)
                   1506:                                key = KEYC_DOUBLECLICK2_STATUS_LEFT;
                   1507:                        if (where == STATUS_RIGHT)
                   1508:                                key = KEYC_DOUBLECLICK2_STATUS_RIGHT;
1.274     nicm     1509:                        if (where == STATUS_DEFAULT)
                   1510:                                key = KEYC_DOUBLECLICK2_STATUS_DEFAULT;
1.192     nicm     1511:                        if (where == BORDER)
                   1512:                                key = KEYC_DOUBLECLICK2_BORDER;
                   1513:                        break;
1.391     nicm     1514:                case MOUSE_BUTTON_3:
1.192     nicm     1515:                        if (where == PANE)
                   1516:                                key = KEYC_DOUBLECLICK3_PANE;
                   1517:                        if (where == STATUS)
                   1518:                                key = KEYC_DOUBLECLICK3_STATUS;
1.258     nicm     1519:                        if (where == STATUS_LEFT)
                   1520:                                key = KEYC_DOUBLECLICK3_STATUS_LEFT;
                   1521:                        if (where == STATUS_RIGHT)
                   1522:                                key = KEYC_DOUBLECLICK3_STATUS_RIGHT;
1.274     nicm     1523:                        if (where == STATUS_DEFAULT)
                   1524:                                key = KEYC_DOUBLECLICK3_STATUS_DEFAULT;
1.192     nicm     1525:                        if (where == BORDER)
                   1526:                                key = KEYC_DOUBLECLICK3_BORDER;
                   1527:                        break;
1.391     nicm     1528:                case MOUSE_BUTTON_6:
                   1529:                        if (where == PANE)
                   1530:                                key = KEYC_DOUBLECLICK6_PANE;
                   1531:                        if (where == STATUS)
                   1532:                                key = KEYC_DOUBLECLICK6_STATUS;
                   1533:                        if (where == STATUS_LEFT)
                   1534:                                key = KEYC_DOUBLECLICK6_STATUS_LEFT;
                   1535:                        if (where == STATUS_RIGHT)
                   1536:                                key = KEYC_DOUBLECLICK6_STATUS_RIGHT;
                   1537:                        if (where == STATUS_DEFAULT)
                   1538:                                key = KEYC_DOUBLECLICK6_STATUS_DEFAULT;
                   1539:                        if (where == BORDER)
                   1540:                                key = KEYC_DOUBLECLICK6_BORDER;
                   1541:                        break;
                   1542:                case MOUSE_BUTTON_7:
                   1543:                        if (where == PANE)
                   1544:                                key = KEYC_DOUBLECLICK7_PANE;
                   1545:                        if (where == STATUS)
                   1546:                                key = KEYC_DOUBLECLICK7_STATUS;
                   1547:                        if (where == STATUS_LEFT)
                   1548:                                key = KEYC_DOUBLECLICK7_STATUS_LEFT;
                   1549:                        if (where == STATUS_RIGHT)
                   1550:                                key = KEYC_DOUBLECLICK7_STATUS_RIGHT;
                   1551:                        if (where == STATUS_DEFAULT)
                   1552:                                key = KEYC_DOUBLECLICK7_STATUS_DEFAULT;
                   1553:                        if (where == BORDER)
                   1554:                                key = KEYC_DOUBLECLICK7_BORDER;
                   1555:                        break;
                   1556:                case MOUSE_BUTTON_8:
                   1557:                        if (where == PANE)
                   1558:                                key = KEYC_DOUBLECLICK8_PANE;
                   1559:                        if (where == STATUS)
                   1560:                                key = KEYC_DOUBLECLICK8_STATUS;
                   1561:                        if (where == STATUS_LEFT)
                   1562:                                key = KEYC_DOUBLECLICK8_STATUS_LEFT;
                   1563:                        if (where == STATUS_RIGHT)
                   1564:                                key = KEYC_DOUBLECLICK8_STATUS_RIGHT;
                   1565:                        if (where == STATUS_DEFAULT)
                   1566:                                key = KEYC_DOUBLECLICK8_STATUS_DEFAULT;
                   1567:                        if (where == BORDER)
                   1568:                                key = KEYC_DOUBLECLICK8_BORDER;
                   1569:                        break;
                   1570:                case MOUSE_BUTTON_9:
                   1571:                        if (where == PANE)
                   1572:                                key = KEYC_DOUBLECLICK9_PANE;
                   1573:                        if (where == STATUS)
                   1574:                                key = KEYC_DOUBLECLICK9_STATUS;
                   1575:                        if (where == STATUS_LEFT)
                   1576:                                key = KEYC_DOUBLECLICK9_STATUS_LEFT;
                   1577:                        if (where == STATUS_RIGHT)
                   1578:                                key = KEYC_DOUBLECLICK9_STATUS_RIGHT;
                   1579:                        if (where == STATUS_DEFAULT)
                   1580:                                key = KEYC_DOUBLECLICK9_STATUS_DEFAULT;
                   1581:                        if (where == BORDER)
                   1582:                                key = KEYC_DOUBLECLICK9_BORDER;
                   1583:                        break;
                   1584:                case MOUSE_BUTTON_10:
                   1585:                        if (where == PANE)
                   1586:                                key = KEYC_DOUBLECLICK10_PANE;
                   1587:                        if (where == STATUS)
                   1588:                                key = KEYC_DOUBLECLICK10_STATUS;
                   1589:                        if (where == STATUS_LEFT)
                   1590:                                key = KEYC_DOUBLECLICK10_STATUS_LEFT;
                   1591:                        if (where == STATUS_RIGHT)
                   1592:                                key = KEYC_DOUBLECLICK10_STATUS_RIGHT;
                   1593:                        if (where == STATUS_DEFAULT)
                   1594:                                key = KEYC_DOUBLECLICK10_STATUS_DEFAULT;
                   1595:                        if (where == BORDER)
                   1596:                                key = KEYC_DOUBLECLICK10_BORDER;
                   1597:                        break;
                   1598:                case MOUSE_BUTTON_11:
                   1599:                        if (where == PANE)
                   1600:                                key = KEYC_DOUBLECLICK11_PANE;
                   1601:                        if (where == STATUS)
                   1602:                                key = KEYC_DOUBLECLICK11_STATUS;
                   1603:                        if (where == STATUS_LEFT)
                   1604:                                key = KEYC_DOUBLECLICK11_STATUS_LEFT;
                   1605:                        if (where == STATUS_RIGHT)
                   1606:                                key = KEYC_DOUBLECLICK11_STATUS_RIGHT;
                   1607:                        if (where == STATUS_DEFAULT)
                   1608:                                key = KEYC_DOUBLECLICK11_STATUS_DEFAULT;
                   1609:                        if (where == BORDER)
                   1610:                                key = KEYC_DOUBLECLICK11_BORDER;
                   1611:                        break;
1.192     nicm     1612:                }
                   1613:                break;
                   1614:        case TRIPLE:
                   1615:                switch (MOUSE_BUTTONS(b)) {
1.391     nicm     1616:                case MOUSE_BUTTON_1:
1.192     nicm     1617:                        if (where == PANE)
                   1618:                                key = KEYC_TRIPLECLICK1_PANE;
                   1619:                        if (where == STATUS)
                   1620:                                key = KEYC_TRIPLECLICK1_STATUS;
1.258     nicm     1621:                        if (where == STATUS_LEFT)
                   1622:                                key = KEYC_TRIPLECLICK1_STATUS_LEFT;
                   1623:                        if (where == STATUS_RIGHT)
                   1624:                                key = KEYC_TRIPLECLICK1_STATUS_RIGHT;
1.274     nicm     1625:                        if (where == STATUS_DEFAULT)
                   1626:                                key = KEYC_TRIPLECLICK1_STATUS_DEFAULT;
1.192     nicm     1627:                        if (where == BORDER)
                   1628:                                key = KEYC_TRIPLECLICK1_BORDER;
                   1629:                        break;
1.391     nicm     1630:                case MOUSE_BUTTON_2:
1.192     nicm     1631:                        if (where == PANE)
                   1632:                                key = KEYC_TRIPLECLICK2_PANE;
                   1633:                        if (where == STATUS)
                   1634:                                key = KEYC_TRIPLECLICK2_STATUS;
1.258     nicm     1635:                        if (where == STATUS_LEFT)
                   1636:                                key = KEYC_TRIPLECLICK2_STATUS_LEFT;
                   1637:                        if (where == STATUS_RIGHT)
                   1638:                                key = KEYC_TRIPLECLICK2_STATUS_RIGHT;
1.274     nicm     1639:                        if (where == STATUS_DEFAULT)
                   1640:                                key = KEYC_TRIPLECLICK2_STATUS_DEFAULT;
1.192     nicm     1641:                        if (where == BORDER)
                   1642:                                key = KEYC_TRIPLECLICK2_BORDER;
                   1643:                        break;
1.391     nicm     1644:                case MOUSE_BUTTON_3:
1.192     nicm     1645:                        if (where == PANE)
                   1646:                                key = KEYC_TRIPLECLICK3_PANE;
                   1647:                        if (where == STATUS)
                   1648:                                key = KEYC_TRIPLECLICK3_STATUS;
1.258     nicm     1649:                        if (where == STATUS_LEFT)
                   1650:                                key = KEYC_TRIPLECLICK3_STATUS_LEFT;
                   1651:                        if (where == STATUS_RIGHT)
                   1652:                                key = KEYC_TRIPLECLICK3_STATUS_RIGHT;
1.274     nicm     1653:                        if (where == STATUS_DEFAULT)
                   1654:                                key = KEYC_TRIPLECLICK3_STATUS_DEFAULT;
1.192     nicm     1655:                        if (where == BORDER)
                   1656:                                key = KEYC_TRIPLECLICK3_BORDER;
1.391     nicm     1657:                        break;
                   1658:                case MOUSE_BUTTON_6:
                   1659:                        if (where == PANE)
                   1660:                                key = KEYC_TRIPLECLICK6_PANE;
                   1661:                        if (where == STATUS)
                   1662:                                key = KEYC_TRIPLECLICK6_STATUS;
                   1663:                        if (where == STATUS_LEFT)
                   1664:                                key = KEYC_TRIPLECLICK6_STATUS_LEFT;
                   1665:                        if (where == STATUS_RIGHT)
                   1666:                                key = KEYC_TRIPLECLICK6_STATUS_RIGHT;
                   1667:                        if (where == STATUS_DEFAULT)
                   1668:                                key = KEYC_TRIPLECLICK6_STATUS_DEFAULT;
                   1669:                        if (where == BORDER)
                   1670:                                key = KEYC_TRIPLECLICK6_BORDER;
                   1671:                        break;
                   1672:                case MOUSE_BUTTON_7:
                   1673:                        if (where == PANE)
                   1674:                                key = KEYC_TRIPLECLICK7_PANE;
                   1675:                        if (where == STATUS)
                   1676:                                key = KEYC_TRIPLECLICK7_STATUS;
                   1677:                        if (where == STATUS_LEFT)
                   1678:                                key = KEYC_TRIPLECLICK7_STATUS_LEFT;
                   1679:                        if (where == STATUS_RIGHT)
                   1680:                                key = KEYC_TRIPLECLICK7_STATUS_RIGHT;
                   1681:                        if (where == STATUS_DEFAULT)
                   1682:                                key = KEYC_TRIPLECLICK7_STATUS_DEFAULT;
                   1683:                        if (where == BORDER)
                   1684:                                key = KEYC_TRIPLECLICK7_BORDER;
                   1685:                        break;
                   1686:                case MOUSE_BUTTON_8:
                   1687:                        if (where == PANE)
                   1688:                                key = KEYC_TRIPLECLICK8_PANE;
                   1689:                        if (where == STATUS)
                   1690:                                key = KEYC_TRIPLECLICK8_STATUS;
                   1691:                        if (where == STATUS_LEFT)
                   1692:                                key = KEYC_TRIPLECLICK8_STATUS_LEFT;
                   1693:                        if (where == STATUS_RIGHT)
                   1694:                                key = KEYC_TRIPLECLICK8_STATUS_RIGHT;
                   1695:                        if (where == STATUS_DEFAULT)
                   1696:                                key = KEYC_TRIPLECLICK8_STATUS_DEFAULT;
                   1697:                        if (where == BORDER)
                   1698:                                key = KEYC_TRIPLECLICK8_BORDER;
                   1699:                        break;
                   1700:                case MOUSE_BUTTON_9:
                   1701:                        if (where == PANE)
                   1702:                                key = KEYC_TRIPLECLICK9_PANE;
                   1703:                        if (where == STATUS)
                   1704:                                key = KEYC_TRIPLECLICK9_STATUS;
                   1705:                        if (where == STATUS_LEFT)
                   1706:                                key = KEYC_TRIPLECLICK9_STATUS_LEFT;
                   1707:                        if (where == STATUS_RIGHT)
                   1708:                                key = KEYC_TRIPLECLICK9_STATUS_RIGHT;
                   1709:                        if (where == STATUS_DEFAULT)
                   1710:                                key = KEYC_TRIPLECLICK9_STATUS_DEFAULT;
                   1711:                        if (where == BORDER)
                   1712:                                key = KEYC_TRIPLECLICK9_BORDER;
                   1713:                        break;
                   1714:                case MOUSE_BUTTON_10:
                   1715:                        if (where == PANE)
                   1716:                                key = KEYC_TRIPLECLICK10_PANE;
                   1717:                        if (where == STATUS)
                   1718:                                key = KEYC_TRIPLECLICK10_STATUS;
                   1719:                        if (where == STATUS_LEFT)
                   1720:                                key = KEYC_TRIPLECLICK10_STATUS_LEFT;
                   1721:                        if (where == STATUS_RIGHT)
                   1722:                                key = KEYC_TRIPLECLICK10_STATUS_RIGHT;
                   1723:                        if (where == STATUS_DEFAULT)
                   1724:                                key = KEYC_TRIPLECLICK10_STATUS_DEFAULT;
                   1725:                        if (where == BORDER)
                   1726:                                key = KEYC_TRIPLECLICK10_BORDER;
                   1727:                        break;
                   1728:                case MOUSE_BUTTON_11:
                   1729:                        if (where == PANE)
                   1730:                                key = KEYC_TRIPLECLICK11_PANE;
                   1731:                        if (where == STATUS)
                   1732:                                key = KEYC_TRIPLECLICK11_STATUS;
                   1733:                        if (where == STATUS_LEFT)
                   1734:                                key = KEYC_TRIPLECLICK11_STATUS_LEFT;
                   1735:                        if (where == STATUS_RIGHT)
                   1736:                                key = KEYC_TRIPLECLICK11_STATUS_RIGHT;
                   1737:                        if (where == STATUS_DEFAULT)
                   1738:                                key = KEYC_TRIPLECLICK11_STATUS_DEFAULT;
                   1739:                        if (where == BORDER)
                   1740:                                key = KEYC_TRIPLECLICK11_BORDER;
1.192     nicm     1741:                        break;
                   1742:                }
                   1743:                break;
1.66      nicm     1744:        }
1.177     nicm     1745:        if (key == KEYC_UNKNOWN)
                   1746:                return (KEYC_UNKNOWN);
1.66      nicm     1747:
1.305     nicm     1748: out:
1.131     nicm     1749:        /* Apply modifiers if any. */
                   1750:        if (b & MOUSE_MASK_META)
1.342     nicm     1751:                key |= KEYC_META;
1.131     nicm     1752:        if (b & MOUSE_MASK_CTRL)
                   1753:                key |= KEYC_CTRL;
                   1754:        if (b & MOUSE_MASK_SHIFT)
                   1755:                key |= KEYC_SHIFT;
1.66      nicm     1756:
1.305     nicm     1757:        if (log_get_level() != 0)
1.343     nicm     1758:                log_debug("mouse key is %s", key_string_lookup_key (key, 1));
1.131     nicm     1759:        return (key);
1.66      nicm     1760: }
                   1761:
1.398     nicm     1762: /* Is this a bracket paste key? */
                   1763: static int
                   1764: server_client_is_bracket_pasting(struct client *c, key_code key)
                   1765: {
                   1766:        if (key == KEYC_PASTE_START) {
                   1767:                c->flags |= CLIENT_BRACKETPASTING;
                   1768:                log_debug("%s: bracket paste on", c->name);
                   1769:                return (1);
                   1770:        }
                   1771:
                   1772:        if (key == KEYC_PASTE_END) {
                   1773:                c->flags &= ~CLIENT_BRACKETPASTING;
                   1774:                log_debug("%s: bracket paste off", c->name);
                   1775:                return (1);
                   1776:        }
                   1777:
                   1778:        return !!(c->flags & CLIENT_BRACKETPASTING);
                   1779: }
                   1780:
1.82      nicm     1781: /* Is this fast enough to probably be a paste? */
1.190     nicm     1782: static int
1.82      nicm     1783: server_client_assume_paste(struct session *s)
                   1784: {
                   1785:        struct timeval  tv;
1.84      nicm     1786:        int             t;
1.82      nicm     1787:
1.163     nicm     1788:        if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1.83      nicm     1789:                return (0);
1.82      nicm     1790:
                   1791:        timersub(&s->activity_time, &s->last_activity_time, &tv);
1.171     nicm     1792:        if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
                   1793:                log_debug("session %s pasting (flag %d)", s->name,
                   1794:                    !!(s->flags & SESSION_PASTING));
                   1795:                if (s->flags & SESSION_PASTING)
                   1796:                        return (1);
                   1797:                s->flags |= SESSION_PASTING;
                   1798:                return (0);
                   1799:        }
                   1800:        log_debug("session %s not pasting", s->name);
                   1801:        s->flags &= ~SESSION_PASTING;
1.83      nicm     1802:        return (0);
1.82      nicm     1803: }
                   1804:
1.295     nicm     1805: /* Has the latest client changed? */
                   1806: static void
                   1807: server_client_update_latest(struct client *c)
                   1808: {
                   1809:        struct window   *w;
                   1810:
                   1811:        if (c->session == NULL)
                   1812:                return;
                   1813:        w = c->session->curw->window;
                   1814:
                   1815:        if (w->latest == c)
                   1816:                return;
                   1817:        w->latest = c;
                   1818:
                   1819:        if (options_get_number(w->options, "window-size") == WINDOW_SIZE_LATEST)
1.355     nicm     1820:                recalculate_size(w, 0);
1.377     nicm     1821:
                   1822:        notify_client("client-active", c);
1.295     nicm     1823: }
                   1824:
1.276     nicm     1825: /*
                   1826:  * Handle data key input from client. This owns and can modify the key event it
                   1827:  * is given and is responsible for freeing it.
                   1828:  */
1.280     nicm     1829: static enum cmd_retval
1.276     nicm     1830: server_client_key_callback(struct cmdq_item *item, void *data)
1.18      nicm     1831: {
1.316     nicm     1832:        struct client                   *c = cmdq_get_client(item);
1.276     nicm     1833:        struct key_event                *event = data;
                   1834:        key_code                         key = event->key;
                   1835:        struct mouse_event              *m = &event->m;
1.267     nicm     1836:        struct session                  *s = c->session;
                   1837:        struct winlink                  *wl;
                   1838:        struct window_pane              *wp;
                   1839:        struct window_mode_entry        *wme;
                   1840:        struct timeval                   tv;
                   1841:        struct key_table                *table, *first;
                   1842:        struct key_binding              *bd;
                   1843:        int                              xtimeout, flags;
                   1844:        struct cmd_find_state            fs;
                   1845:        key_code                         key0;
1.18      nicm     1846:
                   1847:        /* Check the client is good to accept input. */
1.303     nicm     1848:        if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1.276     nicm     1849:                goto out;
1.264     nicm     1850:        wl = s->curw;
1.18      nicm     1851:
                   1852:        /* Update the activity timer. */
                   1853:        if (gettimeofday(&c->activity_time, NULL) != 0)
                   1854:                fatal("gettimeofday failed");
1.149     nicm     1855:        session_update_activity(s, &c->activity_time);
1.18      nicm     1856:
                   1857:        /* Check for mouse keys. */
1.204     nicm     1858:        m->valid = 0;
1.306     nicm     1859:        if (key == KEYC_MOUSE || key == KEYC_DOUBLECLICK) {
1.30      nicm     1860:                if (c->flags & CLIENT_READONLY)
1.276     nicm     1861:                        goto out;
                   1862:                key = server_client_check_mouse(c, event);
1.177     nicm     1863:                if (key == KEYC_UNKNOWN)
1.276     nicm     1864:                        goto out;
1.131     nicm     1865:
                   1866:                m->valid = 1;
                   1867:                m->key = key;
1.203     nicm     1868:
                   1869:                /*
1.204     nicm     1870:                 * Mouse drag is in progress, so fire the callback (now that
                   1871:                 * the mouse event is valid).
1.203     nicm     1872:                 */
1.305     nicm     1873:                if ((key & KEYC_MASK_KEY) == KEYC_DRAGGING) {
1.204     nicm     1874:                        c->tty.mouse_drag_update(c, m);
1.276     nicm     1875:                        goto out;
1.204     nicm     1876:                }
1.338     nicm     1877:                event->key = key;
1.276     nicm     1878:        }
1.18      nicm     1879:
1.202     nicm     1880:        /* Find affected pane. */
1.243     nicm     1881:        if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m, 0) != 0)
1.341     nicm     1882:                cmd_find_from_client(&fs, c, 0);
1.227     nicm     1883:        wp = fs.wp;
1.202     nicm     1884:
                   1885:        /* Forward mouse keys if disabled. */
1.206     nicm     1886:        if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse"))
1.398     nicm     1887:                goto forward_key;
                   1888:
                   1889:        /* Forward if bracket pasting. */
                   1890:        if (server_client_is_bracket_pasting(c, key))
1.253     nicm     1891:                goto forward_key;
1.202     nicm     1892:
1.132     nicm     1893:        /* Treat everything as a regular key when pasting is detected. */
1.399     nicm     1894:        if (!KEYC_IS_MOUSE(key) &&
                   1895:            (~key & KEYC_SENT) &&
                   1896:            server_client_assume_paste(s))
1.253     nicm     1897:                goto forward_key;
1.18      nicm     1898:
1.191     nicm     1899:        /*
                   1900:         * Work out the current key table. If the pane is in a mode, use
                   1901:         * the mode table instead of the default key table.
                   1902:         */
1.223     nicm     1903:        if (server_client_is_default_key_table(c, c->keytable) &&
                   1904:            wp != NULL &&
1.267     nicm     1905:            (wme = TAILQ_FIRST(&wp->modes)) != NULL &&
                   1906:            wme->mode->key_table != NULL)
                   1907:                table = key_bindings_get_table(wme->mode->key_table(wme), 1);
1.223     nicm     1908:        else
1.191     nicm     1909:                table = c->keytable;
1.223     nicm     1910:        first = table;
1.191     nicm     1911:
1.253     nicm     1912: table_changed:
1.205     nicm     1913:        /*
                   1914:         * The prefix always takes precedence and forces a switch to the prefix
                   1915:         * table, unless we are already there.
                   1916:         */
1.343     nicm     1917:        key0 = (key & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS));
1.239     nicm     1918:        if ((key0 == (key_code)options_get_number(s->options, "prefix") ||
                   1919:            key0 == (key_code)options_get_number(s->options, "prefix2")) &&
1.205     nicm     1920:            strcmp(table->name, "prefix") != 0) {
                   1921:                server_client_set_key_table(c, "prefix");
                   1922:                server_status_client(c);
1.276     nicm     1923:                goto out;
1.205     nicm     1924:        }
1.238     nicm     1925:        flags = c->flags;
1.205     nicm     1926:
1.262     nicm     1927: try_again:
1.223     nicm     1928:        /* Log key table. */
                   1929:        if (wp == NULL)
                   1930:                log_debug("key table %s (no pane)", table->name);
                   1931:        else
                   1932:                log_debug("key table %s (pane %%%u)", table->name, wp->id);
1.225     nicm     1933:        if (c->flags & CLIENT_REPEAT)
                   1934:                log_debug("currently repeating");
1.223     nicm     1935:
1.132     nicm     1936:        /* Try to see if there is a key binding in the current table. */
1.254     nicm     1937:        bd = key_bindings_get(table, key0);
1.132     nicm     1938:        if (bd != NULL) {
                   1939:                /*
                   1940:                 * Key was matched in this table. If currently repeating but a
                   1941:                 * non-repeating binding was found, stop repeating and try
                   1942:                 * again in the root table.
                   1943:                 */
1.222     nicm     1944:                if ((c->flags & CLIENT_REPEAT) &&
                   1945:                    (~bd->flags & KEY_BINDING_REPEAT)) {
1.262     nicm     1946:                        log_debug("found in key table %s (not repeating)",
                   1947:                            table->name);
1.178     nicm     1948:                        server_client_set_key_table(c, NULL);
1.262     nicm     1949:                        first = table = c->keytable;
1.132     nicm     1950:                        c->flags &= ~CLIENT_REPEAT;
1.85      nicm     1951:                        server_status_client(c);
1.253     nicm     1952:                        goto table_changed;
1.18      nicm     1953:                }
1.223     nicm     1954:                log_debug("found in key table %s", table->name);
1.82      nicm     1955:
1.132     nicm     1956:                /*
                   1957:                 * Take a reference to this table to make sure the key binding
                   1958:                 * doesn't disappear.
                   1959:                 */
                   1960:                table->references++;
                   1961:
                   1962:                /*
                   1963:                 * If this is a repeating key, start the timer. Otherwise reset
                   1964:                 * the client back to the root table.
                   1965:                 */
1.163     nicm     1966:                xtimeout = options_get_number(s->options, "repeat-time");
1.222     nicm     1967:                if (xtimeout != 0 && (bd->flags & KEY_BINDING_REPEAT)) {
1.132     nicm     1968:                        c->flags |= CLIENT_REPEAT;
                   1969:
                   1970:                        tv.tv_sec = xtimeout / 1000;
                   1971:                        tv.tv_usec = (xtimeout % 1000) * 1000L;
                   1972:                        evtimer_del(&c->repeat_timer);
                   1973:                        evtimer_add(&c->repeat_timer, &tv);
                   1974:                } else {
1.18      nicm     1975:                        c->flags &= ~CLIENT_REPEAT;
1.178     nicm     1976:                        server_client_set_key_table(c, NULL);
1.18      nicm     1977:                }
1.132     nicm     1978:                server_status_client(c);
                   1979:
1.227     nicm     1980:                /* Execute the key binding. */
1.317     nicm     1981:                key_bindings_dispatch(bd, item, c, event, &fs);
1.132     nicm     1982:                key_bindings_unref_table(table);
1.276     nicm     1983:                goto out;
1.18      nicm     1984:        }
                   1985:
1.132     nicm     1986:        /*
1.253     nicm     1987:         * No match, try the ANY key.
                   1988:         */
                   1989:        if (key0 != KEYC_ANY) {
                   1990:                key0 = KEYC_ANY;
                   1991:                goto try_again;
                   1992:        }
                   1993:
                   1994:        /*
1.223     nicm     1995:         * No match in this table. If not in the root table or if repeating,
                   1996:         * switch the client back to the root table and try again.
1.132     nicm     1997:         */
1.223     nicm     1998:        log_debug("not found in key table %s", table->name);
                   1999:        if (!server_client_is_default_key_table(c, table) ||
                   2000:            (c->flags & CLIENT_REPEAT)) {
1.262     nicm     2001:                log_debug("trying in root table");
1.178     nicm     2002:                server_client_set_key_table(c, NULL);
1.262     nicm     2003:                table = c->keytable;
                   2004:                if (c->flags & CLIENT_REPEAT)
                   2005:                        first = table;
1.18      nicm     2006:                c->flags &= ~CLIENT_REPEAT;
1.132     nicm     2007:                server_status_client(c);
1.253     nicm     2008:                goto table_changed;
1.18      nicm     2009:        }
                   2010:
1.223     nicm     2011:        /*
                   2012:         * No match in the root table either. If this wasn't the first table
                   2013:         * tried, don't pass the key to the pane.
                   2014:         */
1.238     nicm     2015:        if (first != table && (~flags & CLIENT_REPEAT)) {
1.178     nicm     2016:                server_client_set_key_table(c, NULL);
1.132     nicm     2017:                server_status_client(c);
1.276     nicm     2018:                goto out;
1.161     nicm     2019:        }
                   2020:
1.253     nicm     2021: forward_key:
1.161     nicm     2022:        if (c->flags & CLIENT_READONLY)
1.276     nicm     2023:                goto out;
1.161     nicm     2024:        if (wp != NULL)
1.264     nicm     2025:                window_pane_key(wp, c, s, wl, key, m);
1.276     nicm     2026:
                   2027: out:
1.347     nicm     2028:        if (s != NULL && key != KEYC_FOCUS_OUT)
1.295     nicm     2029:                server_client_update_latest(c);
1.276     nicm     2030:        free(event);
                   2031:        return (CMD_RETURN_NORMAL);
1.280     nicm     2032: }
                   2033:
                   2034: /* Handle a key event. */
                   2035: int
                   2036: server_client_handle_key(struct client *c, struct key_event *event)
                   2037: {
                   2038:        struct session          *s = c->session;
                   2039:        struct cmdq_item        *item;
                   2040:
                   2041:        /* Check the client is good to accept input. */
1.303     nicm     2042:        if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1.280     nicm     2043:                return (0);
                   2044:
                   2045:        /*
1.291     nicm     2046:         * Key presses in overlay mode and the command prompt are a special
                   2047:         * case. The queue might be blocked so they need to be processed
                   2048:         * immediately rather than queued.
1.280     nicm     2049:         */
1.291     nicm     2050:        if (~c->flags & CLIENT_READONLY) {
1.372     nicm     2051:                if (c->message_string != NULL) {
                   2052:                        if (c->message_ignore_keys)
                   2053:                                return (0);
                   2054:                        status_message_clear(c);
                   2055:                }
1.291     nicm     2056:                if (c->overlay_key != NULL) {
1.380     nicm     2057:                        switch (c->overlay_key(c, c->overlay_data, event)) {
1.291     nicm     2058:                        case 0:
                   2059:                                return (0);
                   2060:                        case 1:
                   2061:                                server_client_clear_overlay(c);
                   2062:                                return (0);
                   2063:                        }
1.290     nicm     2064:                }
1.293     nicm     2065:                server_client_clear_overlay(c);
1.335     nicm     2066:                if (c->prompt_string != NULL) {
                   2067:                        if (status_prompt_key(c, event->key) == 0)
                   2068:                                return (0);
                   2069:                }
1.280     nicm     2070:        }
                   2071:
                   2072:        /*
                   2073:         * Add the key to the queue so it happens after any commands queued by
                   2074:         * previous keys.
                   2075:         */
                   2076:        item = cmdq_get_callback(server_client_key_callback, event);
                   2077:        cmdq_append(c, item);
                   2078:        return (1);
1.18      nicm     2079: }
                   2080:
1.2       nicm     2081: /* Client functions that need to happen every loop. */
                   2082: void
                   2083: server_client_loop(void)
                   2084: {
                   2085:        struct client           *c;
                   2086:        struct window           *w;
                   2087:        struct window_pane      *wp;
1.344     nicm     2088:
                   2089:        /* Check for window resize. This is done before redrawing. */
                   2090:        RB_FOREACH(w, windows, &windows)
                   2091:                server_client_check_window_resize(w);
1.2       nicm     2092:
1.344     nicm     2093:        /* Check clients. */
1.135     nicm     2094:        TAILQ_FOREACH(c, &clients, entry) {
1.36      nicm     2095:                server_client_check_exit(c);
                   2096:                if (c->session != NULL) {
1.366     nicm     2097:                        server_client_check_modes(c);
1.36      nicm     2098:                        server_client_check_redraw(c);
                   2099:                        server_client_reset_state(c);
                   2100:                }
1.2       nicm     2101:        }
                   2102:
                   2103:        /*
                   2104:         * Any windows will have been redrawn as part of clients, so clear
1.379     nicm     2105:         * their flags now.
1.2       nicm     2106:         */
1.134     nicm     2107:        RB_FOREACH(w, windows, &windows) {
1.91      nicm     2108:                TAILQ_FOREACH(wp, &w->panes, entry) {
1.289     nicm     2109:                        if (wp->fd != -1) {
1.344     nicm     2110:                                server_client_check_pane_resize(wp);
1.345     nicm     2111:                                server_client_check_pane_buffer(wp);
1.101     nicm     2112:                        }
1.326     nicm     2113:                        wp->flags &= ~PANE_REDRAW;
1.91      nicm     2114:                }
1.150     nicm     2115:                check_window_name(w);
1.2       nicm     2116:        }
1.92      nicm     2117: }
                   2118:
1.344     nicm     2119: /* Check if window needs to be resized. */
                   2120: static void
                   2121: server_client_check_window_resize(struct window *w)
                   2122: {
                   2123:        struct winlink  *wl;
                   2124:
                   2125:        if (~w->flags & WINDOW_RESIZE)
                   2126:                return;
                   2127:
                   2128:        TAILQ_FOREACH(wl, &w->winlinks, wentry) {
                   2129:                if (wl->session->attached != 0 && wl->session->curw == wl)
                   2130:                        break;
                   2131:        }
                   2132:        if (wl == NULL)
                   2133:                return;
                   2134:
                   2135:        log_debug("%s: resizing window @%u", __func__, w->id);
                   2136:        resize_window(w, w->new_sx, w->new_sy, w->new_xpixel, w->new_ypixel);
                   2137: }
                   2138:
1.356     nicm     2139: /* Resize timer event. */
1.188     nicm     2140: static void
1.356     nicm     2141: server_client_resize_timer(__unused int fd, __unused short events, void *data)
1.92      nicm     2142: {
1.356     nicm     2143:        struct window_pane      *wp = data;
1.92      nicm     2144:
1.356     nicm     2145:        log_debug("%s: %%%u resize timer expired", __func__, wp->id);
                   2146:        evtimer_del(&wp->resize_timer);
1.188     nicm     2147: }
                   2148:
1.374     nicm     2149: /* Check if pane should be resized. */
1.294     nicm     2150: static void
1.374     nicm     2151: server_client_check_pane_resize(struct window_pane *wp)
1.294     nicm     2152: {
1.374     nicm     2153:        struct window_pane_resize       *r;
                   2154:        struct window_pane_resize       *r1;
                   2155:        struct window_pane_resize       *first;
                   2156:        struct window_pane_resize       *last;
                   2157:        struct timeval                   tv = { .tv_usec = 250000 };
1.294     nicm     2158:
1.374     nicm     2159:        if (TAILQ_EMPTY(&wp->resize_queue))
                   2160:                return;
1.294     nicm     2161:
1.356     nicm     2162:        if (!event_initialized(&wp->resize_timer))
                   2163:                evtimer_set(&wp->resize_timer, server_client_resize_timer, wp);
1.374     nicm     2164:        if (evtimer_pending(&wp->resize_timer, NULL))
                   2165:                return;
1.356     nicm     2166:
                   2167:        log_debug("%s: %%%u needs to be resized", __func__, wp->id);
1.374     nicm     2168:        TAILQ_FOREACH(r, &wp->resize_queue, entry) {
                   2169:                log_debug("queued resize: %ux%u -> %ux%u", r->osx, r->osy,
                   2170:                    r->sx, r->sy);
1.356     nicm     2171:        }
1.188     nicm     2172:
1.374     nicm     2173:        /*
                   2174:         * There are three cases that matter:
                   2175:         *
                   2176:         * - Only one resize. It can just be applied.
                   2177:         *
                   2178:         * - Multiple resizes and the ending size is different from the
                   2179:         *   starting size. We can discard all resizes except the most recent.
                   2180:         *
                   2181:         * - Multiple resizes and the ending size is the same as the starting
                   2182:         *   size. We must resize at least twice to force the application to
                   2183:         *   redraw. So apply the first and leave the last on the queue for
                   2184:         *   next time.
                   2185:         */
                   2186:        first = TAILQ_FIRST(&wp->resize_queue);
                   2187:        last = TAILQ_LAST(&wp->resize_queue, window_pane_resizes);
                   2188:        if (first == last) {
                   2189:                /* Only one resize. */
                   2190:                window_pane_send_resize(wp, first->sx, first->sy);
                   2191:                TAILQ_REMOVE(&wp->resize_queue, first, entry);
                   2192:                free(first);
                   2193:        } else if (last->sx != first->osx || last->sy != first->osy) {
                   2194:                /* Multiple resizes ending up with a different size. */
                   2195:                window_pane_send_resize(wp, last->sx, last->sy);
                   2196:                TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
                   2197:                        TAILQ_REMOVE(&wp->resize_queue, r, entry);
                   2198:                        free(r);
                   2199:                }
1.356     nicm     2200:        } else {
                   2201:                /*
1.374     nicm     2202:                 * Multiple resizes ending up with the same size. There will
                   2203:                 * not be more than one to the same size in succession so we
                   2204:                 * can just use the last-but-one on the list and leave the last
                   2205:                 * for later. We reduce the time until the next check to avoid
                   2206:                 * a long delay between the resizes.
1.356     nicm     2207:                 */
1.374     nicm     2208:                r = TAILQ_PREV(last, window_pane_resizes, entry);
                   2209:                window_pane_send_resize(wp, r->sx, r->sy);
                   2210:                TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
                   2211:                        if (r == last)
                   2212:                                break;
                   2213:                        TAILQ_REMOVE(&wp->resize_queue, r, entry);
                   2214:                        free(r);
1.356     nicm     2215:                }
1.374     nicm     2216:                tv.tv_usec = 10000;
1.356     nicm     2217:        }
1.374     nicm     2218:        evtimer_add(&wp->resize_timer, &tv);
1.345     nicm     2219: }
                   2220:
                   2221: /* Check pane buffer size. */
                   2222: static void
                   2223: server_client_check_pane_buffer(struct window_pane *wp)
                   2224: {
                   2225:        struct evbuffer                 *evb = wp->event->input;
                   2226:        size_t                           minimum;
                   2227:        struct client                   *c;
1.346     nicm     2228:        struct window_pane_offset       *wpo;
                   2229:        int                              off = 1, flag;
                   2230:        u_int                            attached_clients = 0;
1.353     nicm     2231:        size_t                           new_size;
1.345     nicm     2232:
                   2233:        /*
1.352     nicm     2234:         * Work out the minimum used size. This is the most that can be removed
                   2235:         * from the buffer.
1.345     nicm     2236:         */
1.352     nicm     2237:        minimum = wp->offset.used;
                   2238:        if (wp->pipe_fd != -1 && wp->pipe_offset.used < minimum)
                   2239:                minimum = wp->pipe_offset.used;
1.345     nicm     2240:        TAILQ_FOREACH(c, &clients, entry) {
                   2241:                if (c->session == NULL)
                   2242:                        continue;
1.346     nicm     2243:                attached_clients++;
                   2244:
                   2245:                if (~c->flags & CLIENT_CONTROL) {
1.345     nicm     2246:                        off = 0;
                   2247:                        continue;
                   2248:                }
1.346     nicm     2249:                wpo = control_pane_offset(c, wp, &flag);
                   2250:                if (wpo == NULL) {
1.397     nicm     2251:                        if (!flag)
                   2252:                                off = 0;
1.346     nicm     2253:                        continue;
                   2254:                }
                   2255:                if (!flag)
                   2256:                        off = 0;
                   2257:
1.353     nicm     2258:                window_pane_get_new_data(wp, wpo, &new_size);
                   2259:                log_debug("%s: %s has %zu bytes used and %zu left for %%%u",
                   2260:                    __func__, c->name, wpo->used - wp->base_offset, new_size,
                   2261:                    wp->id);
                   2262:                if (wpo->used < minimum)
1.352     nicm     2263:                        minimum = wpo->used;
1.345     nicm     2264:        }
1.346     nicm     2265:        if (attached_clients == 0)
                   2266:                off = 0;
1.345     nicm     2267:        minimum -= wp->base_offset;
                   2268:        if (minimum == 0)
                   2269:                goto out;
                   2270:
                   2271:        /* Drain the buffer. */
1.352     nicm     2272:        log_debug("%s: %%%u has %zu minimum (of %zu) bytes used", __func__,
                   2273:            wp->id, minimum, EVBUFFER_LENGTH(evb));
1.345     nicm     2274:        evbuffer_drain(evb, minimum);
                   2275:
                   2276:        /*
                   2277:         * Adjust the base offset. If it would roll over, all the offsets into
                   2278:         * the buffer need to be adjusted.
                   2279:         */
                   2280:        if (wp->base_offset > SIZE_MAX - minimum) {
                   2281:                log_debug("%s: %%%u base offset has wrapped", __func__, wp->id);
                   2282:                wp->offset.used -= wp->base_offset;
1.352     nicm     2283:                if (wp->pipe_fd != -1)
1.345     nicm     2284:                        wp->pipe_offset.used -= wp->base_offset;
                   2285:                TAILQ_FOREACH(c, &clients, entry) {
                   2286:                        if (c->session == NULL || (~c->flags & CLIENT_CONTROL))
                   2287:                                continue;
1.346     nicm     2288:                        wpo = control_pane_offset(c, wp, &flag);
1.352     nicm     2289:                        if (wpo != NULL && !flag)
1.346     nicm     2290:                                wpo->used -= wp->base_offset;
1.345     nicm     2291:                }
                   2292:                wp->base_offset = minimum;
                   2293:        } else
                   2294:                wp->base_offset += minimum;
                   2295:
                   2296: out:
                   2297:        /*
                   2298:         * If there is data remaining, and there are no clients able to consume
1.351     nicm     2299:         * it, do not read any more. This is true when there are attached
                   2300:         * clients, all of which are control clients which are not able to
                   2301:         * accept any more data.
1.345     nicm     2302:         */
1.352     nicm     2303:        log_debug("%s: pane %%%u is %s", __func__, wp->id, off ? "off" : "on");
1.345     nicm     2304:        if (off)
                   2305:                bufferevent_disable(wp->event, EV_READ);
                   2306:        else
                   2307:                bufferevent_enable(wp->event, EV_READ);
1.91      nicm     2308: }
                   2309:
1.18      nicm     2310: /*
                   2311:  * Update cursor position and mode settings. The scroll region and attributes
                   2312:  * are cleared when idle (waiting for an event) as this is the most likely time
                   2313:  * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
                   2314:  * compromise between excessive resets and likelihood of an interrupt.
                   2315:  *
                   2316:  * tty_region/tty_reset/tty_update_mode already take care of not resetting
                   2317:  * things that are already in their default state.
                   2318:  */
1.190     nicm     2319: static void
1.18      nicm     2320: server_client_reset_state(struct client *c)
1.1       nicm     2321: {
1.332     nicm     2322:        struct tty              *tty = &c->tty;
1.18      nicm     2323:        struct window           *w = c->session->curw->window;
1.341     nicm     2324:        struct window_pane      *wp = server_client_get_pane(c), *loop;
1.336     nicm     2325:        struct screen           *s = NULL;
1.163     nicm     2326:        struct options          *oo = c->session->options;
1.390     nicm     2327:        int                      mode = 0, cursor, flags, n;
1.261     nicm     2328:        u_int                    cx = 0, cy = 0, ox, oy, sx, sy;
1.60      nicm     2329:
1.186     nicm     2330:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86      nicm     2331:                return;
                   2332:
1.332     nicm     2333:        /* Disable the block flag. */
                   2334:        flags = (tty->flags & TTY_BLOCK);
                   2335:        tty->flags &= ~TTY_BLOCK;
                   2336:
1.309     nicm     2337:        /* Get mode from overlay if any, else from screen. */
                   2338:        if (c->overlay_draw != NULL) {
1.336     nicm     2339:                if (c->overlay_mode != NULL)
1.380     nicm     2340:                        s = c->overlay_mode(c, c->overlay_data, &cx, &cy);
1.336     nicm     2341:        } else
1.309     nicm     2342:                s = wp->screen;
1.336     nicm     2343:        if (s != NULL)
1.309     nicm     2344:                mode = s->mode;
1.375     nicm     2345:        if (log_get_level() != 0) {
                   2346:                log_debug("%s: client %s mode %s", __func__, c->name,
                   2347:                    screen_mode_to_string(mode));
                   2348:        }
1.309     nicm     2349:
                   2350:        /* Reset region and margin. */
1.332     nicm     2351:        tty_region_off(tty);
                   2352:        tty_margin_off(tty);
1.1       nicm     2353:
1.261     nicm     2354:        /* Move cursor to pane cursor and offset. */
1.390     nicm     2355:        if (c->prompt_string != NULL) {
                   2356:                n = options_get_number(c->session->options, "status-position");
                   2357:                if (n == 0)
                   2358:                        cy = 0;
                   2359:                else {
                   2360:                        n = status_line_size(c);
                   2361:                        if (n == 0)
                   2362:                                cy = tty->sy - 1;
                   2363:                        else
                   2364:                                cy = tty->sy - n;
                   2365:                }
                   2366:                cx = c->prompt_cursor;
                   2367:                mode &= ~MODE_CURSOR;
                   2368:        } else if (c->overlay_draw == NULL) {
1.309     nicm     2369:                cursor = 0;
1.332     nicm     2370:                tty_window_offset(tty, &ox, &oy, &sx, &sy);
1.309     nicm     2371:                if (wp->xoff + s->cx >= ox && wp->xoff + s->cx <= ox + sx &&
                   2372:                    wp->yoff + s->cy >= oy && wp->yoff + s->cy <= oy + sy) {
                   2373:                        cursor = 1;
1.261     nicm     2374:
1.309     nicm     2375:                        cx = wp->xoff + s->cx - ox;
                   2376:                        cy = wp->yoff + s->cy - oy;
1.261     nicm     2377:
1.309     nicm     2378:                        if (status_at_line(c) == 0)
                   2379:                                cy += status_line_size(c);
                   2380:                }
                   2381:                if (!cursor)
                   2382:                        mode &= ~MODE_CURSOR;
1.261     nicm     2383:        }
1.332     nicm     2384:        log_debug("%s: cursor to %u,%u", __func__, cx, cy);
                   2385:        tty_cursor(tty, cx, cy);
1.1       nicm     2386:
1.50      nicm     2387:        /*
1.131     nicm     2388:         * Set mouse mode if requested. To support dragging, always use button
                   2389:         * mode.
1.57      nicm     2390:         */
1.209     nicm     2391:        if (options_get_number(oo, "mouse")) {
1.309     nicm     2392:                if (c->overlay_draw == NULL) {
1.363     nicm     2393:                        mode &= ~ALL_MOUSE_MODES;
1.309     nicm     2394:                        TAILQ_FOREACH(loop, &w->panes, entry) {
                   2395:                                if (loop->screen->mode & MODE_MOUSE_ALL)
                   2396:                                        mode |= MODE_MOUSE_ALL;
                   2397:                        }
1.209     nicm     2398:                }
                   2399:                if (~mode & MODE_MOUSE_ALL)
                   2400:                        mode |= MODE_MOUSE_BUTTON;
                   2401:        }
1.215     nicm     2402:
                   2403:        /* Clear bracketed paste mode if at the prompt. */
1.309     nicm     2404:        if (c->overlay_draw == NULL && c->prompt_string != NULL)
1.215     nicm     2405:                mode &= ~MODE_BRACKETPASTE;
1.48      nicm     2406:
                   2407:        /* Set the terminal mode and reset attributes. */
1.332     nicm     2408:        tty_update_mode(tty, mode, s);
                   2409:        tty_reset(tty);
1.330     nicm     2410:
1.332     nicm     2411:        /* All writing must be done, send a sync end (if it was started). */
                   2412:        tty_sync_end(tty);
                   2413:        tty->flags |= flags;
1.17      nicm     2414: }
                   2415:
                   2416: /* Repeat time callback. */
1.190     nicm     2417: static void
1.170     nicm     2418: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17      nicm     2419: {
                   2420:        struct client   *c = data;
                   2421:
1.85      nicm     2422:        if (c->flags & CLIENT_REPEAT) {
1.178     nicm     2423:                server_client_set_key_table(c, NULL);
1.132     nicm     2424:                c->flags &= ~CLIENT_REPEAT;
                   2425:                server_status_client(c);
1.85      nicm     2426:        }
1.192     nicm     2427: }
                   2428:
                   2429: /* Double-click callback. */
                   2430: static void
                   2431: server_client_click_timer(__unused int fd, __unused short events, void *data)
                   2432: {
1.306     nicm     2433:        struct client           *c = data;
                   2434:        struct key_event        *event;
                   2435:
                   2436:        log_debug("click timer expired");
1.192     nicm     2437:
1.306     nicm     2438:        if (c->flags & CLIENT_TRIPLECLICK) {
                   2439:                /*
                   2440:                 * Waiting for a third click that hasn't happened, so this must
                   2441:                 * have been a double click.
                   2442:                 */
                   2443:                event = xmalloc(sizeof *event);
                   2444:                event->key = KEYC_DOUBLECLICK;
                   2445:                memcpy(&event->m, &c->click_event, sizeof event->m);
                   2446:                if (!server_client_handle_key(c, event))
                   2447:                        free(event);
                   2448:        }
1.192     nicm     2449:        c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1       nicm     2450: }
                   2451:
1.36      nicm     2452: /* Check if client should be exited. */
1.190     nicm     2453: static void
1.36      nicm     2454: server_client_check_exit(struct client *c)
                   2455: {
1.300     nicm     2456:        struct client_file      *cf;
1.352     nicm     2457:        const char              *name = c->exit_session;
1.357     nicm     2458:        char                    *data;
                   2459:        size_t                   size, msize;
1.300     nicm     2460:
1.354     nicm     2461:        if (c->flags & (CLIENT_DEAD|CLIENT_EXITED))
                   2462:                return;
                   2463:        if (~c->flags & CLIENT_EXIT)
1.36      nicm     2464:                return;
                   2465:
1.352     nicm     2466:        if (c->flags & CLIENT_CONTROL) {
1.355     nicm     2467:                control_discard(c);
1.352     nicm     2468:                if (!control_all_done(c))
                   2469:                        return;
                   2470:        }
1.300     nicm     2471:        RB_FOREACH(cf, client_files, &c->files) {
                   2472:                if (EVBUFFER_LENGTH(cf->buffer) != 0)
                   2473:                        return;
                   2474:        }
1.286     nicm     2475:        c->flags |= CLIENT_EXITED;
1.352     nicm     2476:
                   2477:        switch (c->exit_type) {
                   2478:        case CLIENT_EXIT_RETURN:
1.367     nicm     2479:                if (c->exit_message != NULL)
1.357     nicm     2480:                        msize = strlen(c->exit_message) + 1;
1.367     nicm     2481:                else
                   2482:                        msize = 0;
                   2483:                size = (sizeof c->retval) + msize;
1.357     nicm     2484:                data = xmalloc(size);
                   2485:                memcpy(data, &c->retval, sizeof c->retval);
                   2486:                if (c->exit_message != NULL)
                   2487:                        memcpy(data + sizeof c->retval, c->exit_message, msize);
                   2488:                proc_send(c->peer, MSG_EXIT, -1, data, size);
                   2489:                free(data);
1.352     nicm     2490:                break;
                   2491:        case CLIENT_EXIT_SHUTDOWN:
                   2492:                proc_send(c->peer, MSG_SHUTDOWN, -1, NULL, 0);
                   2493:                break;
                   2494:        case CLIENT_EXIT_DETACH:
                   2495:                proc_send(c->peer, c->exit_msgtype, -1, name, strlen(name) + 1);
                   2496:                break;
                   2497:        }
                   2498:        free(c->exit_session);
1.357     nicm     2499:        free(c->exit_message);
1.38      nicm     2500: }
                   2501:
1.218     nicm     2502: /* Redraw timer callback. */
                   2503: static void
                   2504: server_client_redraw_timer(__unused int fd, __unused short events,
1.299     nicm     2505:     __unused void *data)
1.218     nicm     2506: {
                   2507:        log_debug("redraw timer fired");
1.366     nicm     2508: }
                   2509:
                   2510: /*
                   2511:  * Check if modes need to be updated. Only modes in the current window are
                   2512:  * updated and it is done when the status line is redrawn.
                   2513:  */
                   2514: static void
                   2515: server_client_check_modes(struct client *c)
                   2516: {
                   2517:        struct window                   *w = c->session->curw->window;
                   2518:        struct window_pane              *wp;
                   2519:        struct window_mode_entry        *wme;
                   2520:
                   2521:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
                   2522:                return;
                   2523:        if (~c->flags & CLIENT_REDRAWSTATUS)
                   2524:                return;
                   2525:        TAILQ_FOREACH(wp, &w->panes, entry) {
                   2526:                wme = TAILQ_FIRST(&wp->modes);
                   2527:                if (wme != NULL && wme->mode->update != NULL)
                   2528:                        wme->mode->update(wme);
                   2529:        }
1.218     nicm     2530: }
                   2531:
1.1       nicm     2532: /* Check for client redraws. */
1.190     nicm     2533: static void
1.1       nicm     2534: server_client_check_redraw(struct client *c)
                   2535: {
                   2536:        struct session          *s = c->session;
1.137     nicm     2537:        struct tty              *tty = &c->tty;
1.326     nicm     2538:        struct window           *w = c->session->curw->window;
1.1       nicm     2539:        struct window_pane      *wp;
1.325     nicm     2540:        int                      needed, flags, mode = tty->mode, new_flags = 0;
1.326     nicm     2541:        int                      redraw;
                   2542:        u_int                    bit = 0;
1.218     nicm     2543:        struct timeval           tv = { .tv_usec = 1000 };
                   2544:        static struct event      ev;
                   2545:        size_t                   left;
1.1       nicm     2546:
1.86      nicm     2547:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79      nicm     2548:                return;
1.257     nicm     2549:        if (c->flags & CLIENT_ALLREDRAWFLAGS) {
1.325     nicm     2550:                log_debug("%s: redraw%s%s%s%s%s", c->name,
1.257     nicm     2551:                    (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "",
                   2552:                    (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "",
1.282     nicm     2553:                    (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "",
1.325     nicm     2554:                    (c->flags & CLIENT_REDRAWOVERLAY) ? " overlay" : "",
                   2555:                    (c->flags & CLIENT_REDRAWPANES) ? " panes" : "");
1.257     nicm     2556:        }
1.218     nicm     2557:
                   2558:        /*
                   2559:         * If there is outstanding data, defer the redraw until it has been
                   2560:         * consumed. We can just add a timer to get out of the event loop and
                   2561:         * end up back here.
                   2562:         */
                   2563:        needed = 0;
1.256     nicm     2564:        if (c->flags & CLIENT_ALLREDRAWFLAGS)
1.218     nicm     2565:                needed = 1;
                   2566:        else {
1.326     nicm     2567:                TAILQ_FOREACH(wp, &w->panes, entry) {
1.218     nicm     2568:                        if (wp->flags & PANE_REDRAW) {
                   2569:                                needed = 1;
                   2570:                                break;
                   2571:                        }
                   2572:                }
1.325     nicm     2573:                if (needed)
                   2574:                        new_flags |= CLIENT_REDRAWPANES;
1.218     nicm     2575:        }
1.241     nicm     2576:        if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) {
                   2577:                log_debug("%s: redraw deferred (%zu left)", c->name, left);
                   2578:                if (!evtimer_initialized(&ev))
                   2579:                        evtimer_set(&ev, server_client_redraw_timer, NULL);
                   2580:                if (!evtimer_pending(&ev, NULL)) {
1.218     nicm     2581:                        log_debug("redraw timer started");
                   2582:                        evtimer_add(&ev, &tv);
1.241     nicm     2583:                }
1.327     nicm     2584:
                   2585:                if (~c->flags & CLIENT_REDRAWWINDOW) {
1.326     nicm     2586:                        TAILQ_FOREACH(wp, &w->panes, entry) {
1.327     nicm     2587:                                if (wp->flags & PANE_REDRAW) {
                   2588:                                        log_debug("%s: pane %%%u needs redraw",
                   2589:                                            c->name, wp->id);
1.326     nicm     2590:                                        c->redraw_panes |= (1 << bit);
1.327     nicm     2591:                                }
1.326     nicm     2592:                                if (++bit == 64) {
                   2593:                                        /*
                   2594:                                         * If more that 64 panes, give up and
                   2595:                                         * just redraw the window.
                   2596:                                         */
                   2597:                                        new_flags &= CLIENT_REDRAWPANES;
                   2598:                                        new_flags |= CLIENT_REDRAWWINDOW;
                   2599:                                        break;
                   2600:                                }
                   2601:                        }
1.327     nicm     2602:                        if (c->redraw_panes != 0)
                   2603:                                c->flags |= CLIENT_REDRAWPANES;
1.326     nicm     2604:                }
1.325     nicm     2605:                c->flags |= new_flags;
1.241     nicm     2606:                return;
                   2607:        } else if (needed)
1.218     nicm     2608:                log_debug("%s: redraw needed", c->name);
1.79      nicm     2609:
1.219     nicm     2610:        flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR);
1.326     nicm     2611:        tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE))|TTY_NOCURSOR;
1.137     nicm     2612:
1.256     nicm     2613:        if (~c->flags & CLIENT_REDRAWWINDOW) {
                   2614:                /*
                   2615:                 * If not redrawing the entire window, check whether each pane
                   2616:                 * needs to be redrawn.
                   2617:                 */
1.326     nicm     2618:                TAILQ_FOREACH(wp, &w->panes, entry) {
                   2619:                        redraw = 0;
                   2620:                        if (wp->flags & PANE_REDRAW)
                   2621:                                redraw = 1;
                   2622:                        else if (c->flags & CLIENT_REDRAWPANES)
                   2623:                                redraw = !!(c->redraw_panes & (1 << bit));
1.328     nicm     2624:                        bit++;
1.326     nicm     2625:                        if (!redraw)
                   2626:                                continue;
                   2627:                        log_debug("%s: redrawing pane %%%u", __func__, wp->id);
                   2628:                        screen_redraw_pane(c, wp);
1.1       nicm     2629:                }
1.331     nicm     2630:                c->redraw_panes = 0;
1.325     nicm     2631:                c->flags &= ~CLIENT_REDRAWPANES;
1.1       nicm     2632:        }
                   2633:
1.256     nicm     2634:        if (c->flags & CLIENT_ALLREDRAWFLAGS) {
1.393     nicm     2635:                if (options_get_number(s->options, "set-titles")) {
1.256     nicm     2636:                        server_client_set_title(c);
1.393     nicm     2637:                        server_client_set_path(c);
                   2638:                }
1.256     nicm     2639:                screen_redraw_screen(c);
                   2640:        }
1.1       nicm     2641:
1.326     nicm     2642:        tty->flags = (tty->flags & ~TTY_NOCURSOR)|(flags & TTY_NOCURSOR);
1.320     nicm     2643:        tty_update_mode(tty, mode, NULL);
1.326     nicm     2644:        tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR))|flags;
1.1       nicm     2645:
1.256     nicm     2646:        c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE);
1.230     nicm     2647:
                   2648:        if (needed) {
                   2649:                /*
                   2650:                 * We would have deferred the redraw unless the output buffer
                   2651:                 * was empty, so we can record how many bytes the redraw
                   2652:                 * generated.
                   2653:                 */
                   2654:                c->redraw = EVBUFFER_LENGTH(tty->out);
                   2655:                log_debug("%s: redraw added %zu bytes", c->name, c->redraw);
                   2656:        }
1.1       nicm     2657: }
                   2658:
                   2659: /* Set client title. */
1.190     nicm     2660: static void
1.1       nicm     2661: server_client_set_title(struct client *c)
                   2662: {
1.128     nicm     2663:        struct session          *s = c->session;
                   2664:        const char              *template;
                   2665:        char                    *title;
                   2666:        struct format_tree      *ft;
1.1       nicm     2667:
1.163     nicm     2668:        template = options_get_string(s->options, "set-titles-string");
1.25      nicm     2669:
1.228     nicm     2670:        ft = format_create(c, NULL, FORMAT_NONE, 0);
1.128     nicm     2671:        format_defaults(ft, c, NULL, NULL, NULL);
                   2672:
1.269     nicm     2673:        title = format_expand_time(ft, template);
1.1       nicm     2674:        if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77      nicm     2675:                free(c->title);
1.1       nicm     2676:                c->title = xstrdup(title);
                   2677:                tty_set_title(&c->tty, c->title);
                   2678:        }
1.77      nicm     2679:        free(title);
1.128     nicm     2680:
                   2681:        format_free(ft);
1.393     nicm     2682: }
                   2683:
                   2684: /* Set client path. */
                   2685: static void
                   2686: server_client_set_path(struct client *c)
                   2687: {
                   2688:        struct session  *s = c->session;
                   2689:        const char      *path;
                   2690:
                   2691:        if (s->curw == NULL)
                   2692:                return;
                   2693:        if (s->curw->window->active->base.path == NULL)
                   2694:                path = "";
                   2695:        else
                   2696:                path = s->curw->window->active->base.path;
                   2697:        if (c->path == NULL || strcmp(path, c->path) != 0) {
                   2698:                free(c->path);
                   2699:                c->path = xstrdup(path);
                   2700:                tty_set_path(&c->tty, c->path);
                   2701:        }
1.1       nicm     2702: }
                   2703:
                   2704: /* Dispatch message from client. */
1.190     nicm     2705: static void
1.162     nicm     2706: server_client_dispatch(struct imsg *imsg, void *arg)
1.1       nicm     2707: {
1.300     nicm     2708:        struct client   *c = arg;
                   2709:        ssize_t          datalen;
                   2710:        struct session  *s;
1.1       nicm     2711:
1.162     nicm     2712:        if (c->flags & CLIENT_DEAD)
                   2713:                return;
                   2714:
                   2715:        if (imsg == NULL) {
                   2716:                server_client_lost(c);
                   2717:                return;
                   2718:        }
1.1       nicm     2719:
1.162     nicm     2720:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1       nicm     2721:
1.162     nicm     2722:        switch (imsg->hdr.type) {
1.370     nicm     2723:        case MSG_IDENTIFY_CLIENTPID:
                   2724:        case MSG_IDENTIFY_CWD:
                   2725:        case MSG_IDENTIFY_ENVIRON:
1.329     nicm     2726:        case MSG_IDENTIFY_FEATURES:
1.162     nicm     2727:        case MSG_IDENTIFY_FLAGS:
1.361     nicm     2728:        case MSG_IDENTIFY_LONGFLAGS:
1.370     nicm     2729:        case MSG_IDENTIFY_STDIN:
                   2730:        case MSG_IDENTIFY_STDOUT:
1.162     nicm     2731:        case MSG_IDENTIFY_TERM:
1.370     nicm     2732:        case MSG_IDENTIFY_TERMINFO:
1.162     nicm     2733:        case MSG_IDENTIFY_TTYNAME:
                   2734:        case MSG_IDENTIFY_DONE:
                   2735:                server_client_dispatch_identify(c, imsg);
                   2736:                break;
                   2737:        case MSG_COMMAND:
                   2738:                server_client_dispatch_command(c, imsg);
                   2739:                break;
                   2740:        case MSG_RESIZE:
                   2741:                if (datalen != 0)
                   2742:                        fatalx("bad MSG_RESIZE size");
1.1       nicm     2743:
1.162     nicm     2744:                if (c->flags & CLIENT_CONTROL)
1.1       nicm     2745:                        break;
1.295     nicm     2746:                server_client_update_latest(c);
1.236     nicm     2747:                tty_resize(&c->tty);
                   2748:                recalculate_sizes();
1.376     nicm     2749:                if (c->overlay_resize == NULL)
                   2750:                        server_client_clear_overlay(c);
                   2751:                else
1.380     nicm     2752:                        c->overlay_resize(c, c->overlay_data);
1.236     nicm     2753:                server_redraw_client(c);
1.174     nicm     2754:                if (c->session != NULL)
1.196     nicm     2755:                        notify_client("client-resized", c);
1.162     nicm     2756:                break;
                   2757:        case MSG_EXITING:
                   2758:                if (datalen != 0)
                   2759:                        fatalx("bad MSG_EXITING size");
1.379     nicm     2760:                server_client_set_session(c, NULL);
1.387     nicm     2761:                recalculate_sizes();
1.162     nicm     2762:                tty_close(&c->tty);
                   2763:                proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
                   2764:                break;
                   2765:        case MSG_WAKEUP:
                   2766:        case MSG_UNLOCK:
                   2767:                if (datalen != 0)
                   2768:                        fatalx("bad MSG_WAKEUP size");
1.121     nicm     2769:
1.162     nicm     2770:                if (!(c->flags & CLIENT_SUSPENDED))
                   2771:                        break;
                   2772:                c->flags &= ~CLIENT_SUSPENDED;
1.10      nicm     2773:
1.365     nicm     2774:                if (c->fd == -1 || c->session == NULL) /* exited already */
1.1       nicm     2775:                        break;
1.162     nicm     2776:                s = c->session;
1.1       nicm     2777:
1.162     nicm     2778:                if (gettimeofday(&c->activity_time, NULL) != 0)
                   2779:                        fatal("gettimeofday failed");
                   2780:
                   2781:                tty_start_tty(&c->tty);
                   2782:                server_redraw_client(c);
                   2783:                recalculate_sizes();
1.184     nicm     2784:
                   2785:                if (s != NULL)
                   2786:                        session_update_activity(s, &c->activity_time);
1.162     nicm     2787:                break;
                   2788:        case MSG_SHELL:
                   2789:                if (datalen != 0)
                   2790:                        fatalx("bad MSG_SHELL size");
1.1       nicm     2791:
1.162     nicm     2792:                server_client_dispatch_shell(c);
                   2793:                break;
1.300     nicm     2794:        case MSG_WRITE_READY:
1.369     nicm     2795:                file_write_ready(&c->files, imsg);
1.300     nicm     2796:                break;
                   2797:        case MSG_READ:
1.369     nicm     2798:                file_read_data(&c->files, imsg);
1.300     nicm     2799:                break;
                   2800:        case MSG_READ_DONE:
1.369     nicm     2801:                file_read_done(&c->files, imsg);
1.300     nicm     2802:                break;
1.1       nicm     2803:        }
                   2804: }
                   2805:
1.394     nicm     2806: /* Callback when command is not allowed. */
                   2807: static enum cmd_retval
                   2808: server_client_read_only(struct cmdq_item *item, __unused void *data)
                   2809: {
                   2810:        cmdq_error(item, "client is read-only");
                   2811:        return (CMD_RETURN_ERROR);
                   2812: }
                   2813:
1.194     nicm     2814: /* Callback when command is done. */
                   2815: static enum cmd_retval
1.195     nicm     2816: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194     nicm     2817: {
1.316     nicm     2818:        struct client   *c = cmdq_get_client(item);
1.194     nicm     2819:
                   2820:        if (~c->flags & CLIENT_ATTACHED)
                   2821:                c->flags |= CLIENT_EXIT;
1.396     nicm     2822:        else if (~c->flags & CLIENT_EXIT) {
                   2823:                if (c->flags & CLIENT_CONTROL)
                   2824:                        control_ready(c);
1.314     nicm     2825:                tty_send_requests(&c->tty);
1.396     nicm     2826:        }
1.194     nicm     2827:        return (CMD_RETURN_NORMAL);
                   2828: }
                   2829:
1.1       nicm     2830: /* Handle command message. */
1.190     nicm     2831: static void
1.162     nicm     2832: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1       nicm     2833: {
1.300     nicm     2834:        struct msg_command        data;
1.108     nicm     2835:        char                     *buf;
                   2836:        size_t                    len;
                   2837:        int                       argc;
                   2838:        char                    **argv, *cause;
1.285     nicm     2839:        struct cmd_parse_result  *pr;
1.386     nicm     2840:        struct args_value        *values;
1.394     nicm     2841:        struct cmdq_item         *new_item;
1.246     nicm     2842:
                   2843:        if (c->flags & CLIENT_EXIT)
                   2844:                return;
1.108     nicm     2845:
                   2846:        if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
                   2847:                fatalx("bad MSG_COMMAND size");
                   2848:        memcpy(&data, imsg->data, sizeof data);
                   2849:
1.124     nicm     2850:        buf = (char *)imsg->data + sizeof data;
1.108     nicm     2851:        len = imsg->hdr.len  - IMSG_HEADER_SIZE - sizeof data;
                   2852:        if (len > 0 && buf[len - 1] != '\0')
                   2853:                fatalx("bad MSG_COMMAND string");
                   2854:
                   2855:        argc = data.argc;
                   2856:        if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194     nicm     2857:                cause = xstrdup("command too long");
1.1       nicm     2858:                goto error;
                   2859:        }
                   2860:
                   2861:        if (argc == 0) {
                   2862:                argc = 1;
                   2863:                argv = xcalloc(1, sizeof *argv);
                   2864:                *argv = xstrdup("new-session");
                   2865:        }
                   2866:
1.386     nicm     2867:        values = args_from_vector(argc, argv);
                   2868:        pr = cmd_parse_from_arguments(values, argc, NULL);
1.285     nicm     2869:        switch (pr->status) {
                   2870:        case CMD_PARSE_ERROR:
                   2871:                cause = pr->error;
                   2872:                goto error;
                   2873:        case CMD_PARSE_SUCCESS:
                   2874:                break;
1.1       nicm     2875:        }
1.386     nicm     2876:        args_free_values(values, argc);
                   2877:        free(values);
1.1       nicm     2878:        cmd_free_argv(argc, argv);
                   2879:
1.394     nicm     2880:        if ((c->flags & CLIENT_READONLY) &&
                   2881:            !cmd_list_all_have(pr->cmdlist, CMD_READONLY))
                   2882:                new_item = cmdq_get_callback(server_client_read_only, NULL);
                   2883:        else
                   2884:                new_item = cmdq_get_command(pr->cmdlist, NULL);
                   2885:        cmdq_append(c, new_item);
1.194     nicm     2886:        cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.285     nicm     2887:
                   2888:        cmd_list_free(pr->cmdlist);
1.1       nicm     2889:        return;
                   2890:
                   2891: error:
1.285     nicm     2892:        cmd_free_argv(argc, argv);
                   2893:
1.284     nicm     2894:        cmdq_append(c, cmdq_get_error(cause));
                   2895:        free(cause);
1.88      nicm     2896:
1.36      nicm     2897:        c->flags |= CLIENT_EXIT;
1.1       nicm     2898: }
                   2899:
                   2900: /* Handle identify message. */
1.190     nicm     2901: static void
1.162     nicm     2902: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1       nicm     2903: {
1.165     nicm     2904:        const char      *data, *home;
1.232     nicm     2905:        size_t           datalen;
1.329     nicm     2906:        int              flags, feat;
1.361     nicm     2907:        uint64_t         longflags;
1.216     nicm     2908:        char            *name;
1.109     nicm     2909:
                   2910:        if (c->flags & CLIENT_IDENTIFIED)
                   2911:                fatalx("out-of-order identify message");
                   2912:
                   2913:        data = imsg->data;
                   2914:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
                   2915:
                   2916:        switch (imsg->hdr.type) {
1.329     nicm     2917:        case MSG_IDENTIFY_FEATURES:
                   2918:                if (datalen != sizeof feat)
                   2919:                        fatalx("bad MSG_IDENTIFY_FEATURES size");
                   2920:                memcpy(&feat, data, sizeof feat);
                   2921:                c->term_features |= feat;
                   2922:                log_debug("client %p IDENTIFY_FEATURES %s", c,
                   2923:                    tty_get_features(feat));
                   2924:                break;
1.109     nicm     2925:        case MSG_IDENTIFY_FLAGS:
                   2926:                if (datalen != sizeof flags)
                   2927:                        fatalx("bad MSG_IDENTIFY_FLAGS size");
                   2928:                memcpy(&flags, data, sizeof flags);
                   2929:                c->flags |= flags;
1.158     nicm     2930:                log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.361     nicm     2931:                break;
                   2932:        case MSG_IDENTIFY_LONGFLAGS:
                   2933:                if (datalen != sizeof longflags)
                   2934:                        fatalx("bad MSG_IDENTIFY_LONGFLAGS size");
                   2935:                memcpy(&longflags, data, sizeof longflags);
                   2936:                c->flags |= longflags;
                   2937:                log_debug("client %p IDENTIFY_LONGFLAGS %#llx", c,
                   2938:                    (unsigned long long)longflags);
1.109     nicm     2939:                break;
                   2940:        case MSG_IDENTIFY_TERM:
1.110     nicm     2941:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     2942:                        fatalx("bad MSG_IDENTIFY_TERM string");
1.329     nicm     2943:                if (*data == '\0')
                   2944:                        c->term_name = xstrdup("unknown");
                   2945:                else
                   2946:                        c->term_name = xstrdup(data);
1.158     nicm     2947:                log_debug("client %p IDENTIFY_TERM %s", c, data);
1.370     nicm     2948:                break;
                   2949:        case MSG_IDENTIFY_TERMINFO:
                   2950:                if (datalen == 0 || data[datalen - 1] != '\0')
                   2951:                        fatalx("bad MSG_IDENTIFY_TERMINFO string");
                   2952:                c->term_caps = xreallocarray(c->term_caps, c->term_ncaps + 1,
                   2953:                    sizeof *c->term_caps);
                   2954:                c->term_caps[c->term_ncaps++] = xstrdup(data);
                   2955:                log_debug("client %p IDENTIFY_TERMINFO %s", c, data);
1.109     nicm     2956:                break;
                   2957:        case MSG_IDENTIFY_TTYNAME:
1.110     nicm     2958:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     2959:                        fatalx("bad MSG_IDENTIFY_TTYNAME string");
                   2960:                c->ttyname = xstrdup(data);
1.158     nicm     2961:                log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109     nicm     2962:                break;
                   2963:        case MSG_IDENTIFY_CWD:
1.155     nicm     2964:                if (datalen == 0 || data[datalen - 1] != '\0')
                   2965:                        fatalx("bad MSG_IDENTIFY_CWD string");
1.165     nicm     2966:                if (access(data, X_OK) == 0)
                   2967:                        c->cwd = xstrdup(data);
                   2968:                else if ((home = find_home()) != NULL)
                   2969:                        c->cwd = xstrdup(home);
                   2970:                else
                   2971:                        c->cwd = xstrdup("/");
1.158     nicm     2972:                log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109     nicm     2973:                break;
                   2974:        case MSG_IDENTIFY_STDIN:
                   2975:                if (datalen != 0)
                   2976:                        fatalx("bad MSG_IDENTIFY_STDIN size");
                   2977:                c->fd = imsg->fd;
1.158     nicm     2978:                log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109     nicm     2979:                break;
1.351     nicm     2980:        case MSG_IDENTIFY_STDOUT:
                   2981:                if (datalen != 0)
                   2982:                        fatalx("bad MSG_IDENTIFY_STDOUT size");
                   2983:                c->out_fd = imsg->fd;
                   2984:                log_debug("client %p IDENTIFY_STDOUT %d", c, imsg->fd);
                   2985:                break;
1.109     nicm     2986:        case MSG_IDENTIFY_ENVIRON:
1.110     nicm     2987:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     2988:                        fatalx("bad MSG_IDENTIFY_ENVIRON string");
                   2989:                if (strchr(data, '=') != NULL)
1.312     nicm     2990:                        environ_put(c->environ, data, 0);
1.158     nicm     2991:                log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143     nicm     2992:                break;
                   2993:        case MSG_IDENTIFY_CLIENTPID:
                   2994:                if (datalen != sizeof c->pid)
                   2995:                        fatalx("bad MSG_IDENTIFY_CLIENTPID size");
                   2996:                memcpy(&c->pid, data, sizeof c->pid);
1.158     nicm     2997:                log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109     nicm     2998:                break;
                   2999:        default:
                   3000:                break;
                   3001:        }
                   3002:
                   3003:        if (imsg->hdr.type != MSG_IDENTIFY_DONE)
                   3004:                return;
                   3005:        c->flags |= CLIENT_IDENTIFIED;
1.75      nicm     3006:
1.216     nicm     3007:        if (*c->ttyname != '\0')
                   3008:                name = xstrdup(c->ttyname);
                   3009:        else
                   3010:                xasprintf(&name, "client-%ld", (long)c->pid);
                   3011:        c->name = name;
                   3012:        log_debug("client %p name is %s", c, c->name);
                   3013:
1.362     nicm     3014:        if (c->flags & CLIENT_CONTROL)
1.300     nicm     3015:                control_start(c);
1.351     nicm     3016:        else if (c->fd != -1) {
1.348     nicm     3017:                if (tty_init(&c->tty, c) != 0) {
1.288     nicm     3018:                        close(c->fd);
                   3019:                        c->fd = -1;
                   3020:                } else {
                   3021:                        tty_resize(&c->tty);
                   3022:                        c->flags |= CLIENT_TERMINAL;
                   3023:                }
1.351     nicm     3024:                close(c->out_fd);
                   3025:                c->out_fd = -1;
1.75      nicm     3026:        }
1.1       nicm     3027:
1.288     nicm     3028:        /*
1.362     nicm     3029:         * If this is the first client, load configuration files. Any later
                   3030:         * clients are allowed to continue with their command even if the
                   3031:         * config has not been loaded - they might have been run from inside it
1.288     nicm     3032:         */
                   3033:        if ((~c->flags & CLIENT_EXIT) &&
1.362     nicm     3034:             !cfg_finished &&
                   3035:             c == TAILQ_FIRST(&clients))
1.288     nicm     3036:                start_cfg();
1.1       nicm     3037: }
                   3038:
                   3039: /* Handle shell message. */
1.190     nicm     3040: static void
1.162     nicm     3041: server_client_dispatch_shell(struct client *c)
1.1       nicm     3042: {
1.107     nicm     3043:        const char      *shell;
1.25      nicm     3044:
1.163     nicm     3045:        shell = options_get_string(global_s_options, "default-shell");
1.308     nicm     3046:        if (!checkshell(shell))
1.1       nicm     3047:                shell = _PATH_BSHELL;
1.240     nicm     3048:        proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1);
1.25      nicm     3049:
1.162     nicm     3050:        proc_kill_peer(c->peer);
1.213     nicm     3051: }
                   3052:
                   3053: /* Get client working directory. */
                   3054: const char *
1.250     nicm     3055: server_client_get_cwd(struct client *c, struct session *s)
1.213     nicm     3056: {
1.250     nicm     3057:        const char      *home;
1.213     nicm     3058:
1.265     nicm     3059:        if (!cfg_finished && cfg_client != NULL)
                   3060:                return (cfg_client->cwd);
1.213     nicm     3061:        if (c != NULL && c->session == NULL && c->cwd != NULL)
                   3062:                return (c->cwd);
1.250     nicm     3063:        if (s != NULL && s->cwd != NULL)
                   3064:                return (s->cwd);
1.213     nicm     3065:        if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
                   3066:                return (s->cwd);
1.250     nicm     3067:        if ((home = find_home()) != NULL)
                   3068:                return (home);
                   3069:        return ("/");
1.337     nicm     3070: }
                   3071:
1.355     nicm     3072: /* Get control client flags. */
                   3073: static uint64_t
                   3074: server_client_control_flags(struct client *c, const char *next)
                   3075: {
                   3076:        if (strcmp(next, "pause-after") == 0) {
                   3077:                c->pause_age = 0;
                   3078:                return (CLIENT_CONTROL_PAUSEAFTER);
                   3079:        }
                   3080:        if (sscanf(next, "pause-after=%u", &c->pause_age) == 1) {
                   3081:                c->pause_age *= 1000;
                   3082:                return (CLIENT_CONTROL_PAUSEAFTER);
                   3083:        }
                   3084:        if (strcmp(next, "no-output") == 0)
                   3085:                return (CLIENT_CONTROL_NOOUTPUT);
1.358     nicm     3086:        if (strcmp(next, "wait-exit") == 0)
                   3087:                return (CLIENT_CONTROL_WAITEXIT);
1.355     nicm     3088:        return (0);
                   3089: }
                   3090:
1.337     nicm     3091: /* Set client flags. */
                   3092: void
                   3093: server_client_set_flags(struct client *c, const char *flags)
                   3094: {
                   3095:        char    *s, *copy, *next;
1.351     nicm     3096:        uint64_t flag;
                   3097:        int      not;
1.337     nicm     3098:
1.381     nicm     3099:        s = copy = xstrdup(flags);
1.337     nicm     3100:        while ((next = strsep(&s, ",")) != NULL) {
                   3101:                not = (*next == '!');
                   3102:                if (not)
                   3103:                        next++;
                   3104:
1.355     nicm     3105:                if (c->flags & CLIENT_CONTROL)
                   3106:                        flag = server_client_control_flags(c, next);
                   3107:                else
                   3108:                        flag = 0;
1.350     nicm     3109:                if (strcmp(next, "read-only") == 0)
1.337     nicm     3110:                        flag = CLIENT_READONLY;
                   3111:                else if (strcmp(next, "ignore-size") == 0)
                   3112:                        flag = CLIENT_IGNORESIZE;
1.341     nicm     3113:                else if (strcmp(next, "active-pane") == 0)
                   3114:                        flag = CLIENT_ACTIVEPANE;
1.350     nicm     3115:                if (flag == 0)
1.337     nicm     3116:                        continue;
                   3117:
                   3118:                log_debug("client %s set flag %s", c->name, next);
1.394     nicm     3119:                if (not) {
                   3120:                        if (c->flags & CLIENT_READONLY)
                   3121:                                flag &= ~CLIENT_READONLY;
1.337     nicm     3122:                        c->flags &= ~flag;
1.394     nicm     3123:                } else
1.337     nicm     3124:                        c->flags |= flag;
1.346     nicm     3125:                if (flag == CLIENT_CONTROL_NOOUTPUT)
1.352     nicm     3126:                        control_reset_offsets(c);
1.337     nicm     3127:        }
                   3128:        free(copy);
1.358     nicm     3129:        proc_send(c->peer, MSG_FLAGS, -1, &c->flags, sizeof c->flags);
1.337     nicm     3130: }
                   3131:
1.340     nicm     3132: /* Get client flags. This is only flags useful to show to users. */
1.337     nicm     3133: const char *
                   3134: server_client_get_flags(struct client *c)
                   3135: {
1.355     nicm     3136:        static char     s[256];
                   3137:        char            tmp[32];
1.337     nicm     3138:
                   3139:        *s = '\0';
                   3140:        if (c->flags & CLIENT_ATTACHED)
                   3141:                strlcat(s, "attached,", sizeof s);
1.368     nicm     3142:        if (c->flags & CLIENT_FOCUSED)
                   3143:                strlcat(s, "focused,", sizeof s);
1.337     nicm     3144:        if (c->flags & CLIENT_CONTROL)
                   3145:                strlcat(s, "control-mode,", sizeof s);
                   3146:        if (c->flags & CLIENT_IGNORESIZE)
                   3147:                strlcat(s, "ignore-size,", sizeof s);
                   3148:        if (c->flags & CLIENT_CONTROL_NOOUTPUT)
                   3149:                strlcat(s, "no-output,", sizeof s);
1.358     nicm     3150:        if (c->flags & CLIENT_CONTROL_WAITEXIT)
                   3151:                strlcat(s, "wait-exit,", sizeof s);
1.355     nicm     3152:        if (c->flags & CLIENT_CONTROL_PAUSEAFTER) {
                   3153:                xsnprintf(tmp, sizeof tmp, "pause-after=%u,",
                   3154:                    c->pause_age / 1000);
                   3155:                strlcat(s, tmp, sizeof s);
                   3156:        }
1.337     nicm     3157:        if (c->flags & CLIENT_READONLY)
                   3158:                strlcat(s, "read-only,", sizeof s);
1.341     nicm     3159:        if (c->flags & CLIENT_ACTIVEPANE)
                   3160:                strlcat(s, "active-pane,", sizeof s);
1.337     nicm     3161:        if (c->flags & CLIENT_SUSPENDED)
                   3162:                strlcat(s, "suspended,", sizeof s);
                   3163:        if (c->flags & CLIENT_UTF8)
                   3164:                strlcat(s, "UTF-8,", sizeof s);
                   3165:        if (*s != '\0')
                   3166:                s[strlen(s) - 1] = '\0';
                   3167:        return (s);
1.341     nicm     3168: }
                   3169:
                   3170: /* Get client window. */
1.385     nicm     3171: struct client_window *
1.341     nicm     3172: server_client_get_client_window(struct client *c, u_int id)
                   3173: {
                   3174:        struct client_window    cw = { .window = id };
                   3175:
                   3176:        return (RB_FIND(client_windows, &c->windows, &cw));
                   3177: }
                   3178:
1.385     nicm     3179: /* Add client window. */
                   3180: struct client_window *
                   3181: server_client_add_client_window(struct client *c, u_int id)
                   3182: {
                   3183:        struct client_window    *cw;
                   3184:
                   3185:        cw = server_client_get_client_window(c, id);
                   3186:        if (cw == NULL) {
                   3187:                cw = xcalloc(1, sizeof *cw);
                   3188:                cw->window = id;
                   3189:                RB_INSERT(client_windows, &c->windows, cw);
                   3190:        }
1.395     nicm     3191:        return (cw);
1.385     nicm     3192: }
                   3193:
1.341     nicm     3194: /* Get client active pane. */
                   3195: struct window_pane *
                   3196: server_client_get_pane(struct client *c)
                   3197: {
                   3198:        struct session          *s = c->session;
                   3199:        struct client_window    *cw;
                   3200:
                   3201:        if (s == NULL)
                   3202:                return (NULL);
                   3203:
                   3204:        if (~c->flags & CLIENT_ACTIVEPANE)
                   3205:                return (s->curw->window->active);
                   3206:        cw = server_client_get_client_window(c, s->curw->window->id);
                   3207:        if (cw == NULL)
                   3208:                return (s->curw->window->active);
                   3209:        return (cw->pane);
                   3210: }
                   3211:
                   3212: /* Set client active pane. */
                   3213: void
                   3214: server_client_set_pane(struct client *c, struct window_pane *wp)
                   3215: {
                   3216:        struct session          *s = c->session;
                   3217:        struct client_window    *cw;
                   3218:
                   3219:        if (s == NULL)
                   3220:                return;
                   3221:
1.385     nicm     3222:        cw = server_client_add_client_window(c, s->curw->window->id);
1.341     nicm     3223:        cw->pane = wp;
                   3224:        log_debug("%s pane now %%%u", c->name, wp->id);
                   3225: }
                   3226:
                   3227: /* Remove pane from client lists. */
                   3228: void
                   3229: server_client_remove_pane(struct window_pane *wp)
                   3230: {
                   3231:        struct client           *c;
                   3232:        struct window           *w = wp->window;
                   3233:        struct client_window    *cw;
                   3234:
                   3235:        TAILQ_FOREACH(c, &clients, entry) {
                   3236:                cw = server_client_get_client_window(c, w->id);
                   3237:                if (cw != NULL && cw->pane == wp) {
                   3238:                        RB_REMOVE(client_windows, &c->windows, cw);
                   3239:                        free(cw);
                   3240:                }
                   3241:        }
1.400   ! nicm     3242: }
        !          3243:
        !          3244: /* Print to a client. */
        !          3245: void
        !          3246: server_client_print(struct client *c, int parse, struct evbuffer *evb)
        !          3247: {
        !          3248:        void                            *data = EVBUFFER_DATA(evb);
        !          3249:        size_t                           size = EVBUFFER_LENGTH(evb);
        !          3250:        struct window_pane              *wp;
        !          3251:        struct window_mode_entry        *wme;
        !          3252:        char                            *sanitized, *msg, *line;
        !          3253:
        !          3254:        if (!parse) {
        !          3255:                utf8_stravisx(&msg, data, size,
        !          3256:                    VIS_OCTAL|VIS_CSTYLE|VIS_NOSLASH);
        !          3257:                log_debug("%s: %s", __func__, msg);
        !          3258:        } else {
        !          3259:                msg = EVBUFFER_DATA(evb);
        !          3260:                if (msg[size - 1] != '\0')
        !          3261:                        evbuffer_add(evb, "", 1);
        !          3262:        }
        !          3263:
        !          3264:        if (c == NULL)
        !          3265:                goto out;
        !          3266:
        !          3267:        if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
        !          3268:                if (~c->flags & CLIENT_UTF8) {
        !          3269:                        sanitized = utf8_sanitize(msg);
        !          3270:                        if (c->flags & CLIENT_CONTROL)
        !          3271:                                control_write(c, "%s", sanitized);
        !          3272:                        else
        !          3273:                                file_print(c, "%s\n", sanitized);
        !          3274:                        free(sanitized);
        !          3275:                } else {
        !          3276:                        if (c->flags & CLIENT_CONTROL)
        !          3277:                                control_write(c, "%s", msg);
        !          3278:                        else
        !          3279:                                file_print(c, "%s\n", msg);
        !          3280:                }
        !          3281:                goto out;
        !          3282:        }
        !          3283:
        !          3284:        wp = server_client_get_pane(c);
        !          3285:        wme = TAILQ_FIRST(&wp->modes);
        !          3286:        if (wme == NULL || wme->mode != &window_view_mode)
        !          3287:                window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
        !          3288:        if (parse) {
        !          3289:                do {
        !          3290:                        line = evbuffer_readln(evb, NULL, EVBUFFER_EOL_LF);
        !          3291:                        if (line != NULL) {
        !          3292:                                window_copy_add(wp, 1, "%s", line);
        !          3293:                                free(line);
        !          3294:                        }
        !          3295:                } while (line != NULL);
        !          3296:
        !          3297:                size = EVBUFFER_LENGTH(evb);
        !          3298:                if (size != 0) {
        !          3299:                        line = EVBUFFER_DATA(evb);
        !          3300:                        window_copy_add(wp, 1, "%.*s", (int)size, line);
        !          3301:                }
        !          3302:        } else
        !          3303:                window_copy_add(wp, 0, "%s", msg);
        !          3304:
        !          3305: out:
        !          3306:        if (!parse)
        !          3307:                free(msg);
1.1       nicm     3308: }