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

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