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

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