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

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