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

1.225   ! nicm        1: /* $OpenBSD: server-client.c,v 1.224 2017/04/21 17:22:20 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.181     nicm        4:  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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.162     nicm       21: #include <sys/uio.h>
1.1       nicm       22:
1.114     benno      23: #include <errno.h>
1.12      nicm       24: #include <event.h>
1.1       nicm       25: #include <fcntl.h>
1.162     nicm       26: #include <imsg.h>
1.98      nicm       27: #include <paths.h>
                     28: #include <stdlib.h>
1.1       nicm       29: #include <string.h>
1.4       nicm       30: #include <time.h>
1.1       nicm       31: #include <unistd.h>
                     32:
                     33: #include "tmux.h"
                     34:
1.190     nicm       35: static void    server_client_free(int, short, void *);
                     36: static void    server_client_check_focus(struct window_pane *);
                     37: static void    server_client_check_resize(struct window_pane *);
                     38: static key_code        server_client_check_mouse(struct client *);
                     39: static void    server_client_repeat_timer(int, short, void *);
1.192     nicm       40: static void    server_client_click_timer(int, short, void *);
1.190     nicm       41: static void    server_client_check_exit(struct client *);
                     42: static void    server_client_check_redraw(struct client *);
                     43: static void    server_client_set_title(struct client *);
                     44: static void    server_client_reset_state(struct client *);
                     45: static int     server_client_assume_paste(struct session *);
                     46:
                     47: static void    server_client_dispatch(struct imsg *, void *);
                     48: static void    server_client_dispatch_command(struct client *, struct imsg *);
                     49: static void    server_client_dispatch_identify(struct client *, struct imsg *);
                     50: static void    server_client_dispatch_shell(struct client *);
1.140     nicm       51:
1.220     nicm       52: /* Identify mode callback. */
1.214     nicm       53: static void
                     54: server_client_callback_identify(__unused int fd, __unused short events, void *data)
                     55: {
                     56:        server_client_clear_identify(data, NULL);
                     57: }
                     58:
                     59: /* Set identify mode on client. */
                     60: void
                     61: server_client_set_identify(struct client *c)
                     62: {
                     63:        struct timeval  tv;
                     64:        int             delay;
                     65:
                     66:        delay = options_get_number(c->session->options, "display-panes-time");
                     67:        tv.tv_sec = delay / 1000;
                     68:        tv.tv_usec = (delay % 1000) * 1000L;
                     69:
                     70:        if (event_initialized(&c->identify_timer))
                     71:                evtimer_del(&c->identify_timer);
                     72:        evtimer_set(&c->identify_timer, server_client_callback_identify, c);
                     73:        evtimer_add(&c->identify_timer, &tv);
                     74:
                     75:        c->flags |= CLIENT_IDENTIFY;
                     76:        c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
                     77:        server_redraw_client(c);
                     78: }
                     79:
                     80: /* Clear identify mode on client. */
                     81: void
                     82: server_client_clear_identify(struct client *c, struct window_pane *wp)
                     83: {
                     84:        if (~c->flags & CLIENT_IDENTIFY)
                     85:                return;
                     86:        c->flags &= ~CLIENT_IDENTIFY;
                     87:
                     88:        if (c->identify_callback != NULL)
                     89:                c->identify_callback(c, wp);
                     90:
                     91:        c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
                     92:        server_redraw_client(c);
                     93: }
                     94:
1.140     nicm       95: /* Check if this client is inside this server. */
                     96: int
                     97: server_client_check_nested(struct client *c)
                     98: {
                     99:        struct environ_entry    *envent;
                    100:        struct window_pane      *wp;
                    101:
1.164     nicm      102:        envent = environ_find(c->environ, "TMUX");
1.140     nicm      103:        if (envent == NULL || *envent->value == '\0')
                    104:                return (0);
                    105:
                    106:        RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
1.216     nicm      107:                if (strcmp(wp->tty, c->ttyname) == 0)
1.140     nicm      108:                        return (1);
                    109:        }
                    110:        return (0);
                    111: }
1.1       nicm      112:
1.132     nicm      113: /* Set client key table. */
                    114: void
1.178     nicm      115: server_client_set_key_table(struct client *c, const char *name)
1.132     nicm      116: {
1.178     nicm      117:        if (name == NULL)
                    118:                name = server_client_get_key_table(c);
                    119:
1.132     nicm      120:        key_bindings_unref_table(c->keytable);
                    121:        c->keytable = key_bindings_get_table(name, 1);
                    122:        c->keytable->references++;
                    123: }
                    124:
1.178     nicm      125: /* Get default key table. */
                    126: const char *
                    127: server_client_get_key_table(struct client *c)
                    128: {
                    129:        struct session  *s = c->session;
                    130:        const char      *name;
                    131:
                    132:        if (s == NULL)
                    133:                return ("root");
                    134:
                    135:        name = options_get_string(s->options, "key-table");
                    136:        if (*name == '\0')
                    137:                return ("root");
                    138:        return (name);
                    139: }
                    140:
1.223     nicm      141: /* Is this table the default key table? */
                    142: static int
                    143: server_client_is_default_key_table(struct client *c, struct key_table *table)
1.191     nicm      144: {
1.223     nicm      145:        return (strcmp(table->name, server_client_get_key_table(c)) == 0);
1.191     nicm      146: }
                    147:
1.1       nicm      148: /* Create a new client. */
                    149: void
                    150: server_client_create(int fd)
                    151: {
                    152:        struct client   *c;
                    153:
1.49      nicm      154:        setblocking(fd, 0);
1.1       nicm      155:
                    156:        c = xcalloc(1, sizeof *c);
1.141     nicm      157:        c->references = 1;
1.162     nicm      158:        c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c);
1.25      nicm      159:
1.10      nicm      160:        if (gettimeofday(&c->creation_time, NULL) != 0)
1.1       nicm      161:                fatal("gettimeofday failed");
1.11      nicm      162:        memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.111     nicm      163:
1.164     nicm      164:        c->environ = environ_create();
1.1       nicm      165:
1.146     nicm      166:        c->fd = -1;
1.165     nicm      167:        c->cwd = NULL;
1.145     nicm      168:
1.194     nicm      169:        TAILQ_INIT(&c->queue);
1.94      nicm      170:
1.116     nicm      171:        c->stdin_data = evbuffer_new();
                    172:        c->stdout_data = evbuffer_new();
                    173:        c->stderr_data = evbuffer_new();
1.33      nicm      174:
1.1       nicm      175:        c->tty.fd = -1;
                    176:        c->title = NULL;
                    177:
                    178:        c->session = NULL;
1.45      nicm      179:        c->last_session = NULL;
1.1       nicm      180:        c->tty.sx = 80;
                    181:        c->tty.sy = 24;
                    182:
                    183:        screen_init(&c->status, c->tty.sx, 1, 0);
                    184:
                    185:        c->message_string = NULL;
1.136     nicm      186:        TAILQ_INIT(&c->message_log);
1.1       nicm      187:
                    188:        c->prompt_string = NULL;
                    189:        c->prompt_buffer = NULL;
                    190:        c->prompt_index = 0;
                    191:
1.93      nicm      192:        c->flags |= CLIENT_FOCUSED;
                    193:
1.132     nicm      194:        c->keytable = key_bindings_get_table("root", 1);
                    195:        c->keytable->references++;
                    196:
1.17      nicm      197:        evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
1.192     nicm      198:        evtimer_set(&c->click_timer, server_client_click_timer, c);
1.17      nicm      199:
1.135     nicm      200:        TAILQ_INSERT_TAIL(&clients, c, entry);
1.157     nicm      201:        log_debug("new client %p", c);
1.72      nicm      202: }
                    203:
                    204: /* Open client terminal if needed. */
                    205: int
1.119     nicm      206: server_client_open(struct client *c, char **cause)
1.72      nicm      207: {
1.75      nicm      208:        if (c->flags & CLIENT_CONTROL)
                    209:                return (0);
1.120     nicm      210:
                    211:        if (strcmp(c->ttyname, "/dev/tty") == 0) {
                    212:                *cause = xstrdup("can't use /dev/tty");
                    213:                return (-1);
                    214:        }
1.75      nicm      215:
1.72      nicm      216:        if (!(c->flags & CLIENT_TERMINAL)) {
1.116     nicm      217:                *cause = xstrdup("not a terminal");
1.72      nicm      218:                return (-1);
                    219:        }
                    220:
1.119     nicm      221:        if (tty_open(&c->tty, cause) != 0)
1.72      nicm      222:                return (-1);
                    223:
                    224:        return (0);
1.1       nicm      225: }
                    226:
                    227: /* Lost a client. */
                    228: void
                    229: server_client_lost(struct client *c)
                    230: {
1.136     nicm      231:        struct message_entry    *msg, *msg1;
1.1       nicm      232:
1.141     nicm      233:        c->flags |= CLIENT_DEAD;
                    234:
1.214     nicm      235:        server_client_clear_identify(c, NULL);
1.141     nicm      236:        status_prompt_clear(c);
                    237:        status_message_clear(c);
                    238:
                    239:        if (c->stdin_callback != NULL)
                    240:                c->stdin_callback(c, 1, c->stdin_callback_data);
                    241:
1.135     nicm      242:        TAILQ_REMOVE(&clients, c, entry);
1.157     nicm      243:        log_debug("lost client %p", c);
1.1       nicm      244:
                    245:        /*
                    246:         * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
                    247:         * and tty_free might close an unrelated fd.
                    248:         */
                    249:        if (c->flags & CLIENT_TERMINAL)
                    250:                tty_free(&c->tty);
1.109     nicm      251:        free(c->ttyname);
                    252:        free(c->term);
1.1       nicm      253:
1.113     nicm      254:        evbuffer_free(c->stdin_data);
                    255:        evbuffer_free(c->stdout_data);
1.97      nicm      256:        if (c->stderr_data != c->stdout_data)
1.126     nicm      257:                evbuffer_free(c->stderr_data);
1.33      nicm      258:
1.148     nicm      259:        if (event_initialized(&c->status_timer))
                    260:                evtimer_del(&c->status_timer);
1.1       nicm      261:        screen_free(&c->status);
                    262:
1.77      nicm      263:        free(c->title);
1.165     nicm      264:        free((void *)c->cwd);
1.1       nicm      265:
1.17      nicm      266:        evtimer_del(&c->repeat_timer);
1.192     nicm      267:        evtimer_del(&c->click_timer);
1.17      nicm      268:
1.132     nicm      269:        key_bindings_unref_table(c->keytable);
                    270:
1.69      nicm      271:        if (event_initialized(&c->identify_timer))
                    272:                evtimer_del(&c->identify_timer);
1.15      nicm      273:
1.77      nicm      274:        free(c->message_string);
1.116     nicm      275:        if (event_initialized(&c->message_timer))
1.69      nicm      276:                evtimer_del(&c->message_timer);
1.136     nicm      277:        TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
1.77      nicm      278:                free(msg->msg);
1.136     nicm      279:                TAILQ_REMOVE(&c->message_log, msg, entry);
                    280:                free(msg);
1.21      nicm      281:        }
1.1       nicm      282:
1.77      nicm      283:        free(c->prompt_string);
                    284:        free(c->prompt_buffer);
1.61      nicm      285:
1.164     nicm      286:        environ_free(c->environ);
1.1       nicm      287:
1.162     nicm      288:        proc_remove_peer(c->peer);
                    289:        c->peer = NULL;
1.13      nicm      290:
1.142     nicm      291:        server_client_unref(c);
1.71      nicm      292:
                    293:        server_add_accept(0); /* may be more file descriptors now */
1.1       nicm      294:
                    295:        recalculate_sizes();
1.41      nicm      296:        server_check_unattached();
1.19      nicm      297:        server_update_socket();
1.141     nicm      298: }
                    299:
                    300: /* Remove reference from a client. */
                    301: void
1.142     nicm      302: server_client_unref(struct client *c)
1.141     nicm      303: {
1.157     nicm      304:        log_debug("unref client %p (%d references)", c, c->references);
1.141     nicm      305:
                    306:        c->references--;
                    307:        if (c->references == 0)
                    308:                event_once(-1, EV_TIMEOUT, server_client_free, c, NULL);
                    309: }
                    310:
                    311: /* Free dead client. */
1.190     nicm      312: static void
1.170     nicm      313: server_client_free(__unused int fd, __unused short events, void *arg)
1.141     nicm      314: {
                    315:        struct client   *c = arg;
                    316:
1.157     nicm      317:        log_debug("free client %p (%d references)", c, c->references);
1.141     nicm      318:
1.194     nicm      319:        if (!TAILQ_EMPTY(&c->queue))
                    320:                fatalx("queue not empty");
                    321:
1.216     nicm      322:        if (c->references == 0) {
                    323:                free((void *)c->name);
1.141     nicm      324:                free(c);
1.216     nicm      325:        }
1.9       nicm      326: }
                    327:
1.220     nicm      328: /* Suspend a client. */
                    329: void
                    330: server_client_suspend(struct client *c)
                    331: {
                    332:        struct session  *s = c->session;
                    333:
                    334:        if (s == NULL || (c->flags & CLIENT_DETACHING))
                    335:                return;
                    336:
                    337:        tty_stop_tty(&c->tty);
                    338:        c->flags |= CLIENT_SUSPENDED;
                    339:        proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0);
                    340: }
                    341:
1.174     nicm      342: /* Detach a client. */
                    343: void
                    344: server_client_detach(struct client *c, enum msgtype msgtype)
                    345: {
1.220     nicm      346:        struct session  *s = c->session;
1.174     nicm      347:
1.220     nicm      348:        if (s == NULL || (c->flags & CLIENT_DETACHING))
1.174     nicm      349:                return;
                    350:
1.220     nicm      351:        c->flags |= CLIENT_DETACHING;
1.196     nicm      352:        notify_client("client-detached", c);
1.174     nicm      353:        proc_send_s(c->peer, msgtype, s->name);
1.207     nicm      354: }
                    355:
1.208     nicm      356: /* Execute command to replace a client. */
1.207     nicm      357: void
                    358: server_client_exec(struct client *c, const char *cmd)
                    359: {
                    360:        struct session  *s = c->session;
1.208     nicm      361:        char            *msg;
                    362:        const char      *shell;
1.207     nicm      363:        size_t           cmdsize, shellsize;
                    364:
                    365:        if (*cmd == '\0')
                    366:                return;
                    367:        cmdsize = strlen(cmd) + 1;
                    368:
                    369:        if (s != NULL)
                    370:                shell = options_get_string(s->options, "default-shell");
                    371:        else
                    372:                shell = options_get_string(global_s_options, "default-shell");
                    373:        shellsize = strlen(shell) + 1;
                    374:
                    375:        msg = xmalloc(cmdsize + shellsize);
                    376:        memcpy(msg, cmd, cmdsize);
                    377:        memcpy(msg + cmdsize, shell, shellsize);
                    378:
                    379:        proc_send(c->peer, MSG_EXEC, -1, msg, cmdsize + shellsize);
                    380:        free(msg);
1.174     nicm      381: }
                    382:
1.66      nicm      383: /* Check for mouse keys. */
1.190     nicm      384: static key_code
1.131     nicm      385: server_client_check_mouse(struct client *c)
1.66      nicm      386: {
1.192     nicm      387:        struct session          *s = c->session;
                    388:        struct mouse_event      *m = &c->tty.mouse;
                    389:        struct window           *w;
                    390:        struct window_pane      *wp;
                    391:        u_int                    x, y, b;
                    392:        int                      flag;
                    393:        key_code                 key;
                    394:        struct timeval           tv;
1.209     nicm      395:        enum { NOTYPE, MOVE, DOWN, UP, DRAG, WHEEL, DOUBLE, TRIPLE } type;
                    396:        enum { NOWHERE, PANE, STATUS, BORDER } where;
                    397:
                    398:        type = NOTYPE;
                    399:        where = NOWHERE;
1.131     nicm      400:
                    401:        log_debug("mouse %02x at %u,%u (last %u,%u) (%d)", m->b, m->x, m->y,
                    402:            m->lx, m->ly, c->tty.mouse_drag_flag);
                    403:
                    404:        /* What type of event is this? */
1.209     nicm      405:        if ((m->sgr_type != ' ' &&
                    406:            MOUSE_DRAG(m->sgr_b) &&
                    407:            MOUSE_BUTTONS(m->sgr_b) == 3) ||
                    408:            (m->sgr_type == ' ' &&
                    409:            MOUSE_DRAG(m->b) &&
                    410:            MOUSE_BUTTONS(m->b) == 3 &&
                    411:            MOUSE_BUTTONS(m->lb) == 3)) {
                    412:                type = MOVE;
                    413:                x = m->x, y = m->y, b = 0;
                    414:                log_debug("move at %u,%u", x, y);
                    415:        } else if (MOUSE_DRAG(m->b)) {
1.131     nicm      416:                type = DRAG;
                    417:                if (c->tty.mouse_drag_flag) {
                    418:                        x = m->x, y = m->y, b = m->b;
                    419:                        log_debug("drag update at %u,%u", x, y);
                    420:                } else {
                    421:                        x = m->lx, y = m->ly, b = m->lb;
                    422:                        log_debug("drag start at %u,%u", x, y);
                    423:                }
                    424:        } else if (MOUSE_WHEEL(m->b)) {
                    425:                type = WHEEL;
                    426:                x = m->x, y = m->y, b = m->b;
                    427:                log_debug("wheel at %u,%u", x, y);
1.200     nicm      428:        } else if (MOUSE_RELEASE(m->b)) {
1.131     nicm      429:                type = UP;
                    430:                x = m->x, y = m->y, b = m->lb;
                    431:                log_debug("up at %u,%u", x, y);
                    432:        } else {
1.192     nicm      433:                if (c->flags & CLIENT_DOUBLECLICK) {
                    434:                        evtimer_del(&c->click_timer);
                    435:                        c->flags &= ~CLIENT_DOUBLECLICK;
                    436:                        if (m->b == c->click_button) {
                    437:                                type = DOUBLE;
                    438:                                x = m->x, y = m->y, b = m->b;
                    439:                                log_debug("double-click at %u,%u", x, y);
                    440:                                flag = CLIENT_TRIPLECLICK;
                    441:                                goto add_timer;
                    442:                        }
                    443:                } else if (c->flags & CLIENT_TRIPLECLICK) {
                    444:                        evtimer_del(&c->click_timer);
                    445:                        c->flags &= ~CLIENT_TRIPLECLICK;
                    446:                        if (m->b == c->click_button) {
                    447:                                type = TRIPLE;
                    448:                                x = m->x, y = m->y, b = m->b;
                    449:                                log_debug("triple-click at %u,%u", x, y);
                    450:                                goto have_event;
                    451:                        }
                    452:                }
                    453:
1.131     nicm      454:                type = DOWN;
                    455:                x = m->x, y = m->y, b = m->b;
                    456:                log_debug("down at %u,%u", x, y);
1.192     nicm      457:                flag = CLIENT_DOUBLECLICK;
                    458:
                    459:        add_timer:
                    460:                if (KEYC_CLICK_TIMEOUT != 0) {
                    461:                        c->flags |= flag;
                    462:                        c->click_button = m->b;
                    463:
                    464:                        tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000;
                    465:                        tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L;
                    466:                        evtimer_del(&c->click_timer);
                    467:                        evtimer_add(&c->click_timer, &tv);
                    468:                }
1.131     nicm      469:        }
1.192     nicm      470:
                    471: have_event:
1.131     nicm      472:        if (type == NOTYPE)
1.177     nicm      473:                return (KEYC_UNKNOWN);
1.131     nicm      474:
                    475:        /* Always save the session. */
                    476:        m->s = s->id;
                    477:
                    478:        /* Is this on the status line? */
                    479:        m->statusat = status_at_line(c);
                    480:        if (m->statusat != -1 && y == (u_int)m->statusat) {
                    481:                w = status_get_window_at(c, x);
                    482:                if (w == NULL)
1.177     nicm      483:                        return (KEYC_UNKNOWN);
1.131     nicm      484:                m->w = w->id;
                    485:                where = STATUS;
                    486:        } else
                    487:                m->w = -1;
                    488:
                    489:        /* Not on status line. Adjust position and check for border or pane. */
                    490:        if (where == NOWHERE) {
                    491:                if (m->statusat == 0 && y > 0)
                    492:                        y--;
                    493:                else if (m->statusat > 0 && y >= (u_int)m->statusat)
                    494:                        y = m->statusat - 1;
                    495:
                    496:                TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
                    497:                        if ((wp->xoff + wp->sx == x &&
                    498:                            wp->yoff <= 1 + y &&
                    499:                            wp->yoff + wp->sy >= y) ||
                    500:                            (wp->yoff + wp->sy == y &&
                    501:                            wp->xoff <= 1 + x &&
                    502:                            wp->xoff + wp->sx >= x))
                    503:                                break;
                    504:                }
                    505:                if (wp != NULL)
                    506:                        where = BORDER;
                    507:                else {
                    508:                        wp = window_get_active_at(s->curw->window, x, y);
1.173     nicm      509:                        if (wp != NULL) {
1.131     nicm      510:                                where = PANE;
1.173     nicm      511:                                log_debug("mouse at %u,%u is on pane %%%u",
                    512:                                    x, y, wp->id);
                    513:                        }
1.131     nicm      514:                }
                    515:                if (where == NOWHERE)
1.177     nicm      516:                        return (KEYC_UNKNOWN);
1.131     nicm      517:                m->wp = wp->id;
                    518:                m->w = wp->window->id;
                    519:        } else
                    520:                m->wp = -1;
                    521:
                    522:        /* Stop dragging if needed. */
1.200     nicm      523:        if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag) {
1.131     nicm      524:                if (c->tty.mouse_drag_release != NULL)
                    525:                        c->tty.mouse_drag_release(c, m);
                    526:
                    527:                c->tty.mouse_drag_update = NULL;
                    528:                c->tty.mouse_drag_release = NULL;
                    529:
1.182     nicm      530:                /*
1.183     nicm      531:                 * End a mouse drag by passing a MouseDragEnd key corresponding
                    532:                 * to the button that started the drag.
1.182     nicm      533:                 */
                    534:                switch (c->tty.mouse_drag_flag) {
                    535:                case 1:
                    536:                        if (where == PANE)
1.183     nicm      537:                                key = KEYC_MOUSEDRAGEND1_PANE;
1.182     nicm      538:                        if (where == STATUS)
1.183     nicm      539:                                key = KEYC_MOUSEDRAGEND1_STATUS;
1.182     nicm      540:                        if (where == BORDER)
1.183     nicm      541:                                key = KEYC_MOUSEDRAGEND1_BORDER;
1.182     nicm      542:                        break;
                    543:                case 2:
                    544:                        if (where == PANE)
1.183     nicm      545:                                key = KEYC_MOUSEDRAGEND2_PANE;
1.182     nicm      546:                        if (where == STATUS)
1.183     nicm      547:                                key = KEYC_MOUSEDRAGEND2_STATUS;
1.182     nicm      548:                        if (where == BORDER)
1.183     nicm      549:                                key = KEYC_MOUSEDRAGEND2_BORDER;
1.182     nicm      550:                        break;
                    551:                case 3:
                    552:                        if (where == PANE)
1.183     nicm      553:                                key = KEYC_MOUSEDRAGEND3_PANE;
1.182     nicm      554:                        if (where == STATUS)
1.183     nicm      555:                                key = KEYC_MOUSEDRAGEND3_STATUS;
1.182     nicm      556:                        if (where == BORDER)
1.183     nicm      557:                                key = KEYC_MOUSEDRAGEND3_BORDER;
1.182     nicm      558:                        break;
                    559:                default:
                    560:                        key = KEYC_MOUSE;
                    561:                        break;
                    562:                }
1.131     nicm      563:                c->tty.mouse_drag_flag = 0;
1.182     nicm      564:
                    565:                return (key);
1.131     nicm      566:        }
                    567:
                    568:        /* Convert to a key binding. */
1.177     nicm      569:        key = KEYC_UNKNOWN;
1.131     nicm      570:        switch (type) {
                    571:        case NOTYPE:
                    572:                break;
1.209     nicm      573:        case MOVE:
                    574:                if (where == PANE)
                    575:                        key = KEYC_MOUSEMOVE_PANE;
                    576:                if (where == STATUS)
                    577:                        key = KEYC_MOUSEMOVE_STATUS;
                    578:                if (where == BORDER)
                    579:                        key = KEYC_MOUSEMOVE_BORDER;
                    580:                break;
1.131     nicm      581:        case DRAG:
1.204     nicm      582:                if (c->tty.mouse_drag_update != NULL)
                    583:                        key = KEYC_DRAGGING;
                    584:                else {
1.131     nicm      585:                        switch (MOUSE_BUTTONS(b)) {
                    586:                        case 0:
                    587:                                if (where == PANE)
                    588:                                        key = KEYC_MOUSEDRAG1_PANE;
                    589:                                if (where == STATUS)
                    590:                                        key = KEYC_MOUSEDRAG1_STATUS;
                    591:                                if (where == BORDER)
                    592:                                        key = KEYC_MOUSEDRAG1_BORDER;
                    593:                                break;
                    594:                        case 1:
                    595:                                if (where == PANE)
                    596:                                        key = KEYC_MOUSEDRAG2_PANE;
                    597:                                if (where == STATUS)
                    598:                                        key = KEYC_MOUSEDRAG2_STATUS;
                    599:                                if (where == BORDER)
                    600:                                        key = KEYC_MOUSEDRAG2_BORDER;
                    601:                                break;
                    602:                        case 2:
                    603:                                if (where == PANE)
                    604:                                        key = KEYC_MOUSEDRAG3_PANE;
                    605:                                if (where == STATUS)
                    606:                                        key = KEYC_MOUSEDRAG3_STATUS;
                    607:                                if (where == BORDER)
                    608:                                        key = KEYC_MOUSEDRAG3_BORDER;
                    609:                                break;
                    610:                        }
                    611:                }
1.66      nicm      612:
1.182     nicm      613:                /*
                    614:                 * Begin a drag by setting the flag to a non-zero value that
                    615:                 * corresponds to the mouse button in use.
                    616:                 */
                    617:                c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1;
1.131     nicm      618:                break;
                    619:        case WHEEL:
                    620:                if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
                    621:                        if (where == PANE)
                    622:                                key = KEYC_WHEELUP_PANE;
                    623:                        if (where == STATUS)
                    624:                                key = KEYC_WHEELUP_STATUS;
                    625:                        if (where == BORDER)
                    626:                                key = KEYC_WHEELUP_BORDER;
                    627:                } else {
                    628:                        if (where == PANE)
                    629:                                key = KEYC_WHEELDOWN_PANE;
                    630:                        if (where == STATUS)
                    631:                                key = KEYC_WHEELDOWN_STATUS;
                    632:                        if (where == BORDER)
                    633:                                key = KEYC_WHEELDOWN_BORDER;
                    634:                }
                    635:                break;
                    636:        case UP:
                    637:                switch (MOUSE_BUTTONS(b)) {
                    638:                case 0:
                    639:                        if (where == PANE)
                    640:                                key = KEYC_MOUSEUP1_PANE;
                    641:                        if (where == STATUS)
                    642:                                key = KEYC_MOUSEUP1_STATUS;
                    643:                        if (where == BORDER)
                    644:                                key = KEYC_MOUSEUP1_BORDER;
                    645:                        break;
                    646:                case 1:
                    647:                        if (where == PANE)
                    648:                                key = KEYC_MOUSEUP2_PANE;
                    649:                        if (where == STATUS)
                    650:                                key = KEYC_MOUSEUP2_STATUS;
                    651:                        if (where == BORDER)
                    652:                                key = KEYC_MOUSEUP2_BORDER;
                    653:                        break;
                    654:                case 2:
                    655:                        if (where == PANE)
                    656:                                key = KEYC_MOUSEUP3_PANE;
                    657:                        if (where == STATUS)
                    658:                                key = KEYC_MOUSEUP3_STATUS;
                    659:                        if (where == BORDER)
                    660:                                key = KEYC_MOUSEUP3_BORDER;
                    661:                        break;
                    662:                }
                    663:                break;
                    664:        case DOWN:
                    665:                switch (MOUSE_BUTTONS(b)) {
                    666:                case 0:
                    667:                        if (where == PANE)
                    668:                                key = KEYC_MOUSEDOWN1_PANE;
                    669:                        if (where == STATUS)
                    670:                                key = KEYC_MOUSEDOWN1_STATUS;
                    671:                        if (where == BORDER)
                    672:                                key = KEYC_MOUSEDOWN1_BORDER;
                    673:                        break;
                    674:                case 1:
                    675:                        if (where == PANE)
                    676:                                key = KEYC_MOUSEDOWN2_PANE;
                    677:                        if (where == STATUS)
                    678:                                key = KEYC_MOUSEDOWN2_STATUS;
                    679:                        if (where == BORDER)
                    680:                                key = KEYC_MOUSEDOWN2_BORDER;
                    681:                        break;
                    682:                case 2:
                    683:                        if (where == PANE)
                    684:                                key = KEYC_MOUSEDOWN3_PANE;
                    685:                        if (where == STATUS)
                    686:                                key = KEYC_MOUSEDOWN3_STATUS;
                    687:                        if (where == BORDER)
                    688:                                key = KEYC_MOUSEDOWN3_BORDER;
                    689:                        break;
1.66      nicm      690:                }
1.131     nicm      691:                break;
1.192     nicm      692:        case DOUBLE:
                    693:                switch (MOUSE_BUTTONS(b)) {
                    694:                case 0:
                    695:                        if (where == PANE)
                    696:                                key = KEYC_DOUBLECLICK1_PANE;
                    697:                        if (where == STATUS)
                    698:                                key = KEYC_DOUBLECLICK1_STATUS;
                    699:                        if (where == BORDER)
                    700:                                key = KEYC_DOUBLECLICK1_BORDER;
                    701:                        break;
                    702:                case 1:
                    703:                        if (where == PANE)
                    704:                                key = KEYC_DOUBLECLICK2_PANE;
                    705:                        if (where == STATUS)
                    706:                                key = KEYC_DOUBLECLICK2_STATUS;
                    707:                        if (where == BORDER)
                    708:                                key = KEYC_DOUBLECLICK2_BORDER;
                    709:                        break;
                    710:                case 2:
                    711:                        if (where == PANE)
                    712:                                key = KEYC_DOUBLECLICK3_PANE;
                    713:                        if (where == STATUS)
                    714:                                key = KEYC_DOUBLECLICK3_STATUS;
                    715:                        if (where == BORDER)
                    716:                                key = KEYC_DOUBLECLICK3_BORDER;
                    717:                        break;
                    718:                }
                    719:                break;
                    720:        case TRIPLE:
                    721:                switch (MOUSE_BUTTONS(b)) {
                    722:                case 0:
                    723:                        if (where == PANE)
                    724:                                key = KEYC_TRIPLECLICK1_PANE;
                    725:                        if (where == STATUS)
                    726:                                key = KEYC_TRIPLECLICK1_STATUS;
                    727:                        if (where == BORDER)
                    728:                                key = KEYC_TRIPLECLICK1_BORDER;
                    729:                        break;
                    730:                case 1:
                    731:                        if (where == PANE)
                    732:                                key = KEYC_TRIPLECLICK2_PANE;
                    733:                        if (where == STATUS)
                    734:                                key = KEYC_TRIPLECLICK2_STATUS;
                    735:                        if (where == BORDER)
                    736:                                key = KEYC_TRIPLECLICK2_BORDER;
                    737:                        break;
                    738:                case 2:
                    739:                        if (where == PANE)
                    740:                                key = KEYC_TRIPLECLICK3_PANE;
                    741:                        if (where == STATUS)
                    742:                                key = KEYC_TRIPLECLICK3_STATUS;
                    743:                        if (where == BORDER)
                    744:                                key = KEYC_TRIPLECLICK3_BORDER;
                    745:                        break;
                    746:                }
                    747:                break;
1.66      nicm      748:        }
1.177     nicm      749:        if (key == KEYC_UNKNOWN)
                    750:                return (KEYC_UNKNOWN);
1.66      nicm      751:
1.131     nicm      752:        /* Apply modifiers if any. */
                    753:        if (b & MOUSE_MASK_META)
                    754:                key |= KEYC_ESCAPE;
                    755:        if (b & MOUSE_MASK_CTRL)
                    756:                key |= KEYC_CTRL;
                    757:        if (b & MOUSE_MASK_SHIFT)
                    758:                key |= KEYC_SHIFT;
1.66      nicm      759:
1.131     nicm      760:        return (key);
1.66      nicm      761: }
                    762:
1.82      nicm      763: /* Is this fast enough to probably be a paste? */
1.190     nicm      764: static int
1.82      nicm      765: server_client_assume_paste(struct session *s)
                    766: {
                    767:        struct timeval  tv;
1.84      nicm      768:        int             t;
1.82      nicm      769:
1.163     nicm      770:        if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1.83      nicm      771:                return (0);
1.82      nicm      772:
                    773:        timersub(&s->activity_time, &s->last_activity_time, &tv);
1.171     nicm      774:        if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
                    775:                log_debug("session %s pasting (flag %d)", s->name,
                    776:                    !!(s->flags & SESSION_PASTING));
                    777:                if (s->flags & SESSION_PASTING)
                    778:                        return (1);
                    779:                s->flags |= SESSION_PASTING;
                    780:                return (0);
                    781:        }
                    782:        log_debug("session %s not pasting", s->name);
                    783:        s->flags &= ~SESSION_PASTING;
1.83      nicm      784:        return (0);
1.82      nicm      785: }
                    786:
1.18      nicm      787: /* Handle data key input from client. */
                    788: void
1.168     nicm      789: server_client_handle_key(struct client *c, key_code key)
1.18      nicm      790: {
1.131     nicm      791:        struct mouse_event      *m = &c->tty.mouse;
1.132     nicm      792:        struct session          *s = c->session;
1.18      nicm      793:        struct window           *w;
                    794:        struct window_pane      *wp;
                    795:        struct timeval           tv;
1.223     nicm      796:        struct key_table        *table, *first;
1.132     nicm      797:        struct key_binding       bd_find, *bd;
                    798:        int                      xtimeout;
1.201     nicm      799:        struct cmd_find_state    fs;
1.18      nicm      800:
                    801:        /* Check the client is good to accept input. */
1.132     nicm      802:        if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1.18      nicm      803:                return;
1.132     nicm      804:        w = s->curw->window;
1.18      nicm      805:
                    806:        /* Update the activity timer. */
                    807:        if (gettimeofday(&c->activity_time, NULL) != 0)
                    808:                fatal("gettimeofday failed");
1.149     nicm      809:        session_update_activity(s, &c->activity_time);
1.18      nicm      810:
1.132     nicm      811:        /* Number keys jump to pane in identify mode. */
1.25      nicm      812:        if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30      nicm      813:                if (c->flags & CLIENT_READONLY)
                    814:                        return;
1.95      nicm      815:                window_unzoom(w);
1.18      nicm      816:                wp = window_pane_at_index(w, key - '0');
1.187     nicm      817:                if (wp != NULL && !window_pane_visible(wp))
                    818:                        wp = NULL;
1.214     nicm      819:                server_client_clear_identify(c, wp);
1.18      nicm      820:                return;
                    821:        }
                    822:
                    823:        /* Handle status line. */
1.30      nicm      824:        if (!(c->flags & CLIENT_READONLY)) {
                    825:                status_message_clear(c);
1.214     nicm      826:                server_client_clear_identify(c, NULL);
1.30      nicm      827:        }
1.18      nicm      828:        if (c->prompt_string != NULL) {
1.193     nicm      829:                if (c->flags & CLIENT_READONLY)
                    830:                        return;
                    831:                if (status_prompt_key(c, key) == 0)
                    832:                        return;
1.18      nicm      833:        }
                    834:
                    835:        /* Check for mouse keys. */
1.204     nicm      836:        m->valid = 0;
1.18      nicm      837:        if (key == KEYC_MOUSE) {
1.30      nicm      838:                if (c->flags & CLIENT_READONLY)
                    839:                        return;
1.131     nicm      840:                key = server_client_check_mouse(c);
1.177     nicm      841:                if (key == KEYC_UNKNOWN)
1.131     nicm      842:                        return;
                    843:
                    844:                m->valid = 1;
                    845:                m->key = key;
1.203     nicm      846:
                    847:                /*
1.204     nicm      848:                 * Mouse drag is in progress, so fire the callback (now that
                    849:                 * the mouse event is valid).
1.203     nicm      850:                 */
1.204     nicm      851:                if (key == KEYC_DRAGGING) {
                    852:                        c->tty.mouse_drag_update(c, m);
1.203     nicm      853:                        return;
1.204     nicm      854:                }
1.131     nicm      855:        } else
                    856:                m->valid = 0;
1.18      nicm      857:
1.202     nicm      858:        /* Find affected pane. */
                    859:        if (KEYC_IS_MOUSE(key) && m->valid)
                    860:                wp = cmd_mouse_pane(m, NULL, NULL);
                    861:        else
                    862:                wp = w->active;
                    863:
                    864:        /* Forward mouse keys if disabled. */
1.206     nicm      865:        if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse"))
1.202     nicm      866:                goto forward;
                    867:
1.132     nicm      868:        /* Treat everything as a regular key when pasting is detected. */
1.161     nicm      869:        if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
                    870:                goto forward;
1.18      nicm      871:
1.191     nicm      872:        /*
                    873:         * Work out the current key table. If the pane is in a mode, use
                    874:         * the mode table instead of the default key table.
                    875:         */
1.223     nicm      876:        if (server_client_is_default_key_table(c, c->keytable) &&
                    877:            wp != NULL &&
                    878:            wp->mode != NULL &&
                    879:            wp->mode->key_table != NULL)
                    880:                table = key_bindings_get_table(wp->mode->key_table(wp), 1);
                    881:        else
1.191     nicm      882:                table = c->keytable;
1.223     nicm      883:        first = table;
1.191     nicm      884:
1.205     nicm      885:        /*
                    886:         * The prefix always takes precedence and forces a switch to the prefix
                    887:         * table, unless we are already there.
                    888:         */
                    889:        if ((key == (key_code)options_get_number(s->options, "prefix") ||
                    890:            key == (key_code)options_get_number(s->options, "prefix2")) &&
                    891:            strcmp(table->name, "prefix") != 0) {
                    892:                server_client_set_key_table(c, "prefix");
                    893:                server_status_client(c);
                    894:                return;
                    895:        }
                    896:
1.223     nicm      897: retry:
                    898:        /* Log key table. */
                    899:        if (wp == NULL)
                    900:                log_debug("key table %s (no pane)", table->name);
                    901:        else
                    902:                log_debug("key table %s (pane %%%u)", table->name, wp->id);
1.225   ! nicm      903:        if (c->flags & CLIENT_REPEAT)
        !           904:                log_debug("currently repeating");
1.223     nicm      905:
1.132     nicm      906:        /* Try to see if there is a key binding in the current table. */
                    907:        bd_find.key = key;
1.191     nicm      908:        bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
1.132     nicm      909:        if (bd != NULL) {
                    910:                /*
                    911:                 * Key was matched in this table. If currently repeating but a
                    912:                 * non-repeating binding was found, stop repeating and try
                    913:                 * again in the root table.
                    914:                 */
1.222     nicm      915:                if ((c->flags & CLIENT_REPEAT) &&
                    916:                    (~bd->flags & KEY_BINDING_REPEAT)) {
1.178     nicm      917:                        server_client_set_key_table(c, NULL);
1.132     nicm      918:                        c->flags &= ~CLIENT_REPEAT;
1.85      nicm      919:                        server_status_client(c);
1.223     nicm      920:                        table = c->keytable;
1.132     nicm      921:                        goto retry;
1.18      nicm      922:                }
1.223     nicm      923:                log_debug("found in key table %s", table->name);
1.82      nicm      924:
1.132     nicm      925:                /*
                    926:                 * Take a reference to this table to make sure the key binding
                    927:                 * doesn't disappear.
                    928:                 */
                    929:                table->references++;
                    930:
                    931:                /*
                    932:                 * If this is a repeating key, start the timer. Otherwise reset
                    933:                 * the client back to the root table.
                    934:                 */
1.163     nicm      935:                xtimeout = options_get_number(s->options, "repeat-time");
1.222     nicm      936:                if (xtimeout != 0 && (bd->flags & KEY_BINDING_REPEAT)) {
1.132     nicm      937:                        c->flags |= CLIENT_REPEAT;
                    938:
                    939:                        tv.tv_sec = xtimeout / 1000;
                    940:                        tv.tv_usec = (xtimeout % 1000) * 1000L;
                    941:                        evtimer_del(&c->repeat_timer);
                    942:                        evtimer_add(&c->repeat_timer, &tv);
                    943:                } else {
1.18      nicm      944:                        c->flags &= ~CLIENT_REPEAT;
1.178     nicm      945:                        server_client_set_key_table(c, NULL);
1.18      nicm      946:                }
1.132     nicm      947:                server_status_client(c);
                    948:
1.201     nicm      949:                /* Find default state if the pane is known. */
1.221     nicm      950:                if (KEYC_IS_MOUSE(key) && m->valid && wp != NULL) {
1.224     nicm      951:                        cmd_find_from_winlink_pane(&fs, s->curw, wp);
1.201     nicm      952:                        cmd_find_log_state(__func__, &fs);
                    953:
                    954:                        if (!cmd_find_valid_state(&fs))
                    955:                                fatalx("invalid key state");
1.221     nicm      956:                        key_bindings_dispatch(bd, c, m, &fs);
                    957:                } else
                    958:                        key_bindings_dispatch(bd, c, m, NULL);
1.132     nicm      959:                key_bindings_unref_table(table);
1.18      nicm      960:                return;
                    961:        }
                    962:
1.132     nicm      963:        /*
1.223     nicm      964:         * No match in this table. If not in the root table or if repeating,
                    965:         * switch the client back to the root table and try again.
1.132     nicm      966:         */
1.223     nicm      967:        log_debug("not found in key table %s", table->name);
                    968:        if (!server_client_is_default_key_table(c, table) ||
                    969:            (c->flags & CLIENT_REPEAT)) {
1.178     nicm      970:                server_client_set_key_table(c, NULL);
1.18      nicm      971:                c->flags &= ~CLIENT_REPEAT;
1.132     nicm      972:                server_status_client(c);
1.223     nicm      973:                table = c->keytable;
1.132     nicm      974:                goto retry;
1.18      nicm      975:        }
                    976:
1.223     nicm      977:        /*
                    978:         * No match in the root table either. If this wasn't the first table
                    979:         * tried, don't pass the key to the pane.
                    980:         */
                    981:        if (first != table) {
1.178     nicm      982:                server_client_set_key_table(c, NULL);
1.132     nicm      983:                server_status_client(c);
1.161     nicm      984:                return;
                    985:        }
                    986:
                    987: forward:
                    988:        if (c->flags & CLIENT_READONLY)
                    989:                return;
                    990:        if (wp != NULL)
1.132     nicm      991:                window_pane_key(wp, c, s, key, m);
1.18      nicm      992: }
                    993:
1.2       nicm      994: /* Client functions that need to happen every loop. */
                    995: void
                    996: server_client_loop(void)
                    997: {
                    998:        struct client           *c;
                    999:        struct window           *w;
                   1000:        struct window_pane      *wp;
1.197     nicm     1001:        int                      focus;
1.2       nicm     1002:
1.135     nicm     1003:        TAILQ_FOREACH(c, &clients, entry) {
1.36      nicm     1004:                server_client_check_exit(c);
                   1005:                if (c->session != NULL) {
                   1006:                        server_client_check_redraw(c);
                   1007:                        server_client_reset_state(c);
                   1008:                }
1.2       nicm     1009:        }
                   1010:
                   1011:        /*
                   1012:         * Any windows will have been redrawn as part of clients, so clear
1.92      nicm     1013:         * their flags now. Also check pane focus and resize.
1.2       nicm     1014:         */
1.197     nicm     1015:        focus = options_get_number(global_options, "focus-events");
1.134     nicm     1016:        RB_FOREACH(w, windows, &windows) {
1.91      nicm     1017:                TAILQ_FOREACH(wp, &w->panes, entry) {
1.101     nicm     1018:                        if (wp->fd != -1) {
1.197     nicm     1019:                                if (focus)
                   1020:                                        server_client_check_focus(wp);
1.101     nicm     1021:                                server_client_check_resize(wp);
                   1022:                        }
1.2       nicm     1023:                        wp->flags &= ~PANE_REDRAW;
1.91      nicm     1024:                }
1.150     nicm     1025:                check_window_name(w);
1.2       nicm     1026:        }
1.92      nicm     1027: }
                   1028:
1.211     nicm     1029: /* Resize timer event. */
1.188     nicm     1030: static void
                   1031: server_client_resize_event(__unused int fd, __unused short events, void *data)
1.92      nicm     1032: {
1.188     nicm     1033:        struct window_pane      *wp = data;
                   1034:        struct winsize           ws;
                   1035:
                   1036:        evtimer_del(&wp->resize_timer);
1.92      nicm     1037:
1.101     nicm     1038:        if (!(wp->flags & PANE_RESIZE))
1.92      nicm     1039:                return;
                   1040:
                   1041:        memset(&ws, 0, sizeof ws);
                   1042:        ws.ws_col = wp->sx;
                   1043:        ws.ws_row = wp->sy;
                   1044:
1.100     nicm     1045:        if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92      nicm     1046:                fatal("ioctl failed");
                   1047:
                   1048:        wp->flags &= ~PANE_RESIZE;
1.188     nicm     1049: }
                   1050:
                   1051: /* Check if pane should be resized. */
1.190     nicm     1052: static void
1.188     nicm     1053: server_client_check_resize(struct window_pane *wp)
                   1054: {
                   1055:        struct timeval   tv = { .tv_usec = 250000 };
                   1056:
                   1057:        if (!(wp->flags & PANE_RESIZE))
                   1058:                return;
                   1059:
                   1060:        if (!event_initialized(&wp->resize_timer))
                   1061:                evtimer_set(&wp->resize_timer, server_client_resize_event, wp);
                   1062:
                   1063:        /*
                   1064:         * The first resize should happen immediately, so if the timer is not
                   1065:         * running, do it now.
                   1066:         */
                   1067:        if (!evtimer_pending(&wp->resize_timer, NULL))
                   1068:                server_client_resize_event(-1, 0, wp);
                   1069:
                   1070:        /*
                   1071:         * If the pane is in the alternate screen, let the timer expire and
                   1072:         * resize to give the application a chance to redraw. If not, keep
                   1073:         * pushing the timer back.
                   1074:         */
                   1075:        if (wp->saved_grid != NULL && evtimer_pending(&wp->resize_timer, NULL))
                   1076:                return;
                   1077:        evtimer_del(&wp->resize_timer);
                   1078:        evtimer_add(&wp->resize_timer, &tv);
1.91      nicm     1079: }
                   1080:
                   1081: /* Check whether pane should be focused. */
1.190     nicm     1082: static void
1.91      nicm     1083: server_client_check_focus(struct window_pane *wp)
                   1084: {
1.93      nicm     1085:        struct client   *c;
1.102     nicm     1086:        int              push;
                   1087:
                   1088:        /* Do we need to push the focus state? */
                   1089:        push = wp->flags & PANE_FOCUSPUSH;
                   1090:        wp->flags &= ~PANE_FOCUSPUSH;
1.91      nicm     1091:
                   1092:        /* If we don't care about focus, forget it. */
                   1093:        if (!(wp->base.mode & MODE_FOCUSON))
                   1094:                return;
                   1095:
                   1096:        /* If we're not the active pane in our window, we're not focused. */
                   1097:        if (wp->window->active != wp)
                   1098:                goto not_focused;
                   1099:
                   1100:        /* If we're in a mode, we're not focused. */
                   1101:        if (wp->screen != &wp->base)
                   1102:                goto not_focused;
                   1103:
                   1104:        /*
1.93      nicm     1105:         * If our window is the current window in any focused clients with an
                   1106:         * attached session, we're focused.
1.91      nicm     1107:         */
1.135     nicm     1108:        TAILQ_FOREACH(c, &clients, entry) {
                   1109:                if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91      nicm     1110:                        continue;
1.93      nicm     1111:                if (c->session->flags & SESSION_UNATTACHED)
                   1112:                        continue;
                   1113:
                   1114:                if (c->session->curw->window == wp->window)
1.91      nicm     1115:                        goto focused;
                   1116:        }
                   1117:
                   1118: not_focused:
1.102     nicm     1119:        if (push || (wp->flags & PANE_FOCUSED))
1.91      nicm     1120:                bufferevent_write(wp->event, "\033[O", 3);
                   1121:        wp->flags &= ~PANE_FOCUSED;
                   1122:        return;
                   1123:
                   1124: focused:
1.102     nicm     1125:        if (push || !(wp->flags & PANE_FOCUSED))
1.91      nicm     1126:                bufferevent_write(wp->event, "\033[I", 3);
                   1127:        wp->flags |= PANE_FOCUSED;
1.2       nicm     1128: }
                   1129:
1.18      nicm     1130: /*
                   1131:  * Update cursor position and mode settings. The scroll region and attributes
                   1132:  * are cleared when idle (waiting for an event) as this is the most likely time
                   1133:  * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
                   1134:  * compromise between excessive resets and likelihood of an interrupt.
                   1135:  *
                   1136:  * tty_region/tty_reset/tty_update_mode already take care of not resetting
                   1137:  * things that are already in their default state.
                   1138:  */
1.190     nicm     1139: static void
1.18      nicm     1140: server_client_reset_state(struct client *c)
1.1       nicm     1141: {
1.18      nicm     1142:        struct window           *w = c->session->curw->window;
1.209     nicm     1143:        struct window_pane      *wp = w->active, *loop;
1.18      nicm     1144:        struct screen           *s = wp->screen;
1.163     nicm     1145:        struct options          *oo = c->session->options;
1.66      nicm     1146:        int                      status, mode, o;
1.60      nicm     1147:
1.186     nicm     1148:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86      nicm     1149:                return;
                   1150:
1.199     nicm     1151:        tty_region_off(&c->tty);
                   1152:        tty_margin_off(&c->tty);
1.1       nicm     1153:
                   1154:        status = options_get_number(oo, "status");
                   1155:        if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
                   1156:                tty_cursor(&c->tty, 0, 0);
1.66      nicm     1157:        else {
1.126     nicm     1158:                o = status && options_get_number(oo, "status-position") == 0;
1.66      nicm     1159:                tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
                   1160:        }
1.1       nicm     1161:
1.50      nicm     1162:        /*
1.131     nicm     1163:         * Set mouse mode if requested. To support dragging, always use button
                   1164:         * mode.
1.57      nicm     1165:         */
                   1166:        mode = s->mode;
1.209     nicm     1167:        if (options_get_number(oo, "mouse")) {
                   1168:                mode &= ~ALL_MOUSE_MODES;
                   1169:                TAILQ_FOREACH(loop, &w->panes, entry) {
                   1170:                        if (loop->screen->mode & MODE_MOUSE_ALL)
                   1171:                                mode |= MODE_MOUSE_ALL;
                   1172:                }
                   1173:                if (~mode & MODE_MOUSE_ALL)
                   1174:                        mode |= MODE_MOUSE_BUTTON;
                   1175:        }
1.215     nicm     1176:
                   1177:        /* Clear bracketed paste mode if at the prompt. */
                   1178:        if (c->prompt_string != NULL)
                   1179:                mode &= ~MODE_BRACKETPASTE;
1.48      nicm     1180:
                   1181:        /* Set the terminal mode and reset attributes. */
1.59      nicm     1182:        tty_update_mode(&c->tty, mode, s);
1.1       nicm     1183:        tty_reset(&c->tty);
1.17      nicm     1184: }
                   1185:
                   1186: /* Repeat time callback. */
1.190     nicm     1187: static void
1.170     nicm     1188: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17      nicm     1189: {
                   1190:        struct client   *c = data;
                   1191:
1.85      nicm     1192:        if (c->flags & CLIENT_REPEAT) {
1.178     nicm     1193:                server_client_set_key_table(c, NULL);
1.132     nicm     1194:                c->flags &= ~CLIENT_REPEAT;
                   1195:                server_status_client(c);
1.85      nicm     1196:        }
1.192     nicm     1197: }
                   1198:
                   1199: /* Double-click callback. */
                   1200: static void
                   1201: server_client_click_timer(__unused int fd, __unused short events, void *data)
                   1202: {
                   1203:        struct client   *c = data;
                   1204:
                   1205:        c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1       nicm     1206: }
                   1207:
1.36      nicm     1208: /* Check if client should be exited. */
1.190     nicm     1209: static void
1.36      nicm     1210: server_client_check_exit(struct client *c)
                   1211: {
                   1212:        if (!(c->flags & CLIENT_EXIT))
                   1213:                return;
                   1214:
1.73      nicm     1215:        if (EVBUFFER_LENGTH(c->stdin_data) != 0)
                   1216:                return;
                   1217:        if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36      nicm     1218:                return;
1.73      nicm     1219:        if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36      nicm     1220:                return;
                   1221:
1.162     nicm     1222:        proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36      nicm     1223:        c->flags &= ~CLIENT_EXIT;
1.38      nicm     1224: }
                   1225:
1.218     nicm     1226: /* Redraw timer callback. */
                   1227: static void
                   1228: server_client_redraw_timer(__unused int fd, __unused short events,
                   1229:     __unused void* data)
                   1230: {
                   1231:        log_debug("redraw timer fired");
                   1232: }
                   1233:
1.1       nicm     1234: /* Check for client redraws. */
1.190     nicm     1235: static void
1.1       nicm     1236: server_client_check_redraw(struct client *c)
                   1237: {
                   1238:        struct session          *s = c->session;
1.137     nicm     1239:        struct tty              *tty = &c->tty;
1.1       nicm     1240:        struct window_pane      *wp;
1.218     nicm     1241:        int                      needed, flags, masked;
                   1242:        struct timeval           tv = { .tv_usec = 1000 };
                   1243:        static struct event      ev;
                   1244:        size_t                   left;
1.1       nicm     1245:
1.86      nicm     1246:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79      nicm     1247:                return;
1.218     nicm     1248:
                   1249:        /*
                   1250:         * If there is outstanding data, defer the redraw until it has been
                   1251:         * consumed. We can just add a timer to get out of the event loop and
                   1252:         * end up back here.
                   1253:         */
                   1254:        needed = 0;
                   1255:        if (c->flags & CLIENT_REDRAW)
                   1256:                needed = 1;
                   1257:        else {
                   1258:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
                   1259:                        if (wp->flags & PANE_REDRAW) {
                   1260:                                needed = 1;
                   1261:                                break;
                   1262:                        }
                   1263:                }
                   1264:        }
                   1265:        if (needed) {
                   1266:                left = EVBUFFER_LENGTH(tty->out);
                   1267:                if (left != 0) {
                   1268:                        log_debug("%s: redraw deferred (%zu left)", c->name, left);
                   1269:                        if (evtimer_initialized(&ev) && evtimer_pending(&ev, NULL))
                   1270:                                return;
                   1271:                        log_debug("redraw timer started");
                   1272:                        evtimer_set(&ev, server_client_redraw_timer, NULL);
                   1273:                        evtimer_add(&ev, &tv);
                   1274:
                   1275:                        /*
                   1276:                         * We may have got here for a single pane redraw, but
                   1277:                         * force a full redraw next time in case other panes
                   1278:                         * have been updated.
                   1279:                         */
                   1280:                        c->flags |= CLIENT_REDRAW;
                   1281:                        return;
                   1282:                }
                   1283:                if (evtimer_initialized(&ev))
                   1284:                        evtimer_del(&ev);
                   1285:                log_debug("%s: redraw needed", c->name);
                   1286:        }
1.79      nicm     1287:
1.1       nicm     1288:        if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
1.163     nicm     1289:                if (options_get_number(s->options, "set-titles"))
1.1       nicm     1290:                        server_client_set_title(c);
1.189     nicm     1291:                screen_redraw_update(c); /* will adjust flags */
1.1       nicm     1292:        }
                   1293:
1.219     nicm     1294:        flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR);
                   1295:        tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE)) | TTY_NOCURSOR;
1.137     nicm     1296:
1.1       nicm     1297:        if (c->flags & CLIENT_REDRAW) {
1.137     nicm     1298:                tty_update_mode(tty, tty->mode, NULL);
1.115     nicm     1299:                screen_redraw_screen(c, 1, 1, 1);
1.27      nicm     1300:                c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.1       nicm     1301:        } else {
                   1302:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137     nicm     1303:                        if (wp->flags & PANE_REDRAW) {
                   1304:                                tty_update_mode(tty, tty->mode, NULL);
1.1       nicm     1305:                                screen_redraw_pane(c, wp);
1.137     nicm     1306:                        }
1.1       nicm     1307:                }
                   1308:        }
                   1309:
1.185     nicm     1310:        masked = c->flags & (CLIENT_BORDERS|CLIENT_STATUS);
                   1311:        if (masked != 0)
1.137     nicm     1312:                tty_update_mode(tty, tty->mode, NULL);
1.185     nicm     1313:        if (masked == CLIENT_BORDERS)
1.115     nicm     1314:                screen_redraw_screen(c, 0, 0, 1);
1.185     nicm     1315:        else if (masked == CLIENT_STATUS)
1.115     nicm     1316:                screen_redraw_screen(c, 0, 1, 0);
1.185     nicm     1317:        else if (masked != 0)
                   1318:                screen_redraw_screen(c, 0, 1, 1);
1.1       nicm     1319:
1.137     nicm     1320:        tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
                   1321:        tty_update_mode(tty, tty->mode, NULL);
1.1       nicm     1322:
1.153     nicm     1323:        c->flags &= ~(CLIENT_REDRAW|CLIENT_BORDERS|CLIENT_STATUS|
                   1324:            CLIENT_STATUSFORCE);
1.1       nicm     1325: }
                   1326:
                   1327: /* Set client title. */
1.190     nicm     1328: static void
1.1       nicm     1329: server_client_set_title(struct client *c)
                   1330: {
1.128     nicm     1331:        struct session          *s = c->session;
                   1332:        const char              *template;
                   1333:        char                    *title;
                   1334:        struct format_tree      *ft;
1.1       nicm     1335:
1.163     nicm     1336:        template = options_get_string(s->options, "set-titles-string");
1.25      nicm     1337:
1.210     nicm     1338:        ft = format_create(NULL, FORMAT_NONE, 0);
1.128     nicm     1339:        format_defaults(ft, c, NULL, NULL, NULL);
                   1340:
                   1341:        title = format_expand_time(ft, template, time(NULL));
1.1       nicm     1342:        if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77      nicm     1343:                free(c->title);
1.1       nicm     1344:                c->title = xstrdup(title);
                   1345:                tty_set_title(&c->tty, c->title);
                   1346:        }
1.77      nicm     1347:        free(title);
1.128     nicm     1348:
                   1349:        format_free(ft);
1.1       nicm     1350: }
                   1351:
                   1352: /* Dispatch message from client. */
1.190     nicm     1353: static void
1.162     nicm     1354: server_client_dispatch(struct imsg *imsg, void *arg)
1.1       nicm     1355: {
1.162     nicm     1356:        struct client           *c = arg;
1.73      nicm     1357:        struct msg_stdin_data    stdindata;
1.108     nicm     1358:        const char              *data;
1.162     nicm     1359:        ssize_t                  datalen;
1.149     nicm     1360:        struct session          *s;
1.1       nicm     1361:
1.162     nicm     1362:        if (c->flags & CLIENT_DEAD)
                   1363:                return;
                   1364:
                   1365:        if (imsg == NULL) {
                   1366:                server_client_lost(c);
                   1367:                return;
                   1368:        }
1.1       nicm     1369:
1.162     nicm     1370:        data = imsg->data;
                   1371:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1       nicm     1372:
1.162     nicm     1373:        switch (imsg->hdr.type) {
                   1374:        case MSG_IDENTIFY_FLAGS:
                   1375:        case MSG_IDENTIFY_TERM:
                   1376:        case MSG_IDENTIFY_TTYNAME:
                   1377:        case MSG_IDENTIFY_CWD:
                   1378:        case MSG_IDENTIFY_STDIN:
                   1379:        case MSG_IDENTIFY_ENVIRON:
                   1380:        case MSG_IDENTIFY_CLIENTPID:
                   1381:        case MSG_IDENTIFY_DONE:
                   1382:                server_client_dispatch_identify(c, imsg);
                   1383:                break;
                   1384:        case MSG_COMMAND:
                   1385:                server_client_dispatch_command(c, imsg);
                   1386:                break;
                   1387:        case MSG_STDIN:
                   1388:                if (datalen != sizeof stdindata)
                   1389:                        fatalx("bad MSG_STDIN size");
                   1390:                memcpy(&stdindata, data, sizeof stdindata);
1.36      nicm     1391:
1.162     nicm     1392:                if (c->stdin_callback == NULL)
1.33      nicm     1393:                        break;
1.162     nicm     1394:                if (stdindata.size <= 0)
                   1395:                        c->stdin_closed = 1;
                   1396:                else {
                   1397:                        evbuffer_add(c->stdin_data, stdindata.data,
                   1398:                            stdindata.size);
                   1399:                }
                   1400:                c->stdin_callback(c, c->stdin_closed,
                   1401:                    c->stdin_callback_data);
                   1402:                break;
                   1403:        case MSG_RESIZE:
                   1404:                if (datalen != 0)
                   1405:                        fatalx("bad MSG_RESIZE size");
1.1       nicm     1406:
1.162     nicm     1407:                if (c->flags & CLIENT_CONTROL)
1.1       nicm     1408:                        break;
1.162     nicm     1409:                if (tty_resize(&c->tty)) {
                   1410:                        recalculate_sizes();
                   1411:                        server_redraw_client(c);
                   1412:                }
1.174     nicm     1413:                if (c->session != NULL)
1.196     nicm     1414:                        notify_client("client-resized", c);
1.162     nicm     1415:                break;
                   1416:        case MSG_EXITING:
                   1417:                if (datalen != 0)
                   1418:                        fatalx("bad MSG_EXITING size");
1.1       nicm     1419:
1.162     nicm     1420:                c->session = NULL;
                   1421:                tty_close(&c->tty);
                   1422:                proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
                   1423:                break;
                   1424:        case MSG_WAKEUP:
                   1425:        case MSG_UNLOCK:
                   1426:                if (datalen != 0)
                   1427:                        fatalx("bad MSG_WAKEUP size");
1.121     nicm     1428:
1.162     nicm     1429:                if (!(c->flags & CLIENT_SUSPENDED))
                   1430:                        break;
                   1431:                c->flags &= ~CLIENT_SUSPENDED;
1.10      nicm     1432:
1.162     nicm     1433:                if (c->tty.fd == -1) /* exited in the meantime */
1.1       nicm     1434:                        break;
1.162     nicm     1435:                s = c->session;
1.1       nicm     1436:
1.162     nicm     1437:                if (gettimeofday(&c->activity_time, NULL) != 0)
                   1438:                        fatal("gettimeofday failed");
                   1439:
                   1440:                tty_start_tty(&c->tty);
                   1441:                server_redraw_client(c);
                   1442:                recalculate_sizes();
1.184     nicm     1443:
                   1444:                if (s != NULL)
                   1445:                        session_update_activity(s, &c->activity_time);
1.162     nicm     1446:                break;
                   1447:        case MSG_SHELL:
                   1448:                if (datalen != 0)
                   1449:                        fatalx("bad MSG_SHELL size");
1.1       nicm     1450:
1.162     nicm     1451:                server_client_dispatch_shell(c);
                   1452:                break;
1.1       nicm     1453:        }
                   1454: }
                   1455:
1.194     nicm     1456: /* Callback when command is done. */
                   1457: static enum cmd_retval
1.195     nicm     1458: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194     nicm     1459: {
1.195     nicm     1460:        struct client   *c = item->client;
1.194     nicm     1461:
                   1462:        if (~c->flags & CLIENT_ATTACHED)
                   1463:                c->flags |= CLIENT_EXIT;
                   1464:        return (CMD_RETURN_NORMAL);
                   1465: }
                   1466:
                   1467: /* Show an error message. */
                   1468: static enum cmd_retval
1.195     nicm     1469: server_client_command_error(struct cmdq_item *item, void *data)
1.194     nicm     1470: {
                   1471:        char    *error = data;
                   1472:
1.195     nicm     1473:        cmdq_error(item, "%s", error);
1.194     nicm     1474:        free(error);
                   1475:
                   1476:        return (CMD_RETURN_NORMAL);
                   1477: }
                   1478:
1.1       nicm     1479: /* Handle command message. */
1.190     nicm     1480: static void
1.162     nicm     1481: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1       nicm     1482: {
1.108     nicm     1483:        struct msg_command_data   data;
                   1484:        char                     *buf;
                   1485:        size_t                    len;
                   1486:        struct cmd_list          *cmdlist = NULL;
                   1487:        int                       argc;
                   1488:        char                    **argv, *cause;
                   1489:
                   1490:        if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
                   1491:                fatalx("bad MSG_COMMAND size");
                   1492:        memcpy(&data, imsg->data, sizeof data);
                   1493:
1.124     nicm     1494:        buf = (char *)imsg->data + sizeof data;
1.108     nicm     1495:        len = imsg->hdr.len  - IMSG_HEADER_SIZE - sizeof data;
                   1496:        if (len > 0 && buf[len - 1] != '\0')
                   1497:                fatalx("bad MSG_COMMAND string");
                   1498:
                   1499:        argc = data.argc;
                   1500:        if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194     nicm     1501:                cause = xstrdup("command too long");
1.1       nicm     1502:                goto error;
                   1503:        }
                   1504:
                   1505:        if (argc == 0) {
                   1506:                argc = 1;
                   1507:                argv = xcalloc(1, sizeof *argv);
                   1508:                *argv = xstrdup("new-session");
                   1509:        }
                   1510:
1.94      nicm     1511:        if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
1.1       nicm     1512:                cmd_free_argv(argc, argv);
                   1513:                goto error;
                   1514:        }
                   1515:        cmd_free_argv(argc, argv);
                   1516:
1.194     nicm     1517:        cmdq_append(c, cmdq_get_command(cmdlist, NULL, NULL, 0));
                   1518:        cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.1       nicm     1519:        cmd_list_free(cmdlist);
                   1520:        return;
                   1521:
                   1522: error:
1.194     nicm     1523:        cmdq_append(c, cmdq_get_callback(server_client_command_error, cause));
                   1524:
1.1       nicm     1525:        if (cmdlist != NULL)
                   1526:                cmd_list_free(cmdlist);
1.88      nicm     1527:
1.36      nicm     1528:        c->flags |= CLIENT_EXIT;
1.1       nicm     1529: }
                   1530:
                   1531: /* Handle identify message. */
1.190     nicm     1532: static void
1.162     nicm     1533: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1       nicm     1534: {
1.165     nicm     1535:        const char      *data, *home;
1.109     nicm     1536:        size_t           datalen;
                   1537:        int              flags;
1.216     nicm     1538:        char            *name;
1.109     nicm     1539:
                   1540:        if (c->flags & CLIENT_IDENTIFIED)
                   1541:                fatalx("out-of-order identify message");
                   1542:
                   1543:        data = imsg->data;
                   1544:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
                   1545:
                   1546:        switch (imsg->hdr.type) {
                   1547:        case MSG_IDENTIFY_FLAGS:
                   1548:                if (datalen != sizeof flags)
                   1549:                        fatalx("bad MSG_IDENTIFY_FLAGS size");
                   1550:                memcpy(&flags, data, sizeof flags);
                   1551:                c->flags |= flags;
1.158     nicm     1552:                log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109     nicm     1553:                break;
                   1554:        case MSG_IDENTIFY_TERM:
1.110     nicm     1555:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1556:                        fatalx("bad MSG_IDENTIFY_TERM string");
                   1557:                c->term = xstrdup(data);
1.158     nicm     1558:                log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109     nicm     1559:                break;
                   1560:        case MSG_IDENTIFY_TTYNAME:
1.110     nicm     1561:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1562:                        fatalx("bad MSG_IDENTIFY_TTYNAME string");
                   1563:                c->ttyname = xstrdup(data);
1.158     nicm     1564:                log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109     nicm     1565:                break;
                   1566:        case MSG_IDENTIFY_CWD:
1.155     nicm     1567:                if (datalen == 0 || data[datalen - 1] != '\0')
                   1568:                        fatalx("bad MSG_IDENTIFY_CWD string");
1.165     nicm     1569:                if (access(data, X_OK) == 0)
                   1570:                        c->cwd = xstrdup(data);
                   1571:                else if ((home = find_home()) != NULL)
                   1572:                        c->cwd = xstrdup(home);
                   1573:                else
                   1574:                        c->cwd = xstrdup("/");
1.158     nicm     1575:                log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109     nicm     1576:                break;
                   1577:        case MSG_IDENTIFY_STDIN:
                   1578:                if (datalen != 0)
                   1579:                        fatalx("bad MSG_IDENTIFY_STDIN size");
                   1580:                c->fd = imsg->fd;
1.158     nicm     1581:                log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109     nicm     1582:                break;
                   1583:        case MSG_IDENTIFY_ENVIRON:
1.110     nicm     1584:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1585:                        fatalx("bad MSG_IDENTIFY_ENVIRON string");
                   1586:                if (strchr(data, '=') != NULL)
1.164     nicm     1587:                        environ_put(c->environ, data);
1.158     nicm     1588:                log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143     nicm     1589:                break;
                   1590:        case MSG_IDENTIFY_CLIENTPID:
                   1591:                if (datalen != sizeof c->pid)
                   1592:                        fatalx("bad MSG_IDENTIFY_CLIENTPID size");
                   1593:                memcpy(&c->pid, data, sizeof c->pid);
1.158     nicm     1594:                log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109     nicm     1595:                break;
                   1596:        default:
                   1597:                break;
                   1598:        }
                   1599:
                   1600:        if (imsg->hdr.type != MSG_IDENTIFY_DONE)
                   1601:                return;
                   1602:        c->flags |= CLIENT_IDENTIFIED;
1.75      nicm     1603:
1.216     nicm     1604:        if (*c->ttyname != '\0')
                   1605:                name = xstrdup(c->ttyname);
                   1606:        else
                   1607:                xasprintf(&name, "client-%ld", (long)c->pid);
                   1608:        c->name = name;
                   1609:        log_debug("client %p name is %s", c, c->name);
                   1610:
1.109     nicm     1611:        if (c->flags & CLIENT_CONTROL) {
1.75      nicm     1612:                c->stdin_callback = control_callback;
1.109     nicm     1613:
1.97      nicm     1614:                evbuffer_free(c->stderr_data);
                   1615:                c->stderr_data = c->stdout_data;
1.109     nicm     1616:
                   1617:                if (c->flags & CLIENT_CONTROLCONTROL)
1.96      nicm     1618:                        evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162     nicm     1619:                proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75      nicm     1620:
                   1621:                c->tty.fd = -1;
                   1622:
1.109     nicm     1623:                close(c->fd);
                   1624:                c->fd = -1;
                   1625:
1.75      nicm     1626:                return;
                   1627:        }
1.1       nicm     1628:
1.109     nicm     1629:        if (c->fd == -1)
1.104     nicm     1630:                return;
1.145     nicm     1631:        if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109     nicm     1632:                close(c->fd);
                   1633:                c->fd = -1;
1.80      nicm     1634:                return;
                   1635:        }
1.109     nicm     1636:        if (c->flags & CLIENT_UTF8)
1.1       nicm     1637:                c->tty.flags |= TTY_UTF8;
1.109     nicm     1638:        if (c->flags & CLIENT_256COLOURS)
1.1       nicm     1639:                c->tty.term_flags |= TERM_256COLOURS;
                   1640:
                   1641:        tty_resize(&c->tty);
                   1642:
1.109     nicm     1643:        if (!(c->flags & CLIENT_CONTROL))
1.86      nicm     1644:                c->flags |= CLIENT_TERMINAL;
1.1       nicm     1645: }
                   1646:
                   1647: /* Handle shell message. */
1.190     nicm     1648: static void
1.162     nicm     1649: server_client_dispatch_shell(struct client *c)
1.1       nicm     1650: {
1.107     nicm     1651:        const char      *shell;
1.25      nicm     1652:
1.163     nicm     1653:        shell = options_get_string(global_s_options, "default-shell");
1.1       nicm     1654:        if (*shell == '\0' || areshell(shell))
                   1655:                shell = _PATH_BSHELL;
1.162     nicm     1656:        proc_send_s(c->peer, MSG_SHELL, shell);
1.25      nicm     1657:
1.162     nicm     1658:        proc_kill_peer(c->peer);
1.169     nicm     1659: }
                   1660:
                   1661: /* Event callback to push more stdout data if any left. */
                   1662: static void
1.170     nicm     1663: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169     nicm     1664: {
                   1665:        struct client   *c = arg;
                   1666:
                   1667:        if (~c->flags & CLIENT_DEAD)
                   1668:                server_client_push_stdout(c);
                   1669:        server_client_unref(c);
                   1670: }
                   1671:
                   1672: /* Push stdout to client if possible. */
                   1673: void
                   1674: server_client_push_stdout(struct client *c)
                   1675: {
                   1676:        struct msg_stdout_data data;
                   1677:        size_t                 sent, left;
                   1678:
                   1679:        left = EVBUFFER_LENGTH(c->stdout_data);
                   1680:        while (left != 0) {
                   1681:                sent = left;
                   1682:                if (sent > sizeof data.data)
                   1683:                        sent = sizeof data.data;
                   1684:                memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
                   1685:                data.size = sent;
                   1686:
                   1687:                if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
                   1688:                        break;
                   1689:                evbuffer_drain(c->stdout_data, sent);
                   1690:
                   1691:                left = EVBUFFER_LENGTH(c->stdout_data);
                   1692:                log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
                   1693:                    sent, left);
                   1694:        }
                   1695:        if (left != 0) {
                   1696:                c->references++;
                   1697:                event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
                   1698:                log_debug("%s: client %p, queued", __func__, c);
                   1699:        }
                   1700: }
                   1701:
                   1702: /* Event callback to push more stderr data if any left. */
                   1703: static void
1.170     nicm     1704: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169     nicm     1705: {
                   1706:        struct client   *c = arg;
                   1707:
                   1708:        if (~c->flags & CLIENT_DEAD)
                   1709:                server_client_push_stderr(c);
                   1710:        server_client_unref(c);
                   1711: }
                   1712:
                   1713: /* Push stderr to client if possible. */
                   1714: void
                   1715: server_client_push_stderr(struct client *c)
                   1716: {
                   1717:        struct msg_stderr_data data;
                   1718:        size_t                 sent, left;
                   1719:
                   1720:        if (c->stderr_data == c->stdout_data) {
                   1721:                server_client_push_stdout(c);
                   1722:                return;
                   1723:        }
                   1724:
                   1725:        left = EVBUFFER_LENGTH(c->stderr_data);
                   1726:        while (left != 0) {
                   1727:                sent = left;
                   1728:                if (sent > sizeof data.data)
                   1729:                        sent = sizeof data.data;
                   1730:                memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
                   1731:                data.size = sent;
                   1732:
                   1733:                if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
                   1734:                        break;
                   1735:                evbuffer_drain(c->stderr_data, sent);
                   1736:
                   1737:                left = EVBUFFER_LENGTH(c->stderr_data);
                   1738:                log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
                   1739:                    sent, left);
                   1740:        }
                   1741:        if (left != 0) {
                   1742:                c->references++;
                   1743:                event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
                   1744:                log_debug("%s: client %p, queued", __func__, c);
1.212     nicm     1745:        }
                   1746: }
                   1747:
                   1748: /* Add to client message log. */
                   1749: void
                   1750: server_client_add_message(struct client *c, const char *fmt, ...)
                   1751: {
                   1752:        struct message_entry    *msg, *msg1;
                   1753:        char                    *s;
                   1754:        va_list                  ap;
                   1755:        u_int                    limit;
                   1756:
                   1757:        va_start(ap, fmt);
                   1758:        xvasprintf(&s, fmt, ap);
                   1759:        va_end(ap);
                   1760:
1.216     nicm     1761:        log_debug("message %s (client %p)", s, c);
1.212     nicm     1762:
                   1763:        msg = xcalloc(1, sizeof *msg);
                   1764:        msg->msg_time = time(NULL);
                   1765:        msg->msg_num = c->message_next++;
                   1766:        msg->msg = s;
                   1767:        TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
                   1768:
                   1769:        limit = options_get_number(global_options, "message-limit");
                   1770:        TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
                   1771:                if (msg->msg_num + limit >= c->message_next)
                   1772:                        break;
                   1773:                free(msg->msg);
                   1774:                TAILQ_REMOVE(&c->message_log, msg, entry);
                   1775:                free(msg);
1.169     nicm     1776:        }
1.213     nicm     1777: }
                   1778:
                   1779: /* Get client working directory. */
                   1780: const char *
                   1781: server_client_get_cwd(struct client *c)
                   1782: {
                   1783:        struct session  *s;
                   1784:
                   1785:        if (c != NULL && c->session == NULL && c->cwd != NULL)
                   1786:                return (c->cwd);
                   1787:        if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
                   1788:                return (s->cwd);
                   1789:        return (".");
                   1790: }
                   1791:
                   1792: /* Resolve an absolute path or relative to client working directory. */
                   1793: char *
                   1794: server_client_get_path(struct client *c, const char *file)
                   1795: {
                   1796:        char    *path, resolved[PATH_MAX];
                   1797:
                   1798:        if (*file == '/')
                   1799:                path = xstrdup(file);
                   1800:        else
                   1801:                xasprintf(&path, "%s/%s", server_client_get_cwd(c), file);
                   1802:        if (realpath(path, resolved) == NULL)
                   1803:                return (path);
                   1804:        free(path);
                   1805:        return (xstrdup(resolved));
1.1       nicm     1806: }