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

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