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

1.281   ! nicm        1: /* $OpenBSD: server-client.c,v 1.280 2019/05/07 11:24:03 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 *);
                     36: static void    server_client_check_focus(struct window_pane *);
                     37: static void    server_client_check_resize(struct window_pane *);
1.276     nicm       38: static key_code        server_client_check_mouse(struct client *, struct key_event *);
1.190     nicm       39: static void    server_client_repeat_timer(int, short, void *);
1.192     nicm       40: static void    server_client_click_timer(int, short, void *);
1.190     nicm       41: static void    server_client_check_exit(struct client *);
                     42: static void    server_client_check_redraw(struct client *);
                     43: static void    server_client_set_title(struct client *);
                     44: static void    server_client_reset_state(struct client *);
                     45: static int     server_client_assume_paste(struct session *);
1.281   ! nicm       46: static void    server_client_clear_overlay(struct client *);
1.190     nicm       47:
                     48: static void    server_client_dispatch(struct imsg *, void *);
                     49: static void    server_client_dispatch_command(struct client *, struct imsg *);
                     50: static void    server_client_dispatch_identify(struct client *, struct imsg *);
                     51: static void    server_client_dispatch_shell(struct client *);
1.233     nicm       52:
                     53: /* Number of attached clients. */
                     54: u_int
                     55: server_client_how_many(void)
                     56: {
                     57:        struct client   *c;
                     58:        u_int            n;
                     59:
                     60:        n = 0;
                     61:        TAILQ_FOREACH(c, &clients, entry) {
                     62:                if (c->session != NULL && (~c->flags & CLIENT_DETACHING))
                     63:                        n++;
                     64:        }
                     65:        return (n);
                     66: }
1.140     nicm       67:
1.281   ! nicm       68: /* Overlay timer callback. */
1.214     nicm       69: static void
1.281   ! nicm       70: server_client_overlay_timer(__unused int fd, __unused short events, void *data)
1.214     nicm       71: {
1.281   ! nicm       72:        server_client_clear_overlay(data);
1.214     nicm       73: }
                     74:
1.281   ! nicm       75: /* Set an overlay on client. */
1.214     nicm       76: void
1.281   ! nicm       77: server_client_set_overlay(struct client *c, u_int delay, overlay_draw_cb drawcb,
        !            78:     overlay_key_cb keycb, overlay_free_cb freecb, void *data)
1.214     nicm       79: {
                     80:        struct timeval  tv;
                     81:
                     82:        tv.tv_sec = delay / 1000;
                     83:        tv.tv_usec = (delay % 1000) * 1000L;
                     84:
1.281   ! nicm       85:        if (event_initialized(&c->overlay_timer))
        !            86:                evtimer_del(&c->overlay_timer);
        !            87:        evtimer_set(&c->overlay_timer, server_client_overlay_timer, c);
1.242     nicm       88:        if (delay != 0)
1.281   ! nicm       89:                evtimer_add(&c->overlay_timer, &tv);
        !            90:
        !            91:        c->overlay_draw = drawcb;
        !            92:        c->overlay_key = keycb;
        !            93:        c->overlay_free = freecb;
        !            94:        c->overlay_data = data;
1.214     nicm       95:
                     96:        c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
                     97:        server_redraw_client(c);
                     98: }
                     99:
1.281   ! nicm      100: /* Clear overlay mode on client. */
1.254     nicm      101: static void
1.281   ! nicm      102: server_client_clear_overlay(struct client *c)
1.214     nicm      103: {
1.281   ! nicm      104:        if (c->overlay_draw == NULL)
1.214     nicm      105:                return;
                    106:
1.281   ! nicm      107:        if (event_initialized(&c->overlay_timer))
        !           108:                evtimer_del(&c->overlay_timer);
        !           109:
        !           110:        if (c->overlay_free != NULL)
        !           111:                c->overlay_free(c);
        !           112:
        !           113:        c->overlay_draw = NULL;
        !           114:        c->overlay_key = NULL;
1.214     nicm      115:
                    116:        c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
                    117:        server_redraw_client(c);
                    118: }
                    119:
1.140     nicm      120: /* Check if this client is inside this server. */
                    121: int
                    122: server_client_check_nested(struct client *c)
                    123: {
                    124:        struct environ_entry    *envent;
                    125:        struct window_pane      *wp;
                    126:
1.164     nicm      127:        envent = environ_find(c->environ, "TMUX");
1.140     nicm      128:        if (envent == NULL || *envent->value == '\0')
                    129:                return (0);
                    130:
                    131:        RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
1.216     nicm      132:                if (strcmp(wp->tty, c->ttyname) == 0)
1.140     nicm      133:                        return (1);
                    134:        }
                    135:        return (0);
                    136: }
1.1       nicm      137:
1.132     nicm      138: /* Set client key table. */
                    139: void
1.178     nicm      140: server_client_set_key_table(struct client *c, const char *name)
1.132     nicm      141: {
1.178     nicm      142:        if (name == NULL)
                    143:                name = server_client_get_key_table(c);
                    144:
1.132     nicm      145:        key_bindings_unref_table(c->keytable);
                    146:        c->keytable = key_bindings_get_table(name, 1);
                    147:        c->keytable->references++;
                    148: }
                    149:
1.178     nicm      150: /* Get default key table. */
                    151: const char *
                    152: server_client_get_key_table(struct client *c)
                    153: {
                    154:        struct session  *s = c->session;
                    155:        const char      *name;
                    156:
                    157:        if (s == NULL)
                    158:                return ("root");
                    159:
                    160:        name = options_get_string(s->options, "key-table");
                    161:        if (*name == '\0')
                    162:                return ("root");
                    163:        return (name);
                    164: }
                    165:
1.223     nicm      166: /* Is this table the default key table? */
                    167: static int
                    168: server_client_is_default_key_table(struct client *c, struct key_table *table)
1.191     nicm      169: {
1.223     nicm      170:        return (strcmp(table->name, server_client_get_key_table(c)) == 0);
1.191     nicm      171: }
                    172:
1.1       nicm      173: /* Create a new client. */
1.246     nicm      174: struct client *
1.1       nicm      175: server_client_create(int fd)
                    176: {
                    177:        struct client   *c;
                    178:
1.49      nicm      179:        setblocking(fd, 0);
1.1       nicm      180:
                    181:        c = xcalloc(1, sizeof *c);
1.141     nicm      182:        c->references = 1;
1.162     nicm      183:        c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c);
1.25      nicm      184:
1.10      nicm      185:        if (gettimeofday(&c->creation_time, NULL) != 0)
1.1       nicm      186:                fatal("gettimeofday failed");
1.11      nicm      187:        memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.111     nicm      188:
1.164     nicm      189:        c->environ = environ_create();
1.1       nicm      190:
1.146     nicm      191:        c->fd = -1;
1.165     nicm      192:        c->cwd = NULL;
1.145     nicm      193:
1.194     nicm      194:        TAILQ_INIT(&c->queue);
1.94      nicm      195:
1.116     nicm      196:        c->stdin_data = evbuffer_new();
1.263     nicm      197:        if (c->stdin_data == NULL)
                    198:                fatalx("out of memory");
1.116     nicm      199:        c->stdout_data = evbuffer_new();
1.263     nicm      200:        if (c->stdout_data == NULL)
                    201:                fatalx("out of memory");
1.116     nicm      202:        c->stderr_data = evbuffer_new();
1.263     nicm      203:        if (c->stderr_data == NULL)
                    204:                fatalx("out of memory");
1.33      nicm      205:
1.1       nicm      206:        c->tty.fd = -1;
                    207:        c->title = NULL;
                    208:
                    209:        c->session = NULL;
1.45      nicm      210:        c->last_session = NULL;
1.261     nicm      211:
1.1       nicm      212:        c->tty.sx = 80;
                    213:        c->tty.sy = 24;
                    214:
1.271     nicm      215:        status_init(c);
1.1       nicm      216:
                    217:        c->message_string = NULL;
1.136     nicm      218:        TAILQ_INIT(&c->message_log);
1.1       nicm      219:
                    220:        c->prompt_string = NULL;
                    221:        c->prompt_buffer = NULL;
                    222:        c->prompt_index = 0;
                    223:
1.93      nicm      224:        c->flags |= CLIENT_FOCUSED;
                    225:
1.132     nicm      226:        c->keytable = key_bindings_get_table("root", 1);
                    227:        c->keytable->references++;
                    228:
1.17      nicm      229:        evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
1.192     nicm      230:        evtimer_set(&c->click_timer, server_client_click_timer, c);
1.17      nicm      231:
1.135     nicm      232:        TAILQ_INSERT_TAIL(&clients, c, entry);
1.157     nicm      233:        log_debug("new client %p", c);
1.246     nicm      234:        return (c);
1.72      nicm      235: }
                    236:
                    237: /* Open client terminal if needed. */
                    238: int
1.119     nicm      239: server_client_open(struct client *c, char **cause)
1.72      nicm      240: {
1.75      nicm      241:        if (c->flags & CLIENT_CONTROL)
                    242:                return (0);
1.120     nicm      243:
                    244:        if (strcmp(c->ttyname, "/dev/tty") == 0) {
                    245:                *cause = xstrdup("can't use /dev/tty");
                    246:                return (-1);
                    247:        }
1.75      nicm      248:
1.72      nicm      249:        if (!(c->flags & CLIENT_TERMINAL)) {
1.116     nicm      250:                *cause = xstrdup("not a terminal");
1.72      nicm      251:                return (-1);
                    252:        }
                    253:
1.119     nicm      254:        if (tty_open(&c->tty, cause) != 0)
1.72      nicm      255:                return (-1);
                    256:
                    257:        return (0);
1.1       nicm      258: }
                    259:
                    260: /* Lost a client. */
                    261: void
                    262: server_client_lost(struct client *c)
                    263: {
1.136     nicm      264:        struct message_entry    *msg, *msg1;
1.1       nicm      265:
1.141     nicm      266:        c->flags |= CLIENT_DEAD;
                    267:
1.281   ! nicm      268:        server_client_clear_overlay(c);
1.141     nicm      269:        status_prompt_clear(c);
                    270:        status_message_clear(c);
                    271:
                    272:        if (c->stdin_callback != NULL)
                    273:                c->stdin_callback(c, 1, c->stdin_callback_data);
                    274:
1.135     nicm      275:        TAILQ_REMOVE(&clients, c, entry);
1.157     nicm      276:        log_debug("lost client %p", c);
1.1       nicm      277:
                    278:        /*
                    279:         * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
                    280:         * and tty_free might close an unrelated fd.
                    281:         */
                    282:        if (c->flags & CLIENT_TERMINAL)
                    283:                tty_free(&c->tty);
1.109     nicm      284:        free(c->ttyname);
                    285:        free(c->term);
1.1       nicm      286:
1.113     nicm      287:        evbuffer_free(c->stdin_data);
                    288:        evbuffer_free(c->stdout_data);
1.97      nicm      289:        if (c->stderr_data != c->stdout_data)
1.126     nicm      290:                evbuffer_free(c->stderr_data);
1.33      nicm      291:
1.270     nicm      292:        status_free(c);
1.1       nicm      293:
1.77      nicm      294:        free(c->title);
1.165     nicm      295:        free((void *)c->cwd);
1.1       nicm      296:
1.17      nicm      297:        evtimer_del(&c->repeat_timer);
1.192     nicm      298:        evtimer_del(&c->click_timer);
1.17      nicm      299:
1.132     nicm      300:        key_bindings_unref_table(c->keytable);
                    301:
1.77      nicm      302:        free(c->message_string);
1.116     nicm      303:        if (event_initialized(&c->message_timer))
1.69      nicm      304:                evtimer_del(&c->message_timer);
1.136     nicm      305:        TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
1.77      nicm      306:                free(msg->msg);
1.136     nicm      307:                TAILQ_REMOVE(&c->message_log, msg, entry);
                    308:                free(msg);
1.21      nicm      309:        }
1.1       nicm      310:
1.259     nicm      311:        free(c->prompt_saved);
1.77      nicm      312:        free(c->prompt_string);
                    313:        free(c->prompt_buffer);
1.61      nicm      314:
1.228     nicm      315:        format_lost_client(c);
1.164     nicm      316:        environ_free(c->environ);
1.1       nicm      317:
1.162     nicm      318:        proc_remove_peer(c->peer);
                    319:        c->peer = NULL;
1.13      nicm      320:
1.142     nicm      321:        server_client_unref(c);
1.71      nicm      322:
                    323:        server_add_accept(0); /* may be more file descriptors now */
1.1       nicm      324:
                    325:        recalculate_sizes();
1.41      nicm      326:        server_check_unattached();
1.19      nicm      327:        server_update_socket();
1.141     nicm      328: }
                    329:
                    330: /* Remove reference from a client. */
                    331: void
1.142     nicm      332: server_client_unref(struct client *c)
1.141     nicm      333: {
1.157     nicm      334:        log_debug("unref client %p (%d references)", c, c->references);
1.141     nicm      335:
                    336:        c->references--;
                    337:        if (c->references == 0)
                    338:                event_once(-1, EV_TIMEOUT, server_client_free, c, NULL);
                    339: }
                    340:
                    341: /* Free dead client. */
1.190     nicm      342: static void
1.170     nicm      343: server_client_free(__unused int fd, __unused short events, void *arg)
1.141     nicm      344: {
                    345:        struct client   *c = arg;
                    346:
1.157     nicm      347:        log_debug("free client %p (%d references)", c, c->references);
1.141     nicm      348:
1.194     nicm      349:        if (!TAILQ_EMPTY(&c->queue))
                    350:                fatalx("queue not empty");
                    351:
1.216     nicm      352:        if (c->references == 0) {
                    353:                free((void *)c->name);
1.141     nicm      354:                free(c);
1.216     nicm      355:        }
1.9       nicm      356: }
                    357:
1.220     nicm      358: /* Suspend a client. */
                    359: void
                    360: server_client_suspend(struct client *c)
                    361: {
1.232     nicm      362:        struct session  *s = c->session;
1.220     nicm      363:
                    364:        if (s == NULL || (c->flags & CLIENT_DETACHING))
                    365:                return;
                    366:
                    367:        tty_stop_tty(&c->tty);
                    368:        c->flags |= CLIENT_SUSPENDED;
                    369:        proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0);
                    370: }
                    371:
1.174     nicm      372: /* Detach a client. */
                    373: void
                    374: server_client_detach(struct client *c, enum msgtype msgtype)
                    375: {
1.232     nicm      376:        struct session  *s = c->session;
1.174     nicm      377:
1.220     nicm      378:        if (s == NULL || (c->flags & CLIENT_DETACHING))
1.174     nicm      379:                return;
                    380:
1.220     nicm      381:        c->flags |= CLIENT_DETACHING;
1.196     nicm      382:        notify_client("client-detached", c);
1.240     nicm      383:        proc_send(c->peer, msgtype, -1, s->name, strlen(s->name) + 1);
1.207     nicm      384: }
                    385:
1.208     nicm      386: /* Execute command to replace a client. */
1.207     nicm      387: void
                    388: server_client_exec(struct client *c, const char *cmd)
                    389: {
                    390:        struct session  *s = c->session;
1.208     nicm      391:        char            *msg;
                    392:        const char      *shell;
1.207     nicm      393:        size_t           cmdsize, shellsize;
                    394:
                    395:        if (*cmd == '\0')
                    396:                return;
                    397:        cmdsize = strlen(cmd) + 1;
                    398:
                    399:        if (s != NULL)
                    400:                shell = options_get_string(s->options, "default-shell");
                    401:        else
                    402:                shell = options_get_string(global_s_options, "default-shell");
                    403:        shellsize = strlen(shell) + 1;
                    404:
                    405:        msg = xmalloc(cmdsize + shellsize);
                    406:        memcpy(msg, cmd, cmdsize);
                    407:        memcpy(msg + cmdsize, shell, shellsize);
                    408:
                    409:        proc_send(c->peer, MSG_EXEC, -1, msg, cmdsize + shellsize);
                    410:        free(msg);
1.174     nicm      411: }
                    412:
1.66      nicm      413: /* Check for mouse keys. */
1.190     nicm      414: static key_code
1.276     nicm      415: server_client_check_mouse(struct client *c, struct key_event *event)
1.66      nicm      416: {
1.276     nicm      417:        struct mouse_event      *m = &event->m;
1.192     nicm      418:        struct session          *s = c->session;
1.272     nicm      419:        struct winlink          *wl;
1.192     nicm      420:        struct window_pane      *wp;
1.261     nicm      421:        u_int                    x, y, b, sx, sy, px, py;
1.192     nicm      422:        int                      flag;
                    423:        key_code                 key;
                    424:        struct timeval           tv;
1.272     nicm      425:        struct style_range      *sr;
1.209     nicm      426:        enum { NOTYPE, MOVE, DOWN, UP, DRAG, WHEEL, DOUBLE, TRIPLE } type;
1.276     nicm      427:        enum { NOWHERE,
                    428:               PANE,
                    429:               STATUS,
                    430:               STATUS_LEFT,
                    431:               STATUS_RIGHT,
                    432:               STATUS_DEFAULT,
                    433:               BORDER } where;
1.209     nicm      434:
                    435:        type = NOTYPE;
                    436:        where = NOWHERE;
1.131     nicm      437:
1.261     nicm      438:        log_debug("%s mouse %02x at %u,%u (last %u,%u) (%d)", c->name, m->b,
                    439:            m->x, m->y, m->lx, m->ly, c->tty.mouse_drag_flag);
1.131     nicm      440:
                    441:        /* What type of event is this? */
1.209     nicm      442:        if ((m->sgr_type != ' ' &&
                    443:            MOUSE_DRAG(m->sgr_b) &&
                    444:            MOUSE_BUTTONS(m->sgr_b) == 3) ||
                    445:            (m->sgr_type == ' ' &&
                    446:            MOUSE_DRAG(m->b) &&
                    447:            MOUSE_BUTTONS(m->b) == 3 &&
                    448:            MOUSE_BUTTONS(m->lb) == 3)) {
                    449:                type = MOVE;
                    450:                x = m->x, y = m->y, b = 0;
                    451:                log_debug("move at %u,%u", x, y);
                    452:        } else if (MOUSE_DRAG(m->b)) {
1.131     nicm      453:                type = DRAG;
                    454:                if (c->tty.mouse_drag_flag) {
                    455:                        x = m->x, y = m->y, b = m->b;
1.278     nicm      456:                        if (x == m->lx && y == m->ly)
                    457:                                return (KEYC_UNKNOWN);
1.131     nicm      458:                        log_debug("drag update at %u,%u", x, y);
                    459:                } else {
1.277     nicm      460:                        x = m->lx, y = m->ly, b = m->lb;
1.131     nicm      461:                        log_debug("drag start at %u,%u", x, y);
                    462:                }
                    463:        } else if (MOUSE_WHEEL(m->b)) {
                    464:                type = WHEEL;
                    465:                x = m->x, y = m->y, b = m->b;
                    466:                log_debug("wheel at %u,%u", x, y);
1.200     nicm      467:        } else if (MOUSE_RELEASE(m->b)) {
1.131     nicm      468:                type = UP;
                    469:                x = m->x, y = m->y, b = m->lb;
                    470:                log_debug("up at %u,%u", x, y);
                    471:        } else {
1.192     nicm      472:                if (c->flags & CLIENT_DOUBLECLICK) {
                    473:                        evtimer_del(&c->click_timer);
                    474:                        c->flags &= ~CLIENT_DOUBLECLICK;
                    475:                        if (m->b == c->click_button) {
                    476:                                type = DOUBLE;
                    477:                                x = m->x, y = m->y, b = m->b;
                    478:                                log_debug("double-click at %u,%u", x, y);
                    479:                                flag = CLIENT_TRIPLECLICK;
                    480:                                goto add_timer;
                    481:                        }
                    482:                } else if (c->flags & CLIENT_TRIPLECLICK) {
                    483:                        evtimer_del(&c->click_timer);
                    484:                        c->flags &= ~CLIENT_TRIPLECLICK;
                    485:                        if (m->b == c->click_button) {
                    486:                                type = TRIPLE;
                    487:                                x = m->x, y = m->y, b = m->b;
                    488:                                log_debug("triple-click at %u,%u", x, y);
                    489:                                goto have_event;
                    490:                        }
                    491:                }
                    492:
1.131     nicm      493:                type = DOWN;
                    494:                x = m->x, y = m->y, b = m->b;
                    495:                log_debug("down at %u,%u", x, y);
1.192     nicm      496:                flag = CLIENT_DOUBLECLICK;
                    497:
                    498:        add_timer:
                    499:                if (KEYC_CLICK_TIMEOUT != 0) {
                    500:                        c->flags |= flag;
                    501:                        c->click_button = m->b;
                    502:
                    503:                        tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000;
                    504:                        tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L;
                    505:                        evtimer_del(&c->click_timer);
                    506:                        evtimer_add(&c->click_timer, &tv);
                    507:                }
1.131     nicm      508:        }
1.192     nicm      509:
                    510: have_event:
1.131     nicm      511:        if (type == NOTYPE)
1.177     nicm      512:                return (KEYC_UNKNOWN);
1.131     nicm      513:
1.258     nicm      514:        /* Save the session. */
1.131     nicm      515:        m->s = s->id;
1.258     nicm      516:        m->w = -1;
1.131     nicm      517:
                    518:        /* Is this on the status line? */
                    519:        m->statusat = status_at_line(c);
1.272     nicm      520:        if (m->statusat != -1 &&
                    521:            y >= (u_int)m->statusat &&
1.273     nicm      522:            y < m->statusat + status_line_size(c)) {
1.272     nicm      523:                sr = status_get_range(c, x, y - m->statusat);
1.274     nicm      524:                if (sr == NULL) {
                    525:                        where = STATUS_DEFAULT;
                    526:                } else {
                    527:                        switch (sr->type) {
                    528:                        case STYLE_RANGE_NONE:
1.273     nicm      529:                                return (KEYC_UNKNOWN);
1.274     nicm      530:                        case STYLE_RANGE_LEFT:
                    531:                                where = STATUS_LEFT;
                    532:                                break;
                    533:                        case STYLE_RANGE_RIGHT:
                    534:                                where = STATUS_RIGHT;
                    535:                                break;
                    536:                        case STYLE_RANGE_WINDOW:
                    537:                                wl = winlink_find_by_index(&s->windows, sr->argument);
                    538:                                if (wl == NULL)
                    539:                                        return (KEYC_UNKNOWN);
                    540:                                m->w = wl->window->id;
1.273     nicm      541:
1.274     nicm      542:                                where = STATUS;
                    543:                                break;
                    544:                        }
1.258     nicm      545:                }
                    546:        }
1.131     nicm      547:
                    548:        /* Not on status line. Adjust position and check for border or pane. */
                    549:        if (where == NOWHERE) {
1.261     nicm      550:                px = x;
1.131     nicm      551:                if (m->statusat == 0 && y > 0)
1.261     nicm      552:                        py = y - 1;
1.131     nicm      553:                else if (m->statusat > 0 && y >= (u_int)m->statusat)
1.261     nicm      554:                        py = m->statusat - 1;
                    555:                else
                    556:                        py = y;
                    557:
                    558:                tty_window_offset(&c->tty, &m->ox, &m->oy, &sx, &sy);
                    559:                log_debug("mouse window @%u at %u,%u (%ux%u)",
                    560:                    s->curw->window->id, m->ox, m->oy, sx, sy);
                    561:                if (px > sx || py > sy)
                    562:                        return (KEYC_UNKNOWN);
                    563:                px = px + m->ox;
                    564:                py = py + m->oy;
1.131     nicm      565:
1.260     nicm      566:                /* Try the pane borders if not zoomed. */
                    567:                if (~s->curw->window->flags & WINDOW_ZOOMED) {
                    568:                        TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
1.261     nicm      569:                                if ((wp->xoff + wp->sx == px &&
                    570:                                    wp->yoff <= 1 + py &&
                    571:                                    wp->yoff + wp->sy >= py) ||
                    572:                                    (wp->yoff + wp->sy == py &&
                    573:                                    wp->xoff <= 1 + px &&
                    574:                                    wp->xoff + wp->sx >= px))
1.260     nicm      575:                                        break;
                    576:                        }
                    577:                        if (wp != NULL)
                    578:                                where = BORDER;
1.131     nicm      579:                }
1.260     nicm      580:
                    581:                /* Otherwise try inside the pane. */
                    582:                if (where == NOWHERE) {
1.261     nicm      583:                        wp = window_get_active_at(s->curw->window, px, py);
1.260     nicm      584:                        if (wp != NULL)
1.131     nicm      585:                                where = PANE;
                    586:                }
1.260     nicm      587:
1.131     nicm      588:                if (where == NOWHERE)
1.177     nicm      589:                        return (KEYC_UNKNOWN);
1.260     nicm      590:                if (where == PANE)
                    591:                        log_debug("mouse %u,%u on pane %%%u", x, y, wp->id);
                    592:                else if (where == BORDER)
                    593:                        log_debug("mouse on pane %%%u border", wp->id);
1.131     nicm      594:                m->wp = wp->id;
                    595:                m->w = wp->window->id;
                    596:        } else
                    597:                m->wp = -1;
                    598:
                    599:        /* Stop dragging if needed. */
1.200     nicm      600:        if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag) {
1.131     nicm      601:                if (c->tty.mouse_drag_release != NULL)
                    602:                        c->tty.mouse_drag_release(c, m);
                    603:
                    604:                c->tty.mouse_drag_update = NULL;
                    605:                c->tty.mouse_drag_release = NULL;
                    606:
1.182     nicm      607:                /*
1.183     nicm      608:                 * End a mouse drag by passing a MouseDragEnd key corresponding
                    609:                 * to the button that started the drag.
1.182     nicm      610:                 */
                    611:                switch (c->tty.mouse_drag_flag) {
                    612:                case 1:
                    613:                        if (where == PANE)
1.183     nicm      614:                                key = KEYC_MOUSEDRAGEND1_PANE;
1.182     nicm      615:                        if (where == STATUS)
1.183     nicm      616:                                key = KEYC_MOUSEDRAGEND1_STATUS;
1.258     nicm      617:                        if (where == STATUS_LEFT)
                    618:                                key = KEYC_MOUSEDRAGEND1_STATUS_LEFT;
                    619:                        if (where == STATUS_RIGHT)
                    620:                                key = KEYC_MOUSEDRAGEND1_STATUS_RIGHT;
1.274     nicm      621:                        if (where == STATUS_DEFAULT)
                    622:                                key = KEYC_MOUSEDRAGEND1_STATUS_DEFAULT;
1.182     nicm      623:                        if (where == BORDER)
1.183     nicm      624:                                key = KEYC_MOUSEDRAGEND1_BORDER;
1.182     nicm      625:                        break;
                    626:                case 2:
                    627:                        if (where == PANE)
1.183     nicm      628:                                key = KEYC_MOUSEDRAGEND2_PANE;
1.182     nicm      629:                        if (where == STATUS)
1.183     nicm      630:                                key = KEYC_MOUSEDRAGEND2_STATUS;
1.258     nicm      631:                        if (where == STATUS_LEFT)
                    632:                                key = KEYC_MOUSEDRAGEND2_STATUS_LEFT;
                    633:                        if (where == STATUS_RIGHT)
                    634:                                key = KEYC_MOUSEDRAGEND2_STATUS_RIGHT;
1.274     nicm      635:                        if (where == STATUS_DEFAULT)
                    636:                                key = KEYC_MOUSEDRAGEND2_STATUS_DEFAULT;
1.182     nicm      637:                        if (where == BORDER)
1.183     nicm      638:                                key = KEYC_MOUSEDRAGEND2_BORDER;
1.182     nicm      639:                        break;
                    640:                case 3:
                    641:                        if (where == PANE)
1.183     nicm      642:                                key = KEYC_MOUSEDRAGEND3_PANE;
1.182     nicm      643:                        if (where == STATUS)
1.183     nicm      644:                                key = KEYC_MOUSEDRAGEND3_STATUS;
1.258     nicm      645:                        if (where == STATUS_LEFT)
                    646:                                key = KEYC_MOUSEDRAGEND3_STATUS_LEFT;
                    647:                        if (where == STATUS_RIGHT)
                    648:                                key = KEYC_MOUSEDRAGEND3_STATUS_RIGHT;
1.274     nicm      649:                        if (where == STATUS_DEFAULT)
                    650:                                key = KEYC_MOUSEDRAGEND3_STATUS_DEFAULT;
1.182     nicm      651:                        if (where == BORDER)
1.183     nicm      652:                                key = KEYC_MOUSEDRAGEND3_BORDER;
1.182     nicm      653:                        break;
                    654:                default:
                    655:                        key = KEYC_MOUSE;
                    656:                        break;
                    657:                }
1.131     nicm      658:                c->tty.mouse_drag_flag = 0;
1.182     nicm      659:
                    660:                return (key);
1.131     nicm      661:        }
                    662:
                    663:        /* Convert to a key binding. */
1.177     nicm      664:        key = KEYC_UNKNOWN;
1.131     nicm      665:        switch (type) {
                    666:        case NOTYPE:
                    667:                break;
1.209     nicm      668:        case MOVE:
                    669:                if (where == PANE)
                    670:                        key = KEYC_MOUSEMOVE_PANE;
                    671:                if (where == STATUS)
                    672:                        key = KEYC_MOUSEMOVE_STATUS;
1.274     nicm      673:                if (where == STATUS_LEFT)
                    674:                        key = KEYC_MOUSEMOVE_STATUS_LEFT;
                    675:                if (where == STATUS_RIGHT)
                    676:                        key = KEYC_MOUSEMOVE_STATUS_RIGHT;
                    677:                if (where == STATUS_DEFAULT)
                    678:                        key = KEYC_MOUSEMOVE_STATUS_DEFAULT;
1.209     nicm      679:                if (where == BORDER)
                    680:                        key = KEYC_MOUSEMOVE_BORDER;
                    681:                break;
1.131     nicm      682:        case DRAG:
1.204     nicm      683:                if (c->tty.mouse_drag_update != NULL)
                    684:                        key = KEYC_DRAGGING;
                    685:                else {
1.131     nicm      686:                        switch (MOUSE_BUTTONS(b)) {
                    687:                        case 0:
                    688:                                if (where == PANE)
                    689:                                        key = KEYC_MOUSEDRAG1_PANE;
                    690:                                if (where == STATUS)
                    691:                                        key = KEYC_MOUSEDRAG1_STATUS;
1.258     nicm      692:                                if (where == STATUS_LEFT)
                    693:                                        key = KEYC_MOUSEDRAG1_STATUS_LEFT;
                    694:                                if (where == STATUS_RIGHT)
                    695:                                        key = KEYC_MOUSEDRAG1_STATUS_RIGHT;
1.274     nicm      696:                                if (where == STATUS_DEFAULT)
                    697:                                        key = KEYC_MOUSEDRAG1_STATUS_DEFAULT;
1.131     nicm      698:                                if (where == BORDER)
                    699:                                        key = KEYC_MOUSEDRAG1_BORDER;
                    700:                                break;
                    701:                        case 1:
                    702:                                if (where == PANE)
                    703:                                        key = KEYC_MOUSEDRAG2_PANE;
                    704:                                if (where == STATUS)
                    705:                                        key = KEYC_MOUSEDRAG2_STATUS;
1.258     nicm      706:                                if (where == STATUS_LEFT)
                    707:                                        key = KEYC_MOUSEDRAG2_STATUS_LEFT;
                    708:                                if (where == STATUS_RIGHT)
                    709:                                        key = KEYC_MOUSEDRAG2_STATUS_RIGHT;
1.274     nicm      710:                                if (where == STATUS_DEFAULT)
                    711:                                        key = KEYC_MOUSEDRAG2_STATUS_DEFAULT;
1.131     nicm      712:                                if (where == BORDER)
                    713:                                        key = KEYC_MOUSEDRAG2_BORDER;
                    714:                                break;
                    715:                        case 2:
                    716:                                if (where == PANE)
                    717:                                        key = KEYC_MOUSEDRAG3_PANE;
                    718:                                if (where == STATUS)
                    719:                                        key = KEYC_MOUSEDRAG3_STATUS;
1.258     nicm      720:                                if (where == STATUS_LEFT)
                    721:                                        key = KEYC_MOUSEDRAG3_STATUS_LEFT;
                    722:                                if (where == STATUS_RIGHT)
                    723:                                        key = KEYC_MOUSEDRAG3_STATUS_RIGHT;
1.274     nicm      724:                                if (where == STATUS_DEFAULT)
                    725:                                        key = KEYC_MOUSEDRAG3_STATUS_DEFAULT;
1.131     nicm      726:                                if (where == BORDER)
                    727:                                        key = KEYC_MOUSEDRAG3_BORDER;
                    728:                                break;
                    729:                        }
                    730:                }
1.66      nicm      731:
1.182     nicm      732:                /*
                    733:                 * Begin a drag by setting the flag to a non-zero value that
                    734:                 * corresponds to the mouse button in use.
                    735:                 */
                    736:                c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1;
1.131     nicm      737:                break;
                    738:        case WHEEL:
                    739:                if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
                    740:                        if (where == PANE)
                    741:                                key = KEYC_WHEELUP_PANE;
                    742:                        if (where == STATUS)
                    743:                                key = KEYC_WHEELUP_STATUS;
1.258     nicm      744:                        if (where == STATUS_LEFT)
                    745:                                key = KEYC_WHEELUP_STATUS_LEFT;
                    746:                        if (where == STATUS_RIGHT)
                    747:                                key = KEYC_WHEELUP_STATUS_RIGHT;
1.274     nicm      748:                        if (where == STATUS_DEFAULT)
                    749:                                key = KEYC_WHEELUP_STATUS_DEFAULT;
1.131     nicm      750:                        if (where == BORDER)
                    751:                                key = KEYC_WHEELUP_BORDER;
                    752:                } else {
                    753:                        if (where == PANE)
                    754:                                key = KEYC_WHEELDOWN_PANE;
                    755:                        if (where == STATUS)
                    756:                                key = KEYC_WHEELDOWN_STATUS;
1.274     nicm      757:                        if (where == STATUS_LEFT)
                    758:                                key = KEYC_WHEELDOWN_STATUS_LEFT;
                    759:                        if (where == STATUS_RIGHT)
                    760:                                key = KEYC_WHEELDOWN_STATUS_RIGHT;
                    761:                        if (where == STATUS_DEFAULT)
                    762:                                key = KEYC_WHEELDOWN_STATUS_DEFAULT;
1.131     nicm      763:                        if (where == BORDER)
                    764:                                key = KEYC_WHEELDOWN_BORDER;
                    765:                }
                    766:                break;
                    767:        case UP:
                    768:                switch (MOUSE_BUTTONS(b)) {
                    769:                case 0:
                    770:                        if (where == PANE)
                    771:                                key = KEYC_MOUSEUP1_PANE;
                    772:                        if (where == STATUS)
                    773:                                key = KEYC_MOUSEUP1_STATUS;
1.258     nicm      774:                        if (where == STATUS_LEFT)
                    775:                                key = KEYC_MOUSEUP1_STATUS_LEFT;
                    776:                        if (where == STATUS_RIGHT)
                    777:                                key = KEYC_MOUSEUP1_STATUS_RIGHT;
1.274     nicm      778:                        if (where == STATUS_DEFAULT)
                    779:                                key = KEYC_MOUSEUP1_STATUS_DEFAULT;
1.131     nicm      780:                        if (where == BORDER)
                    781:                                key = KEYC_MOUSEUP1_BORDER;
                    782:                        break;
                    783:                case 1:
                    784:                        if (where == PANE)
                    785:                                key = KEYC_MOUSEUP2_PANE;
                    786:                        if (where == STATUS)
                    787:                                key = KEYC_MOUSEUP2_STATUS;
1.258     nicm      788:                        if (where == STATUS_LEFT)
                    789:                                key = KEYC_MOUSEUP2_STATUS_LEFT;
                    790:                        if (where == STATUS_RIGHT)
                    791:                                key = KEYC_MOUSEUP2_STATUS_RIGHT;
1.274     nicm      792:                        if (where == STATUS_DEFAULT)
                    793:                                key = KEYC_MOUSEUP2_STATUS_DEFAULT;
1.131     nicm      794:                        if (where == BORDER)
                    795:                                key = KEYC_MOUSEUP2_BORDER;
                    796:                        break;
                    797:                case 2:
                    798:                        if (where == PANE)
                    799:                                key = KEYC_MOUSEUP3_PANE;
                    800:                        if (where == STATUS)
                    801:                                key = KEYC_MOUSEUP3_STATUS;
1.258     nicm      802:                        if (where == STATUS_LEFT)
                    803:                                key = KEYC_MOUSEUP3_STATUS_LEFT;
                    804:                        if (where == STATUS_RIGHT)
                    805:                                key = KEYC_MOUSEUP3_STATUS_RIGHT;
1.274     nicm      806:                        if (where == STATUS_DEFAULT)
                    807:                                key = KEYC_MOUSEUP3_STATUS_DEFAULT;
1.131     nicm      808:                        if (where == BORDER)
                    809:                                key = KEYC_MOUSEUP3_BORDER;
                    810:                        break;
                    811:                }
                    812:                break;
                    813:        case DOWN:
                    814:                switch (MOUSE_BUTTONS(b)) {
                    815:                case 0:
                    816:                        if (where == PANE)
                    817:                                key = KEYC_MOUSEDOWN1_PANE;
                    818:                        if (where == STATUS)
                    819:                                key = KEYC_MOUSEDOWN1_STATUS;
1.258     nicm      820:                        if (where == STATUS_LEFT)
                    821:                                key = KEYC_MOUSEDOWN1_STATUS_LEFT;
                    822:                        if (where == STATUS_RIGHT)
                    823:                                key = KEYC_MOUSEDOWN1_STATUS_RIGHT;
1.274     nicm      824:                        if (where == STATUS_DEFAULT)
                    825:                                key = KEYC_MOUSEDOWN1_STATUS_DEFAULT;
1.131     nicm      826:                        if (where == BORDER)
                    827:                                key = KEYC_MOUSEDOWN1_BORDER;
                    828:                        break;
                    829:                case 1:
                    830:                        if (where == PANE)
                    831:                                key = KEYC_MOUSEDOWN2_PANE;
                    832:                        if (where == STATUS)
                    833:                                key = KEYC_MOUSEDOWN2_STATUS;
1.258     nicm      834:                        if (where == STATUS_LEFT)
                    835:                                key = KEYC_MOUSEDOWN2_STATUS_LEFT;
                    836:                        if (where == STATUS_RIGHT)
                    837:                                key = KEYC_MOUSEDOWN2_STATUS_RIGHT;
1.274     nicm      838:                        if (where == STATUS_DEFAULT)
                    839:                                key = KEYC_MOUSEDOWN2_STATUS_DEFAULT;
1.131     nicm      840:                        if (where == BORDER)
                    841:                                key = KEYC_MOUSEDOWN2_BORDER;
                    842:                        break;
                    843:                case 2:
                    844:                        if (where == PANE)
                    845:                                key = KEYC_MOUSEDOWN3_PANE;
                    846:                        if (where == STATUS)
                    847:                                key = KEYC_MOUSEDOWN3_STATUS;
1.258     nicm      848:                        if (where == STATUS_LEFT)
                    849:                                key = KEYC_MOUSEDOWN3_STATUS_LEFT;
                    850:                        if (where == STATUS_RIGHT)
                    851:                                key = KEYC_MOUSEDOWN3_STATUS_RIGHT;
1.274     nicm      852:                        if (where == STATUS_DEFAULT)
                    853:                                key = KEYC_MOUSEDOWN3_STATUS_DEFAULT;
1.131     nicm      854:                        if (where == BORDER)
                    855:                                key = KEYC_MOUSEDOWN3_BORDER;
                    856:                        break;
1.66      nicm      857:                }
1.131     nicm      858:                break;
1.192     nicm      859:        case DOUBLE:
                    860:                switch (MOUSE_BUTTONS(b)) {
                    861:                case 0:
                    862:                        if (where == PANE)
                    863:                                key = KEYC_DOUBLECLICK1_PANE;
                    864:                        if (where == STATUS)
                    865:                                key = KEYC_DOUBLECLICK1_STATUS;
1.258     nicm      866:                        if (where == STATUS_LEFT)
                    867:                                key = KEYC_DOUBLECLICK1_STATUS_LEFT;
                    868:                        if (where == STATUS_RIGHT)
                    869:                                key = KEYC_DOUBLECLICK1_STATUS_RIGHT;
1.274     nicm      870:                        if (where == STATUS_DEFAULT)
                    871:                                key = KEYC_DOUBLECLICK1_STATUS_DEFAULT;
1.192     nicm      872:                        if (where == BORDER)
                    873:                                key = KEYC_DOUBLECLICK1_BORDER;
                    874:                        break;
                    875:                case 1:
                    876:                        if (where == PANE)
                    877:                                key = KEYC_DOUBLECLICK2_PANE;
                    878:                        if (where == STATUS)
                    879:                                key = KEYC_DOUBLECLICK2_STATUS;
1.258     nicm      880:                        if (where == STATUS_LEFT)
                    881:                                key = KEYC_DOUBLECLICK2_STATUS_LEFT;
                    882:                        if (where == STATUS_RIGHT)
                    883:                                key = KEYC_DOUBLECLICK2_STATUS_RIGHT;
1.274     nicm      884:                        if (where == STATUS_DEFAULT)
                    885:                                key = KEYC_DOUBLECLICK2_STATUS_DEFAULT;
1.192     nicm      886:                        if (where == BORDER)
                    887:                                key = KEYC_DOUBLECLICK2_BORDER;
                    888:                        break;
                    889:                case 2:
                    890:                        if (where == PANE)
                    891:                                key = KEYC_DOUBLECLICK3_PANE;
                    892:                        if (where == STATUS)
                    893:                                key = KEYC_DOUBLECLICK3_STATUS;
1.258     nicm      894:                        if (where == STATUS_LEFT)
                    895:                                key = KEYC_DOUBLECLICK3_STATUS_LEFT;
                    896:                        if (where == STATUS_RIGHT)
                    897:                                key = KEYC_DOUBLECLICK3_STATUS_RIGHT;
1.274     nicm      898:                        if (where == STATUS_DEFAULT)
                    899:                                key = KEYC_DOUBLECLICK3_STATUS_DEFAULT;
1.192     nicm      900:                        if (where == BORDER)
                    901:                                key = KEYC_DOUBLECLICK3_BORDER;
                    902:                        break;
                    903:                }
                    904:                break;
                    905:        case TRIPLE:
                    906:                switch (MOUSE_BUTTONS(b)) {
                    907:                case 0:
                    908:                        if (where == PANE)
                    909:                                key = KEYC_TRIPLECLICK1_PANE;
                    910:                        if (where == STATUS)
                    911:                                key = KEYC_TRIPLECLICK1_STATUS;
1.258     nicm      912:                        if (where == STATUS_LEFT)
                    913:                                key = KEYC_TRIPLECLICK1_STATUS_LEFT;
                    914:                        if (where == STATUS_RIGHT)
                    915:                                key = KEYC_TRIPLECLICK1_STATUS_RIGHT;
1.274     nicm      916:                        if (where == STATUS_DEFAULT)
                    917:                                key = KEYC_TRIPLECLICK1_STATUS_DEFAULT;
1.192     nicm      918:                        if (where == BORDER)
                    919:                                key = KEYC_TRIPLECLICK1_BORDER;
                    920:                        break;
                    921:                case 1:
                    922:                        if (where == PANE)
                    923:                                key = KEYC_TRIPLECLICK2_PANE;
                    924:                        if (where == STATUS)
                    925:                                key = KEYC_TRIPLECLICK2_STATUS;
1.258     nicm      926:                        if (where == STATUS_LEFT)
                    927:                                key = KEYC_TRIPLECLICK2_STATUS_LEFT;
                    928:                        if (where == STATUS_RIGHT)
                    929:                                key = KEYC_TRIPLECLICK2_STATUS_RIGHT;
1.274     nicm      930:                        if (where == STATUS_DEFAULT)
                    931:                                key = KEYC_TRIPLECLICK2_STATUS_DEFAULT;
1.192     nicm      932:                        if (where == BORDER)
                    933:                                key = KEYC_TRIPLECLICK2_BORDER;
                    934:                        break;
                    935:                case 2:
                    936:                        if (where == PANE)
                    937:                                key = KEYC_TRIPLECLICK3_PANE;
                    938:                        if (where == STATUS)
                    939:                                key = KEYC_TRIPLECLICK3_STATUS;
1.258     nicm      940:                        if (where == STATUS_LEFT)
                    941:                                key = KEYC_TRIPLECLICK3_STATUS_LEFT;
                    942:                        if (where == STATUS_RIGHT)
                    943:                                key = KEYC_TRIPLECLICK3_STATUS_RIGHT;
1.274     nicm      944:                        if (where == STATUS_DEFAULT)
                    945:                                key = KEYC_TRIPLECLICK3_STATUS_DEFAULT;
1.192     nicm      946:                        if (where == BORDER)
                    947:                                key = KEYC_TRIPLECLICK3_BORDER;
                    948:                        break;
                    949:                }
                    950:                break;
1.66      nicm      951:        }
1.177     nicm      952:        if (key == KEYC_UNKNOWN)
                    953:                return (KEYC_UNKNOWN);
1.66      nicm      954:
1.131     nicm      955:        /* Apply modifiers if any. */
                    956:        if (b & MOUSE_MASK_META)
                    957:                key |= KEYC_ESCAPE;
                    958:        if (b & MOUSE_MASK_CTRL)
                    959:                key |= KEYC_CTRL;
                    960:        if (b & MOUSE_MASK_SHIFT)
                    961:                key |= KEYC_SHIFT;
1.66      nicm      962:
1.131     nicm      963:        return (key);
1.66      nicm      964: }
                    965:
1.82      nicm      966: /* Is this fast enough to probably be a paste? */
1.190     nicm      967: static int
1.82      nicm      968: server_client_assume_paste(struct session *s)
                    969: {
                    970:        struct timeval  tv;
1.84      nicm      971:        int             t;
1.82      nicm      972:
1.163     nicm      973:        if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1.83      nicm      974:                return (0);
1.82      nicm      975:
                    976:        timersub(&s->activity_time, &s->last_activity_time, &tv);
1.171     nicm      977:        if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
                    978:                log_debug("session %s pasting (flag %d)", s->name,
                    979:                    !!(s->flags & SESSION_PASTING));
                    980:                if (s->flags & SESSION_PASTING)
                    981:                        return (1);
                    982:                s->flags |= SESSION_PASTING;
                    983:                return (0);
                    984:        }
                    985:        log_debug("session %s not pasting", s->name);
                    986:        s->flags &= ~SESSION_PASTING;
1.83      nicm      987:        return (0);
1.82      nicm      988: }
                    989:
1.276     nicm      990: /*
                    991:  * Handle data key input from client. This owns and can modify the key event it
                    992:  * is given and is responsible for freeing it.
                    993:  */
1.280     nicm      994: static enum cmd_retval
1.276     nicm      995: server_client_key_callback(struct cmdq_item *item, void *data)
1.18      nicm      996: {
1.276     nicm      997:        struct client                   *c = item->client;
                    998:        struct key_event                *event = data;
                    999:        key_code                         key = event->key;
                   1000:        struct mouse_event              *m = &event->m;
1.267     nicm     1001:        struct session                  *s = c->session;
                   1002:        struct winlink                  *wl;
                   1003:        struct window                   *w;
                   1004:        struct window_pane              *wp;
                   1005:        struct window_mode_entry        *wme;
                   1006:        struct timeval                   tv;
                   1007:        struct key_table                *table, *first;
                   1008:        struct key_binding              *bd;
                   1009:        int                              xtimeout, flags;
                   1010:        struct cmd_find_state            fs;
                   1011:        key_code                         key0;
1.18      nicm     1012:
                   1013:        /* Check the client is good to accept input. */
1.132     nicm     1014:        if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1.276     nicm     1015:                goto out;
1.264     nicm     1016:        wl = s->curw;
                   1017:        w = wl->window;
1.18      nicm     1018:
                   1019:        /* Update the activity timer. */
                   1020:        if (gettimeofday(&c->activity_time, NULL) != 0)
                   1021:                fatal("gettimeofday failed");
1.149     nicm     1022:        session_update_activity(s, &c->activity_time);
1.18      nicm     1023:
                   1024:        /* Handle status line. */
1.281   ! nicm     1025:        if (~c->flags & CLIENT_READONLY)
1.30      nicm     1026:                status_message_clear(c);
1.18      nicm     1027:        if (c->prompt_string != NULL) {
1.193     nicm     1028:                if (c->flags & CLIENT_READONLY)
1.276     nicm     1029:                        goto out;
1.193     nicm     1030:                if (status_prompt_key(c, key) == 0)
1.276     nicm     1031:                        goto out;
1.18      nicm     1032:        }
                   1033:
                   1034:        /* Check for mouse keys. */
1.204     nicm     1035:        m->valid = 0;
1.18      nicm     1036:        if (key == KEYC_MOUSE) {
1.30      nicm     1037:                if (c->flags & CLIENT_READONLY)
1.276     nicm     1038:                        goto out;
                   1039:                key = server_client_check_mouse(c, event);
1.177     nicm     1040:                if (key == KEYC_UNKNOWN)
1.276     nicm     1041:                        goto out;
1.131     nicm     1042:
                   1043:                m->valid = 1;
                   1044:                m->key = key;
1.203     nicm     1045:
                   1046:                /*
1.204     nicm     1047:                 * Mouse drag is in progress, so fire the callback (now that
                   1048:                 * the mouse event is valid).
1.203     nicm     1049:                 */
1.204     nicm     1050:                if (key == KEYC_DRAGGING) {
                   1051:                        c->tty.mouse_drag_update(c, m);
1.276     nicm     1052:                        goto out;
1.204     nicm     1053:                }
1.276     nicm     1054:        }
1.18      nicm     1055:
1.202     nicm     1056:        /* Find affected pane. */
1.243     nicm     1057:        if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m, 0) != 0)
                   1058:                cmd_find_from_session(&fs, s, 0);
1.227     nicm     1059:        wp = fs.wp;
1.202     nicm     1060:
                   1061:        /* Forward mouse keys if disabled. */
1.206     nicm     1062:        if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse"))
1.253     nicm     1063:                goto forward_key;
1.202     nicm     1064:
1.132     nicm     1065:        /* Treat everything as a regular key when pasting is detected. */
1.161     nicm     1066:        if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
1.253     nicm     1067:                goto forward_key;
1.18      nicm     1068:
1.191     nicm     1069:        /*
                   1070:         * Work out the current key table. If the pane is in a mode, use
                   1071:         * the mode table instead of the default key table.
                   1072:         */
1.223     nicm     1073:        if (server_client_is_default_key_table(c, c->keytable) &&
                   1074:            wp != NULL &&
1.267     nicm     1075:            (wme = TAILQ_FIRST(&wp->modes)) != NULL &&
                   1076:            wme->mode->key_table != NULL)
                   1077:                table = key_bindings_get_table(wme->mode->key_table(wme), 1);
1.223     nicm     1078:        else
1.191     nicm     1079:                table = c->keytable;
1.223     nicm     1080:        first = table;
1.191     nicm     1081:
1.253     nicm     1082: table_changed:
1.205     nicm     1083:        /*
                   1084:         * The prefix always takes precedence and forces a switch to the prefix
                   1085:         * table, unless we are already there.
                   1086:         */
1.252     nicm     1087:        key0 = (key & ~KEYC_XTERM);
1.239     nicm     1088:        if ((key0 == (key_code)options_get_number(s->options, "prefix") ||
                   1089:            key0 == (key_code)options_get_number(s->options, "prefix2")) &&
1.205     nicm     1090:            strcmp(table->name, "prefix") != 0) {
                   1091:                server_client_set_key_table(c, "prefix");
                   1092:                server_status_client(c);
1.276     nicm     1093:                goto out;
1.205     nicm     1094:        }
1.238     nicm     1095:        flags = c->flags;
1.205     nicm     1096:
1.262     nicm     1097: try_again:
1.223     nicm     1098:        /* Log key table. */
                   1099:        if (wp == NULL)
                   1100:                log_debug("key table %s (no pane)", table->name);
                   1101:        else
                   1102:                log_debug("key table %s (pane %%%u)", table->name, wp->id);
1.225     nicm     1103:        if (c->flags & CLIENT_REPEAT)
                   1104:                log_debug("currently repeating");
1.223     nicm     1105:
1.132     nicm     1106:        /* Try to see if there is a key binding in the current table. */
1.254     nicm     1107:        bd = key_bindings_get(table, key0);
1.132     nicm     1108:        if (bd != NULL) {
                   1109:                /*
                   1110:                 * Key was matched in this table. If currently repeating but a
                   1111:                 * non-repeating binding was found, stop repeating and try
                   1112:                 * again in the root table.
                   1113:                 */
1.222     nicm     1114:                if ((c->flags & CLIENT_REPEAT) &&
                   1115:                    (~bd->flags & KEY_BINDING_REPEAT)) {
1.262     nicm     1116:                        log_debug("found in key table %s (not repeating)",
                   1117:                            table->name);
1.178     nicm     1118:                        server_client_set_key_table(c, NULL);
1.262     nicm     1119:                        first = table = c->keytable;
1.132     nicm     1120:                        c->flags &= ~CLIENT_REPEAT;
1.85      nicm     1121:                        server_status_client(c);
1.253     nicm     1122:                        goto table_changed;
1.18      nicm     1123:                }
1.223     nicm     1124:                log_debug("found in key table %s", table->name);
1.82      nicm     1125:
1.132     nicm     1126:                /*
                   1127:                 * Take a reference to this table to make sure the key binding
                   1128:                 * doesn't disappear.
                   1129:                 */
                   1130:                table->references++;
                   1131:
                   1132:                /*
                   1133:                 * If this is a repeating key, start the timer. Otherwise reset
                   1134:                 * the client back to the root table.
                   1135:                 */
1.163     nicm     1136:                xtimeout = options_get_number(s->options, "repeat-time");
1.222     nicm     1137:                if (xtimeout != 0 && (bd->flags & KEY_BINDING_REPEAT)) {
1.132     nicm     1138:                        c->flags |= CLIENT_REPEAT;
                   1139:
                   1140:                        tv.tv_sec = xtimeout / 1000;
                   1141:                        tv.tv_usec = (xtimeout % 1000) * 1000L;
                   1142:                        evtimer_del(&c->repeat_timer);
                   1143:                        evtimer_add(&c->repeat_timer, &tv);
                   1144:                } else {
1.18      nicm     1145:                        c->flags &= ~CLIENT_REPEAT;
1.178     nicm     1146:                        server_client_set_key_table(c, NULL);
1.18      nicm     1147:                }
1.132     nicm     1148:                server_status_client(c);
                   1149:
1.227     nicm     1150:                /* Execute the key binding. */
1.276     nicm     1151:                key_bindings_dispatch(bd, item, c, m, &fs);
1.132     nicm     1152:                key_bindings_unref_table(table);
1.276     nicm     1153:                goto out;
1.18      nicm     1154:        }
                   1155:
1.132     nicm     1156:        /*
1.253     nicm     1157:         * No match, try the ANY key.
                   1158:         */
                   1159:        if (key0 != KEYC_ANY) {
                   1160:                key0 = KEYC_ANY;
                   1161:                goto try_again;
                   1162:        }
                   1163:
                   1164:        /*
1.223     nicm     1165:         * No match in this table. If not in the root table or if repeating,
                   1166:         * switch the client back to the root table and try again.
1.132     nicm     1167:         */
1.223     nicm     1168:        log_debug("not found in key table %s", table->name);
                   1169:        if (!server_client_is_default_key_table(c, table) ||
                   1170:            (c->flags & CLIENT_REPEAT)) {
1.262     nicm     1171:                log_debug("trying in root table");
1.178     nicm     1172:                server_client_set_key_table(c, NULL);
1.262     nicm     1173:                table = c->keytable;
                   1174:                if (c->flags & CLIENT_REPEAT)
                   1175:                        first = table;
1.18      nicm     1176:                c->flags &= ~CLIENT_REPEAT;
1.132     nicm     1177:                server_status_client(c);
1.253     nicm     1178:                goto table_changed;
1.18      nicm     1179:        }
                   1180:
1.223     nicm     1181:        /*
                   1182:         * No match in the root table either. If this wasn't the first table
                   1183:         * tried, don't pass the key to the pane.
                   1184:         */
1.238     nicm     1185:        if (first != table && (~flags & CLIENT_REPEAT)) {
1.178     nicm     1186:                server_client_set_key_table(c, NULL);
1.132     nicm     1187:                server_status_client(c);
1.276     nicm     1188:                goto out;
1.161     nicm     1189:        }
                   1190:
1.253     nicm     1191: forward_key:
1.161     nicm     1192:        if (c->flags & CLIENT_READONLY)
1.276     nicm     1193:                goto out;
1.161     nicm     1194:        if (wp != NULL)
1.264     nicm     1195:                window_pane_key(wp, c, s, wl, key, m);
1.276     nicm     1196:
                   1197: out:
                   1198:        free(event);
                   1199:        return (CMD_RETURN_NORMAL);
1.280     nicm     1200: }
                   1201:
                   1202: /* Handle a key event. */
                   1203: int
                   1204: server_client_handle_key(struct client *c, struct key_event *event)
                   1205: {
                   1206:        struct session          *s = c->session;
                   1207:        struct cmdq_item        *item;
                   1208:
                   1209:        /* Check the client is good to accept input. */
                   1210:        if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
                   1211:                return (0);
                   1212:
                   1213:        /*
1.281   ! nicm     1214:         * Key presses in overlay mode are a special case. The queue might be
1.280     nicm     1215:         * blocked so they need to be processed immediately rather than queued.
                   1216:         */
1.281   ! nicm     1217:        if ((~c->flags & CLIENT_READONLY) && c->overlay_key != NULL) {
        !          1218:                if (c->overlay_key(c, event) != 0)
        !          1219:                        server_client_clear_overlay(c);
1.280     nicm     1220:                return (0);
                   1221:        }
                   1222:
                   1223:        /*
                   1224:         * Add the key to the queue so it happens after any commands queued by
                   1225:         * previous keys.
                   1226:         */
                   1227:        item = cmdq_get_callback(server_client_key_callback, event);
                   1228:        cmdq_append(c, item);
                   1229:        return (1);
1.18      nicm     1230: }
                   1231:
1.2       nicm     1232: /* Client functions that need to happen every loop. */
                   1233: void
                   1234: server_client_loop(void)
                   1235: {
                   1236:        struct client           *c;
                   1237:        struct window           *w;
                   1238:        struct window_pane      *wp;
1.197     nicm     1239:        int                      focus;
1.2       nicm     1240:
1.135     nicm     1241:        TAILQ_FOREACH(c, &clients, entry) {
1.36      nicm     1242:                server_client_check_exit(c);
                   1243:                if (c->session != NULL) {
                   1244:                        server_client_check_redraw(c);
                   1245:                        server_client_reset_state(c);
                   1246:                }
1.2       nicm     1247:        }
                   1248:
                   1249:        /*
                   1250:         * Any windows will have been redrawn as part of clients, so clear
1.92      nicm     1251:         * their flags now. Also check pane focus and resize.
1.2       nicm     1252:         */
1.197     nicm     1253:        focus = options_get_number(global_options, "focus-events");
1.134     nicm     1254:        RB_FOREACH(w, windows, &windows) {
1.91      nicm     1255:                TAILQ_FOREACH(wp, &w->panes, entry) {
1.101     nicm     1256:                        if (wp->fd != -1) {
1.197     nicm     1257:                                if (focus)
                   1258:                                        server_client_check_focus(wp);
1.101     nicm     1259:                                server_client_check_resize(wp);
                   1260:                        }
1.2       nicm     1261:                        wp->flags &= ~PANE_REDRAW;
1.91      nicm     1262:                }
1.150     nicm     1263:                check_window_name(w);
1.2       nicm     1264:        }
1.92      nicm     1265: }
                   1266:
1.237     nicm     1267: /* Check if we need to force a resize. */
                   1268: static int
                   1269: server_client_resize_force(struct window_pane *wp)
                   1270: {
                   1271:        struct timeval  tv = { .tv_usec = 100000 };
                   1272:        struct winsize  ws;
                   1273:
                   1274:        /*
                   1275:         * If we are resizing to the same size as when we entered the loop
                   1276:         * (that is, to the same size the application currently thinks it is),
                   1277:         * tmux may have gone through several resizes internally and thrown
                   1278:         * away parts of the screen. So we need the application to actually
                   1279:         * redraw even though its final size has not changed.
                   1280:         */
                   1281:
                   1282:        if (wp->flags & PANE_RESIZEFORCE) {
                   1283:                wp->flags &= ~PANE_RESIZEFORCE;
                   1284:                return (0);
                   1285:        }
                   1286:
                   1287:        if (wp->sx != wp->osx ||
                   1288:            wp->sy != wp->osy ||
                   1289:            wp->sx <= 1 ||
                   1290:            wp->sy <= 1)
                   1291:                return (0);
                   1292:
                   1293:        memset(&ws, 0, sizeof ws);
                   1294:        ws.ws_col = wp->sx;
                   1295:        ws.ws_row = wp->sy - 1;
1.244     nicm     1296:        if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.237     nicm     1297:                fatal("ioctl failed");
                   1298:        log_debug("%s: %%%u forcing resize", __func__, wp->id);
                   1299:
                   1300:        evtimer_add(&wp->resize_timer, &tv);
                   1301:        wp->flags |= PANE_RESIZEFORCE;
                   1302:        return (1);
                   1303: }
                   1304:
1.211     nicm     1305: /* Resize timer event. */
1.188     nicm     1306: static void
                   1307: server_client_resize_event(__unused int fd, __unused short events, void *data)
1.92      nicm     1308: {
1.188     nicm     1309:        struct window_pane      *wp = data;
                   1310:        struct winsize           ws;
                   1311:
                   1312:        evtimer_del(&wp->resize_timer);
1.92      nicm     1313:
1.101     nicm     1314:        if (!(wp->flags & PANE_RESIZE))
1.92      nicm     1315:                return;
1.237     nicm     1316:        if (server_client_resize_force(wp))
                   1317:                return;
1.235     nicm     1318:
1.92      nicm     1319:        memset(&ws, 0, sizeof ws);
                   1320:        ws.ws_col = wp->sx;
                   1321:        ws.ws_row = wp->sy;
1.244     nicm     1322:        if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92      nicm     1323:                fatal("ioctl failed");
1.235     nicm     1324:        log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, wp->sx, wp->sy);
1.92      nicm     1325:
                   1326:        wp->flags &= ~PANE_RESIZE;
1.235     nicm     1327:
                   1328:        wp->osx = wp->sx;
                   1329:        wp->osy = wp->sy;
1.188     nicm     1330: }
                   1331:
                   1332: /* Check if pane should be resized. */
1.190     nicm     1333: static void
1.188     nicm     1334: server_client_check_resize(struct window_pane *wp)
                   1335: {
                   1336:        struct timeval   tv = { .tv_usec = 250000 };
                   1337:
                   1338:        if (!(wp->flags & PANE_RESIZE))
                   1339:                return;
1.235     nicm     1340:        log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, wp->sx, wp->sy);
1.188     nicm     1341:
                   1342:        if (!event_initialized(&wp->resize_timer))
                   1343:                evtimer_set(&wp->resize_timer, server_client_resize_event, wp);
                   1344:
                   1345:        /*
                   1346:         * The first resize should happen immediately, so if the timer is not
                   1347:         * running, do it now.
                   1348:         */
                   1349:        if (!evtimer_pending(&wp->resize_timer, NULL))
                   1350:                server_client_resize_event(-1, 0, wp);
                   1351:
                   1352:        /*
                   1353:         * If the pane is in the alternate screen, let the timer expire and
                   1354:         * resize to give the application a chance to redraw. If not, keep
                   1355:         * pushing the timer back.
                   1356:         */
                   1357:        if (wp->saved_grid != NULL && evtimer_pending(&wp->resize_timer, NULL))
                   1358:                return;
                   1359:        evtimer_del(&wp->resize_timer);
                   1360:        evtimer_add(&wp->resize_timer, &tv);
1.91      nicm     1361: }
                   1362:
                   1363: /* Check whether pane should be focused. */
1.190     nicm     1364: static void
1.91      nicm     1365: server_client_check_focus(struct window_pane *wp)
                   1366: {
1.93      nicm     1367:        struct client   *c;
1.102     nicm     1368:        int              push;
                   1369:
                   1370:        /* Do we need to push the focus state? */
                   1371:        push = wp->flags & PANE_FOCUSPUSH;
                   1372:        wp->flags &= ~PANE_FOCUSPUSH;
1.91      nicm     1373:
                   1374:        /* If we're not the active pane in our window, we're not focused. */
                   1375:        if (wp->window->active != wp)
                   1376:                goto not_focused;
                   1377:
                   1378:        /* If we're in a mode, we're not focused. */
                   1379:        if (wp->screen != &wp->base)
                   1380:                goto not_focused;
                   1381:
                   1382:        /*
1.93      nicm     1383:         * If our window is the current window in any focused clients with an
                   1384:         * attached session, we're focused.
1.91      nicm     1385:         */
1.135     nicm     1386:        TAILQ_FOREACH(c, &clients, entry) {
                   1387:                if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91      nicm     1388:                        continue;
1.255     nicm     1389:                if (c->session->attached == 0)
1.93      nicm     1390:                        continue;
                   1391:
                   1392:                if (c->session->curw->window == wp->window)
1.91      nicm     1393:                        goto focused;
                   1394:        }
                   1395:
                   1396: not_focused:
1.251     nicm     1397:        if (push || (wp->flags & PANE_FOCUSED)) {
                   1398:                if (wp->base.mode & MODE_FOCUSON)
                   1399:                        bufferevent_write(wp->event, "\033[O", 3);
                   1400:                notify_pane("pane-focus-out", wp);
                   1401:        }
1.91      nicm     1402:        wp->flags &= ~PANE_FOCUSED;
                   1403:        return;
                   1404:
                   1405: focused:
1.251     nicm     1406:        if (push || !(wp->flags & PANE_FOCUSED)) {
                   1407:                if (wp->base.mode & MODE_FOCUSON)
                   1408:                        bufferevent_write(wp->event, "\033[I", 3);
                   1409:                notify_pane("pane-focus-in", wp);
1.275     nicm     1410:                session_update_activity(c->session, NULL);
1.251     nicm     1411:        }
1.91      nicm     1412:        wp->flags |= PANE_FOCUSED;
1.2       nicm     1413: }
                   1414:
1.18      nicm     1415: /*
                   1416:  * Update cursor position and mode settings. The scroll region and attributes
                   1417:  * are cleared when idle (waiting for an event) as this is the most likely time
                   1418:  * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
                   1419:  * compromise between excessive resets and likelihood of an interrupt.
                   1420:  *
                   1421:  * tty_region/tty_reset/tty_update_mode already take care of not resetting
                   1422:  * things that are already in their default state.
                   1423:  */
1.190     nicm     1424: static void
1.18      nicm     1425: server_client_reset_state(struct client *c)
1.1       nicm     1426: {
1.18      nicm     1427:        struct window           *w = c->session->curw->window;
1.209     nicm     1428:        struct window_pane      *wp = w->active, *loop;
1.18      nicm     1429:        struct screen           *s = wp->screen;
1.163     nicm     1430:        struct options          *oo = c->session->options;
1.261     nicm     1431:        int                      mode, cursor = 0;
                   1432:        u_int                    cx = 0, cy = 0, ox, oy, sx, sy;
1.60      nicm     1433:
1.186     nicm     1434:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86      nicm     1435:                return;
1.261     nicm     1436:        mode = s->mode;
1.86      nicm     1437:
1.199     nicm     1438:        tty_region_off(&c->tty);
                   1439:        tty_margin_off(&c->tty);
1.1       nicm     1440:
1.261     nicm     1441:        /* Move cursor to pane cursor and offset. */
                   1442:        cursor = 0;
                   1443:        tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
                   1444:        if (wp->xoff + s->cx >= ox && wp->xoff + s->cx <= ox + sx &&
                   1445:            wp->yoff + s->cy >= oy && wp->yoff + s->cy <= oy + sy) {
                   1446:                cursor = 1;
                   1447:
                   1448:                cx = wp->xoff + s->cx - ox;
                   1449:                cy = wp->yoff + s->cy - oy;
                   1450:
                   1451:                if (status_at_line(c) == 0)
                   1452:                        cy += status_line_size(c);
                   1453:        }
                   1454:        if (!cursor)
                   1455:                mode &= ~MODE_CURSOR;
                   1456:        tty_cursor(&c->tty, cx, cy);
1.1       nicm     1457:
1.50      nicm     1458:        /*
1.131     nicm     1459:         * Set mouse mode if requested. To support dragging, always use button
                   1460:         * mode.
1.57      nicm     1461:         */
1.209     nicm     1462:        if (options_get_number(oo, "mouse")) {
                   1463:                mode &= ~ALL_MOUSE_MODES;
                   1464:                TAILQ_FOREACH(loop, &w->panes, entry) {
                   1465:                        if (loop->screen->mode & MODE_MOUSE_ALL)
                   1466:                                mode |= MODE_MOUSE_ALL;
                   1467:                }
                   1468:                if (~mode & MODE_MOUSE_ALL)
                   1469:                        mode |= MODE_MOUSE_BUTTON;
                   1470:        }
1.215     nicm     1471:
                   1472:        /* Clear bracketed paste mode if at the prompt. */
                   1473:        if (c->prompt_string != NULL)
                   1474:                mode &= ~MODE_BRACKETPASTE;
1.48      nicm     1475:
                   1476:        /* Set the terminal mode and reset attributes. */
1.59      nicm     1477:        tty_update_mode(&c->tty, mode, s);
1.1       nicm     1478:        tty_reset(&c->tty);
1.17      nicm     1479: }
                   1480:
                   1481: /* Repeat time callback. */
1.190     nicm     1482: static void
1.170     nicm     1483: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17      nicm     1484: {
                   1485:        struct client   *c = data;
                   1486:
1.85      nicm     1487:        if (c->flags & CLIENT_REPEAT) {
1.178     nicm     1488:                server_client_set_key_table(c, NULL);
1.132     nicm     1489:                c->flags &= ~CLIENT_REPEAT;
                   1490:                server_status_client(c);
1.85      nicm     1491:        }
1.192     nicm     1492: }
                   1493:
                   1494: /* Double-click callback. */
                   1495: static void
                   1496: server_client_click_timer(__unused int fd, __unused short events, void *data)
                   1497: {
                   1498:        struct client   *c = data;
                   1499:
                   1500:        c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1       nicm     1501: }
                   1502:
1.36      nicm     1503: /* Check if client should be exited. */
1.190     nicm     1504: static void
1.36      nicm     1505: server_client_check_exit(struct client *c)
                   1506: {
                   1507:        if (!(c->flags & CLIENT_EXIT))
                   1508:                return;
                   1509:
1.73      nicm     1510:        if (EVBUFFER_LENGTH(c->stdin_data) != 0)
                   1511:                return;
                   1512:        if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36      nicm     1513:                return;
1.73      nicm     1514:        if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36      nicm     1515:                return;
                   1516:
1.249     nicm     1517:        if (c->flags & CLIENT_ATTACHED)
                   1518:                notify_client("client-detached", c);
1.162     nicm     1519:        proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36      nicm     1520:        c->flags &= ~CLIENT_EXIT;
1.38      nicm     1521: }
                   1522:
1.218     nicm     1523: /* Redraw timer callback. */
                   1524: static void
                   1525: server_client_redraw_timer(__unused int fd, __unused short events,
                   1526:     __unused void* data)
                   1527: {
                   1528:        log_debug("redraw timer fired");
                   1529: }
                   1530:
1.1       nicm     1531: /* Check for client redraws. */
1.190     nicm     1532: static void
1.1       nicm     1533: server_client_check_redraw(struct client *c)
                   1534: {
                   1535:        struct session          *s = c->session;
1.137     nicm     1536:        struct tty              *tty = &c->tty;
1.1       nicm     1537:        struct window_pane      *wp;
1.256     nicm     1538:        int                      needed, flags;
1.218     nicm     1539:        struct timeval           tv = { .tv_usec = 1000 };
                   1540:        static struct event      ev;
                   1541:        size_t                   left;
1.1       nicm     1542:
1.86      nicm     1543:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79      nicm     1544:                return;
1.257     nicm     1545:        if (c->flags & CLIENT_ALLREDRAWFLAGS) {
                   1546:                log_debug("%s: redraw%s%s%s", c->name,
                   1547:                    (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "",
                   1548:                    (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "",
                   1549:                    (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "");
                   1550:        }
1.218     nicm     1551:
                   1552:        /*
                   1553:         * If there is outstanding data, defer the redraw until it has been
                   1554:         * consumed. We can just add a timer to get out of the event loop and
                   1555:         * end up back here.
                   1556:         */
                   1557:        needed = 0;
1.256     nicm     1558:        if (c->flags & CLIENT_ALLREDRAWFLAGS)
1.218     nicm     1559:                needed = 1;
                   1560:        else {
                   1561:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
                   1562:                        if (wp->flags & PANE_REDRAW) {
                   1563:                                needed = 1;
                   1564:                                break;
                   1565:                        }
                   1566:                }
                   1567:        }
1.241     nicm     1568:        if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) {
                   1569:                log_debug("%s: redraw deferred (%zu left)", c->name, left);
                   1570:                if (!evtimer_initialized(&ev))
                   1571:                        evtimer_set(&ev, server_client_redraw_timer, NULL);
                   1572:                if (!evtimer_pending(&ev, NULL)) {
1.218     nicm     1573:                        log_debug("redraw timer started");
                   1574:                        evtimer_add(&ev, &tv);
1.241     nicm     1575:                }
1.218     nicm     1576:
1.241     nicm     1577:                /*
                   1578:                 * We may have got here for a single pane redraw, but force a
                   1579:                 * full redraw next time in case other panes have been updated.
                   1580:                 */
1.256     nicm     1581:                c->flags |= CLIENT_ALLREDRAWFLAGS;
1.241     nicm     1582:                return;
                   1583:        } else if (needed)
1.218     nicm     1584:                log_debug("%s: redraw needed", c->name);
1.79      nicm     1585:
1.219     nicm     1586:        flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR);
                   1587:        tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE)) | TTY_NOCURSOR;
1.137     nicm     1588:
1.256     nicm     1589:        if (~c->flags & CLIENT_REDRAWWINDOW) {
                   1590:                /*
                   1591:                 * If not redrawing the entire window, check whether each pane
                   1592:                 * needs to be redrawn.
                   1593:                 */
1.1       nicm     1594:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137     nicm     1595:                        if (wp->flags & PANE_REDRAW) {
                   1596:                                tty_update_mode(tty, tty->mode, NULL);
1.1       nicm     1597:                                screen_redraw_pane(c, wp);
1.137     nicm     1598:                        }
1.1       nicm     1599:                }
                   1600:        }
                   1601:
1.256     nicm     1602:        if (c->flags & CLIENT_ALLREDRAWFLAGS) {
                   1603:                if (options_get_number(s->options, "set-titles"))
                   1604:                        server_client_set_title(c);
                   1605:                screen_redraw_screen(c);
                   1606:        }
1.1       nicm     1607:
1.137     nicm     1608:        tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
                   1609:        tty_update_mode(tty, tty->mode, NULL);
1.1       nicm     1610:
1.256     nicm     1611:        c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE);
1.230     nicm     1612:
                   1613:        if (needed) {
                   1614:                /*
                   1615:                 * We would have deferred the redraw unless the output buffer
                   1616:                 * was empty, so we can record how many bytes the redraw
                   1617:                 * generated.
                   1618:                 */
                   1619:                c->redraw = EVBUFFER_LENGTH(tty->out);
                   1620:                log_debug("%s: redraw added %zu bytes", c->name, c->redraw);
                   1621:        }
1.1       nicm     1622: }
                   1623:
                   1624: /* Set client title. */
1.190     nicm     1625: static void
1.1       nicm     1626: server_client_set_title(struct client *c)
                   1627: {
1.128     nicm     1628:        struct session          *s = c->session;
                   1629:        const char              *template;
                   1630:        char                    *title;
                   1631:        struct format_tree      *ft;
1.1       nicm     1632:
1.163     nicm     1633:        template = options_get_string(s->options, "set-titles-string");
1.25      nicm     1634:
1.228     nicm     1635:        ft = format_create(c, NULL, FORMAT_NONE, 0);
1.128     nicm     1636:        format_defaults(ft, c, NULL, NULL, NULL);
                   1637:
1.269     nicm     1638:        title = format_expand_time(ft, template);
1.1       nicm     1639:        if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77      nicm     1640:                free(c->title);
1.1       nicm     1641:                c->title = xstrdup(title);
                   1642:                tty_set_title(&c->tty, c->title);
                   1643:        }
1.77      nicm     1644:        free(title);
1.128     nicm     1645:
                   1646:        format_free(ft);
1.1       nicm     1647: }
                   1648:
                   1649: /* Dispatch message from client. */
1.190     nicm     1650: static void
1.162     nicm     1651: server_client_dispatch(struct imsg *imsg, void *arg)
1.1       nicm     1652: {
1.162     nicm     1653:        struct client           *c = arg;
1.73      nicm     1654:        struct msg_stdin_data    stdindata;
1.108     nicm     1655:        const char              *data;
1.162     nicm     1656:        ssize_t                  datalen;
1.149     nicm     1657:        struct session          *s;
1.1       nicm     1658:
1.162     nicm     1659:        if (c->flags & CLIENT_DEAD)
                   1660:                return;
                   1661:
                   1662:        if (imsg == NULL) {
                   1663:                server_client_lost(c);
                   1664:                return;
                   1665:        }
1.1       nicm     1666:
1.162     nicm     1667:        data = imsg->data;
                   1668:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1       nicm     1669:
1.162     nicm     1670:        switch (imsg->hdr.type) {
                   1671:        case MSG_IDENTIFY_FLAGS:
                   1672:        case MSG_IDENTIFY_TERM:
                   1673:        case MSG_IDENTIFY_TTYNAME:
                   1674:        case MSG_IDENTIFY_CWD:
                   1675:        case MSG_IDENTIFY_STDIN:
                   1676:        case MSG_IDENTIFY_ENVIRON:
                   1677:        case MSG_IDENTIFY_CLIENTPID:
                   1678:        case MSG_IDENTIFY_DONE:
                   1679:                server_client_dispatch_identify(c, imsg);
                   1680:                break;
                   1681:        case MSG_COMMAND:
                   1682:                server_client_dispatch_command(c, imsg);
                   1683:                break;
                   1684:        case MSG_STDIN:
                   1685:                if (datalen != sizeof stdindata)
                   1686:                        fatalx("bad MSG_STDIN size");
                   1687:                memcpy(&stdindata, data, sizeof stdindata);
1.36      nicm     1688:
1.162     nicm     1689:                if (c->stdin_callback == NULL)
1.33      nicm     1690:                        break;
1.162     nicm     1691:                if (stdindata.size <= 0)
                   1692:                        c->stdin_closed = 1;
                   1693:                else {
                   1694:                        evbuffer_add(c->stdin_data, stdindata.data,
                   1695:                            stdindata.size);
                   1696:                }
1.279     nicm     1697:                c->stdin_callback(c, c->stdin_closed, c->stdin_callback_data);
1.162     nicm     1698:                break;
                   1699:        case MSG_RESIZE:
                   1700:                if (datalen != 0)
                   1701:                        fatalx("bad MSG_RESIZE size");
1.1       nicm     1702:
1.162     nicm     1703:                if (c->flags & CLIENT_CONTROL)
1.1       nicm     1704:                        break;
1.236     nicm     1705:                tty_resize(&c->tty);
                   1706:                recalculate_sizes();
                   1707:                server_redraw_client(c);
1.174     nicm     1708:                if (c->session != NULL)
1.196     nicm     1709:                        notify_client("client-resized", c);
1.162     nicm     1710:                break;
                   1711:        case MSG_EXITING:
                   1712:                if (datalen != 0)
                   1713:                        fatalx("bad MSG_EXITING size");
1.1       nicm     1714:
1.162     nicm     1715:                c->session = NULL;
                   1716:                tty_close(&c->tty);
                   1717:                proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
                   1718:                break;
                   1719:        case MSG_WAKEUP:
                   1720:        case MSG_UNLOCK:
                   1721:                if (datalen != 0)
                   1722:                        fatalx("bad MSG_WAKEUP size");
1.121     nicm     1723:
1.162     nicm     1724:                if (!(c->flags & CLIENT_SUSPENDED))
                   1725:                        break;
                   1726:                c->flags &= ~CLIENT_SUSPENDED;
1.10      nicm     1727:
1.162     nicm     1728:                if (c->tty.fd == -1) /* exited in the meantime */
1.1       nicm     1729:                        break;
1.162     nicm     1730:                s = c->session;
1.1       nicm     1731:
1.162     nicm     1732:                if (gettimeofday(&c->activity_time, NULL) != 0)
                   1733:                        fatal("gettimeofday failed");
                   1734:
                   1735:                tty_start_tty(&c->tty);
                   1736:                server_redraw_client(c);
                   1737:                recalculate_sizes();
1.184     nicm     1738:
                   1739:                if (s != NULL)
                   1740:                        session_update_activity(s, &c->activity_time);
1.162     nicm     1741:                break;
                   1742:        case MSG_SHELL:
                   1743:                if (datalen != 0)
                   1744:                        fatalx("bad MSG_SHELL size");
1.1       nicm     1745:
1.162     nicm     1746:                server_client_dispatch_shell(c);
                   1747:                break;
1.1       nicm     1748:        }
                   1749: }
                   1750:
1.194     nicm     1751: /* Callback when command is done. */
                   1752: static enum cmd_retval
1.195     nicm     1753: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194     nicm     1754: {
1.195     nicm     1755:        struct client   *c = item->client;
1.194     nicm     1756:
                   1757:        if (~c->flags & CLIENT_ATTACHED)
                   1758:                c->flags |= CLIENT_EXIT;
                   1759:        return (CMD_RETURN_NORMAL);
                   1760: }
                   1761:
                   1762: /* Show an error message. */
                   1763: static enum cmd_retval
1.195     nicm     1764: server_client_command_error(struct cmdq_item *item, void *data)
1.194     nicm     1765: {
                   1766:        char    *error = data;
                   1767:
1.195     nicm     1768:        cmdq_error(item, "%s", error);
1.194     nicm     1769:        free(error);
                   1770:
                   1771:        return (CMD_RETURN_NORMAL);
                   1772: }
                   1773:
1.1       nicm     1774: /* Handle command message. */
1.190     nicm     1775: static void
1.162     nicm     1776: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1       nicm     1777: {
1.108     nicm     1778:        struct msg_command_data   data;
                   1779:        char                     *buf;
                   1780:        size_t                    len;
                   1781:        struct cmd_list          *cmdlist = NULL;
                   1782:        int                       argc;
                   1783:        char                    **argv, *cause;
1.246     nicm     1784:
                   1785:        if (c->flags & CLIENT_EXIT)
                   1786:                return;
1.108     nicm     1787:
                   1788:        if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
                   1789:                fatalx("bad MSG_COMMAND size");
                   1790:        memcpy(&data, imsg->data, sizeof data);
                   1791:
1.124     nicm     1792:        buf = (char *)imsg->data + sizeof data;
1.108     nicm     1793:        len = imsg->hdr.len  - IMSG_HEADER_SIZE - sizeof data;
                   1794:        if (len > 0 && buf[len - 1] != '\0')
                   1795:                fatalx("bad MSG_COMMAND string");
                   1796:
                   1797:        argc = data.argc;
                   1798:        if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194     nicm     1799:                cause = xstrdup("command too long");
1.1       nicm     1800:                goto error;
                   1801:        }
                   1802:
                   1803:        if (argc == 0) {
                   1804:                argc = 1;
                   1805:                argv = xcalloc(1, sizeof *argv);
                   1806:                *argv = xstrdup("new-session");
                   1807:        }
                   1808:
1.94      nicm     1809:        if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
1.1       nicm     1810:                cmd_free_argv(argc, argv);
                   1811:                goto error;
                   1812:        }
                   1813:        cmd_free_argv(argc, argv);
                   1814:
1.194     nicm     1815:        cmdq_append(c, cmdq_get_command(cmdlist, NULL, NULL, 0));
                   1816:        cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.1       nicm     1817:        cmd_list_free(cmdlist);
                   1818:        return;
                   1819:
                   1820: error:
1.194     nicm     1821:        cmdq_append(c, cmdq_get_callback(server_client_command_error, cause));
                   1822:
1.1       nicm     1823:        if (cmdlist != NULL)
                   1824:                cmd_list_free(cmdlist);
1.88      nicm     1825:
1.36      nicm     1826:        c->flags |= CLIENT_EXIT;
1.1       nicm     1827: }
                   1828:
                   1829: /* Handle identify message. */
1.190     nicm     1830: static void
1.162     nicm     1831: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1       nicm     1832: {
1.165     nicm     1833:        const char      *data, *home;
1.232     nicm     1834:        size_t           datalen;
1.109     nicm     1835:        int              flags;
1.216     nicm     1836:        char            *name;
1.109     nicm     1837:
                   1838:        if (c->flags & CLIENT_IDENTIFIED)
                   1839:                fatalx("out-of-order identify message");
                   1840:
                   1841:        data = imsg->data;
                   1842:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
                   1843:
                   1844:        switch (imsg->hdr.type) {
                   1845:        case MSG_IDENTIFY_FLAGS:
                   1846:                if (datalen != sizeof flags)
                   1847:                        fatalx("bad MSG_IDENTIFY_FLAGS size");
                   1848:                memcpy(&flags, data, sizeof flags);
                   1849:                c->flags |= flags;
1.158     nicm     1850:                log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109     nicm     1851:                break;
                   1852:        case MSG_IDENTIFY_TERM:
1.110     nicm     1853:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1854:                        fatalx("bad MSG_IDENTIFY_TERM string");
                   1855:                c->term = xstrdup(data);
1.158     nicm     1856:                log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109     nicm     1857:                break;
                   1858:        case MSG_IDENTIFY_TTYNAME:
1.110     nicm     1859:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1860:                        fatalx("bad MSG_IDENTIFY_TTYNAME string");
                   1861:                c->ttyname = xstrdup(data);
1.158     nicm     1862:                log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109     nicm     1863:                break;
                   1864:        case MSG_IDENTIFY_CWD:
1.155     nicm     1865:                if (datalen == 0 || data[datalen - 1] != '\0')
                   1866:                        fatalx("bad MSG_IDENTIFY_CWD string");
1.165     nicm     1867:                if (access(data, X_OK) == 0)
                   1868:                        c->cwd = xstrdup(data);
                   1869:                else if ((home = find_home()) != NULL)
                   1870:                        c->cwd = xstrdup(home);
                   1871:                else
                   1872:                        c->cwd = xstrdup("/");
1.158     nicm     1873:                log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109     nicm     1874:                break;
                   1875:        case MSG_IDENTIFY_STDIN:
                   1876:                if (datalen != 0)
                   1877:                        fatalx("bad MSG_IDENTIFY_STDIN size");
                   1878:                c->fd = imsg->fd;
1.158     nicm     1879:                log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109     nicm     1880:                break;
                   1881:        case MSG_IDENTIFY_ENVIRON:
1.110     nicm     1882:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1883:                        fatalx("bad MSG_IDENTIFY_ENVIRON string");
                   1884:                if (strchr(data, '=') != NULL)
1.164     nicm     1885:                        environ_put(c->environ, data);
1.158     nicm     1886:                log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143     nicm     1887:                break;
                   1888:        case MSG_IDENTIFY_CLIENTPID:
                   1889:                if (datalen != sizeof c->pid)
                   1890:                        fatalx("bad MSG_IDENTIFY_CLIENTPID size");
                   1891:                memcpy(&c->pid, data, sizeof c->pid);
1.158     nicm     1892:                log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109     nicm     1893:                break;
                   1894:        default:
                   1895:                break;
                   1896:        }
                   1897:
                   1898:        if (imsg->hdr.type != MSG_IDENTIFY_DONE)
                   1899:                return;
                   1900:        c->flags |= CLIENT_IDENTIFIED;
1.75      nicm     1901:
1.216     nicm     1902:        if (*c->ttyname != '\0')
                   1903:                name = xstrdup(c->ttyname);
                   1904:        else
                   1905:                xasprintf(&name, "client-%ld", (long)c->pid);
                   1906:        c->name = name;
                   1907:        log_debug("client %p name is %s", c, c->name);
                   1908:
1.109     nicm     1909:        if (c->flags & CLIENT_CONTROL) {
1.75      nicm     1910:                c->stdin_callback = control_callback;
1.109     nicm     1911:
1.97      nicm     1912:                evbuffer_free(c->stderr_data);
                   1913:                c->stderr_data = c->stdout_data;
1.109     nicm     1914:
                   1915:                if (c->flags & CLIENT_CONTROLCONTROL)
1.96      nicm     1916:                        evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162     nicm     1917:                proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75      nicm     1918:
                   1919:                c->tty.fd = -1;
                   1920:
1.109     nicm     1921:                close(c->fd);
                   1922:                c->fd = -1;
                   1923:
1.75      nicm     1924:                return;
                   1925:        }
1.1       nicm     1926:
1.109     nicm     1927:        if (c->fd == -1)
1.104     nicm     1928:                return;
1.145     nicm     1929:        if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109     nicm     1930:                close(c->fd);
                   1931:                c->fd = -1;
1.80      nicm     1932:                return;
                   1933:        }
1.109     nicm     1934:        if (c->flags & CLIENT_UTF8)
1.1       nicm     1935:                c->tty.flags |= TTY_UTF8;
1.109     nicm     1936:        if (c->flags & CLIENT_256COLOURS)
1.1       nicm     1937:                c->tty.term_flags |= TERM_256COLOURS;
                   1938:
                   1939:        tty_resize(&c->tty);
                   1940:
1.109     nicm     1941:        if (!(c->flags & CLIENT_CONTROL))
1.86      nicm     1942:                c->flags |= CLIENT_TERMINAL;
1.1       nicm     1943: }
                   1944:
                   1945: /* Handle shell message. */
1.190     nicm     1946: static void
1.162     nicm     1947: server_client_dispatch_shell(struct client *c)
1.1       nicm     1948: {
1.107     nicm     1949:        const char      *shell;
1.25      nicm     1950:
1.163     nicm     1951:        shell = options_get_string(global_s_options, "default-shell");
1.1       nicm     1952:        if (*shell == '\0' || areshell(shell))
                   1953:                shell = _PATH_BSHELL;
1.240     nicm     1954:        proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1);
1.25      nicm     1955:
1.162     nicm     1956:        proc_kill_peer(c->peer);
1.169     nicm     1957: }
                   1958:
                   1959: /* Event callback to push more stdout data if any left. */
                   1960: static void
1.170     nicm     1961: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169     nicm     1962: {
                   1963:        struct client   *c = arg;
                   1964:
                   1965:        if (~c->flags & CLIENT_DEAD)
                   1966:                server_client_push_stdout(c);
                   1967:        server_client_unref(c);
                   1968: }
                   1969:
                   1970: /* Push stdout to client if possible. */
                   1971: void
                   1972: server_client_push_stdout(struct client *c)
                   1973: {
                   1974:        struct msg_stdout_data data;
1.232     nicm     1975:        size_t                 sent, left;
1.169     nicm     1976:
                   1977:        left = EVBUFFER_LENGTH(c->stdout_data);
                   1978:        while (left != 0) {
                   1979:                sent = left;
                   1980:                if (sent > sizeof data.data)
                   1981:                        sent = sizeof data.data;
                   1982:                memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
                   1983:                data.size = sent;
                   1984:
                   1985:                if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
                   1986:                        break;
                   1987:                evbuffer_drain(c->stdout_data, sent);
                   1988:
                   1989:                left = EVBUFFER_LENGTH(c->stdout_data);
                   1990:                log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
                   1991:                    sent, left);
                   1992:        }
                   1993:        if (left != 0) {
                   1994:                c->references++;
                   1995:                event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
                   1996:                log_debug("%s: client %p, queued", __func__, c);
                   1997:        }
                   1998: }
                   1999:
                   2000: /* Event callback to push more stderr data if any left. */
                   2001: static void
1.170     nicm     2002: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169     nicm     2003: {
                   2004:        struct client   *c = arg;
                   2005:
                   2006:        if (~c->flags & CLIENT_DEAD)
                   2007:                server_client_push_stderr(c);
                   2008:        server_client_unref(c);
                   2009: }
                   2010:
                   2011: /* Push stderr to client if possible. */
                   2012: void
                   2013: server_client_push_stderr(struct client *c)
                   2014: {
                   2015:        struct msg_stderr_data data;
1.232     nicm     2016:        size_t                 sent, left;
1.169     nicm     2017:
                   2018:        if (c->stderr_data == c->stdout_data) {
                   2019:                server_client_push_stdout(c);
                   2020:                return;
                   2021:        }
                   2022:
                   2023:        left = EVBUFFER_LENGTH(c->stderr_data);
                   2024:        while (left != 0) {
                   2025:                sent = left;
                   2026:                if (sent > sizeof data.data)
                   2027:                        sent = sizeof data.data;
                   2028:                memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
                   2029:                data.size = sent;
                   2030:
                   2031:                if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
                   2032:                        break;
                   2033:                evbuffer_drain(c->stderr_data, sent);
                   2034:
                   2035:                left = EVBUFFER_LENGTH(c->stderr_data);
                   2036:                log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
                   2037:                    sent, left);
                   2038:        }
                   2039:        if (left != 0) {
                   2040:                c->references++;
                   2041:                event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
                   2042:                log_debug("%s: client %p, queued", __func__, c);
1.212     nicm     2043:        }
                   2044: }
                   2045:
                   2046: /* Add to client message log. */
                   2047: void
                   2048: server_client_add_message(struct client *c, const char *fmt, ...)
                   2049: {
                   2050:        struct message_entry    *msg, *msg1;
                   2051:        char                    *s;
                   2052:        va_list                  ap;
                   2053:        u_int                    limit;
                   2054:
                   2055:        va_start(ap, fmt);
                   2056:        xvasprintf(&s, fmt, ap);
                   2057:        va_end(ap);
                   2058:
1.216     nicm     2059:        log_debug("message %s (client %p)", s, c);
1.212     nicm     2060:
                   2061:        msg = xcalloc(1, sizeof *msg);
                   2062:        msg->msg_time = time(NULL);
                   2063:        msg->msg_num = c->message_next++;
                   2064:        msg->msg = s;
                   2065:        TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
                   2066:
                   2067:        limit = options_get_number(global_options, "message-limit");
                   2068:        TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
                   2069:                if (msg->msg_num + limit >= c->message_next)
                   2070:                        break;
                   2071:                free(msg->msg);
                   2072:                TAILQ_REMOVE(&c->message_log, msg, entry);
                   2073:                free(msg);
1.169     nicm     2074:        }
1.213     nicm     2075: }
                   2076:
                   2077: /* Get client working directory. */
                   2078: const char *
1.250     nicm     2079: server_client_get_cwd(struct client *c, struct session *s)
1.213     nicm     2080: {
1.250     nicm     2081:        const char      *home;
1.213     nicm     2082:
1.265     nicm     2083:        if (!cfg_finished && cfg_client != NULL)
                   2084:                return (cfg_client->cwd);
1.213     nicm     2085:        if (c != NULL && c->session == NULL && c->cwd != NULL)
                   2086:                return (c->cwd);
1.250     nicm     2087:        if (s != NULL && s->cwd != NULL)
                   2088:                return (s->cwd);
1.213     nicm     2089:        if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
                   2090:                return (s->cwd);
1.250     nicm     2091:        if ((home = find_home()) != NULL)
                   2092:                return (home);
                   2093:        return ("/");
1.213     nicm     2094: }
                   2095:
                   2096: /* Resolve an absolute path or relative to client working directory. */
                   2097: char *
                   2098: server_client_get_path(struct client *c, const char *file)
                   2099: {
                   2100:        char    *path, resolved[PATH_MAX];
                   2101:
                   2102:        if (*file == '/')
                   2103:                path = xstrdup(file);
                   2104:        else
1.250     nicm     2105:                xasprintf(&path, "%s/%s", server_client_get_cwd(c, NULL), file);
1.213     nicm     2106:        if (realpath(path, resolved) == NULL)
                   2107:                return (path);
                   2108:        free(path);
                   2109:        return (xstrdup(resolved));
1.1       nicm     2110: }