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

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