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

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