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

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