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

1.261   ! nicm        1: /* $OpenBSD: server-client.c,v 1.260 2018/09/11 06:37:54 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 *);
                     36: static void    server_client_check_focus(struct window_pane *);
                     37: static void    server_client_check_resize(struct window_pane *);
                     38: static key_code        server_client_check_mouse(struct client *);
                     39: static void    server_client_repeat_timer(int, short, void *);
1.192     nicm       40: static void    server_client_click_timer(int, short, void *);
1.190     nicm       41: static void    server_client_check_exit(struct client *);
                     42: static void    server_client_check_redraw(struct client *);
                     43: static void    server_client_set_title(struct client *);
                     44: static void    server_client_reset_state(struct client *);
                     45: static int     server_client_assume_paste(struct session *);
1.254     nicm       46: static void    server_client_clear_identify(struct client *,
                     47:                    struct window_pane *);
1.190     nicm       48:
                     49: static void    server_client_dispatch(struct imsg *, void *);
                     50: static void    server_client_dispatch_command(struct client *, struct imsg *);
                     51: static void    server_client_dispatch_identify(struct client *, struct imsg *);
                     52: static void    server_client_dispatch_shell(struct client *);
1.233     nicm       53:
                     54: /* Number of attached clients. */
                     55: u_int
                     56: server_client_how_many(void)
                     57: {
                     58:        struct client   *c;
                     59:        u_int            n;
                     60:
                     61:        n = 0;
                     62:        TAILQ_FOREACH(c, &clients, entry) {
                     63:                if (c->session != NULL && (~c->flags & CLIENT_DETACHING))
                     64:                        n++;
                     65:        }
                     66:        return (n);
                     67: }
1.140     nicm       68:
1.220     nicm       69: /* Identify mode callback. */
1.214     nicm       70: static void
1.232     nicm       71: server_client_callback_identify(__unused int fd, __unused short events,
                     72:     void *data)
1.214     nicm       73: {
                     74:        server_client_clear_identify(data, NULL);
                     75: }
                     76:
                     77: /* Set identify mode on client. */
                     78: void
1.242     nicm       79: server_client_set_identify(struct client *c, u_int delay)
1.214     nicm       80: {
                     81:        struct timeval  tv;
                     82:
                     83:        tv.tv_sec = delay / 1000;
                     84:        tv.tv_usec = (delay % 1000) * 1000L;
                     85:
                     86:        if (event_initialized(&c->identify_timer))
                     87:                evtimer_del(&c->identify_timer);
                     88:        evtimer_set(&c->identify_timer, server_client_callback_identify, c);
1.242     nicm       89:        if (delay != 0)
                     90:                evtimer_add(&c->identify_timer, &tv);
1.214     nicm       91:
                     92:        c->flags |= CLIENT_IDENTIFY;
                     93:        c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
                     94:        server_redraw_client(c);
                     95: }
                     96:
                     97: /* Clear identify mode on client. */
1.254     nicm       98: static void
1.214     nicm       99: server_client_clear_identify(struct client *c, struct window_pane *wp)
                    100: {
                    101:        if (~c->flags & CLIENT_IDENTIFY)
                    102:                return;
                    103:        c->flags &= ~CLIENT_IDENTIFY;
                    104:
                    105:        if (c->identify_callback != NULL)
                    106:                c->identify_callback(c, wp);
                    107:
                    108:        c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
                    109:        server_redraw_client(c);
                    110: }
                    111:
1.140     nicm      112: /* Check if this client is inside this server. */
                    113: int
                    114: server_client_check_nested(struct client *c)
                    115: {
                    116:        struct environ_entry    *envent;
                    117:        struct window_pane      *wp;
                    118:
1.164     nicm      119:        envent = environ_find(c->environ, "TMUX");
1.140     nicm      120:        if (envent == NULL || *envent->value == '\0')
                    121:                return (0);
                    122:
                    123:        RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
1.216     nicm      124:                if (strcmp(wp->tty, c->ttyname) == 0)
1.140     nicm      125:                        return (1);
                    126:        }
                    127:        return (0);
                    128: }
1.1       nicm      129:
1.132     nicm      130: /* Set client key table. */
                    131: void
1.178     nicm      132: server_client_set_key_table(struct client *c, const char *name)
1.132     nicm      133: {
1.178     nicm      134:        if (name == NULL)
                    135:                name = server_client_get_key_table(c);
                    136:
1.132     nicm      137:        key_bindings_unref_table(c->keytable);
                    138:        c->keytable = key_bindings_get_table(name, 1);
                    139:        c->keytable->references++;
                    140: }
                    141:
1.178     nicm      142: /* Get default key table. */
                    143: const char *
                    144: server_client_get_key_table(struct client *c)
                    145: {
                    146:        struct session  *s = c->session;
                    147:        const char      *name;
                    148:
                    149:        if (s == NULL)
                    150:                return ("root");
                    151:
                    152:        name = options_get_string(s->options, "key-table");
                    153:        if (*name == '\0')
                    154:                return ("root");
                    155:        return (name);
                    156: }
                    157:
1.223     nicm      158: /* Is this table the default key table? */
                    159: static int
                    160: server_client_is_default_key_table(struct client *c, struct key_table *table)
1.191     nicm      161: {
1.223     nicm      162:        return (strcmp(table->name, server_client_get_key_table(c)) == 0);
1.191     nicm      163: }
                    164:
1.1       nicm      165: /* Create a new client. */
1.246     nicm      166: struct client *
1.1       nicm      167: server_client_create(int fd)
                    168: {
                    169:        struct client   *c;
                    170:
1.49      nicm      171:        setblocking(fd, 0);
1.1       nicm      172:
                    173:        c = xcalloc(1, sizeof *c);
1.141     nicm      174:        c->references = 1;
1.162     nicm      175:        c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c);
1.25      nicm      176:
1.10      nicm      177:        if (gettimeofday(&c->creation_time, NULL) != 0)
1.1       nicm      178:                fatal("gettimeofday failed");
1.11      nicm      179:        memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.111     nicm      180:
1.164     nicm      181:        c->environ = environ_create();
1.1       nicm      182:
1.146     nicm      183:        c->fd = -1;
1.165     nicm      184:        c->cwd = NULL;
1.145     nicm      185:
1.194     nicm      186:        TAILQ_INIT(&c->queue);
1.94      nicm      187:
1.116     nicm      188:        c->stdin_data = evbuffer_new();
                    189:        c->stdout_data = evbuffer_new();
                    190:        c->stderr_data = evbuffer_new();
1.33      nicm      191:
1.1       nicm      192:        c->tty.fd = -1;
                    193:        c->title = NULL;
                    194:
                    195:        c->session = NULL;
1.45      nicm      196:        c->last_session = NULL;
1.261   ! nicm      197:
1.1       nicm      198:        c->tty.sx = 80;
                    199:        c->tty.sy = 24;
                    200:
1.247     nicm      201:        screen_init(&c->status.status, c->tty.sx, 1, 0);
1.1       nicm      202:
                    203:        c->message_string = NULL;
1.136     nicm      204:        TAILQ_INIT(&c->message_log);
1.1       nicm      205:
                    206:        c->prompt_string = NULL;
                    207:        c->prompt_buffer = NULL;
                    208:        c->prompt_index = 0;
                    209:
1.93      nicm      210:        c->flags |= CLIENT_FOCUSED;
                    211:
1.132     nicm      212:        c->keytable = key_bindings_get_table("root", 1);
                    213:        c->keytable->references++;
                    214:
1.17      nicm      215:        evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
1.192     nicm      216:        evtimer_set(&c->click_timer, server_client_click_timer, c);
1.17      nicm      217:
1.135     nicm      218:        TAILQ_INSERT_TAIL(&clients, c, entry);
1.157     nicm      219:        log_debug("new client %p", c);
1.246     nicm      220:        return (c);
1.72      nicm      221: }
                    222:
                    223: /* Open client terminal if needed. */
                    224: int
1.119     nicm      225: server_client_open(struct client *c, char **cause)
1.72      nicm      226: {
1.75      nicm      227:        if (c->flags & CLIENT_CONTROL)
                    228:                return (0);
1.120     nicm      229:
                    230:        if (strcmp(c->ttyname, "/dev/tty") == 0) {
                    231:                *cause = xstrdup("can't use /dev/tty");
                    232:                return (-1);
                    233:        }
1.75      nicm      234:
1.72      nicm      235:        if (!(c->flags & CLIENT_TERMINAL)) {
1.116     nicm      236:                *cause = xstrdup("not a terminal");
1.72      nicm      237:                return (-1);
                    238:        }
                    239:
1.119     nicm      240:        if (tty_open(&c->tty, cause) != 0)
1.72      nicm      241:                return (-1);
                    242:
                    243:        return (0);
1.1       nicm      244: }
                    245:
                    246: /* Lost a client. */
                    247: void
                    248: server_client_lost(struct client *c)
                    249: {
1.136     nicm      250:        struct message_entry    *msg, *msg1;
1.1       nicm      251:
1.141     nicm      252:        c->flags |= CLIENT_DEAD;
                    253:
1.214     nicm      254:        server_client_clear_identify(c, NULL);
1.141     nicm      255:        status_prompt_clear(c);
                    256:        status_message_clear(c);
                    257:
                    258:        if (c->stdin_callback != NULL)
                    259:                c->stdin_callback(c, 1, c->stdin_callback_data);
                    260:
1.135     nicm      261:        TAILQ_REMOVE(&clients, c, entry);
1.157     nicm      262:        log_debug("lost client %p", c);
1.1       nicm      263:
                    264:        /*
                    265:         * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
                    266:         * and tty_free might close an unrelated fd.
                    267:         */
                    268:        if (c->flags & CLIENT_TERMINAL)
                    269:                tty_free(&c->tty);
1.109     nicm      270:        free(c->ttyname);
                    271:        free(c->term);
1.1       nicm      272:
1.113     nicm      273:        evbuffer_free(c->stdin_data);
                    274:        evbuffer_free(c->stdout_data);
1.97      nicm      275:        if (c->stderr_data != c->stdout_data)
1.126     nicm      276:                evbuffer_free(c->stderr_data);
1.33      nicm      277:
1.247     nicm      278:        if (event_initialized(&c->status.timer))
                    279:                evtimer_del(&c->status.timer);
                    280:        screen_free(&c->status.status);
                    281:        if (c->status.old_status != NULL) {
                    282:                screen_free(c->status.old_status);
                    283:                free(c->status.old_status);
1.234     nicm      284:        }
1.1       nicm      285:
1.77      nicm      286:        free(c->title);
1.165     nicm      287:        free((void *)c->cwd);
1.1       nicm      288:
1.17      nicm      289:        evtimer_del(&c->repeat_timer);
1.192     nicm      290:        evtimer_del(&c->click_timer);
1.17      nicm      291:
1.132     nicm      292:        key_bindings_unref_table(c->keytable);
                    293:
1.69      nicm      294:        if (event_initialized(&c->identify_timer))
                    295:                evtimer_del(&c->identify_timer);
1.15      nicm      296:
1.77      nicm      297:        free(c->message_string);
1.116     nicm      298:        if (event_initialized(&c->message_timer))
1.69      nicm      299:                evtimer_del(&c->message_timer);
1.136     nicm      300:        TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
1.77      nicm      301:                free(msg->msg);
1.136     nicm      302:                TAILQ_REMOVE(&c->message_log, msg, entry);
                    303:                free(msg);
1.21      nicm      304:        }
1.1       nicm      305:
1.259     nicm      306:        free(c->prompt_saved);
1.77      nicm      307:        free(c->prompt_string);
                    308:        free(c->prompt_buffer);
1.61      nicm      309:
1.228     nicm      310:        format_lost_client(c);
1.164     nicm      311:        environ_free(c->environ);
1.1       nicm      312:
1.162     nicm      313:        proc_remove_peer(c->peer);
                    314:        c->peer = NULL;
1.13      nicm      315:
1.142     nicm      316:        server_client_unref(c);
1.71      nicm      317:
                    318:        server_add_accept(0); /* may be more file descriptors now */
1.1       nicm      319:
                    320:        recalculate_sizes();
1.41      nicm      321:        server_check_unattached();
1.19      nicm      322:        server_update_socket();
1.141     nicm      323: }
                    324:
                    325: /* Remove reference from a client. */
                    326: void
1.142     nicm      327: server_client_unref(struct client *c)
1.141     nicm      328: {
1.157     nicm      329:        log_debug("unref client %p (%d references)", c, c->references);
1.141     nicm      330:
                    331:        c->references--;
                    332:        if (c->references == 0)
                    333:                event_once(-1, EV_TIMEOUT, server_client_free, c, NULL);
                    334: }
                    335:
                    336: /* Free dead client. */
1.190     nicm      337: static void
1.170     nicm      338: server_client_free(__unused int fd, __unused short events, void *arg)
1.141     nicm      339: {
                    340:        struct client   *c = arg;
                    341:
1.157     nicm      342:        log_debug("free client %p (%d references)", c, c->references);
1.141     nicm      343:
1.194     nicm      344:        if (!TAILQ_EMPTY(&c->queue))
                    345:                fatalx("queue not empty");
                    346:
1.216     nicm      347:        if (c->references == 0) {
                    348:                free((void *)c->name);
1.141     nicm      349:                free(c);
1.216     nicm      350:        }
1.9       nicm      351: }
                    352:
1.220     nicm      353: /* Suspend a client. */
                    354: void
                    355: server_client_suspend(struct client *c)
                    356: {
1.232     nicm      357:        struct session  *s = c->session;
1.220     nicm      358:
                    359:        if (s == NULL || (c->flags & CLIENT_DETACHING))
                    360:                return;
                    361:
                    362:        tty_stop_tty(&c->tty);
                    363:        c->flags |= CLIENT_SUSPENDED;
                    364:        proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0);
                    365: }
                    366:
1.174     nicm      367: /* Detach a client. */
                    368: void
                    369: server_client_detach(struct client *c, enum msgtype msgtype)
                    370: {
1.232     nicm      371:        struct session  *s = c->session;
1.174     nicm      372:
1.220     nicm      373:        if (s == NULL || (c->flags & CLIENT_DETACHING))
1.174     nicm      374:                return;
                    375:
1.220     nicm      376:        c->flags |= CLIENT_DETACHING;
1.196     nicm      377:        notify_client("client-detached", c);
1.240     nicm      378:        proc_send(c->peer, msgtype, -1, s->name, strlen(s->name) + 1);
1.207     nicm      379: }
                    380:
1.208     nicm      381: /* Execute command to replace a client. */
1.207     nicm      382: void
                    383: server_client_exec(struct client *c, const char *cmd)
                    384: {
                    385:        struct session  *s = c->session;
1.208     nicm      386:        char            *msg;
                    387:        const char      *shell;
1.207     nicm      388:        size_t           cmdsize, shellsize;
                    389:
                    390:        if (*cmd == '\0')
                    391:                return;
                    392:        cmdsize = strlen(cmd) + 1;
                    393:
                    394:        if (s != NULL)
                    395:                shell = options_get_string(s->options, "default-shell");
                    396:        else
                    397:                shell = options_get_string(global_s_options, "default-shell");
                    398:        shellsize = strlen(shell) + 1;
                    399:
                    400:        msg = xmalloc(cmdsize + shellsize);
                    401:        memcpy(msg, cmd, cmdsize);
                    402:        memcpy(msg + cmdsize, shell, shellsize);
                    403:
                    404:        proc_send(c->peer, MSG_EXEC, -1, msg, cmdsize + shellsize);
                    405:        free(msg);
1.174     nicm      406: }
                    407:
1.66      nicm      408: /* Check for mouse keys. */
1.190     nicm      409: static key_code
1.131     nicm      410: server_client_check_mouse(struct client *c)
1.66      nicm      411: {
1.192     nicm      412:        struct session          *s = c->session;
                    413:        struct mouse_event      *m = &c->tty.mouse;
                    414:        struct window           *w;
                    415:        struct window_pane      *wp;
1.261   ! nicm      416:        u_int                    x, y, b, sx, sy, px, py;
1.192     nicm      417:        int                      flag;
                    418:        key_code                 key;
                    419:        struct timeval           tv;
1.209     nicm      420:        enum { NOTYPE, MOVE, DOWN, UP, DRAG, WHEEL, DOUBLE, TRIPLE } type;
1.258     nicm      421:        enum { NOWHERE, PANE, STATUS, STATUS_LEFT, STATUS_RIGHT, BORDER } where;
1.209     nicm      422:
                    423:        type = NOTYPE;
                    424:        where = NOWHERE;
1.131     nicm      425:
1.261   ! nicm      426:        log_debug("%s mouse %02x at %u,%u (last %u,%u) (%d)", c->name, m->b,
        !           427:            m->x, m->y, m->lx, m->ly, c->tty.mouse_drag_flag);
1.131     nicm      428:
                    429:        /* What type of event is this? */
1.209     nicm      430:        if ((m->sgr_type != ' ' &&
                    431:            MOUSE_DRAG(m->sgr_b) &&
                    432:            MOUSE_BUTTONS(m->sgr_b) == 3) ||
                    433:            (m->sgr_type == ' ' &&
                    434:            MOUSE_DRAG(m->b) &&
                    435:            MOUSE_BUTTONS(m->b) == 3 &&
                    436:            MOUSE_BUTTONS(m->lb) == 3)) {
                    437:                type = MOVE;
                    438:                x = m->x, y = m->y, b = 0;
                    439:                log_debug("move at %u,%u", x, y);
                    440:        } else if (MOUSE_DRAG(m->b)) {
1.131     nicm      441:                type = DRAG;
                    442:                if (c->tty.mouse_drag_flag) {
                    443:                        x = m->x, y = m->y, b = m->b;
                    444:                        log_debug("drag update at %u,%u", x, y);
                    445:                } else {
1.261   ! nicm      446:                        x = m->lx - m->ox, y = m->ly - m->oy, b = m->lb;
1.131     nicm      447:                        log_debug("drag start at %u,%u", x, y);
                    448:                }
                    449:        } else if (MOUSE_WHEEL(m->b)) {
                    450:                type = WHEEL;
                    451:                x = m->x, y = m->y, b = m->b;
                    452:                log_debug("wheel at %u,%u", x, y);
1.200     nicm      453:        } else if (MOUSE_RELEASE(m->b)) {
1.131     nicm      454:                type = UP;
                    455:                x = m->x, y = m->y, b = m->lb;
                    456:                log_debug("up at %u,%u", x, y);
                    457:        } else {
1.192     nicm      458:                if (c->flags & CLIENT_DOUBLECLICK) {
                    459:                        evtimer_del(&c->click_timer);
                    460:                        c->flags &= ~CLIENT_DOUBLECLICK;
                    461:                        if (m->b == c->click_button) {
                    462:                                type = DOUBLE;
                    463:                                x = m->x, y = m->y, b = m->b;
                    464:                                log_debug("double-click at %u,%u", x, y);
                    465:                                flag = CLIENT_TRIPLECLICK;
                    466:                                goto add_timer;
                    467:                        }
                    468:                } else if (c->flags & CLIENT_TRIPLECLICK) {
                    469:                        evtimer_del(&c->click_timer);
                    470:                        c->flags &= ~CLIENT_TRIPLECLICK;
                    471:                        if (m->b == c->click_button) {
                    472:                                type = TRIPLE;
                    473:                                x = m->x, y = m->y, b = m->b;
                    474:                                log_debug("triple-click at %u,%u", x, y);
                    475:                                goto have_event;
                    476:                        }
                    477:                }
                    478:
1.131     nicm      479:                type = DOWN;
                    480:                x = m->x, y = m->y, b = m->b;
                    481:                log_debug("down at %u,%u", x, y);
1.192     nicm      482:                flag = CLIENT_DOUBLECLICK;
                    483:
                    484:        add_timer:
                    485:                if (KEYC_CLICK_TIMEOUT != 0) {
                    486:                        c->flags |= flag;
                    487:                        c->click_button = m->b;
                    488:
                    489:                        tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000;
                    490:                        tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L;
                    491:                        evtimer_del(&c->click_timer);
                    492:                        evtimer_add(&c->click_timer, &tv);
                    493:                }
1.131     nicm      494:        }
1.192     nicm      495:
                    496: have_event:
1.131     nicm      497:        if (type == NOTYPE)
1.177     nicm      498:                return (KEYC_UNKNOWN);
1.131     nicm      499:
1.258     nicm      500:        /* Save the session. */
1.131     nicm      501:        m->s = s->id;
1.258     nicm      502:        m->w = -1;
1.131     nicm      503:
                    504:        /* Is this on the status line? */
                    505:        m->statusat = status_at_line(c);
                    506:        if (m->statusat != -1 && y == (u_int)m->statusat) {
1.258     nicm      507:                if (x < c->status.left_size)
                    508:                        where = STATUS_LEFT;
                    509:                else if (x > c->tty.sx - c->status.right_size)
                    510:                        where = STATUS_RIGHT;
                    511:                else {
                    512:                        w = status_get_window_at(c, x);
                    513:                        if (w == NULL)
                    514:                                return (KEYC_UNKNOWN);
                    515:                        m->w = w->id;
                    516:                        where = STATUS;
                    517:                }
                    518:        }
1.131     nicm      519:
                    520:        /* Not on status line. Adjust position and check for border or pane. */
                    521:        if (where == NOWHERE) {
1.261   ! nicm      522:                px = x;
1.131     nicm      523:                if (m->statusat == 0 && y > 0)
1.261   ! nicm      524:                        py = y - 1;
1.131     nicm      525:                else if (m->statusat > 0 && y >= (u_int)m->statusat)
1.261   ! nicm      526:                        py = m->statusat - 1;
        !           527:                else
        !           528:                        py = y;
        !           529:
        !           530:                tty_window_offset(&c->tty, &m->ox, &m->oy, &sx, &sy);
        !           531:                log_debug("mouse window @%u at %u,%u (%ux%u)",
        !           532:                    s->curw->window->id, m->ox, m->oy, sx, sy);
        !           533:                if (px > sx || py > sy)
        !           534:                        return (KEYC_UNKNOWN);
        !           535:                px = px + m->ox;
        !           536:                py = py + m->oy;
        !           537:                m->x = x + m->ox;
        !           538:                m->y = y + m->oy;
1.131     nicm      539:
1.260     nicm      540:                /* Try the pane borders if not zoomed. */
                    541:                if (~s->curw->window->flags & WINDOW_ZOOMED) {
                    542:                        TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
1.261   ! nicm      543:                                if ((wp->xoff + wp->sx == px &&
        !           544:                                    wp->yoff <= 1 + py &&
        !           545:                                    wp->yoff + wp->sy >= py) ||
        !           546:                                    (wp->yoff + wp->sy == py &&
        !           547:                                    wp->xoff <= 1 + px &&
        !           548:                                    wp->xoff + wp->sx >= px))
1.260     nicm      549:                                        break;
                    550:                        }
                    551:                        if (wp != NULL)
                    552:                                where = BORDER;
1.131     nicm      553:                }
1.260     nicm      554:
                    555:                /* Otherwise try inside the pane. */
                    556:                if (where == NOWHERE) {
1.261   ! nicm      557:                        wp = window_get_active_at(s->curw->window, px, py);
1.260     nicm      558:                        if (wp != NULL)
1.131     nicm      559:                                where = PANE;
                    560:                }
1.260     nicm      561:
1.131     nicm      562:                if (where == NOWHERE)
1.177     nicm      563:                        return (KEYC_UNKNOWN);
1.260     nicm      564:                if (where == PANE)
                    565:                        log_debug("mouse %u,%u on pane %%%u", x, y, wp->id);
                    566:                else if (where == BORDER)
                    567:                        log_debug("mouse on pane %%%u border", wp->id);
1.131     nicm      568:                m->wp = wp->id;
                    569:                m->w = wp->window->id;
                    570:        } else
                    571:                m->wp = -1;
                    572:
                    573:        /* Stop dragging if needed. */
1.200     nicm      574:        if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag) {
1.131     nicm      575:                if (c->tty.mouse_drag_release != NULL)
                    576:                        c->tty.mouse_drag_release(c, m);
                    577:
                    578:                c->tty.mouse_drag_update = NULL;
                    579:                c->tty.mouse_drag_release = NULL;
                    580:
1.182     nicm      581:                /*
1.183     nicm      582:                 * End a mouse drag by passing a MouseDragEnd key corresponding
                    583:                 * to the button that started the drag.
1.182     nicm      584:                 */
                    585:                switch (c->tty.mouse_drag_flag) {
                    586:                case 1:
                    587:                        if (where == PANE)
1.183     nicm      588:                                key = KEYC_MOUSEDRAGEND1_PANE;
1.182     nicm      589:                        if (where == STATUS)
1.183     nicm      590:                                key = KEYC_MOUSEDRAGEND1_STATUS;
1.258     nicm      591:                        if (where == STATUS_LEFT)
                    592:                                key = KEYC_MOUSEDRAGEND1_STATUS_LEFT;
                    593:                        if (where == STATUS_RIGHT)
                    594:                                key = KEYC_MOUSEDRAGEND1_STATUS_RIGHT;
1.182     nicm      595:                        if (where == BORDER)
1.183     nicm      596:                                key = KEYC_MOUSEDRAGEND1_BORDER;
1.182     nicm      597:                        break;
                    598:                case 2:
                    599:                        if (where == PANE)
1.183     nicm      600:                                key = KEYC_MOUSEDRAGEND2_PANE;
1.182     nicm      601:                        if (where == STATUS)
1.183     nicm      602:                                key = KEYC_MOUSEDRAGEND2_STATUS;
1.258     nicm      603:                        if (where == STATUS_LEFT)
                    604:                                key = KEYC_MOUSEDRAGEND2_STATUS_LEFT;
                    605:                        if (where == STATUS_RIGHT)
                    606:                                key = KEYC_MOUSEDRAGEND2_STATUS_RIGHT;
1.182     nicm      607:                        if (where == BORDER)
1.183     nicm      608:                                key = KEYC_MOUSEDRAGEND2_BORDER;
1.182     nicm      609:                        break;
                    610:                case 3:
                    611:                        if (where == PANE)
1.183     nicm      612:                                key = KEYC_MOUSEDRAGEND3_PANE;
1.182     nicm      613:                        if (where == STATUS)
1.183     nicm      614:                                key = KEYC_MOUSEDRAGEND3_STATUS;
1.258     nicm      615:                        if (where == STATUS_LEFT)
                    616:                                key = KEYC_MOUSEDRAGEND3_STATUS_LEFT;
                    617:                        if (where == STATUS_RIGHT)
                    618:                                key = KEYC_MOUSEDRAGEND3_STATUS_RIGHT;
1.182     nicm      619:                        if (where == BORDER)
1.183     nicm      620:                                key = KEYC_MOUSEDRAGEND3_BORDER;
1.182     nicm      621:                        break;
                    622:                default:
                    623:                        key = KEYC_MOUSE;
                    624:                        break;
                    625:                }
1.131     nicm      626:                c->tty.mouse_drag_flag = 0;
1.182     nicm      627:
                    628:                return (key);
1.131     nicm      629:        }
                    630:
                    631:        /* Convert to a key binding. */
1.177     nicm      632:        key = KEYC_UNKNOWN;
1.131     nicm      633:        switch (type) {
                    634:        case NOTYPE:
                    635:                break;
1.209     nicm      636:        case MOVE:
                    637:                if (where == PANE)
                    638:                        key = KEYC_MOUSEMOVE_PANE;
                    639:                if (where == STATUS)
                    640:                        key = KEYC_MOUSEMOVE_STATUS;
                    641:                if (where == BORDER)
                    642:                        key = KEYC_MOUSEMOVE_BORDER;
                    643:                break;
1.131     nicm      644:        case DRAG:
1.204     nicm      645:                if (c->tty.mouse_drag_update != NULL)
                    646:                        key = KEYC_DRAGGING;
                    647:                else {
1.131     nicm      648:                        switch (MOUSE_BUTTONS(b)) {
                    649:                        case 0:
                    650:                                if (where == PANE)
                    651:                                        key = KEYC_MOUSEDRAG1_PANE;
                    652:                                if (where == STATUS)
                    653:                                        key = KEYC_MOUSEDRAG1_STATUS;
1.258     nicm      654:                                if (where == STATUS_LEFT)
                    655:                                        key = KEYC_MOUSEDRAG1_STATUS_LEFT;
                    656:                                if (where == STATUS_RIGHT)
                    657:                                        key = KEYC_MOUSEDRAG1_STATUS_RIGHT;
1.131     nicm      658:                                if (where == BORDER)
                    659:                                        key = KEYC_MOUSEDRAG1_BORDER;
                    660:                                break;
                    661:                        case 1:
                    662:                                if (where == PANE)
                    663:                                        key = KEYC_MOUSEDRAG2_PANE;
                    664:                                if (where == STATUS)
                    665:                                        key = KEYC_MOUSEDRAG2_STATUS;
1.258     nicm      666:                                if (where == STATUS_LEFT)
                    667:                                        key = KEYC_MOUSEDRAG2_STATUS_LEFT;
                    668:                                if (where == STATUS_RIGHT)
                    669:                                        key = KEYC_MOUSEDRAG2_STATUS_RIGHT;
1.131     nicm      670:                                if (where == BORDER)
                    671:                                        key = KEYC_MOUSEDRAG2_BORDER;
                    672:                                break;
                    673:                        case 2:
                    674:                                if (where == PANE)
                    675:                                        key = KEYC_MOUSEDRAG3_PANE;
                    676:                                if (where == STATUS)
                    677:                                        key = KEYC_MOUSEDRAG3_STATUS;
1.258     nicm      678:                                if (where == STATUS_LEFT)
                    679:                                        key = KEYC_MOUSEDRAG3_STATUS_LEFT;
                    680:                                if (where == STATUS_RIGHT)
                    681:                                        key = KEYC_MOUSEDRAG3_STATUS_RIGHT;
1.131     nicm      682:                                if (where == BORDER)
                    683:                                        key = KEYC_MOUSEDRAG3_BORDER;
                    684:                                break;
                    685:                        }
                    686:                }
1.66      nicm      687:
1.182     nicm      688:                /*
                    689:                 * Begin a drag by setting the flag to a non-zero value that
                    690:                 * corresponds to the mouse button in use.
                    691:                 */
                    692:                c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1;
1.131     nicm      693:                break;
                    694:        case WHEEL:
                    695:                if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
                    696:                        if (where == PANE)
                    697:                                key = KEYC_WHEELUP_PANE;
                    698:                        if (where == STATUS)
                    699:                                key = KEYC_WHEELUP_STATUS;
1.258     nicm      700:                        if (where == STATUS_LEFT)
                    701:                                key = KEYC_WHEELUP_STATUS_LEFT;
                    702:                        if (where == STATUS_RIGHT)
                    703:                                key = KEYC_WHEELUP_STATUS_RIGHT;
1.131     nicm      704:                        if (where == BORDER)
                    705:                                key = KEYC_WHEELUP_BORDER;
                    706:                } else {
                    707:                        if (where == PANE)
                    708:                                key = KEYC_WHEELDOWN_PANE;
                    709:                        if (where == STATUS)
                    710:                                key = KEYC_WHEELDOWN_STATUS;
                    711:                        if (where == BORDER)
                    712:                                key = KEYC_WHEELDOWN_BORDER;
                    713:                }
                    714:                break;
                    715:        case UP:
                    716:                switch (MOUSE_BUTTONS(b)) {
                    717:                case 0:
                    718:                        if (where == PANE)
                    719:                                key = KEYC_MOUSEUP1_PANE;
                    720:                        if (where == STATUS)
                    721:                                key = KEYC_MOUSEUP1_STATUS;
1.258     nicm      722:                        if (where == STATUS_LEFT)
                    723:                                key = KEYC_MOUSEUP1_STATUS_LEFT;
                    724:                        if (where == STATUS_RIGHT)
                    725:                                key = KEYC_MOUSEUP1_STATUS_RIGHT;
1.131     nicm      726:                        if (where == BORDER)
                    727:                                key = KEYC_MOUSEUP1_BORDER;
                    728:                        break;
                    729:                case 1:
                    730:                        if (where == PANE)
                    731:                                key = KEYC_MOUSEUP2_PANE;
                    732:                        if (where == STATUS)
                    733:                                key = KEYC_MOUSEUP2_STATUS;
1.258     nicm      734:                        if (where == STATUS_LEFT)
                    735:                                key = KEYC_MOUSEUP2_STATUS_LEFT;
                    736:                        if (where == STATUS_RIGHT)
                    737:                                key = KEYC_MOUSEUP2_STATUS_RIGHT;
1.131     nicm      738:                        if (where == BORDER)
                    739:                                key = KEYC_MOUSEUP2_BORDER;
                    740:                        break;
                    741:                case 2:
                    742:                        if (where == PANE)
                    743:                                key = KEYC_MOUSEUP3_PANE;
                    744:                        if (where == STATUS)
                    745:                                key = KEYC_MOUSEUP3_STATUS;
1.258     nicm      746:                        if (where == STATUS_LEFT)
                    747:                                key = KEYC_MOUSEUP3_STATUS_LEFT;
                    748:                        if (where == STATUS_RIGHT)
                    749:                                key = KEYC_MOUSEUP3_STATUS_RIGHT;
1.131     nicm      750:                        if (where == BORDER)
                    751:                                key = KEYC_MOUSEUP3_BORDER;
                    752:                        break;
                    753:                }
                    754:                break;
                    755:        case DOWN:
                    756:                switch (MOUSE_BUTTONS(b)) {
                    757:                case 0:
                    758:                        if (where == PANE)
                    759:                                key = KEYC_MOUSEDOWN1_PANE;
                    760:                        if (where == STATUS)
                    761:                                key = KEYC_MOUSEDOWN1_STATUS;
1.258     nicm      762:                        if (where == STATUS_LEFT)
                    763:                                key = KEYC_MOUSEDOWN1_STATUS_LEFT;
                    764:                        if (where == STATUS_RIGHT)
                    765:                                key = KEYC_MOUSEDOWN1_STATUS_RIGHT;
1.131     nicm      766:                        if (where == BORDER)
                    767:                                key = KEYC_MOUSEDOWN1_BORDER;
                    768:                        break;
                    769:                case 1:
                    770:                        if (where == PANE)
                    771:                                key = KEYC_MOUSEDOWN2_PANE;
                    772:                        if (where == STATUS)
                    773:                                key = KEYC_MOUSEDOWN2_STATUS;
1.258     nicm      774:                        if (where == STATUS_LEFT)
                    775:                                key = KEYC_MOUSEDOWN2_STATUS_LEFT;
                    776:                        if (where == STATUS_RIGHT)
                    777:                                key = KEYC_MOUSEDOWN2_STATUS_RIGHT;
1.131     nicm      778:                        if (where == BORDER)
                    779:                                key = KEYC_MOUSEDOWN2_BORDER;
                    780:                        break;
                    781:                case 2:
                    782:                        if (where == PANE)
                    783:                                key = KEYC_MOUSEDOWN3_PANE;
                    784:                        if (where == STATUS)
                    785:                                key = KEYC_MOUSEDOWN3_STATUS;
1.258     nicm      786:                        if (where == STATUS_LEFT)
                    787:                                key = KEYC_MOUSEDOWN3_STATUS_LEFT;
                    788:                        if (where == STATUS_RIGHT)
                    789:                                key = KEYC_MOUSEDOWN3_STATUS_RIGHT;
1.131     nicm      790:                        if (where == BORDER)
                    791:                                key = KEYC_MOUSEDOWN3_BORDER;
                    792:                        break;
1.66      nicm      793:                }
1.131     nicm      794:                break;
1.192     nicm      795:        case DOUBLE:
                    796:                switch (MOUSE_BUTTONS(b)) {
                    797:                case 0:
                    798:                        if (where == PANE)
                    799:                                key = KEYC_DOUBLECLICK1_PANE;
                    800:                        if (where == STATUS)
                    801:                                key = KEYC_DOUBLECLICK1_STATUS;
1.258     nicm      802:                        if (where == STATUS_LEFT)
                    803:                                key = KEYC_DOUBLECLICK1_STATUS_LEFT;
                    804:                        if (where == STATUS_RIGHT)
                    805:                                key = KEYC_DOUBLECLICK1_STATUS_RIGHT;
1.192     nicm      806:                        if (where == BORDER)
                    807:                                key = KEYC_DOUBLECLICK1_BORDER;
                    808:                        break;
                    809:                case 1:
                    810:                        if (where == PANE)
                    811:                                key = KEYC_DOUBLECLICK2_PANE;
                    812:                        if (where == STATUS)
                    813:                                key = KEYC_DOUBLECLICK2_STATUS;
1.258     nicm      814:                        if (where == STATUS_LEFT)
                    815:                                key = KEYC_DOUBLECLICK2_STATUS_LEFT;
                    816:                        if (where == STATUS_RIGHT)
                    817:                                key = KEYC_DOUBLECLICK2_STATUS_RIGHT;
1.192     nicm      818:                        if (where == BORDER)
                    819:                                key = KEYC_DOUBLECLICK2_BORDER;
                    820:                        break;
                    821:                case 2:
                    822:                        if (where == PANE)
                    823:                                key = KEYC_DOUBLECLICK3_PANE;
                    824:                        if (where == STATUS)
                    825:                                key = KEYC_DOUBLECLICK3_STATUS;
1.258     nicm      826:                        if (where == STATUS_LEFT)
                    827:                                key = KEYC_DOUBLECLICK3_STATUS_LEFT;
                    828:                        if (where == STATUS_RIGHT)
                    829:                                key = KEYC_DOUBLECLICK3_STATUS_RIGHT;
1.192     nicm      830:                        if (where == BORDER)
                    831:                                key = KEYC_DOUBLECLICK3_BORDER;
                    832:                        break;
                    833:                }
                    834:                break;
                    835:        case TRIPLE:
                    836:                switch (MOUSE_BUTTONS(b)) {
                    837:                case 0:
                    838:                        if (where == PANE)
                    839:                                key = KEYC_TRIPLECLICK1_PANE;
                    840:                        if (where == STATUS)
                    841:                                key = KEYC_TRIPLECLICK1_STATUS;
1.258     nicm      842:                        if (where == STATUS_LEFT)
                    843:                                key = KEYC_TRIPLECLICK1_STATUS_LEFT;
                    844:                        if (where == STATUS_RIGHT)
                    845:                                key = KEYC_TRIPLECLICK1_STATUS_RIGHT;
1.192     nicm      846:                        if (where == BORDER)
                    847:                                key = KEYC_TRIPLECLICK1_BORDER;
                    848:                        break;
                    849:                case 1:
                    850:                        if (where == PANE)
                    851:                                key = KEYC_TRIPLECLICK2_PANE;
                    852:                        if (where == STATUS)
                    853:                                key = KEYC_TRIPLECLICK2_STATUS;
1.258     nicm      854:                        if (where == STATUS_LEFT)
                    855:                                key = KEYC_TRIPLECLICK2_STATUS_LEFT;
                    856:                        if (where == STATUS_RIGHT)
                    857:                                key = KEYC_TRIPLECLICK2_STATUS_RIGHT;
1.192     nicm      858:                        if (where == BORDER)
                    859:                                key = KEYC_TRIPLECLICK2_BORDER;
                    860:                        break;
                    861:                case 2:
                    862:                        if (where == PANE)
                    863:                                key = KEYC_TRIPLECLICK3_PANE;
                    864:                        if (where == STATUS)
                    865:                                key = KEYC_TRIPLECLICK3_STATUS;
1.258     nicm      866:                        if (where == STATUS_LEFT)
                    867:                                key = KEYC_TRIPLECLICK3_STATUS_LEFT;
                    868:                        if (where == STATUS_RIGHT)
                    869:                                key = KEYC_TRIPLECLICK3_STATUS_RIGHT;
1.192     nicm      870:                        if (where == BORDER)
                    871:                                key = KEYC_TRIPLECLICK3_BORDER;
                    872:                        break;
                    873:                }
                    874:                break;
1.66      nicm      875:        }
1.177     nicm      876:        if (key == KEYC_UNKNOWN)
                    877:                return (KEYC_UNKNOWN);
1.66      nicm      878:
1.131     nicm      879:        /* Apply modifiers if any. */
                    880:        if (b & MOUSE_MASK_META)
                    881:                key |= KEYC_ESCAPE;
                    882:        if (b & MOUSE_MASK_CTRL)
                    883:                key |= KEYC_CTRL;
                    884:        if (b & MOUSE_MASK_SHIFT)
                    885:                key |= KEYC_SHIFT;
1.66      nicm      886:
1.131     nicm      887:        return (key);
1.66      nicm      888: }
                    889:
1.82      nicm      890: /* Is this fast enough to probably be a paste? */
1.190     nicm      891: static int
1.82      nicm      892: server_client_assume_paste(struct session *s)
                    893: {
                    894:        struct timeval  tv;
1.84      nicm      895:        int             t;
1.82      nicm      896:
1.163     nicm      897:        if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1.83      nicm      898:                return (0);
1.82      nicm      899:
                    900:        timersub(&s->activity_time, &s->last_activity_time, &tv);
1.171     nicm      901:        if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
                    902:                log_debug("session %s pasting (flag %d)", s->name,
                    903:                    !!(s->flags & SESSION_PASTING));
                    904:                if (s->flags & SESSION_PASTING)
                    905:                        return (1);
                    906:                s->flags |= SESSION_PASTING;
                    907:                return (0);
                    908:        }
                    909:        log_debug("session %s not pasting", s->name);
                    910:        s->flags &= ~SESSION_PASTING;
1.83      nicm      911:        return (0);
1.82      nicm      912: }
                    913:
1.18      nicm      914: /* Handle data key input from client. */
                    915: void
1.168     nicm      916: server_client_handle_key(struct client *c, key_code key)
1.18      nicm      917: {
1.131     nicm      918:        struct mouse_event      *m = &c->tty.mouse;
1.132     nicm      919:        struct session          *s = c->session;
1.18      nicm      920:        struct window           *w;
                    921:        struct window_pane      *wp;
                    922:        struct timeval           tv;
1.223     nicm      923:        struct key_table        *table, *first;
1.254     nicm      924:        struct key_binding      *bd;
1.238     nicm      925:        int                      xtimeout, flags;
1.201     nicm      926:        struct cmd_find_state    fs;
1.239     nicm      927:        key_code                 key0;
1.18      nicm      928:
                    929:        /* Check the client is good to accept input. */
1.132     nicm      930:        if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1.18      nicm      931:                return;
1.132     nicm      932:        w = s->curw->window;
1.18      nicm      933:
                    934:        /* Update the activity timer. */
                    935:        if (gettimeofday(&c->activity_time, NULL) != 0)
                    936:                fatal("gettimeofday failed");
1.149     nicm      937:        session_update_activity(s, &c->activity_time);
1.18      nicm      938:
1.132     nicm      939:        /* Number keys jump to pane in identify mode. */
1.25      nicm      940:        if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30      nicm      941:                if (c->flags & CLIENT_READONLY)
                    942:                        return;
1.95      nicm      943:                window_unzoom(w);
1.18      nicm      944:                wp = window_pane_at_index(w, key - '0');
1.214     nicm      945:                server_client_clear_identify(c, wp);
1.18      nicm      946:                return;
                    947:        }
                    948:
                    949:        /* Handle status line. */
1.30      nicm      950:        if (!(c->flags & CLIENT_READONLY)) {
                    951:                status_message_clear(c);
1.214     nicm      952:                server_client_clear_identify(c, NULL);
1.30      nicm      953:        }
1.18      nicm      954:        if (c->prompt_string != NULL) {
1.193     nicm      955:                if (c->flags & CLIENT_READONLY)
                    956:                        return;
                    957:                if (status_prompt_key(c, key) == 0)
                    958:                        return;
1.18      nicm      959:        }
                    960:
                    961:        /* Check for mouse keys. */
1.204     nicm      962:        m->valid = 0;
1.18      nicm      963:        if (key == KEYC_MOUSE) {
1.30      nicm      964:                if (c->flags & CLIENT_READONLY)
                    965:                        return;
1.131     nicm      966:                key = server_client_check_mouse(c);
1.177     nicm      967:                if (key == KEYC_UNKNOWN)
1.131     nicm      968:                        return;
                    969:
                    970:                m->valid = 1;
                    971:                m->key = key;
1.203     nicm      972:
                    973:                /*
1.204     nicm      974:                 * Mouse drag is in progress, so fire the callback (now that
                    975:                 * the mouse event is valid).
1.203     nicm      976:                 */
1.204     nicm      977:                if (key == KEYC_DRAGGING) {
                    978:                        c->tty.mouse_drag_update(c, m);
1.203     nicm      979:                        return;
1.204     nicm      980:                }
1.131     nicm      981:        } else
                    982:                m->valid = 0;
1.18      nicm      983:
1.202     nicm      984:        /* Find affected pane. */
1.243     nicm      985:        if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m, 0) != 0)
                    986:                cmd_find_from_session(&fs, s, 0);
1.227     nicm      987:        wp = fs.wp;
1.202     nicm      988:
                    989:        /* Forward mouse keys if disabled. */
1.206     nicm      990:        if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse"))
1.253     nicm      991:                goto forward_key;
1.202     nicm      992:
1.132     nicm      993:        /* Treat everything as a regular key when pasting is detected. */
1.161     nicm      994:        if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
1.253     nicm      995:                goto forward_key;
1.18      nicm      996:
1.191     nicm      997:        /*
                    998:         * Work out the current key table. If the pane is in a mode, use
                    999:         * the mode table instead of the default key table.
                   1000:         */
1.223     nicm     1001:        if (server_client_is_default_key_table(c, c->keytable) &&
                   1002:            wp != NULL &&
                   1003:            wp->mode != NULL &&
                   1004:            wp->mode->key_table != NULL)
                   1005:                table = key_bindings_get_table(wp->mode->key_table(wp), 1);
                   1006:        else
1.191     nicm     1007:                table = c->keytable;
1.223     nicm     1008:        first = table;
1.191     nicm     1009:
1.253     nicm     1010: table_changed:
1.205     nicm     1011:        /*
                   1012:         * The prefix always takes precedence and forces a switch to the prefix
                   1013:         * table, unless we are already there.
                   1014:         */
1.252     nicm     1015:        key0 = (key & ~KEYC_XTERM);
1.239     nicm     1016:        if ((key0 == (key_code)options_get_number(s->options, "prefix") ||
                   1017:            key0 == (key_code)options_get_number(s->options, "prefix2")) &&
1.205     nicm     1018:            strcmp(table->name, "prefix") != 0) {
                   1019:                server_client_set_key_table(c, "prefix");
                   1020:                server_status_client(c);
                   1021:                return;
                   1022:        }
1.238     nicm     1023:        flags = c->flags;
1.205     nicm     1024:
1.223     nicm     1025:        /* Log key table. */
                   1026:        if (wp == NULL)
                   1027:                log_debug("key table %s (no pane)", table->name);
                   1028:        else
                   1029:                log_debug("key table %s (pane %%%u)", table->name, wp->id);
1.225     nicm     1030:        if (c->flags & CLIENT_REPEAT)
                   1031:                log_debug("currently repeating");
1.223     nicm     1032:
1.253     nicm     1033: try_again:
1.132     nicm     1034:        /* Try to see if there is a key binding in the current table. */
1.254     nicm     1035:        bd = key_bindings_get(table, key0);
1.132     nicm     1036:        if (bd != NULL) {
                   1037:                /*
                   1038:                 * Key was matched in this table. If currently repeating but a
                   1039:                 * non-repeating binding was found, stop repeating and try
                   1040:                 * again in the root table.
                   1041:                 */
1.222     nicm     1042:                if ((c->flags & CLIENT_REPEAT) &&
                   1043:                    (~bd->flags & KEY_BINDING_REPEAT)) {
1.178     nicm     1044:                        server_client_set_key_table(c, NULL);
1.132     nicm     1045:                        c->flags &= ~CLIENT_REPEAT;
1.85      nicm     1046:                        server_status_client(c);
1.223     nicm     1047:                        table = c->keytable;
1.253     nicm     1048:                        goto table_changed;
1.18      nicm     1049:                }
1.223     nicm     1050:                log_debug("found in key table %s", table->name);
1.82      nicm     1051:
1.132     nicm     1052:                /*
                   1053:                 * Take a reference to this table to make sure the key binding
                   1054:                 * doesn't disappear.
                   1055:                 */
                   1056:                table->references++;
                   1057:
                   1058:                /*
                   1059:                 * If this is a repeating key, start the timer. Otherwise reset
                   1060:                 * the client back to the root table.
                   1061:                 */
1.163     nicm     1062:                xtimeout = options_get_number(s->options, "repeat-time");
1.222     nicm     1063:                if (xtimeout != 0 && (bd->flags & KEY_BINDING_REPEAT)) {
1.132     nicm     1064:                        c->flags |= CLIENT_REPEAT;
                   1065:
                   1066:                        tv.tv_sec = xtimeout / 1000;
                   1067:                        tv.tv_usec = (xtimeout % 1000) * 1000L;
                   1068:                        evtimer_del(&c->repeat_timer);
                   1069:                        evtimer_add(&c->repeat_timer, &tv);
                   1070:                } else {
1.18      nicm     1071:                        c->flags &= ~CLIENT_REPEAT;
1.178     nicm     1072:                        server_client_set_key_table(c, NULL);
1.18      nicm     1073:                }
1.132     nicm     1074:                server_status_client(c);
                   1075:
1.227     nicm     1076:                /* Execute the key binding. */
1.231     nicm     1077:                key_bindings_dispatch(bd, NULL, c, m, &fs);
1.132     nicm     1078:                key_bindings_unref_table(table);
1.18      nicm     1079:                return;
                   1080:        }
                   1081:
1.132     nicm     1082:        /*
1.253     nicm     1083:         * No match, try the ANY key.
                   1084:         */
                   1085:        if (key0 != KEYC_ANY) {
                   1086:                key0 = KEYC_ANY;
                   1087:                goto try_again;
                   1088:        }
                   1089:
                   1090:        /*
1.223     nicm     1091:         * No match in this table. If not in the root table or if repeating,
                   1092:         * switch the client back to the root table and try again.
1.132     nicm     1093:         */
1.223     nicm     1094:        log_debug("not found in key table %s", table->name);
                   1095:        if (!server_client_is_default_key_table(c, table) ||
                   1096:            (c->flags & CLIENT_REPEAT)) {
1.178     nicm     1097:                server_client_set_key_table(c, NULL);
1.18      nicm     1098:                c->flags &= ~CLIENT_REPEAT;
1.132     nicm     1099:                server_status_client(c);
1.223     nicm     1100:                table = c->keytable;
1.253     nicm     1101:                goto table_changed;
1.18      nicm     1102:        }
                   1103:
1.223     nicm     1104:        /*
                   1105:         * No match in the root table either. If this wasn't the first table
                   1106:         * tried, don't pass the key to the pane.
                   1107:         */
1.238     nicm     1108:        if (first != table && (~flags & CLIENT_REPEAT)) {
1.178     nicm     1109:                server_client_set_key_table(c, NULL);
1.132     nicm     1110:                server_status_client(c);
1.161     nicm     1111:                return;
                   1112:        }
                   1113:
1.253     nicm     1114: forward_key:
1.161     nicm     1115:        if (c->flags & CLIENT_READONLY)
                   1116:                return;
                   1117:        if (wp != NULL)
1.132     nicm     1118:                window_pane_key(wp, c, s, key, m);
1.18      nicm     1119: }
                   1120:
1.2       nicm     1121: /* Client functions that need to happen every loop. */
                   1122: void
                   1123: server_client_loop(void)
                   1124: {
                   1125:        struct client           *c;
                   1126:        struct window           *w;
                   1127:        struct window_pane      *wp;
1.197     nicm     1128:        int                      focus;
1.2       nicm     1129:
1.135     nicm     1130:        TAILQ_FOREACH(c, &clients, entry) {
1.36      nicm     1131:                server_client_check_exit(c);
                   1132:                if (c->session != NULL) {
                   1133:                        server_client_check_redraw(c);
                   1134:                        server_client_reset_state(c);
                   1135:                }
1.2       nicm     1136:        }
                   1137:
                   1138:        /*
                   1139:         * Any windows will have been redrawn as part of clients, so clear
1.92      nicm     1140:         * their flags now. Also check pane focus and resize.
1.2       nicm     1141:         */
1.197     nicm     1142:        focus = options_get_number(global_options, "focus-events");
1.134     nicm     1143:        RB_FOREACH(w, windows, &windows) {
1.91      nicm     1144:                TAILQ_FOREACH(wp, &w->panes, entry) {
1.101     nicm     1145:                        if (wp->fd != -1) {
1.197     nicm     1146:                                if (focus)
                   1147:                                        server_client_check_focus(wp);
1.101     nicm     1148:                                server_client_check_resize(wp);
                   1149:                        }
1.2       nicm     1150:                        wp->flags &= ~PANE_REDRAW;
1.91      nicm     1151:                }
1.150     nicm     1152:                check_window_name(w);
1.2       nicm     1153:        }
1.92      nicm     1154: }
                   1155:
1.237     nicm     1156: /* Check if we need to force a resize. */
                   1157: static int
                   1158: server_client_resize_force(struct window_pane *wp)
                   1159: {
                   1160:        struct timeval  tv = { .tv_usec = 100000 };
                   1161:        struct winsize  ws;
                   1162:
                   1163:        /*
                   1164:         * If we are resizing to the same size as when we entered the loop
                   1165:         * (that is, to the same size the application currently thinks it is),
                   1166:         * tmux may have gone through several resizes internally and thrown
                   1167:         * away parts of the screen. So we need the application to actually
                   1168:         * redraw even though its final size has not changed.
                   1169:         */
                   1170:
                   1171:        if (wp->flags & PANE_RESIZEFORCE) {
                   1172:                wp->flags &= ~PANE_RESIZEFORCE;
                   1173:                return (0);
                   1174:        }
                   1175:
                   1176:        if (wp->sx != wp->osx ||
                   1177:            wp->sy != wp->osy ||
                   1178:            wp->sx <= 1 ||
                   1179:            wp->sy <= 1)
                   1180:                return (0);
                   1181:
                   1182:        memset(&ws, 0, sizeof ws);
                   1183:        ws.ws_col = wp->sx;
                   1184:        ws.ws_row = wp->sy - 1;
1.244     nicm     1185:        if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.237     nicm     1186:                fatal("ioctl failed");
                   1187:        log_debug("%s: %%%u forcing resize", __func__, wp->id);
                   1188:
                   1189:        evtimer_add(&wp->resize_timer, &tv);
                   1190:        wp->flags |= PANE_RESIZEFORCE;
                   1191:        return (1);
                   1192: }
                   1193:
1.211     nicm     1194: /* Resize timer event. */
1.188     nicm     1195: static void
                   1196: server_client_resize_event(__unused int fd, __unused short events, void *data)
1.92      nicm     1197: {
1.188     nicm     1198:        struct window_pane      *wp = data;
                   1199:        struct winsize           ws;
                   1200:
                   1201:        evtimer_del(&wp->resize_timer);
1.92      nicm     1202:
1.101     nicm     1203:        if (!(wp->flags & PANE_RESIZE))
1.92      nicm     1204:                return;
1.237     nicm     1205:        if (server_client_resize_force(wp))
                   1206:                return;
1.235     nicm     1207:
1.92      nicm     1208:        memset(&ws, 0, sizeof ws);
                   1209:        ws.ws_col = wp->sx;
                   1210:        ws.ws_row = wp->sy;
1.244     nicm     1211:        if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92      nicm     1212:                fatal("ioctl failed");
1.235     nicm     1213:        log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, wp->sx, wp->sy);
1.92      nicm     1214:
                   1215:        wp->flags &= ~PANE_RESIZE;
1.235     nicm     1216:
                   1217:        wp->osx = wp->sx;
                   1218:        wp->osy = wp->sy;
1.188     nicm     1219: }
                   1220:
                   1221: /* Check if pane should be resized. */
1.190     nicm     1222: static void
1.188     nicm     1223: server_client_check_resize(struct window_pane *wp)
                   1224: {
                   1225:        struct timeval   tv = { .tv_usec = 250000 };
                   1226:
                   1227:        if (!(wp->flags & PANE_RESIZE))
                   1228:                return;
1.235     nicm     1229:        log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, wp->sx, wp->sy);
1.188     nicm     1230:
                   1231:        if (!event_initialized(&wp->resize_timer))
                   1232:                evtimer_set(&wp->resize_timer, server_client_resize_event, wp);
                   1233:
                   1234:        /*
                   1235:         * The first resize should happen immediately, so if the timer is not
                   1236:         * running, do it now.
                   1237:         */
                   1238:        if (!evtimer_pending(&wp->resize_timer, NULL))
                   1239:                server_client_resize_event(-1, 0, wp);
                   1240:
                   1241:        /*
                   1242:         * If the pane is in the alternate screen, let the timer expire and
                   1243:         * resize to give the application a chance to redraw. If not, keep
                   1244:         * pushing the timer back.
                   1245:         */
                   1246:        if (wp->saved_grid != NULL && evtimer_pending(&wp->resize_timer, NULL))
                   1247:                return;
                   1248:        evtimer_del(&wp->resize_timer);
                   1249:        evtimer_add(&wp->resize_timer, &tv);
1.91      nicm     1250: }
                   1251:
                   1252: /* Check whether pane should be focused. */
1.190     nicm     1253: static void
1.91      nicm     1254: server_client_check_focus(struct window_pane *wp)
                   1255: {
1.93      nicm     1256:        struct client   *c;
1.102     nicm     1257:        int              push;
                   1258:
                   1259:        /* Do we need to push the focus state? */
                   1260:        push = wp->flags & PANE_FOCUSPUSH;
                   1261:        wp->flags &= ~PANE_FOCUSPUSH;
1.91      nicm     1262:
                   1263:        /* If we're not the active pane in our window, we're not focused. */
                   1264:        if (wp->window->active != wp)
                   1265:                goto not_focused;
                   1266:
                   1267:        /* If we're in a mode, we're not focused. */
                   1268:        if (wp->screen != &wp->base)
                   1269:                goto not_focused;
                   1270:
                   1271:        /*
1.93      nicm     1272:         * If our window is the current window in any focused clients with an
                   1273:         * attached session, we're focused.
1.91      nicm     1274:         */
1.135     nicm     1275:        TAILQ_FOREACH(c, &clients, entry) {
                   1276:                if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91      nicm     1277:                        continue;
1.255     nicm     1278:                if (c->session->attached == 0)
1.93      nicm     1279:                        continue;
                   1280:
                   1281:                if (c->session->curw->window == wp->window)
1.91      nicm     1282:                        goto focused;
                   1283:        }
                   1284:
                   1285: not_focused:
1.251     nicm     1286:        if (push || (wp->flags & PANE_FOCUSED)) {
                   1287:                if (wp->base.mode & MODE_FOCUSON)
                   1288:                        bufferevent_write(wp->event, "\033[O", 3);
                   1289:                notify_pane("pane-focus-out", wp);
                   1290:        }
1.91      nicm     1291:        wp->flags &= ~PANE_FOCUSED;
                   1292:        return;
                   1293:
                   1294: focused:
1.251     nicm     1295:        if (push || !(wp->flags & PANE_FOCUSED)) {
                   1296:                if (wp->base.mode & MODE_FOCUSON)
                   1297:                        bufferevent_write(wp->event, "\033[I", 3);
                   1298:                notify_pane("pane-focus-in", wp);
                   1299:        }
1.91      nicm     1300:        wp->flags |= PANE_FOCUSED;
1.2       nicm     1301: }
                   1302:
1.18      nicm     1303: /*
                   1304:  * Update cursor position and mode settings. The scroll region and attributes
                   1305:  * are cleared when idle (waiting for an event) as this is the most likely time
                   1306:  * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
                   1307:  * compromise between excessive resets and likelihood of an interrupt.
                   1308:  *
                   1309:  * tty_region/tty_reset/tty_update_mode already take care of not resetting
                   1310:  * things that are already in their default state.
                   1311:  */
1.190     nicm     1312: static void
1.18      nicm     1313: server_client_reset_state(struct client *c)
1.1       nicm     1314: {
1.18      nicm     1315:        struct window           *w = c->session->curw->window;
1.209     nicm     1316:        struct window_pane      *wp = w->active, *loop;
1.18      nicm     1317:        struct screen           *s = wp->screen;
1.163     nicm     1318:        struct options          *oo = c->session->options;
1.261   ! nicm     1319:        int                      mode, cursor = 0;
        !          1320:        u_int                    cx = 0, cy = 0, ox, oy, sx, sy;
1.60      nicm     1321:
1.186     nicm     1322:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86      nicm     1323:                return;
1.261   ! nicm     1324:        mode = s->mode;
1.86      nicm     1325:
1.199     nicm     1326:        tty_region_off(&c->tty);
                   1327:        tty_margin_off(&c->tty);
1.1       nicm     1328:
1.261   ! nicm     1329:        /* Move cursor to pane cursor and offset. */
        !          1330:        cursor = 0;
        !          1331:        tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
        !          1332:        if (wp->xoff + s->cx >= ox && wp->xoff + s->cx <= ox + sx &&
        !          1333:            wp->yoff + s->cy >= oy && wp->yoff + s->cy <= oy + sy) {
        !          1334:                cursor = 1;
        !          1335:
        !          1336:                cx = wp->xoff + s->cx - ox;
        !          1337:                cy = wp->yoff + s->cy - oy;
        !          1338:
        !          1339:                if (status_at_line(c) == 0)
        !          1340:                        cy += status_line_size(c);
        !          1341:        }
        !          1342:        if (!cursor)
        !          1343:                mode &= ~MODE_CURSOR;
        !          1344:        tty_cursor(&c->tty, cx, cy);
1.1       nicm     1345:
1.50      nicm     1346:        /*
1.131     nicm     1347:         * Set mouse mode if requested. To support dragging, always use button
                   1348:         * mode.
1.57      nicm     1349:         */
1.209     nicm     1350:        if (options_get_number(oo, "mouse")) {
                   1351:                mode &= ~ALL_MOUSE_MODES;
                   1352:                TAILQ_FOREACH(loop, &w->panes, entry) {
                   1353:                        if (loop->screen->mode & MODE_MOUSE_ALL)
                   1354:                                mode |= MODE_MOUSE_ALL;
                   1355:                }
                   1356:                if (~mode & MODE_MOUSE_ALL)
                   1357:                        mode |= MODE_MOUSE_BUTTON;
                   1358:        }
1.215     nicm     1359:
                   1360:        /* Clear bracketed paste mode if at the prompt. */
                   1361:        if (c->prompt_string != NULL)
                   1362:                mode &= ~MODE_BRACKETPASTE;
1.48      nicm     1363:
                   1364:        /* Set the terminal mode and reset attributes. */
1.59      nicm     1365:        tty_update_mode(&c->tty, mode, s);
1.1       nicm     1366:        tty_reset(&c->tty);
1.17      nicm     1367: }
                   1368:
                   1369: /* Repeat time callback. */
1.190     nicm     1370: static void
1.170     nicm     1371: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17      nicm     1372: {
                   1373:        struct client   *c = data;
                   1374:
1.85      nicm     1375:        if (c->flags & CLIENT_REPEAT) {
1.178     nicm     1376:                server_client_set_key_table(c, NULL);
1.132     nicm     1377:                c->flags &= ~CLIENT_REPEAT;
                   1378:                server_status_client(c);
1.85      nicm     1379:        }
1.192     nicm     1380: }
                   1381:
                   1382: /* Double-click callback. */
                   1383: static void
                   1384: server_client_click_timer(__unused int fd, __unused short events, void *data)
                   1385: {
                   1386:        struct client   *c = data;
                   1387:
                   1388:        c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1       nicm     1389: }
                   1390:
1.36      nicm     1391: /* Check if client should be exited. */
1.190     nicm     1392: static void
1.36      nicm     1393: server_client_check_exit(struct client *c)
                   1394: {
                   1395:        if (!(c->flags & CLIENT_EXIT))
                   1396:                return;
                   1397:
1.73      nicm     1398:        if (EVBUFFER_LENGTH(c->stdin_data) != 0)
                   1399:                return;
                   1400:        if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36      nicm     1401:                return;
1.73      nicm     1402:        if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36      nicm     1403:                return;
                   1404:
1.249     nicm     1405:        if (c->flags & CLIENT_ATTACHED)
                   1406:                notify_client("client-detached", c);
1.162     nicm     1407:        proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36      nicm     1408:        c->flags &= ~CLIENT_EXIT;
1.38      nicm     1409: }
                   1410:
1.218     nicm     1411: /* Redraw timer callback. */
                   1412: static void
                   1413: server_client_redraw_timer(__unused int fd, __unused short events,
                   1414:     __unused void* data)
                   1415: {
                   1416:        log_debug("redraw timer fired");
                   1417: }
                   1418:
1.1       nicm     1419: /* Check for client redraws. */
1.190     nicm     1420: static void
1.1       nicm     1421: server_client_check_redraw(struct client *c)
                   1422: {
                   1423:        struct session          *s = c->session;
1.137     nicm     1424:        struct tty              *tty = &c->tty;
1.1       nicm     1425:        struct window_pane      *wp;
1.256     nicm     1426:        int                      needed, flags;
1.218     nicm     1427:        struct timeval           tv = { .tv_usec = 1000 };
                   1428:        static struct event      ev;
                   1429:        size_t                   left;
1.1       nicm     1430:
1.86      nicm     1431:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79      nicm     1432:                return;
1.257     nicm     1433:        if (c->flags & CLIENT_ALLREDRAWFLAGS) {
                   1434:                log_debug("%s: redraw%s%s%s", c->name,
                   1435:                    (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "",
                   1436:                    (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "",
                   1437:                    (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "");
                   1438:        }
1.218     nicm     1439:
                   1440:        /*
                   1441:         * If there is outstanding data, defer the redraw until it has been
                   1442:         * consumed. We can just add a timer to get out of the event loop and
                   1443:         * end up back here.
                   1444:         */
                   1445:        needed = 0;
1.256     nicm     1446:        if (c->flags & CLIENT_ALLREDRAWFLAGS)
1.218     nicm     1447:                needed = 1;
                   1448:        else {
                   1449:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
                   1450:                        if (wp->flags & PANE_REDRAW) {
                   1451:                                needed = 1;
                   1452:                                break;
                   1453:                        }
                   1454:                }
                   1455:        }
1.241     nicm     1456:        if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) {
                   1457:                log_debug("%s: redraw deferred (%zu left)", c->name, left);
                   1458:                if (!evtimer_initialized(&ev))
                   1459:                        evtimer_set(&ev, server_client_redraw_timer, NULL);
                   1460:                if (!evtimer_pending(&ev, NULL)) {
1.218     nicm     1461:                        log_debug("redraw timer started");
                   1462:                        evtimer_add(&ev, &tv);
1.241     nicm     1463:                }
1.218     nicm     1464:
1.241     nicm     1465:                /*
                   1466:                 * We may have got here for a single pane redraw, but force a
                   1467:                 * full redraw next time in case other panes have been updated.
                   1468:                 */
1.256     nicm     1469:                c->flags |= CLIENT_ALLREDRAWFLAGS;
1.241     nicm     1470:                return;
                   1471:        } else if (needed)
1.218     nicm     1472:                log_debug("%s: redraw needed", c->name);
1.79      nicm     1473:
1.219     nicm     1474:        flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR);
                   1475:        tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE)) | TTY_NOCURSOR;
1.137     nicm     1476:
1.256     nicm     1477:        if (~c->flags & CLIENT_REDRAWWINDOW) {
                   1478:                /*
                   1479:                 * If not redrawing the entire window, check whether each pane
                   1480:                 * needs to be redrawn.
                   1481:                 */
1.1       nicm     1482:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137     nicm     1483:                        if (wp->flags & PANE_REDRAW) {
                   1484:                                tty_update_mode(tty, tty->mode, NULL);
1.1       nicm     1485:                                screen_redraw_pane(c, wp);
1.137     nicm     1486:                        }
1.1       nicm     1487:                }
                   1488:        }
                   1489:
1.256     nicm     1490:        if (c->flags & CLIENT_ALLREDRAWFLAGS) {
                   1491:                if (options_get_number(s->options, "set-titles"))
                   1492:                        server_client_set_title(c);
                   1493:                screen_redraw_screen(c);
                   1494:        }
1.1       nicm     1495:
1.137     nicm     1496:        tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
                   1497:        tty_update_mode(tty, tty->mode, NULL);
1.1       nicm     1498:
1.256     nicm     1499:        c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE);
1.230     nicm     1500:
                   1501:        if (needed) {
                   1502:                /*
                   1503:                 * We would have deferred the redraw unless the output buffer
                   1504:                 * was empty, so we can record how many bytes the redraw
                   1505:                 * generated.
                   1506:                 */
                   1507:                c->redraw = EVBUFFER_LENGTH(tty->out);
                   1508:                log_debug("%s: redraw added %zu bytes", c->name, c->redraw);
                   1509:        }
1.1       nicm     1510: }
                   1511:
                   1512: /* Set client title. */
1.190     nicm     1513: static void
1.1       nicm     1514: server_client_set_title(struct client *c)
                   1515: {
1.128     nicm     1516:        struct session          *s = c->session;
                   1517:        const char              *template;
                   1518:        char                    *title;
                   1519:        struct format_tree      *ft;
1.1       nicm     1520:
1.163     nicm     1521:        template = options_get_string(s->options, "set-titles-string");
1.25      nicm     1522:
1.228     nicm     1523:        ft = format_create(c, NULL, FORMAT_NONE, 0);
1.128     nicm     1524:        format_defaults(ft, c, NULL, NULL, NULL);
                   1525:
                   1526:        title = format_expand_time(ft, template, time(NULL));
1.1       nicm     1527:        if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77      nicm     1528:                free(c->title);
1.1       nicm     1529:                c->title = xstrdup(title);
                   1530:                tty_set_title(&c->tty, c->title);
                   1531:        }
1.77      nicm     1532:        free(title);
1.128     nicm     1533:
                   1534:        format_free(ft);
1.1       nicm     1535: }
                   1536:
                   1537: /* Dispatch message from client. */
1.190     nicm     1538: static void
1.162     nicm     1539: server_client_dispatch(struct imsg *imsg, void *arg)
1.1       nicm     1540: {
1.162     nicm     1541:        struct client           *c = arg;
1.73      nicm     1542:        struct msg_stdin_data    stdindata;
1.108     nicm     1543:        const char              *data;
1.162     nicm     1544:        ssize_t                  datalen;
1.149     nicm     1545:        struct session          *s;
1.1       nicm     1546:
1.162     nicm     1547:        if (c->flags & CLIENT_DEAD)
                   1548:                return;
                   1549:
                   1550:        if (imsg == NULL) {
                   1551:                server_client_lost(c);
                   1552:                return;
                   1553:        }
1.1       nicm     1554:
1.162     nicm     1555:        data = imsg->data;
                   1556:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1       nicm     1557:
1.162     nicm     1558:        switch (imsg->hdr.type) {
                   1559:        case MSG_IDENTIFY_FLAGS:
                   1560:        case MSG_IDENTIFY_TERM:
                   1561:        case MSG_IDENTIFY_TTYNAME:
                   1562:        case MSG_IDENTIFY_CWD:
                   1563:        case MSG_IDENTIFY_STDIN:
                   1564:        case MSG_IDENTIFY_ENVIRON:
                   1565:        case MSG_IDENTIFY_CLIENTPID:
                   1566:        case MSG_IDENTIFY_DONE:
                   1567:                server_client_dispatch_identify(c, imsg);
                   1568:                break;
                   1569:        case MSG_COMMAND:
                   1570:                server_client_dispatch_command(c, imsg);
                   1571:                break;
                   1572:        case MSG_STDIN:
                   1573:                if (datalen != sizeof stdindata)
                   1574:                        fatalx("bad MSG_STDIN size");
                   1575:                memcpy(&stdindata, data, sizeof stdindata);
1.36      nicm     1576:
1.162     nicm     1577:                if (c->stdin_callback == NULL)
1.33      nicm     1578:                        break;
1.162     nicm     1579:                if (stdindata.size <= 0)
                   1580:                        c->stdin_closed = 1;
                   1581:                else {
                   1582:                        evbuffer_add(c->stdin_data, stdindata.data,
                   1583:                            stdindata.size);
                   1584:                }
                   1585:                c->stdin_callback(c, c->stdin_closed,
                   1586:                    c->stdin_callback_data);
                   1587:                break;
                   1588:        case MSG_RESIZE:
                   1589:                if (datalen != 0)
                   1590:                        fatalx("bad MSG_RESIZE size");
1.1       nicm     1591:
1.162     nicm     1592:                if (c->flags & CLIENT_CONTROL)
1.1       nicm     1593:                        break;
1.236     nicm     1594:                tty_resize(&c->tty);
                   1595:                recalculate_sizes();
                   1596:                server_redraw_client(c);
1.174     nicm     1597:                if (c->session != NULL)
1.196     nicm     1598:                        notify_client("client-resized", c);
1.162     nicm     1599:                break;
                   1600:        case MSG_EXITING:
                   1601:                if (datalen != 0)
                   1602:                        fatalx("bad MSG_EXITING size");
1.1       nicm     1603:
1.162     nicm     1604:                c->session = NULL;
                   1605:                tty_close(&c->tty);
                   1606:                proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
                   1607:                break;
                   1608:        case MSG_WAKEUP:
                   1609:        case MSG_UNLOCK:
                   1610:                if (datalen != 0)
                   1611:                        fatalx("bad MSG_WAKEUP size");
1.121     nicm     1612:
1.162     nicm     1613:                if (!(c->flags & CLIENT_SUSPENDED))
                   1614:                        break;
                   1615:                c->flags &= ~CLIENT_SUSPENDED;
1.10      nicm     1616:
1.162     nicm     1617:                if (c->tty.fd == -1) /* exited in the meantime */
1.1       nicm     1618:                        break;
1.162     nicm     1619:                s = c->session;
1.1       nicm     1620:
1.162     nicm     1621:                if (gettimeofday(&c->activity_time, NULL) != 0)
                   1622:                        fatal("gettimeofday failed");
                   1623:
                   1624:                tty_start_tty(&c->tty);
                   1625:                server_redraw_client(c);
                   1626:                recalculate_sizes();
1.184     nicm     1627:
                   1628:                if (s != NULL)
                   1629:                        session_update_activity(s, &c->activity_time);
1.162     nicm     1630:                break;
                   1631:        case MSG_SHELL:
                   1632:                if (datalen != 0)
                   1633:                        fatalx("bad MSG_SHELL size");
1.1       nicm     1634:
1.162     nicm     1635:                server_client_dispatch_shell(c);
                   1636:                break;
1.1       nicm     1637:        }
                   1638: }
                   1639:
1.194     nicm     1640: /* Callback when command is done. */
                   1641: static enum cmd_retval
1.195     nicm     1642: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194     nicm     1643: {
1.195     nicm     1644:        struct client   *c = item->client;
1.194     nicm     1645:
                   1646:        if (~c->flags & CLIENT_ATTACHED)
                   1647:                c->flags |= CLIENT_EXIT;
                   1648:        return (CMD_RETURN_NORMAL);
                   1649: }
                   1650:
                   1651: /* Show an error message. */
                   1652: static enum cmd_retval
1.195     nicm     1653: server_client_command_error(struct cmdq_item *item, void *data)
1.194     nicm     1654: {
                   1655:        char    *error = data;
                   1656:
1.195     nicm     1657:        cmdq_error(item, "%s", error);
1.194     nicm     1658:        free(error);
                   1659:
                   1660:        return (CMD_RETURN_NORMAL);
                   1661: }
                   1662:
1.1       nicm     1663: /* Handle command message. */
1.190     nicm     1664: static void
1.162     nicm     1665: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1       nicm     1666: {
1.108     nicm     1667:        struct msg_command_data   data;
                   1668:        char                     *buf;
                   1669:        size_t                    len;
                   1670:        struct cmd_list          *cmdlist = NULL;
                   1671:        int                       argc;
                   1672:        char                    **argv, *cause;
1.246     nicm     1673:
                   1674:        if (c->flags & CLIENT_EXIT)
                   1675:                return;
1.108     nicm     1676:
                   1677:        if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
                   1678:                fatalx("bad MSG_COMMAND size");
                   1679:        memcpy(&data, imsg->data, sizeof data);
                   1680:
1.124     nicm     1681:        buf = (char *)imsg->data + sizeof data;
1.108     nicm     1682:        len = imsg->hdr.len  - IMSG_HEADER_SIZE - sizeof data;
                   1683:        if (len > 0 && buf[len - 1] != '\0')
                   1684:                fatalx("bad MSG_COMMAND string");
                   1685:
                   1686:        argc = data.argc;
                   1687:        if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194     nicm     1688:                cause = xstrdup("command too long");
1.1       nicm     1689:                goto error;
                   1690:        }
                   1691:
                   1692:        if (argc == 0) {
                   1693:                argc = 1;
                   1694:                argv = xcalloc(1, sizeof *argv);
                   1695:                *argv = xstrdup("new-session");
                   1696:        }
                   1697:
1.94      nicm     1698:        if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
1.1       nicm     1699:                cmd_free_argv(argc, argv);
                   1700:                goto error;
                   1701:        }
                   1702:        cmd_free_argv(argc, argv);
                   1703:
1.194     nicm     1704:        cmdq_append(c, cmdq_get_command(cmdlist, NULL, NULL, 0));
                   1705:        cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.1       nicm     1706:        cmd_list_free(cmdlist);
                   1707:        return;
                   1708:
                   1709: error:
1.194     nicm     1710:        cmdq_append(c, cmdq_get_callback(server_client_command_error, cause));
                   1711:
1.1       nicm     1712:        if (cmdlist != NULL)
                   1713:                cmd_list_free(cmdlist);
1.88      nicm     1714:
1.36      nicm     1715:        c->flags |= CLIENT_EXIT;
1.1       nicm     1716: }
                   1717:
                   1718: /* Handle identify message. */
1.190     nicm     1719: static void
1.162     nicm     1720: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1       nicm     1721: {
1.165     nicm     1722:        const char      *data, *home;
1.232     nicm     1723:        size_t           datalen;
1.109     nicm     1724:        int              flags;
1.216     nicm     1725:        char            *name;
1.109     nicm     1726:
                   1727:        if (c->flags & CLIENT_IDENTIFIED)
                   1728:                fatalx("out-of-order identify message");
                   1729:
                   1730:        data = imsg->data;
                   1731:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
                   1732:
                   1733:        switch (imsg->hdr.type) {
                   1734:        case MSG_IDENTIFY_FLAGS:
                   1735:                if (datalen != sizeof flags)
                   1736:                        fatalx("bad MSG_IDENTIFY_FLAGS size");
                   1737:                memcpy(&flags, data, sizeof flags);
                   1738:                c->flags |= flags;
1.158     nicm     1739:                log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109     nicm     1740:                break;
                   1741:        case MSG_IDENTIFY_TERM:
1.110     nicm     1742:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1743:                        fatalx("bad MSG_IDENTIFY_TERM string");
                   1744:                c->term = xstrdup(data);
1.158     nicm     1745:                log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109     nicm     1746:                break;
                   1747:        case MSG_IDENTIFY_TTYNAME:
1.110     nicm     1748:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1749:                        fatalx("bad MSG_IDENTIFY_TTYNAME string");
                   1750:                c->ttyname = xstrdup(data);
1.158     nicm     1751:                log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109     nicm     1752:                break;
                   1753:        case MSG_IDENTIFY_CWD:
1.155     nicm     1754:                if (datalen == 0 || data[datalen - 1] != '\0')
                   1755:                        fatalx("bad MSG_IDENTIFY_CWD string");
1.165     nicm     1756:                if (access(data, X_OK) == 0)
                   1757:                        c->cwd = xstrdup(data);
                   1758:                else if ((home = find_home()) != NULL)
                   1759:                        c->cwd = xstrdup(home);
                   1760:                else
                   1761:                        c->cwd = xstrdup("/");
1.158     nicm     1762:                log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109     nicm     1763:                break;
                   1764:        case MSG_IDENTIFY_STDIN:
                   1765:                if (datalen != 0)
                   1766:                        fatalx("bad MSG_IDENTIFY_STDIN size");
                   1767:                c->fd = imsg->fd;
1.158     nicm     1768:                log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109     nicm     1769:                break;
                   1770:        case MSG_IDENTIFY_ENVIRON:
1.110     nicm     1771:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1772:                        fatalx("bad MSG_IDENTIFY_ENVIRON string");
                   1773:                if (strchr(data, '=') != NULL)
1.164     nicm     1774:                        environ_put(c->environ, data);
1.158     nicm     1775:                log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143     nicm     1776:                break;
                   1777:        case MSG_IDENTIFY_CLIENTPID:
                   1778:                if (datalen != sizeof c->pid)
                   1779:                        fatalx("bad MSG_IDENTIFY_CLIENTPID size");
                   1780:                memcpy(&c->pid, data, sizeof c->pid);
1.158     nicm     1781:                log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109     nicm     1782:                break;
                   1783:        default:
                   1784:                break;
                   1785:        }
                   1786:
                   1787:        if (imsg->hdr.type != MSG_IDENTIFY_DONE)
                   1788:                return;
                   1789:        c->flags |= CLIENT_IDENTIFIED;
1.75      nicm     1790:
1.216     nicm     1791:        if (*c->ttyname != '\0')
                   1792:                name = xstrdup(c->ttyname);
                   1793:        else
                   1794:                xasprintf(&name, "client-%ld", (long)c->pid);
                   1795:        c->name = name;
                   1796:        log_debug("client %p name is %s", c, c->name);
                   1797:
1.109     nicm     1798:        if (c->flags & CLIENT_CONTROL) {
1.75      nicm     1799:                c->stdin_callback = control_callback;
1.109     nicm     1800:
1.97      nicm     1801:                evbuffer_free(c->stderr_data);
                   1802:                c->stderr_data = c->stdout_data;
1.109     nicm     1803:
                   1804:                if (c->flags & CLIENT_CONTROLCONTROL)
1.96      nicm     1805:                        evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162     nicm     1806:                proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75      nicm     1807:
                   1808:                c->tty.fd = -1;
                   1809:
1.109     nicm     1810:                close(c->fd);
                   1811:                c->fd = -1;
                   1812:
1.75      nicm     1813:                return;
                   1814:        }
1.1       nicm     1815:
1.109     nicm     1816:        if (c->fd == -1)
1.104     nicm     1817:                return;
1.145     nicm     1818:        if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109     nicm     1819:                close(c->fd);
                   1820:                c->fd = -1;
1.80      nicm     1821:                return;
                   1822:        }
1.109     nicm     1823:        if (c->flags & CLIENT_UTF8)
1.1       nicm     1824:                c->tty.flags |= TTY_UTF8;
1.109     nicm     1825:        if (c->flags & CLIENT_256COLOURS)
1.1       nicm     1826:                c->tty.term_flags |= TERM_256COLOURS;
                   1827:
                   1828:        tty_resize(&c->tty);
                   1829:
1.109     nicm     1830:        if (!(c->flags & CLIENT_CONTROL))
1.86      nicm     1831:                c->flags |= CLIENT_TERMINAL;
1.1       nicm     1832: }
                   1833:
                   1834: /* Handle shell message. */
1.190     nicm     1835: static void
1.162     nicm     1836: server_client_dispatch_shell(struct client *c)
1.1       nicm     1837: {
1.107     nicm     1838:        const char      *shell;
1.25      nicm     1839:
1.163     nicm     1840:        shell = options_get_string(global_s_options, "default-shell");
1.1       nicm     1841:        if (*shell == '\0' || areshell(shell))
                   1842:                shell = _PATH_BSHELL;
1.240     nicm     1843:        proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1);
1.25      nicm     1844:
1.162     nicm     1845:        proc_kill_peer(c->peer);
1.169     nicm     1846: }
                   1847:
                   1848: /* Event callback to push more stdout data if any left. */
                   1849: static void
1.170     nicm     1850: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169     nicm     1851: {
                   1852:        struct client   *c = arg;
                   1853:
                   1854:        if (~c->flags & CLIENT_DEAD)
                   1855:                server_client_push_stdout(c);
                   1856:        server_client_unref(c);
                   1857: }
                   1858:
                   1859: /* Push stdout to client if possible. */
                   1860: void
                   1861: server_client_push_stdout(struct client *c)
                   1862: {
                   1863:        struct msg_stdout_data data;
1.232     nicm     1864:        size_t                 sent, left;
1.169     nicm     1865:
                   1866:        left = EVBUFFER_LENGTH(c->stdout_data);
                   1867:        while (left != 0) {
                   1868:                sent = left;
                   1869:                if (sent > sizeof data.data)
                   1870:                        sent = sizeof data.data;
                   1871:                memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
                   1872:                data.size = sent;
                   1873:
                   1874:                if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
                   1875:                        break;
                   1876:                evbuffer_drain(c->stdout_data, sent);
                   1877:
                   1878:                left = EVBUFFER_LENGTH(c->stdout_data);
                   1879:                log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
                   1880:                    sent, left);
                   1881:        }
                   1882:        if (left != 0) {
                   1883:                c->references++;
                   1884:                event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
                   1885:                log_debug("%s: client %p, queued", __func__, c);
                   1886:        }
                   1887: }
                   1888:
                   1889: /* Event callback to push more stderr data if any left. */
                   1890: static void
1.170     nicm     1891: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169     nicm     1892: {
                   1893:        struct client   *c = arg;
                   1894:
                   1895:        if (~c->flags & CLIENT_DEAD)
                   1896:                server_client_push_stderr(c);
                   1897:        server_client_unref(c);
                   1898: }
                   1899:
                   1900: /* Push stderr to client if possible. */
                   1901: void
                   1902: server_client_push_stderr(struct client *c)
                   1903: {
                   1904:        struct msg_stderr_data data;
1.232     nicm     1905:        size_t                 sent, left;
1.169     nicm     1906:
                   1907:        if (c->stderr_data == c->stdout_data) {
                   1908:                server_client_push_stdout(c);
                   1909:                return;
                   1910:        }
                   1911:
                   1912:        left = EVBUFFER_LENGTH(c->stderr_data);
                   1913:        while (left != 0) {
                   1914:                sent = left;
                   1915:                if (sent > sizeof data.data)
                   1916:                        sent = sizeof data.data;
                   1917:                memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
                   1918:                data.size = sent;
                   1919:
                   1920:                if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
                   1921:                        break;
                   1922:                evbuffer_drain(c->stderr_data, sent);
                   1923:
                   1924:                left = EVBUFFER_LENGTH(c->stderr_data);
                   1925:                log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
                   1926:                    sent, left);
                   1927:        }
                   1928:        if (left != 0) {
                   1929:                c->references++;
                   1930:                event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
                   1931:                log_debug("%s: client %p, queued", __func__, c);
1.212     nicm     1932:        }
                   1933: }
                   1934:
                   1935: /* Add to client message log. */
                   1936: void
                   1937: server_client_add_message(struct client *c, const char *fmt, ...)
                   1938: {
                   1939:        struct message_entry    *msg, *msg1;
                   1940:        char                    *s;
                   1941:        va_list                  ap;
                   1942:        u_int                    limit;
                   1943:
                   1944:        va_start(ap, fmt);
                   1945:        xvasprintf(&s, fmt, ap);
                   1946:        va_end(ap);
                   1947:
1.216     nicm     1948:        log_debug("message %s (client %p)", s, c);
1.212     nicm     1949:
                   1950:        msg = xcalloc(1, sizeof *msg);
                   1951:        msg->msg_time = time(NULL);
                   1952:        msg->msg_num = c->message_next++;
                   1953:        msg->msg = s;
                   1954:        TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
                   1955:
                   1956:        limit = options_get_number(global_options, "message-limit");
                   1957:        TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
                   1958:                if (msg->msg_num + limit >= c->message_next)
                   1959:                        break;
                   1960:                free(msg->msg);
                   1961:                TAILQ_REMOVE(&c->message_log, msg, entry);
                   1962:                free(msg);
1.169     nicm     1963:        }
1.213     nicm     1964: }
                   1965:
                   1966: /* Get client working directory. */
                   1967: const char *
1.250     nicm     1968: server_client_get_cwd(struct client *c, struct session *s)
1.213     nicm     1969: {
1.250     nicm     1970:        const char      *home;
1.213     nicm     1971:
                   1972:        if (c != NULL && c->session == NULL && c->cwd != NULL)
                   1973:                return (c->cwd);
1.250     nicm     1974:        if (s != NULL && s->cwd != NULL)
                   1975:                return (s->cwd);
1.213     nicm     1976:        if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
                   1977:                return (s->cwd);
1.250     nicm     1978:        if ((home = find_home()) != NULL)
                   1979:                return (home);
                   1980:        return ("/");
1.213     nicm     1981: }
                   1982:
                   1983: /* Resolve an absolute path or relative to client working directory. */
                   1984: char *
                   1985: server_client_get_path(struct client *c, const char *file)
                   1986: {
                   1987:        char    *path, resolved[PATH_MAX];
                   1988:
                   1989:        if (*file == '/')
                   1990:                path = xstrdup(file);
                   1991:        else
1.250     nicm     1992:                xasprintf(&path, "%s/%s", server_client_get_cwd(c, NULL), file);
1.213     nicm     1993:        if (realpath(path, resolved) == NULL)
                   1994:                return (path);
                   1995:        free(path);
                   1996:        return (xstrdup(resolved));
1.1       nicm     1997: }