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

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