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

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