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

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