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

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