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

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