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

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