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

1.202   ! nicm        1: /* $OpenBSD: server-client.c,v 1.201 2016/11/23 17:01:24 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.201     nicm      695:        struct cmd_find_state    fs;
1.18      nicm      696:
                    697:        /* Check the client is good to accept input. */
1.132     nicm      698:        if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1.18      nicm      699:                return;
1.132     nicm      700:        w = s->curw->window;
1.18      nicm      701:
                    702:        /* Update the activity timer. */
                    703:        if (gettimeofday(&c->activity_time, NULL) != 0)
                    704:                fatal("gettimeofday failed");
1.149     nicm      705:        session_update_activity(s, &c->activity_time);
1.18      nicm      706:
1.132     nicm      707:        /* Number keys jump to pane in identify mode. */
1.25      nicm      708:        if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30      nicm      709:                if (c->flags & CLIENT_READONLY)
                    710:                        return;
1.95      nicm      711:                window_unzoom(w);
1.18      nicm      712:                wp = window_pane_at_index(w, key - '0');
1.187     nicm      713:                if (wp != NULL && !window_pane_visible(wp))
                    714:                        wp = NULL;
                    715:                server_clear_identify(c, wp);
1.18      nicm      716:                return;
                    717:        }
                    718:
                    719:        /* Handle status line. */
1.30      nicm      720:        if (!(c->flags & CLIENT_READONLY)) {
                    721:                status_message_clear(c);
1.187     nicm      722:                server_clear_identify(c, NULL);
1.30      nicm      723:        }
1.18      nicm      724:        if (c->prompt_string != NULL) {
1.193     nicm      725:                if (c->flags & CLIENT_READONLY)
                    726:                        return;
                    727:                if (status_prompt_key(c, key) == 0)
                    728:                        return;
1.18      nicm      729:        }
                    730:
                    731:        /* Check for mouse keys. */
                    732:        if (key == KEYC_MOUSE) {
1.30      nicm      733:                if (c->flags & CLIENT_READONLY)
                    734:                        return;
1.131     nicm      735:                key = server_client_check_mouse(c);
1.177     nicm      736:                if (key == KEYC_UNKNOWN)
1.131     nicm      737:                        return;
                    738:
                    739:                m->valid = 1;
                    740:                m->key = key;
                    741:        } else
                    742:                m->valid = 0;
1.18      nicm      743:
1.202   ! nicm      744:        /* Find affected pane. */
        !           745:        if (KEYC_IS_MOUSE(key) && m->valid)
        !           746:                wp = cmd_mouse_pane(m, NULL, NULL);
        !           747:        else
        !           748:                wp = w->active;
        !           749:
        !           750:        /* Forward mouse keys if disabled. */
        !           751:        if (key == KEYC_MOUSE && !options_get_number(s->options, "mouse"))
        !           752:                goto forward;
        !           753:
1.132     nicm      754:        /* Treat everything as a regular key when pasting is detected. */
1.161     nicm      755:        if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
                    756:                goto forward;
1.18      nicm      757:
1.132     nicm      758: retry:
1.191     nicm      759:        /*
                    760:         * Work out the current key table. If the pane is in a mode, use
                    761:         * the mode table instead of the default key table.
                    762:         */
                    763:        name = NULL;
                    764:        if (wp != NULL && wp->mode != NULL && wp->mode->key_table != NULL)
                    765:                name = wp->mode->key_table(wp);
                    766:        if (name == NULL || !server_client_is_default_key_table(c))
                    767:                table = c->keytable;
                    768:        else
                    769:                table = key_bindings_get_table(name, 1);
1.202   ! nicm      770:        if (wp == NULL)
        !           771:                log_debug("key table %s (no pane)", table->name);
        !           772:        else
        !           773:                log_debug("key table %s (pane %%%u)", table->name, wp->id);
1.191     nicm      774:
1.132     nicm      775:        /* Try to see if there is a key binding in the current table. */
                    776:        bd_find.key = key;
1.191     nicm      777:        bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
1.132     nicm      778:        if (bd != NULL) {
                    779:                /*
                    780:                 * Key was matched in this table. If currently repeating but a
                    781:                 * non-repeating binding was found, stop repeating and try
                    782:                 * again in the root table.
                    783:                 */
                    784:                if ((c->flags & CLIENT_REPEAT) && !bd->can_repeat) {
1.178     nicm      785:                        server_client_set_key_table(c, NULL);
1.132     nicm      786:                        c->flags &= ~CLIENT_REPEAT;
1.85      nicm      787:                        server_status_client(c);
1.132     nicm      788:                        goto retry;
1.18      nicm      789:                }
1.82      nicm      790:
1.132     nicm      791:                /*
                    792:                 * Take a reference to this table to make sure the key binding
                    793:                 * doesn't disappear.
                    794:                 */
                    795:                table->references++;
                    796:
                    797:                /*
                    798:                 * If this is a repeating key, start the timer. Otherwise reset
                    799:                 * the client back to the root table.
                    800:                 */
1.163     nicm      801:                xtimeout = options_get_number(s->options, "repeat-time");
1.132     nicm      802:                if (xtimeout != 0 && bd->can_repeat) {
                    803:                        c->flags |= CLIENT_REPEAT;
                    804:
                    805:                        tv.tv_sec = xtimeout / 1000;
                    806:                        tv.tv_usec = (xtimeout % 1000) * 1000L;
                    807:                        evtimer_del(&c->repeat_timer);
                    808:                        evtimer_add(&c->repeat_timer, &tv);
                    809:                } else {
1.18      nicm      810:                        c->flags &= ~CLIENT_REPEAT;
1.178     nicm      811:                        server_client_set_key_table(c, NULL);
1.18      nicm      812:                }
1.132     nicm      813:                server_status_client(c);
                    814:
1.201     nicm      815:                /* Find default state if the pane is known. */
                    816:                cmd_find_clear_state(&fs, NULL, 0);
                    817:                if (wp != NULL) {
                    818:                        fs.s = s;
                    819:                        fs.wl = fs.s->curw;
                    820:                        fs.w = fs.wl->window;
                    821:                        fs.wp = wp;
                    822:                        cmd_find_log_state(__func__, &fs);
                    823:
                    824:                        if (!cmd_find_valid_state(&fs))
                    825:                                fatalx("invalid key state");
                    826:                }
                    827:
1.132     nicm      828:                /* Dispatch the key binding. */
1.201     nicm      829:                key_bindings_dispatch(bd, c, m, &fs);
1.132     nicm      830:                key_bindings_unref_table(table);
1.18      nicm      831:                return;
                    832:        }
                    833:
1.132     nicm      834:        /*
                    835:         * No match in this table. If repeating, switch the client back to the
                    836:         * root table and try again.
                    837:         */
                    838:        if (c->flags & CLIENT_REPEAT) {
1.178     nicm      839:                server_client_set_key_table(c, NULL);
1.18      nicm      840:                c->flags &= ~CLIENT_REPEAT;
1.132     nicm      841:                server_status_client(c);
                    842:                goto retry;
1.18      nicm      843:        }
                    844:
1.132     nicm      845:        /* If no match and we're not in the root table, that's it. */
1.191     nicm      846:        if (name == NULL && !server_client_is_default_key_table(c)) {
1.178     nicm      847:                server_client_set_key_table(c, NULL);
1.132     nicm      848:                server_status_client(c);
                    849:                return;
1.18      nicm      850:        }
                    851:
1.132     nicm      852:        /*
                    853:         * No match, but in the root table. Prefix switches to the prefix table
                    854:         * and everything else is passed through.
                    855:         */
1.168     nicm      856:        if (key == (key_code)options_get_number(s->options, "prefix") ||
                    857:            key == (key_code)options_get_number(s->options, "prefix2")) {
1.178     nicm      858:                server_client_set_key_table(c, "prefix");
1.132     nicm      859:                server_status_client(c);
1.161     nicm      860:                return;
                    861:        }
                    862:
                    863: forward:
                    864:        if (c->flags & CLIENT_READONLY)
                    865:                return;
                    866:        if (wp != NULL)
1.132     nicm      867:                window_pane_key(wp, c, s, key, m);
1.18      nicm      868: }
                    869:
1.2       nicm      870: /* Client functions that need to happen every loop. */
                    871: void
                    872: server_client_loop(void)
                    873: {
                    874:        struct client           *c;
                    875:        struct window           *w;
                    876:        struct window_pane      *wp;
1.197     nicm      877:        int                      focus;
1.2       nicm      878:
1.135     nicm      879:        TAILQ_FOREACH(c, &clients, entry) {
1.36      nicm      880:                server_client_check_exit(c);
                    881:                if (c->session != NULL) {
                    882:                        server_client_check_redraw(c);
                    883:                        server_client_reset_state(c);
                    884:                }
1.2       nicm      885:        }
                    886:
                    887:        /*
                    888:         * Any windows will have been redrawn as part of clients, so clear
1.92      nicm      889:         * their flags now. Also check pane focus and resize.
1.2       nicm      890:         */
1.197     nicm      891:        focus = options_get_number(global_options, "focus-events");
1.134     nicm      892:        RB_FOREACH(w, windows, &windows) {
1.2       nicm      893:                w->flags &= ~WINDOW_REDRAW;
1.91      nicm      894:                TAILQ_FOREACH(wp, &w->panes, entry) {
1.101     nicm      895:                        if (wp->fd != -1) {
1.197     nicm      896:                                if (focus)
                    897:                                        server_client_check_focus(wp);
1.101     nicm      898:                                server_client_check_resize(wp);
                    899:                        }
1.2       nicm      900:                        wp->flags &= ~PANE_REDRAW;
1.91      nicm      901:                }
1.150     nicm      902:                check_window_name(w);
1.2       nicm      903:        }
1.92      nicm      904: }
                    905:
1.188     nicm      906: static void
                    907: server_client_resize_event(__unused int fd, __unused short events, void *data)
1.92      nicm      908: {
1.188     nicm      909:        struct window_pane      *wp = data;
                    910:        struct winsize           ws;
                    911:
                    912:        evtimer_del(&wp->resize_timer);
1.92      nicm      913:
1.101     nicm      914:        if (!(wp->flags & PANE_RESIZE))
1.92      nicm      915:                return;
                    916:
                    917:        memset(&ws, 0, sizeof ws);
                    918:        ws.ws_col = wp->sx;
                    919:        ws.ws_row = wp->sy;
                    920:
1.100     nicm      921:        if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92      nicm      922:                fatal("ioctl failed");
                    923:
                    924:        wp->flags &= ~PANE_RESIZE;
1.188     nicm      925: }
                    926:
                    927: /* Check if pane should be resized. */
1.190     nicm      928: static void
1.188     nicm      929: server_client_check_resize(struct window_pane *wp)
                    930: {
                    931:        struct timeval   tv = { .tv_usec = 250000 };
                    932:
                    933:        if (!(wp->flags & PANE_RESIZE))
                    934:                return;
                    935:
                    936:        if (!event_initialized(&wp->resize_timer))
                    937:                evtimer_set(&wp->resize_timer, server_client_resize_event, wp);
                    938:
                    939:        /*
                    940:         * The first resize should happen immediately, so if the timer is not
                    941:         * running, do it now.
                    942:         */
                    943:        if (!evtimer_pending(&wp->resize_timer, NULL))
                    944:                server_client_resize_event(-1, 0, wp);
                    945:
                    946:        /*
                    947:         * If the pane is in the alternate screen, let the timer expire and
                    948:         * resize to give the application a chance to redraw. If not, keep
                    949:         * pushing the timer back.
                    950:         */
                    951:        if (wp->saved_grid != NULL && evtimer_pending(&wp->resize_timer, NULL))
                    952:                return;
                    953:        evtimer_del(&wp->resize_timer);
                    954:        evtimer_add(&wp->resize_timer, &tv);
1.91      nicm      955: }
                    956:
                    957: /* Check whether pane should be focused. */
1.190     nicm      958: static void
1.91      nicm      959: server_client_check_focus(struct window_pane *wp)
                    960: {
1.93      nicm      961:        struct client   *c;
1.102     nicm      962:        int              push;
                    963:
                    964:        /* Do we need to push the focus state? */
                    965:        push = wp->flags & PANE_FOCUSPUSH;
                    966:        wp->flags &= ~PANE_FOCUSPUSH;
1.91      nicm      967:
                    968:        /* If we don't care about focus, forget it. */
                    969:        if (!(wp->base.mode & MODE_FOCUSON))
                    970:                return;
                    971:
                    972:        /* If we're not the active pane in our window, we're not focused. */
                    973:        if (wp->window->active != wp)
                    974:                goto not_focused;
                    975:
                    976:        /* If we're in a mode, we're not focused. */
                    977:        if (wp->screen != &wp->base)
                    978:                goto not_focused;
                    979:
                    980:        /*
1.93      nicm      981:         * If our window is the current window in any focused clients with an
                    982:         * attached session, we're focused.
1.91      nicm      983:         */
1.135     nicm      984:        TAILQ_FOREACH(c, &clients, entry) {
                    985:                if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91      nicm      986:                        continue;
1.93      nicm      987:                if (c->session->flags & SESSION_UNATTACHED)
                    988:                        continue;
                    989:
                    990:                if (c->session->curw->window == wp->window)
1.91      nicm      991:                        goto focused;
                    992:        }
                    993:
                    994: not_focused:
1.102     nicm      995:        if (push || (wp->flags & PANE_FOCUSED))
1.91      nicm      996:                bufferevent_write(wp->event, "\033[O", 3);
                    997:        wp->flags &= ~PANE_FOCUSED;
                    998:        return;
                    999:
                   1000: focused:
1.102     nicm     1001:        if (push || !(wp->flags & PANE_FOCUSED))
1.91      nicm     1002:                bufferevent_write(wp->event, "\033[I", 3);
                   1003:        wp->flags |= PANE_FOCUSED;
1.2       nicm     1004: }
                   1005:
1.18      nicm     1006: /*
                   1007:  * Update cursor position and mode settings. The scroll region and attributes
                   1008:  * are cleared when idle (waiting for an event) as this is the most likely time
                   1009:  * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
                   1010:  * compromise between excessive resets and likelihood of an interrupt.
                   1011:  *
                   1012:  * tty_region/tty_reset/tty_update_mode already take care of not resetting
                   1013:  * things that are already in their default state.
                   1014:  */
1.190     nicm     1015: static void
1.18      nicm     1016: server_client_reset_state(struct client *c)
1.1       nicm     1017: {
1.18      nicm     1018:        struct window           *w = c->session->curw->window;
                   1019:        struct window_pane      *wp = w->active;
                   1020:        struct screen           *s = wp->screen;
1.163     nicm     1021:        struct options          *oo = c->session->options;
1.66      nicm     1022:        int                      status, mode, o;
1.60      nicm     1023:
1.186     nicm     1024:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86      nicm     1025:                return;
                   1026:
1.199     nicm     1027:        tty_region_off(&c->tty);
                   1028:        tty_margin_off(&c->tty);
1.1       nicm     1029:
                   1030:        status = options_get_number(oo, "status");
                   1031:        if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
                   1032:                tty_cursor(&c->tty, 0, 0);
1.66      nicm     1033:        else {
1.126     nicm     1034:                o = status && options_get_number(oo, "status-position") == 0;
1.66      nicm     1035:                tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
                   1036:        }
1.1       nicm     1037:
1.50      nicm     1038:        /*
1.131     nicm     1039:         * Set mouse mode if requested. To support dragging, always use button
                   1040:         * mode.
1.57      nicm     1041:         */
                   1042:        mode = s->mode;
1.131     nicm     1043:        if (options_get_number(oo, "mouse"))
                   1044:                mode = (mode & ~ALL_MOUSE_MODES) | MODE_MOUSE_BUTTON;
1.48      nicm     1045:
                   1046:        /* Set the terminal mode and reset attributes. */
1.59      nicm     1047:        tty_update_mode(&c->tty, mode, s);
1.1       nicm     1048:        tty_reset(&c->tty);
1.17      nicm     1049: }
                   1050:
                   1051: /* Repeat time callback. */
1.190     nicm     1052: static void
1.170     nicm     1053: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17      nicm     1054: {
                   1055:        struct client   *c = data;
                   1056:
1.85      nicm     1057:        if (c->flags & CLIENT_REPEAT) {
1.178     nicm     1058:                server_client_set_key_table(c, NULL);
1.132     nicm     1059:                c->flags &= ~CLIENT_REPEAT;
                   1060:                server_status_client(c);
1.85      nicm     1061:        }
1.192     nicm     1062: }
                   1063:
                   1064: /* Double-click callback. */
                   1065: static void
                   1066: server_client_click_timer(__unused int fd, __unused short events, void *data)
                   1067: {
                   1068:        struct client   *c = data;
                   1069:
                   1070:        c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1       nicm     1071: }
                   1072:
1.36      nicm     1073: /* Check if client should be exited. */
1.190     nicm     1074: static void
1.36      nicm     1075: server_client_check_exit(struct client *c)
                   1076: {
                   1077:        if (!(c->flags & CLIENT_EXIT))
                   1078:                return;
                   1079:
1.73      nicm     1080:        if (EVBUFFER_LENGTH(c->stdin_data) != 0)
                   1081:                return;
                   1082:        if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36      nicm     1083:                return;
1.73      nicm     1084:        if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36      nicm     1085:                return;
                   1086:
1.162     nicm     1087:        proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36      nicm     1088:        c->flags &= ~CLIENT_EXIT;
1.38      nicm     1089: }
                   1090:
1.1       nicm     1091: /* Check for client redraws. */
1.190     nicm     1092: static void
1.1       nicm     1093: server_client_check_redraw(struct client *c)
                   1094: {
                   1095:        struct session          *s = c->session;
1.137     nicm     1096:        struct tty              *tty = &c->tty;
1.1       nicm     1097:        struct window_pane      *wp;
1.189     nicm     1098:        int                      flags, masked;
1.1       nicm     1099:
1.86      nicm     1100:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79      nicm     1101:                return;
                   1102:
1.1       nicm     1103:        if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
1.163     nicm     1104:                if (options_get_number(s->options, "set-titles"))
1.1       nicm     1105:                        server_client_set_title(c);
1.189     nicm     1106:                screen_redraw_update(c); /* will adjust flags */
1.1       nicm     1107:        }
                   1108:
1.137     nicm     1109:        flags = tty->flags & (TTY_FREEZE|TTY_NOCURSOR);
                   1110:        tty->flags = (tty->flags & ~TTY_FREEZE) | TTY_NOCURSOR;
                   1111:
1.1       nicm     1112:        if (c->flags & CLIENT_REDRAW) {
1.137     nicm     1113:                tty_update_mode(tty, tty->mode, NULL);
1.115     nicm     1114:                screen_redraw_screen(c, 1, 1, 1);
1.27      nicm     1115:                c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38      nicm     1116:        } else if (c->flags & CLIENT_REDRAWWINDOW) {
1.137     nicm     1117:                tty_update_mode(tty, tty->mode, NULL);
1.38      nicm     1118:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
                   1119:                        screen_redraw_pane(c, wp);
                   1120:                c->flags &= ~CLIENT_REDRAWWINDOW;
1.1       nicm     1121:        } else {
                   1122:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137     nicm     1123:                        if (wp->flags & PANE_REDRAW) {
                   1124:                                tty_update_mode(tty, tty->mode, NULL);
1.1       nicm     1125:                                screen_redraw_pane(c, wp);
1.137     nicm     1126:                        }
1.1       nicm     1127:                }
                   1128:        }
                   1129:
1.185     nicm     1130:        masked = c->flags & (CLIENT_BORDERS|CLIENT_STATUS);
                   1131:        if (masked != 0)
1.137     nicm     1132:                tty_update_mode(tty, tty->mode, NULL);
1.185     nicm     1133:        if (masked == CLIENT_BORDERS)
1.115     nicm     1134:                screen_redraw_screen(c, 0, 0, 1);
1.185     nicm     1135:        else if (masked == CLIENT_STATUS)
1.115     nicm     1136:                screen_redraw_screen(c, 0, 1, 0);
1.185     nicm     1137:        else if (masked != 0)
                   1138:                screen_redraw_screen(c, 0, 1, 1);
1.1       nicm     1139:
1.137     nicm     1140:        tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
                   1141:        tty_update_mode(tty, tty->mode, NULL);
1.1       nicm     1142:
1.153     nicm     1143:        c->flags &= ~(CLIENT_REDRAW|CLIENT_BORDERS|CLIENT_STATUS|
                   1144:            CLIENT_STATUSFORCE);
1.1       nicm     1145: }
                   1146:
                   1147: /* Set client title. */
1.190     nicm     1148: static void
1.1       nicm     1149: server_client_set_title(struct client *c)
                   1150: {
1.128     nicm     1151:        struct session          *s = c->session;
                   1152:        const char              *template;
                   1153:        char                    *title;
                   1154:        struct format_tree      *ft;
1.1       nicm     1155:
1.163     nicm     1156:        template = options_get_string(s->options, "set-titles-string");
1.25      nicm     1157:
1.176     nicm     1158:        ft = format_create(NULL, 0);
1.128     nicm     1159:        format_defaults(ft, c, NULL, NULL, NULL);
                   1160:
                   1161:        title = format_expand_time(ft, template, time(NULL));
1.1       nicm     1162:        if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77      nicm     1163:                free(c->title);
1.1       nicm     1164:                c->title = xstrdup(title);
                   1165:                tty_set_title(&c->tty, c->title);
                   1166:        }
1.77      nicm     1167:        free(title);
1.128     nicm     1168:
                   1169:        format_free(ft);
1.1       nicm     1170: }
                   1171:
                   1172: /* Dispatch message from client. */
1.190     nicm     1173: static void
1.162     nicm     1174: server_client_dispatch(struct imsg *imsg, void *arg)
1.1       nicm     1175: {
1.162     nicm     1176:        struct client           *c = arg;
1.73      nicm     1177:        struct msg_stdin_data    stdindata;
1.108     nicm     1178:        const char              *data;
1.162     nicm     1179:        ssize_t                  datalen;
1.149     nicm     1180:        struct session          *s;
1.1       nicm     1181:
1.162     nicm     1182:        if (c->flags & CLIENT_DEAD)
                   1183:                return;
                   1184:
                   1185:        if (imsg == NULL) {
                   1186:                server_client_lost(c);
                   1187:                return;
                   1188:        }
1.1       nicm     1189:
1.162     nicm     1190:        data = imsg->data;
                   1191:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1       nicm     1192:
1.162     nicm     1193:        switch (imsg->hdr.type) {
                   1194:        case MSG_IDENTIFY_FLAGS:
                   1195:        case MSG_IDENTIFY_TERM:
                   1196:        case MSG_IDENTIFY_TTYNAME:
                   1197:        case MSG_IDENTIFY_CWD:
                   1198:        case MSG_IDENTIFY_STDIN:
                   1199:        case MSG_IDENTIFY_ENVIRON:
                   1200:        case MSG_IDENTIFY_CLIENTPID:
                   1201:        case MSG_IDENTIFY_DONE:
                   1202:                server_client_dispatch_identify(c, imsg);
                   1203:                break;
                   1204:        case MSG_COMMAND:
                   1205:                server_client_dispatch_command(c, imsg);
                   1206:                break;
                   1207:        case MSG_STDIN:
                   1208:                if (datalen != sizeof stdindata)
                   1209:                        fatalx("bad MSG_STDIN size");
                   1210:                memcpy(&stdindata, data, sizeof stdindata);
1.36      nicm     1211:
1.162     nicm     1212:                if (c->stdin_callback == NULL)
1.33      nicm     1213:                        break;
1.162     nicm     1214:                if (stdindata.size <= 0)
                   1215:                        c->stdin_closed = 1;
                   1216:                else {
                   1217:                        evbuffer_add(c->stdin_data, stdindata.data,
                   1218:                            stdindata.size);
                   1219:                }
                   1220:                c->stdin_callback(c, c->stdin_closed,
                   1221:                    c->stdin_callback_data);
                   1222:                break;
                   1223:        case MSG_RESIZE:
                   1224:                if (datalen != 0)
                   1225:                        fatalx("bad MSG_RESIZE size");
1.1       nicm     1226:
1.162     nicm     1227:                if (c->flags & CLIENT_CONTROL)
1.1       nicm     1228:                        break;
1.162     nicm     1229:                if (tty_resize(&c->tty)) {
                   1230:                        recalculate_sizes();
                   1231:                        server_redraw_client(c);
                   1232:                }
1.174     nicm     1233:                if (c->session != NULL)
1.196     nicm     1234:                        notify_client("client-resized", c);
1.162     nicm     1235:                break;
                   1236:        case MSG_EXITING:
                   1237:                if (datalen != 0)
                   1238:                        fatalx("bad MSG_EXITING size");
1.1       nicm     1239:
1.162     nicm     1240:                c->session = NULL;
                   1241:                tty_close(&c->tty);
                   1242:                proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
                   1243:                break;
                   1244:        case MSG_WAKEUP:
                   1245:        case MSG_UNLOCK:
                   1246:                if (datalen != 0)
                   1247:                        fatalx("bad MSG_WAKEUP size");
1.121     nicm     1248:
1.162     nicm     1249:                if (!(c->flags & CLIENT_SUSPENDED))
                   1250:                        break;
                   1251:                c->flags &= ~CLIENT_SUSPENDED;
1.10      nicm     1252:
1.162     nicm     1253:                if (c->tty.fd == -1) /* exited in the meantime */
1.1       nicm     1254:                        break;
1.162     nicm     1255:                s = c->session;
1.1       nicm     1256:
1.162     nicm     1257:                if (gettimeofday(&c->activity_time, NULL) != 0)
                   1258:                        fatal("gettimeofday failed");
                   1259:
                   1260:                tty_start_tty(&c->tty);
                   1261:                server_redraw_client(c);
                   1262:                recalculate_sizes();
1.184     nicm     1263:
                   1264:                if (s != NULL)
                   1265:                        session_update_activity(s, &c->activity_time);
1.162     nicm     1266:                break;
                   1267:        case MSG_SHELL:
                   1268:                if (datalen != 0)
                   1269:                        fatalx("bad MSG_SHELL size");
1.1       nicm     1270:
1.162     nicm     1271:                server_client_dispatch_shell(c);
                   1272:                break;
1.1       nicm     1273:        }
                   1274: }
                   1275:
1.194     nicm     1276: /* Callback when command is done. */
                   1277: static enum cmd_retval
1.195     nicm     1278: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194     nicm     1279: {
1.195     nicm     1280:        struct client   *c = item->client;
1.194     nicm     1281:
                   1282:        if (~c->flags & CLIENT_ATTACHED)
                   1283:                c->flags |= CLIENT_EXIT;
                   1284:        return (CMD_RETURN_NORMAL);
                   1285: }
                   1286:
                   1287: /* Show an error message. */
                   1288: static enum cmd_retval
1.195     nicm     1289: server_client_command_error(struct cmdq_item *item, void *data)
1.194     nicm     1290: {
                   1291:        char    *error = data;
                   1292:
1.195     nicm     1293:        cmdq_error(item, "%s", error);
1.194     nicm     1294:        free(error);
                   1295:
                   1296:        return (CMD_RETURN_NORMAL);
                   1297: }
                   1298:
1.1       nicm     1299: /* Handle command message. */
1.190     nicm     1300: static void
1.162     nicm     1301: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1       nicm     1302: {
1.108     nicm     1303:        struct msg_command_data   data;
                   1304:        char                     *buf;
                   1305:        size_t                    len;
                   1306:        struct cmd_list          *cmdlist = NULL;
                   1307:        int                       argc;
                   1308:        char                    **argv, *cause;
                   1309:
                   1310:        if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
                   1311:                fatalx("bad MSG_COMMAND size");
                   1312:        memcpy(&data, imsg->data, sizeof data);
                   1313:
1.124     nicm     1314:        buf = (char *)imsg->data + sizeof data;
1.108     nicm     1315:        len = imsg->hdr.len  - IMSG_HEADER_SIZE - sizeof data;
                   1316:        if (len > 0 && buf[len - 1] != '\0')
                   1317:                fatalx("bad MSG_COMMAND string");
                   1318:
                   1319:        argc = data.argc;
                   1320:        if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194     nicm     1321:                cause = xstrdup("command too long");
1.1       nicm     1322:                goto error;
                   1323:        }
                   1324:
                   1325:        if (argc == 0) {
                   1326:                argc = 1;
                   1327:                argv = xcalloc(1, sizeof *argv);
                   1328:                *argv = xstrdup("new-session");
                   1329:        }
                   1330:
1.94      nicm     1331:        if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
1.1       nicm     1332:                cmd_free_argv(argc, argv);
                   1333:                goto error;
                   1334:        }
                   1335:        cmd_free_argv(argc, argv);
                   1336:
1.194     nicm     1337:        cmdq_append(c, cmdq_get_command(cmdlist, NULL, NULL, 0));
                   1338:        cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.1       nicm     1339:        cmd_list_free(cmdlist);
                   1340:        return;
                   1341:
                   1342: error:
1.194     nicm     1343:        cmdq_append(c, cmdq_get_callback(server_client_command_error, cause));
                   1344:
1.1       nicm     1345:        if (cmdlist != NULL)
                   1346:                cmd_list_free(cmdlist);
1.88      nicm     1347:
1.36      nicm     1348:        c->flags |= CLIENT_EXIT;
1.1       nicm     1349: }
                   1350:
                   1351: /* Handle identify message. */
1.190     nicm     1352: static void
1.162     nicm     1353: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1       nicm     1354: {
1.165     nicm     1355:        const char      *data, *home;
1.109     nicm     1356:        size_t           datalen;
                   1357:        int              flags;
                   1358:
                   1359:        if (c->flags & CLIENT_IDENTIFIED)
                   1360:                fatalx("out-of-order identify message");
                   1361:
                   1362:        data = imsg->data;
                   1363:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
                   1364:
                   1365:        switch (imsg->hdr.type) {
                   1366:        case MSG_IDENTIFY_FLAGS:
                   1367:                if (datalen != sizeof flags)
                   1368:                        fatalx("bad MSG_IDENTIFY_FLAGS size");
                   1369:                memcpy(&flags, data, sizeof flags);
                   1370:                c->flags |= flags;
1.158     nicm     1371:                log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109     nicm     1372:                break;
                   1373:        case MSG_IDENTIFY_TERM:
1.110     nicm     1374:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1375:                        fatalx("bad MSG_IDENTIFY_TERM string");
                   1376:                c->term = xstrdup(data);
1.158     nicm     1377:                log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109     nicm     1378:                break;
                   1379:        case MSG_IDENTIFY_TTYNAME:
1.110     nicm     1380:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1381:                        fatalx("bad MSG_IDENTIFY_TTYNAME string");
                   1382:                c->ttyname = xstrdup(data);
1.158     nicm     1383:                log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109     nicm     1384:                break;
                   1385:        case MSG_IDENTIFY_CWD:
1.155     nicm     1386:                if (datalen == 0 || data[datalen - 1] != '\0')
                   1387:                        fatalx("bad MSG_IDENTIFY_CWD string");
1.165     nicm     1388:                if (access(data, X_OK) == 0)
                   1389:                        c->cwd = xstrdup(data);
                   1390:                else if ((home = find_home()) != NULL)
                   1391:                        c->cwd = xstrdup(home);
                   1392:                else
                   1393:                        c->cwd = xstrdup("/");
1.158     nicm     1394:                log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109     nicm     1395:                break;
                   1396:        case MSG_IDENTIFY_STDIN:
                   1397:                if (datalen != 0)
                   1398:                        fatalx("bad MSG_IDENTIFY_STDIN size");
                   1399:                c->fd = imsg->fd;
1.158     nicm     1400:                log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109     nicm     1401:                break;
                   1402:        case MSG_IDENTIFY_ENVIRON:
1.110     nicm     1403:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1404:                        fatalx("bad MSG_IDENTIFY_ENVIRON string");
                   1405:                if (strchr(data, '=') != NULL)
1.164     nicm     1406:                        environ_put(c->environ, data);
1.158     nicm     1407:                log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143     nicm     1408:                break;
                   1409:        case MSG_IDENTIFY_CLIENTPID:
                   1410:                if (datalen != sizeof c->pid)
                   1411:                        fatalx("bad MSG_IDENTIFY_CLIENTPID size");
                   1412:                memcpy(&c->pid, data, sizeof c->pid);
1.158     nicm     1413:                log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109     nicm     1414:                break;
                   1415:        default:
                   1416:                break;
                   1417:        }
                   1418:
                   1419:        if (imsg->hdr.type != MSG_IDENTIFY_DONE)
                   1420:                return;
                   1421:        c->flags |= CLIENT_IDENTIFIED;
1.75      nicm     1422:
1.109     nicm     1423:        if (c->flags & CLIENT_CONTROL) {
1.75      nicm     1424:                c->stdin_callback = control_callback;
1.109     nicm     1425:
1.97      nicm     1426:                evbuffer_free(c->stderr_data);
                   1427:                c->stderr_data = c->stdout_data;
1.109     nicm     1428:
                   1429:                if (c->flags & CLIENT_CONTROLCONTROL)
1.96      nicm     1430:                        evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162     nicm     1431:                proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75      nicm     1432:
                   1433:                c->tty.fd = -1;
                   1434:
1.109     nicm     1435:                close(c->fd);
                   1436:                c->fd = -1;
                   1437:
1.75      nicm     1438:                return;
                   1439:        }
1.1       nicm     1440:
1.109     nicm     1441:        if (c->fd == -1)
1.104     nicm     1442:                return;
1.145     nicm     1443:        if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109     nicm     1444:                close(c->fd);
                   1445:                c->fd = -1;
1.80      nicm     1446:                return;
                   1447:        }
1.109     nicm     1448:        if (c->flags & CLIENT_UTF8)
1.1       nicm     1449:                c->tty.flags |= TTY_UTF8;
1.109     nicm     1450:        if (c->flags & CLIENT_256COLOURS)
1.1       nicm     1451:                c->tty.term_flags |= TERM_256COLOURS;
                   1452:
                   1453:        tty_resize(&c->tty);
                   1454:
1.109     nicm     1455:        if (!(c->flags & CLIENT_CONTROL))
1.86      nicm     1456:                c->flags |= CLIENT_TERMINAL;
1.1       nicm     1457: }
                   1458:
                   1459: /* Handle shell message. */
1.190     nicm     1460: static void
1.162     nicm     1461: server_client_dispatch_shell(struct client *c)
1.1       nicm     1462: {
1.107     nicm     1463:        const char      *shell;
1.25      nicm     1464:
1.163     nicm     1465:        shell = options_get_string(global_s_options, "default-shell");
1.1       nicm     1466:        if (*shell == '\0' || areshell(shell))
                   1467:                shell = _PATH_BSHELL;
1.162     nicm     1468:        proc_send_s(c->peer, MSG_SHELL, shell);
1.25      nicm     1469:
1.162     nicm     1470:        proc_kill_peer(c->peer);
1.169     nicm     1471: }
                   1472:
                   1473: /* Event callback to push more stdout data if any left. */
                   1474: static void
1.170     nicm     1475: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169     nicm     1476: {
                   1477:        struct client   *c = arg;
                   1478:
                   1479:        if (~c->flags & CLIENT_DEAD)
                   1480:                server_client_push_stdout(c);
                   1481:        server_client_unref(c);
                   1482: }
                   1483:
                   1484: /* Push stdout to client if possible. */
                   1485: void
                   1486: server_client_push_stdout(struct client *c)
                   1487: {
                   1488:        struct msg_stdout_data data;
                   1489:        size_t                 sent, left;
                   1490:
                   1491:        left = EVBUFFER_LENGTH(c->stdout_data);
                   1492:        while (left != 0) {
                   1493:                sent = left;
                   1494:                if (sent > sizeof data.data)
                   1495:                        sent = sizeof data.data;
                   1496:                memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
                   1497:                data.size = sent;
                   1498:
                   1499:                if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
                   1500:                        break;
                   1501:                evbuffer_drain(c->stdout_data, sent);
                   1502:
                   1503:                left = EVBUFFER_LENGTH(c->stdout_data);
                   1504:                log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
                   1505:                    sent, left);
                   1506:        }
                   1507:        if (left != 0) {
                   1508:                c->references++;
                   1509:                event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
                   1510:                log_debug("%s: client %p, queued", __func__, c);
                   1511:        }
                   1512: }
                   1513:
                   1514: /* Event callback to push more stderr data if any left. */
                   1515: static void
1.170     nicm     1516: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169     nicm     1517: {
                   1518:        struct client   *c = arg;
                   1519:
                   1520:        if (~c->flags & CLIENT_DEAD)
                   1521:                server_client_push_stderr(c);
                   1522:        server_client_unref(c);
                   1523: }
                   1524:
                   1525: /* Push stderr to client if possible. */
                   1526: void
                   1527: server_client_push_stderr(struct client *c)
                   1528: {
                   1529:        struct msg_stderr_data data;
                   1530:        size_t                 sent, left;
                   1531:
                   1532:        if (c->stderr_data == c->stdout_data) {
                   1533:                server_client_push_stdout(c);
                   1534:                return;
                   1535:        }
                   1536:
                   1537:        left = EVBUFFER_LENGTH(c->stderr_data);
                   1538:        while (left != 0) {
                   1539:                sent = left;
                   1540:                if (sent > sizeof data.data)
                   1541:                        sent = sizeof data.data;
                   1542:                memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
                   1543:                data.size = sent;
                   1544:
                   1545:                if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
                   1546:                        break;
                   1547:                evbuffer_drain(c->stderr_data, sent);
                   1548:
                   1549:                left = EVBUFFER_LENGTH(c->stderr_data);
                   1550:                log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
                   1551:                    sent, left);
                   1552:        }
                   1553:        if (left != 0) {
                   1554:                c->references++;
                   1555:                event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
                   1556:                log_debug("%s: client %p, queued", __func__, c);
                   1557:        }
1.1       nicm     1558: }