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

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