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

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