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

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