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

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