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

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