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

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