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

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