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

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