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

1.66    ! nicm        1: /* $OpenBSD: server-client.c,v 1.65 2012/01/29 02:22:11 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
                      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>
                     20:
1.12      nicm       21: #include <event.h>
1.1       nicm       22: #include <fcntl.h>
                     23: #include <string.h>
1.4       nicm       24: #include <time.h>
1.1       nicm       25: #include <paths.h>
                     26: #include <unistd.h>
                     27:
                     28: #include "tmux.h"
                     29:
1.66    ! nicm       30: void   server_client_check_mouse(struct client *c,
        !            31:            struct window_pane *wp, struct mouse_event *mouse);
1.18      nicm       32: void   server_client_handle_key(int, struct mouse_event *, void *);
1.17      nicm       33: void   server_client_repeat_timer(int, short, void *);
1.36      nicm       34: void   server_client_check_exit(struct client *);
1.40      nicm       35: void   server_client_check_backoff(struct client *);
1.1       nicm       36: void   server_client_check_redraw(struct client *);
                     37: void   server_client_set_title(struct client *);
1.18      nicm       38: void   server_client_reset_state(struct client *);
1.36      nicm       39: void   server_client_in_callback(struct bufferevent *, short, void *);
                     40: void   server_client_out_callback(struct bufferevent *, short, void *);
                     41: void   server_client_err_callback(struct bufferevent *, short, void *);
1.1       nicm       42:
                     43: int    server_client_msg_dispatch(struct client *);
                     44: void   server_client_msg_command(struct client *, struct msg_command_data *);
                     45: void   server_client_msg_identify(
1.25      nicm       46:            struct client *, struct msg_identify_data *, int);
1.1       nicm       47: void   server_client_msg_shell(struct client *);
                     48:
                     49: void printflike2 server_client_msg_error(struct cmd_ctx *, const char *, ...);
                     50: void printflike2 server_client_msg_print(struct cmd_ctx *, const char *, ...);
                     51: void printflike2 server_client_msg_info(struct cmd_ctx *, const char *, ...);
                     52:
                     53: /* Create a new client. */
                     54: void
                     55: server_client_create(int fd)
                     56: {
                     57:        struct client   *c;
                     58:        u_int            i;
                     59:
1.49      nicm       60:        setblocking(fd, 0);
1.1       nicm       61:
                     62:        c = xcalloc(1, sizeof *c);
                     63:        c->references = 0;
                     64:        imsg_init(&c->ibuf, fd);
1.14      nicm       65:        server_update_event(c);
1.25      nicm       66:
1.10      nicm       67:        if (gettimeofday(&c->creation_time, NULL) != 0)
1.1       nicm       68:                fatal("gettimeofday failed");
1.11      nicm       69:        memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.1       nicm       70:
1.36      nicm       71:        c->stdin_event = NULL;
                     72:        c->stdout_event = NULL;
                     73:        c->stderr_event = NULL;
1.33      nicm       74:
1.1       nicm       75:        c->tty.fd = -1;
                     76:        c->title = NULL;
                     77:
                     78:        c->session = NULL;
1.45      nicm       79:        c->last_session = NULL;
1.1       nicm       80:        c->tty.sx = 80;
                     81:        c->tty.sy = 24;
                     82:
                     83:        screen_init(&c->status, c->tty.sx, 1, 0);
1.51      nicm       84:        RB_INIT(&c->status_new);
                     85:        RB_INIT(&c->status_old);
1.1       nicm       86:
                     87:        c->message_string = NULL;
1.21      nicm       88:        ARRAY_INIT(&c->message_log);
1.1       nicm       89:
                     90:        c->prompt_string = NULL;
                     91:        c->prompt_buffer = NULL;
                     92:        c->prompt_index = 0;
                     93:
1.57      nicm       94:        c->last_mouse.b = MOUSE_UP;
                     95:        c->last_mouse.x = c->last_mouse.y = -1;
                     96:
1.17      nicm       97:        evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
                     98:
1.1       nicm       99:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    100:                if (ARRAY_ITEM(&clients, i) == NULL) {
                    101:                        ARRAY_SET(&clients, i, c);
                    102:                        return;
                    103:                }
                    104:        }
                    105:        ARRAY_ADD(&clients, c);
                    106:        log_debug("new client %d", fd);
                    107: }
                    108:
                    109: /* Lost a client. */
                    110: void
                    111: server_client_lost(struct client *c)
                    112: {
1.21      nicm      113:        struct message_entry    *msg;
                    114:        u_int                    i;
1.1       nicm      115:
                    116:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    117:                if (ARRAY_ITEM(&clients, i) == c)
                    118:                        ARRAY_SET(&clients, i, NULL);
                    119:        }
                    120:        log_debug("lost client %d", c->ibuf.fd);
                    121:
                    122:        /*
                    123:         * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
                    124:         * and tty_free might close an unrelated fd.
                    125:         */
                    126:        if (c->flags & CLIENT_TERMINAL)
                    127:                tty_free(&c->tty);
                    128:
1.65      nicm      129:        if (c->stdin_event != NULL)
                    130:                bufferevent_free(c->stdin_event);
1.49      nicm      131:        if (c->stdin_fd != -1) {
                    132:                setblocking(c->stdin_fd, 1);
1.36      nicm      133:                close(c->stdin_fd);
1.49      nicm      134:        }
1.65      nicm      135:        if (c->stdout_event != NULL)
                    136:                bufferevent_free(c->stdout_event);
1.49      nicm      137:        if (c->stdout_fd != -1) {
                    138:                setblocking(c->stdout_fd, 1);
1.36      nicm      139:                close(c->stdout_fd);
1.49      nicm      140:        }
1.65      nicm      141:        if (c->stderr_event != NULL)
                    142:                bufferevent_free(c->stderr_event);
1.49      nicm      143:        if (c->stderr_fd != -1) {
                    144:                setblocking(c->stderr_fd, 1);
1.36      nicm      145:                close(c->stderr_fd);
1.49      nicm      146:        }
1.33      nicm      147:
1.51      nicm      148:        status_free_jobs(&c->status_new);
                    149:        status_free_jobs(&c->status_old);
1.1       nicm      150:        screen_free(&c->status);
                    151:
                    152:        if (c->title != NULL)
                    153:                xfree(c->title);
                    154:
1.17      nicm      155:        evtimer_del(&c->repeat_timer);
                    156:
1.15      nicm      157:        evtimer_del(&c->identify_timer);
                    158:
1.1       nicm      159:        if (c->message_string != NULL)
                    160:                xfree(c->message_string);
1.15      nicm      161:        evtimer_del(&c->message_timer);
1.21      nicm      162:        for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
                    163:                msg = &ARRAY_ITEM(&c->message_log, i);
                    164:                xfree(msg->msg);
                    165:        }
                    166:        ARRAY_FREE(&c->message_log);
1.1       nicm      167:
                    168:        if (c->prompt_string != NULL)
                    169:                xfree(c->prompt_string);
                    170:        if (c->prompt_buffer != NULL)
                    171:                xfree(c->prompt_buffer);
                    172:
                    173:        if (c->cwd != NULL)
                    174:                xfree(c->cwd);
1.61      nicm      175:
                    176:        environ_free(&c->environ);
1.1       nicm      177:
                    178:        close(c->ibuf.fd);
                    179:        imsg_clear(&c->ibuf);
1.12      nicm      180:        event_del(&c->event);
1.13      nicm      181:
1.1       nicm      182:        for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
                    183:                if (ARRAY_ITEM(&dead_clients, i) == NULL) {
                    184:                        ARRAY_SET(&dead_clients, i, c);
                    185:                        break;
                    186:                }
                    187:        }
                    188:        if (i == ARRAY_LENGTH(&dead_clients))
                    189:                ARRAY_ADD(&dead_clients, c);
                    190:        c->flags |= CLIENT_DEAD;
                    191:
                    192:        recalculate_sizes();
1.41      nicm      193:        server_check_unattached();
1.19      nicm      194:        server_update_socket();
1.9       nicm      195: }
                    196:
1.1       nicm      197: /* Process a single client event. */
                    198: void
1.12      nicm      199: server_client_callback(int fd, short events, void *data)
1.1       nicm      200: {
                    201:        struct client   *c = data;
1.7       nicm      202:
                    203:        if (c->flags & CLIENT_DEAD)
                    204:                return;
1.1       nicm      205:
                    206:        if (fd == c->ibuf.fd) {
1.12      nicm      207:                if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) < 0)
1.1       nicm      208:                        goto client_lost;
                    209:
                    210:                if (c->flags & CLIENT_BAD) {
                    211:                        if (c->ibuf.w.queued == 0)
                    212:                                goto client_lost;
                    213:                        return;
                    214:                }
                    215:
1.12      nicm      216:                if (events & EV_READ && server_client_msg_dispatch(c) != 0)
1.1       nicm      217:                        goto client_lost;
                    218:        }
1.14      nicm      219:
1.25      nicm      220:        server_update_event(c);
1.1       nicm      221:        return;
                    222:
                    223: client_lost:
                    224:        server_client_lost(c);
                    225: }
                    226:
1.16      nicm      227: /* Handle client status timer. */
                    228: void
                    229: server_client_status_timer(void)
                    230: {
                    231:        struct client   *c;
                    232:        struct session  *s;
                    233:        struct timeval   tv;
1.20      nicm      234:        u_int            i;
                    235:        int              interval;
                    236:        time_t           difference;
1.16      nicm      237:
                    238:        if (gettimeofday(&tv, NULL) != 0)
                    239:                fatal("gettimeofday failed");
                    240:
                    241:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    242:                c = ARRAY_ITEM(&clients, i);
                    243:                if (c == NULL || c->session == NULL)
                    244:                        continue;
                    245:                if (c->message_string != NULL || c->prompt_string != NULL) {
                    246:                        /*
                    247:                         * Don't need timed redraw for messages/prompts so bail
                    248:                         * now. The status timer isn't reset when they are
                    249:                         * redrawn anyway.
                    250:                         */
                    251:                        continue;
                    252:                }
                    253:                s = c->session;
                    254:
                    255:                if (!options_get_number(&s->options, "status"))
                    256:                        continue;
                    257:                interval = options_get_number(&s->options, "status-interval");
                    258:
1.20      nicm      259:                difference = tv.tv_sec - c->status_timer.tv_sec;
                    260:                if (difference >= interval) {
1.51      nicm      261:                        status_update_jobs(c);
1.16      nicm      262:                        c->flags |= CLIENT_STATUS;
                    263:                }
                    264:        }
                    265: }
                    266:
1.66    ! nicm      267: /* Check for mouse keys. */
        !           268: void
        !           269: server_client_check_mouse(
        !           270:     struct client *c, struct window_pane *wp, struct mouse_event *mouse)
        !           271: {
        !           272:        struct session  *s = c->session;
        !           273:        struct options  *oo = &s->options;
        !           274:        int              statusat;
        !           275:
        !           276:        statusat = status_at_line(c);
        !           277:
        !           278:        /* Is this a window selection click on the status line? */
        !           279:        if (statusat != -1 && mouse->y == (u_int)statusat &&
        !           280:            options_get_number(oo, "mouse-select-window")) {
        !           281:                if (mouse->b == MOUSE_UP && c->last_mouse.b != MOUSE_UP) {
        !           282:                        status_set_window_at(c, mouse->x);
        !           283:                        return;
        !           284:                }
        !           285:                if (mouse->b & MOUSE_45) {
        !           286:                        if ((mouse->b & MOUSE_BUTTON) == MOUSE_1) {
        !           287:                                session_previous(c->session, 0);
        !           288:                                server_redraw_session(s);
        !           289:                        }
        !           290:                        if ((mouse->b & MOUSE_BUTTON) == MOUSE_2) {
        !           291:                                session_next(c->session, 0);
        !           292:                                server_redraw_session(s);
        !           293:                        }
        !           294:                        return;
        !           295:                }
        !           296:        }
        !           297:
        !           298:        /*
        !           299:         * Not on status line - adjust mouse position if status line is at the
        !           300:         * top and limit if at the bottom. From here on a struct mouse
        !           301:         * represents the offset onto the window itself.
        !           302:         */
        !           303:        if (statusat == 0 &&mouse->y > 0)
        !           304:                mouse->y--;
        !           305:        else if (statusat > 0 && mouse->y >= (u_int)statusat)
        !           306:                mouse->y = statusat - 1;
        !           307:
        !           308:        /* Is this a pane selection? Allow down only in copy mode. */
        !           309:        if (options_get_number(oo, "mouse-select-pane") &&
        !           310:            ((!(mouse->b & MOUSE_DRAG) && mouse->b != MOUSE_UP) ||
        !           311:            wp->mode != &window_copy_mode)) {
        !           312:                window_set_active_at(wp->window, mouse->x, mouse->y);
        !           313:                server_redraw_window_borders(wp->window);
        !           314:                wp = wp->window->active; /* may have changed */
        !           315:        }
        !           316:
        !           317:        /* Check if trying to resize pane. */
        !           318:        if (options_get_number(oo, "mouse-resize-pane"))
        !           319:                layout_resize_pane_mouse(c, mouse);
        !           320:
        !           321:        /* Update last and pass through to client. */
        !           322:        memcpy(&c->last_mouse, mouse, sizeof c->last_mouse);
        !           323:        window_pane_mouse(wp, c->session, mouse);
        !           324: }
        !           325:
1.18      nicm      326: /* Handle data key input from client. */
                    327: void
                    328: server_client_handle_key(int key, struct mouse_event *mouse, void *data)
                    329: {
                    330:        struct client           *c = data;
                    331:        struct session          *s;
                    332:        struct window           *w;
                    333:        struct window_pane      *wp;
                    334:        struct options          *oo;
                    335:        struct timeval           tv;
                    336:        struct key_binding      *bd;
                    337:        int                      xtimeout, isprefix;
                    338:
                    339:        /* Check the client is good to accept input. */
                    340:        if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
                    341:                return;
                    342:        if (c->session == NULL)
                    343:                return;
                    344:        s = c->session;
                    345:
                    346:        /* Update the activity timer. */
                    347:        if (gettimeofday(&c->activity_time, NULL) != 0)
                    348:                fatal("gettimeofday failed");
                    349:        memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
                    350:
                    351:        w = c->session->curw->window;
                    352:        wp = w->active;
                    353:        oo = &c->session->options;
                    354:
                    355:        /* Special case: number keys jump to pane in identify mode. */
1.25      nicm      356:        if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30      nicm      357:                if (c->flags & CLIENT_READONLY)
                    358:                        return;
1.18      nicm      359:                wp = window_pane_at_index(w, key - '0');
                    360:                if (wp != NULL && window_pane_visible(wp))
                    361:                        window_set_active_pane(w, wp);
                    362:                server_clear_identify(c);
                    363:                return;
                    364:        }
                    365:
                    366:        /* Handle status line. */
1.30      nicm      367:        if (!(c->flags & CLIENT_READONLY)) {
                    368:                status_message_clear(c);
                    369:                server_clear_identify(c);
                    370:        }
1.18      nicm      371:        if (c->prompt_string != NULL) {
1.30      nicm      372:                if (!(c->flags & CLIENT_READONLY))
                    373:                        status_prompt_key(c, key);
1.18      nicm      374:                return;
                    375:        }
                    376:
                    377:        /* Check for mouse keys. */
                    378:        if (key == KEYC_MOUSE) {
1.30      nicm      379:                if (c->flags & CLIENT_READONLY)
                    380:                        return;
1.66    ! nicm      381:                server_client_check_mouse(c, wp, mouse);
1.18      nicm      382:                return;
                    383:        }
                    384:
                    385:        /* Is this a prefix key? */
1.63      nicm      386:        if (key == options_get_number(&c->session->options, "prefix"))
                    387:                isprefix = 1;
                    388:        else if (key == options_get_number(&c->session->options, "prefix2"))
                    389:                isprefix = 1;
                    390:        else
                    391:                isprefix = 0;
1.18      nicm      392:
                    393:        /* No previous prefix key. */
                    394:        if (!(c->flags & CLIENT_PREFIX)) {
                    395:                if (isprefix)
                    396:                        c->flags |= CLIENT_PREFIX;
                    397:                else {
                    398:                        /* Try as a non-prefix key binding. */
1.30      nicm      399:                        if ((bd = key_bindings_lookup(key)) == NULL) {
                    400:                                if (!(c->flags & CLIENT_READONLY))
1.31      nicm      401:                                        window_pane_key(wp, c->session, key);
1.30      nicm      402:                        } else
1.18      nicm      403:                                key_bindings_dispatch(bd, c);
                    404:                }
                    405:                return;
                    406:        }
                    407:
                    408:        /* Prefix key already pressed. Reset prefix and lookup key. */
                    409:        c->flags &= ~CLIENT_PREFIX;
                    410:        if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
                    411:                /* If repeating, treat this as a key, else ignore. */
                    412:                if (c->flags & CLIENT_REPEAT) {
                    413:                        c->flags &= ~CLIENT_REPEAT;
                    414:                        if (isprefix)
                    415:                                c->flags |= CLIENT_PREFIX;
1.30      nicm      416:                        else if (!(c->flags & CLIENT_READONLY))
1.31      nicm      417:                                window_pane_key(wp, c->session, key);
1.18      nicm      418:                }
                    419:                return;
                    420:        }
                    421:
                    422:        /* If already repeating, but this key can't repeat, skip it. */
                    423:        if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
                    424:                c->flags &= ~CLIENT_REPEAT;
                    425:                if (isprefix)
                    426:                        c->flags |= CLIENT_PREFIX;
1.30      nicm      427:                else if (!(c->flags & CLIENT_READONLY))
1.31      nicm      428:                        window_pane_key(wp, c->session, key);
1.18      nicm      429:                return;
                    430:        }
                    431:
                    432:        /* If this key can repeat, reset the repeat flags and timer. */
                    433:        xtimeout = options_get_number(&c->session->options, "repeat-time");
                    434:        if (xtimeout != 0 && bd->can_repeat) {
                    435:                c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
1.25      nicm      436:
1.18      nicm      437:                tv.tv_sec = xtimeout / 1000;
                    438:                tv.tv_usec = (xtimeout % 1000) * 1000L;
                    439:                evtimer_del(&c->repeat_timer);
                    440:                evtimer_add(&c->repeat_timer, &tv);
                    441:        }
                    442:
                    443:        /* Dispatch the command. */
                    444:        key_bindings_dispatch(bd, c);
                    445: }
                    446:
1.2       nicm      447: /* Client functions that need to happen every loop. */
                    448: void
                    449: server_client_loop(void)
                    450: {
                    451:        struct client           *c;
                    452:        struct window           *w;
                    453:        struct window_pane      *wp;
                    454:        u_int                    i;
                    455:
                    456:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    457:                c = ARRAY_ITEM(&clients, i);
1.36      nicm      458:                if (c == NULL)
1.2       nicm      459:                        continue;
                    460:
1.36      nicm      461:                server_client_check_exit(c);
                    462:                if (c->session != NULL) {
                    463:                        server_client_check_redraw(c);
                    464:                        server_client_reset_state(c);
                    465:                }
1.2       nicm      466:        }
                    467:
                    468:        /*
                    469:         * Any windows will have been redrawn as part of clients, so clear
                    470:         * their flags now.
                    471:         */
                    472:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    473:                w = ARRAY_ITEM(&windows, i);
                    474:                if (w == NULL)
                    475:                        continue;
                    476:
                    477:                w->flags &= ~WINDOW_REDRAW;
                    478:                TAILQ_FOREACH(wp, &w->panes, entry)
                    479:                        wp->flags &= ~PANE_REDRAW;
                    480:        }
                    481: }
                    482:
1.18      nicm      483: /*
                    484:  * Update cursor position and mode settings. The scroll region and attributes
                    485:  * are cleared when idle (waiting for an event) as this is the most likely time
                    486:  * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
                    487:  * compromise between excessive resets and likelihood of an interrupt.
                    488:  *
                    489:  * tty_region/tty_reset/tty_update_mode already take care of not resetting
                    490:  * things that are already in their default state.
                    491:  */
1.1       nicm      492: void
1.18      nicm      493: server_client_reset_state(struct client *c)
1.1       nicm      494: {
1.18      nicm      495:        struct window           *w = c->session->curw->window;
                    496:        struct window_pane      *wp = w->active;
                    497:        struct screen           *s = wp->screen;
                    498:        struct options          *oo = &c->session->options;
1.54      nicm      499:        struct options          *wo = &w->options;
1.66    ! nicm      500:        int                      status, mode, o;
1.60      nicm      501:
                    502:        if (c->flags & CLIENT_SUSPENDED)
                    503:                return;
1.1       nicm      504:
                    505:        tty_region(&c->tty, 0, c->tty.sy - 1);
                    506:
                    507:        status = options_get_number(oo, "status");
                    508:        if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
                    509:                tty_cursor(&c->tty, 0, 0);
1.66    ! nicm      510:        else {
        !           511:                o = status && options_get_number (oo, "status-position") == 0;
        !           512:                tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
        !           513:        }
1.1       nicm      514:
1.50      nicm      515:        /*
1.57      nicm      516:         * Resizing panes with the mouse requires at least button mode to give
                    517:         * a smooth appearance.
                    518:         */
                    519:        mode = s->mode;
                    520:        if ((c->last_mouse.b & MOUSE_RESIZE_PANE) &&
                    521:            !(mode & (MODE_MOUSE_BUTTON|MODE_MOUSE_ANY)))
                    522:                mode |= MODE_MOUSE_BUTTON;
                    523:
                    524:        /*
1.50      nicm      525:         * Any mode will do for mouse-select-pane, but set standard mode if
                    526:         * none.
                    527:         */
1.54      nicm      528:        if ((mode & ALL_MOUSE_MODES) == 0) {
                    529:                if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
1.55      nicm      530:                    options_get_number(oo, "mouse-select-pane"))
1.57      nicm      531:                        mode |= MODE_MOUSE_STANDARD;
                    532:                else if (options_get_number(oo, "mouse-resize-pane"))
1.54      nicm      533:                        mode |= MODE_MOUSE_STANDARD;
                    534:                else if (options_get_number(oo, "mouse-select-window"))
                    535:                        mode |= MODE_MOUSE_STANDARD;
                    536:                else if (options_get_number(wo, "mode-mouse"))
                    537:                        mode |= MODE_MOUSE_STANDARD;
                    538:        }
1.48      nicm      539:
                    540:        /*
                    541:         * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
                    542:         * user has set mouse-utf8 and any mouse mode is in effect, turn on
                    543:         * UTF-8 mouse input. If the receiving terminal hasn't requested it
                    544:         * (that is, it isn't in s->mode), then it'll be converted in
                    545:         * input_mouse.
                    546:         */
                    547:        if ((c->tty.flags & TTY_UTF8) &&
                    548:            (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
                    549:                mode |= MODE_MOUSE_UTF8;
                    550:        else
                    551:                mode &= ~MODE_MOUSE_UTF8;
                    552:
                    553:        /* Set the terminal mode and reset attributes. */
1.59      nicm      554:        tty_update_mode(&c->tty, mode, s);
1.1       nicm      555:        tty_reset(&c->tty);
1.17      nicm      556: }
                    557:
                    558: /* Repeat time callback. */
1.24      nicm      559: /* ARGSUSED */
1.17      nicm      560: void
                    561: server_client_repeat_timer(unused int fd, unused short events, void *data)
                    562: {
                    563:        struct client   *c = data;
                    564:
                    565:        if (c->flags & CLIENT_REPEAT)
                    566:                c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
1.1       nicm      567: }
                    568:
1.36      nicm      569: /* Check if client should be exited. */
                    570: void
                    571: server_client_check_exit(struct client *c)
                    572: {
                    573:        struct msg_exit_data    exitdata;
                    574:
                    575:        if (!(c->flags & CLIENT_EXIT))
                    576:                return;
                    577:
                    578:        if (c->stdout_fd != -1 && c->stdout_event != NULL &&
                    579:            EVBUFFER_LENGTH(c->stdout_event->output) != 0)
                    580:                return;
                    581:        if (c->stderr_fd != -1 && c->stderr_event != NULL &&
                    582:            EVBUFFER_LENGTH(c->stderr_event->output) != 0)
                    583:                return;
                    584:
                    585:        exitdata.retcode = c->retcode;
                    586:        server_write_client(c, MSG_EXIT, &exitdata, sizeof exitdata);
                    587:
                    588:        c->flags &= ~CLIENT_EXIT;
1.38      nicm      589: }
                    590:
1.1       nicm      591: /* Check for client redraws. */
                    592: void
                    593: server_client_check_redraw(struct client *c)
                    594: {
                    595:        struct session          *s = c->session;
                    596:        struct window_pane      *wp;
                    597:        int                      flags, redraw;
                    598:
                    599:        flags = c->tty.flags & TTY_FREEZE;
                    600:        c->tty.flags &= ~TTY_FREEZE;
                    601:
                    602:        if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
                    603:                if (options_get_number(&s->options, "set-titles"))
                    604:                        server_client_set_title(c);
1.12      nicm      605:
1.1       nicm      606:                if (c->message_string != NULL)
                    607:                        redraw = status_message_redraw(c);
                    608:                else if (c->prompt_string != NULL)
                    609:                        redraw = status_prompt_redraw(c);
                    610:                else
                    611:                        redraw = status_redraw(c);
                    612:                if (!redraw)
                    613:                        c->flags &= ~CLIENT_STATUS;
                    614:        }
                    615:
                    616:        if (c->flags & CLIENT_REDRAW) {
1.27      nicm      617:                screen_redraw_screen(c, 0, 0);
                    618:                c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38      nicm      619:        } else if (c->flags & CLIENT_REDRAWWINDOW) {
                    620:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
                    621:                        screen_redraw_pane(c, wp);
                    622:                c->flags &= ~CLIENT_REDRAWWINDOW;
1.1       nicm      623:        } else {
                    624:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
                    625:                        if (wp->flags & PANE_REDRAW)
                    626:                                screen_redraw_pane(c, wp);
                    627:                }
                    628:        }
                    629:
1.27      nicm      630:        if (c->flags & CLIENT_BORDERS)
                    631:                screen_redraw_screen(c, 0, 1);
                    632:
1.1       nicm      633:        if (c->flags & CLIENT_STATUS)
1.27      nicm      634:                screen_redraw_screen(c, 1, 0);
1.1       nicm      635:
                    636:        c->tty.flags |= flags;
                    637:
1.27      nicm      638:        c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
1.1       nicm      639: }
                    640:
                    641: /* Set client title. */
                    642: void
                    643: server_client_set_title(struct client *c)
                    644: {
                    645:        struct session  *s = c->session;
                    646:        const char      *template;
                    647:        char            *title;
                    648:
                    649:        template = options_get_string(&s->options, "set-titles-string");
1.25      nicm      650:
1.52      nicm      651:        title = status_replace(c, NULL, NULL, NULL, template, time(NULL), 1);
1.1       nicm      652:        if (c->title == NULL || strcmp(title, c->title) != 0) {
                    653:                if (c->title != NULL)
                    654:                        xfree(c->title);
                    655:                c->title = xstrdup(title);
                    656:                tty_set_title(&c->tty, c->title);
                    657:        }
                    658:        xfree(title);
                    659: }
                    660:
1.36      nicm      661: /*
                    662:  * Error callback for client stdin. Caller must increase reference count when
                    663:  * enabling event!
                    664:  */
                    665: void
                    666: server_client_in_callback(
                    667:     unused struct bufferevent *bufev, unused short what, void *data)
                    668: {
                    669:        struct client   *c = data;
                    670:
                    671:        c->references--;
                    672:        if (c->flags & CLIENT_DEAD)
                    673:                return;
                    674:
                    675:        bufferevent_disable(c->stdin_event, EV_READ|EV_WRITE);
1.49      nicm      676:        setblocking(c->stdin_fd, 1);
1.36      nicm      677:        close(c->stdin_fd);
                    678:        c->stdin_fd = -1;
                    679:
                    680:        if (c->stdin_callback != NULL)
                    681:                c->stdin_callback(c, c->stdin_data);
                    682: }
                    683:
                    684: /* Error callback for client stdout. */
                    685: void
                    686: server_client_out_callback(
                    687:     unused struct bufferevent *bufev, unused short what, unused void *data)
                    688: {
                    689:        struct client   *c = data;
                    690:
                    691:        bufferevent_disable(c->stdout_event, EV_READ|EV_WRITE);
1.49      nicm      692:        setblocking(c->stdout_fd, 1);
1.36      nicm      693:        close(c->stdout_fd);
                    694:        c->stdout_fd = -1;
                    695: }
                    696:
                    697: /* Error callback for client stderr. */
                    698: void
                    699: server_client_err_callback(
                    700:     unused struct bufferevent *bufev, unused short what, unused void *data)
                    701: {
                    702:        struct client   *c = data;
                    703:
                    704:        bufferevent_disable(c->stderr_event, EV_READ|EV_WRITE);
1.49      nicm      705:        setblocking(c->stderr_fd, 1);
1.36      nicm      706:        close(c->stderr_fd);
                    707:        c->stderr_fd = -1;
                    708: }
                    709:
1.1       nicm      710: /* Dispatch message from client. */
                    711: int
                    712: server_client_msg_dispatch(struct client *c)
                    713: {
                    714:        struct imsg              imsg;
                    715:        struct msg_command_data  commanddata;
                    716:        struct msg_identify_data identifydata;
                    717:        struct msg_environ_data  environdata;
                    718:        ssize_t                  n, datalen;
                    719:
1.8       deraadt   720:        if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
                    721:                return (-1);
1.1       nicm      722:
                    723:        for (;;) {
                    724:                if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
                    725:                        return (-1);
                    726:                if (n == 0)
                    727:                        return (0);
                    728:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
                    729:
                    730:                if (imsg.hdr.peerid != PROTOCOL_VERSION) {
                    731:                        server_write_client(c, MSG_VERSION, NULL, 0);
                    732:                        c->flags |= CLIENT_BAD;
                    733:                        imsg_free(&imsg);
                    734:                        continue;
                    735:                }
                    736:
                    737:                log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
                    738:                switch (imsg.hdr.type) {
                    739:                case MSG_COMMAND:
                    740:                        if (datalen != sizeof commanddata)
                    741:                                fatalx("bad MSG_COMMAND size");
                    742:                        memcpy(&commanddata, imsg.data, sizeof commanddata);
                    743:
                    744:                        server_client_msg_command(c, &commanddata);
                    745:                        break;
                    746:                case MSG_IDENTIFY:
                    747:                        if (datalen != sizeof identifydata)
                    748:                                fatalx("bad MSG_IDENTIFY size");
                    749:                        if (imsg.fd == -1)
                    750:                                fatalx("MSG_IDENTIFY missing fd");
                    751:                        memcpy(&identifydata, imsg.data, sizeof identifydata);
                    752:
1.39      nicm      753:                        c->stdin_fd = imsg.fd;
1.37      nicm      754:                        c->stdin_event = bufferevent_new(c->stdin_fd,
                    755:                            NULL, NULL, server_client_in_callback, c);
1.36      nicm      756:                        if (c->stdin_event == NULL)
                    757:                                fatalx("failed to create stdin event");
1.49      nicm      758:                        setblocking(c->stdin_fd, 0);
1.36      nicm      759:
1.1       nicm      760:                        server_client_msg_identify(c, &identifydata, imsg.fd);
                    761:                        break;
1.33      nicm      762:                case MSG_STDOUT:
                    763:                        if (datalen != 0)
                    764:                                fatalx("bad MSG_STDOUT size");
                    765:                        if (imsg.fd == -1)
                    766:                                fatalx("MSG_STDOUT missing fd");
                    767:
1.36      nicm      768:                        c->stdout_fd = imsg.fd;
1.37      nicm      769:                        c->stdout_event = bufferevent_new(c->stdout_fd,
                    770:                            NULL, NULL, server_client_out_callback, c);
1.36      nicm      771:                        if (c->stdout_event == NULL)
                    772:                                fatalx("failed to create stdout event");
1.49      nicm      773:                        setblocking(c->stdout_fd, 0);
1.36      nicm      774:
1.33      nicm      775:                        break;
                    776:                case MSG_STDERR:
                    777:                        if (datalen != 0)
                    778:                                fatalx("bad MSG_STDERR size");
                    779:                        if (imsg.fd == -1)
                    780:                                fatalx("MSG_STDERR missing fd");
                    781:
1.36      nicm      782:                        c->stderr_fd = imsg.fd;
1.37      nicm      783:                        c->stderr_event = bufferevent_new(c->stderr_fd,
                    784:                            NULL, NULL, server_client_err_callback, c);
1.36      nicm      785:                        if (c->stderr_event == NULL)
                    786:                                fatalx("failed to create stderr event");
1.49      nicm      787:                        setblocking(c->stderr_fd, 0);
1.36      nicm      788:
1.33      nicm      789:                        break;
1.1       nicm      790:                case MSG_RESIZE:
                    791:                        if (datalen != 0)
                    792:                                fatalx("bad MSG_RESIZE size");
                    793:
1.32      nicm      794:                        if (tty_resize(&c->tty)) {
                    795:                                recalculate_sizes();
                    796:                                server_redraw_client(c);
                    797:                        }
1.1       nicm      798:                        break;
                    799:                case MSG_EXITING:
                    800:                        if (datalen != 0)
                    801:                                fatalx("bad MSG_EXITING size");
                    802:
                    803:                        c->session = NULL;
                    804:                        tty_close(&c->tty);
                    805:                        server_write_client(c, MSG_EXITED, NULL, 0);
                    806:                        break;
                    807:                case MSG_WAKEUP:
                    808:                case MSG_UNLOCK:
                    809:                        if (datalen != 0)
                    810:                                fatalx("bad MSG_WAKEUP size");
                    811:
                    812:                        if (!(c->flags & CLIENT_SUSPENDED))
                    813:                                break;
                    814:                        c->flags &= ~CLIENT_SUSPENDED;
1.10      nicm      815:
1.11      nicm      816:                        if (gettimeofday(&c->activity_time, NULL) != 0)
                    817:                                fatal("gettimeofday");
1.47      nicm      818:                        if (c->session != NULL)
                    819:                                session_update_activity(c->session);
1.10      nicm      820:
1.1       nicm      821:                        tty_start_tty(&c->tty);
                    822:                        server_redraw_client(c);
                    823:                        recalculate_sizes();
                    824:                        break;
                    825:                case MSG_ENVIRON:
                    826:                        if (datalen != sizeof environdata)
                    827:                                fatalx("bad MSG_ENVIRON size");
                    828:                        memcpy(&environdata, imsg.data, sizeof environdata);
                    829:
                    830:                        environdata.var[(sizeof environdata.var) - 1] = '\0';
                    831:                        if (strchr(environdata.var, '=') != NULL)
                    832:                                environ_put(&c->environ, environdata.var);
                    833:                        break;
                    834:                case MSG_SHELL:
                    835:                        if (datalen != 0)
                    836:                                fatalx("bad MSG_SHELL size");
                    837:
                    838:                        server_client_msg_shell(c);
                    839:                        break;
                    840:                default:
                    841:                        fatalx("unexpected message");
                    842:                }
                    843:
                    844:                imsg_free(&imsg);
                    845:        }
                    846: }
                    847:
                    848: /* Callback to send error message to client. */
                    849: void printflike2
                    850: server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
                    851: {
1.33      nicm      852:        va_list ap;
1.1       nicm      853:
                    854:        va_start(ap, fmt);
1.36      nicm      855:        evbuffer_add_vprintf(ctx->cmdclient->stderr_event->output, fmt, ap);
1.1       nicm      856:        va_end(ap);
                    857:
1.36      nicm      858:        bufferevent_write(ctx->cmdclient->stderr_event, "\n", 1);
1.34      nicm      859:        ctx->cmdclient->retcode = 1;
1.1       nicm      860: }
                    861:
                    862: /* Callback to send print message to client. */
                    863: void printflike2
                    864: server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
                    865: {
1.33      nicm      866:        va_list ap;
1.1       nicm      867:
                    868:        va_start(ap, fmt);
1.36      nicm      869:        evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
1.1       nicm      870:        va_end(ap);
                    871:
1.36      nicm      872:        bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
1.1       nicm      873: }
                    874:
                    875: /* Callback to send print message to client, if not quiet. */
                    876: void printflike2
                    877: server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
                    878: {
1.33      nicm      879:        va_list ap;
1.1       nicm      880:
1.26      nicm      881:        if (options_get_number(&global_options, "quiet"))
1.1       nicm      882:                return;
                    883:
                    884:        va_start(ap, fmt);
1.36      nicm      885:        evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
1.1       nicm      886:        va_end(ap);
                    887:
1.36      nicm      888:        bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
1.1       nicm      889: }
                    890:
                    891: /* Handle command message. */
                    892: void
                    893: server_client_msg_command(struct client *c, struct msg_command_data *data)
                    894: {
1.36      nicm      895:        struct cmd_ctx   ctx;
                    896:        struct cmd_list *cmdlist = NULL;
                    897:        int              argc;
                    898:        char           **argv, *cause;
1.1       nicm      899:
                    900:        ctx.error = server_client_msg_error;
                    901:        ctx.print = server_client_msg_print;
                    902:        ctx.info = server_client_msg_info;
                    903:
                    904:        ctx.msgdata = data;
                    905:        ctx.curclient = NULL;
                    906:
                    907:        ctx.cmdclient = c;
                    908:
                    909:        argc = data->argc;
                    910:        data->argv[(sizeof data->argv) - 1] = '\0';
                    911:        if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
                    912:                server_client_msg_error(&ctx, "command too long");
                    913:                goto error;
                    914:        }
                    915:
                    916:        if (argc == 0) {
                    917:                argc = 1;
                    918:                argv = xcalloc(1, sizeof *argv);
                    919:                *argv = xstrdup("new-session");
                    920:        }
                    921:
                    922:        if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
                    923:                server_client_msg_error(&ctx, "%s", cause);
                    924:                cmd_free_argv(argc, argv);
                    925:                goto error;
                    926:        }
                    927:        cmd_free_argv(argc, argv);
                    928:
1.36      nicm      929:        if (cmd_list_exec(cmdlist, &ctx) != 1)
                    930:                c->flags |= CLIENT_EXIT;
1.1       nicm      931:        cmd_list_free(cmdlist);
                    932:        return;
                    933:
                    934: error:
                    935:        if (cmdlist != NULL)
                    936:                cmd_list_free(cmdlist);
1.36      nicm      937:        c->flags |= CLIENT_EXIT;
1.1       nicm      938: }
                    939:
                    940: /* Handle identify message. */
                    941: void
                    942: server_client_msg_identify(
                    943:     struct client *c, struct msg_identify_data *data, int fd)
                    944: {
1.33      nicm      945:        int     tty_fd;
                    946:
1.1       nicm      947:        c->cwd = NULL;
                    948:        data->cwd[(sizeof data->cwd) - 1] = '\0';
                    949:        if (*data->cwd != '\0')
                    950:                c->cwd = xstrdup(data->cwd);
                    951:
1.33      nicm      952:        if (!isatty(fd))
                    953:            return;
                    954:        if ((tty_fd = dup(fd)) == -1)
                    955:                fatal("dup failed");
1.1       nicm      956:        data->term[(sizeof data->term) - 1] = '\0';
1.33      nicm      957:        tty_init(&c->tty, tty_fd, data->term);
1.1       nicm      958:        if (data->flags & IDENTIFY_UTF8)
                    959:                c->tty.flags |= TTY_UTF8;
                    960:        if (data->flags & IDENTIFY_256COLOURS)
                    961:                c->tty.term_flags |= TERM_256COLOURS;
                    962:        else if (data->flags & IDENTIFY_88COLOURS)
                    963:                c->tty.term_flags |= TERM_88COLOURS;
1.18      nicm      964:        c->tty.key_callback = server_client_handle_key;
                    965:        c->tty.key_data = c;
1.1       nicm      966:
                    967:        tty_resize(&c->tty);
                    968:
                    969:        c->flags |= CLIENT_TERMINAL;
                    970: }
                    971:
                    972: /* Handle shell message. */
                    973: void
                    974: server_client_msg_shell(struct client *c)
                    975: {
                    976:        struct msg_shell_data    data;
                    977:        const char              *shell;
1.25      nicm      978:
1.1       nicm      979:        shell = options_get_string(&global_s_options, "default-shell");
                    980:
                    981:        if (*shell == '\0' || areshell(shell))
                    982:                shell = _PATH_BSHELL;
                    983:        if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
                    984:                strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
1.25      nicm      985:
1.1       nicm      986:        server_write_client(c, MSG_SHELL, &data, sizeof data);
                    987:        c->flags |= CLIENT_BAD; /* it will die after exec */
                    988: }