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

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