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

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