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

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