[BACK]Return to server-fn.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/server-fn.c, Revision 1.51

1.51    ! nicm        1: /* $OpenBSD: server-fn.c,v 1.50 2011/01/23 11:03:43 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20:
                     21: #include <string.h>
1.5       nicm       22: #include <time.h>
1.1       nicm       23: #include <unistd.h>
                     24:
                     25: #include "tmux.h"
                     26:
1.39      nicm       27: struct session *server_next_session(struct session *);
                     28: void           server_callback_identify(int, short, void *);
1.28      nicm       29:
1.13      nicm       30: void
                     31: server_fill_environ(struct session *s, struct environ *env)
1.1       nicm       32: {
1.50      nicm       33:        char    var[MAXPATHLEN], *term;
                     34:        u_int   idx;
                     35:        long    pid;
                     36:
                     37:        if (s != NULL) {
                     38:                term = options_get_string(&s->options, "default-terminal");
                     39:                environ_set(env, "TERM", term);
                     40:
                     41:                idx = s->idx;
                     42:        } else
                     43:                idx = -1;
                     44:        pid = getpid();
                     45:        xsnprintf(var, sizeof var, "%s,%ld,%d", socket_path, pid, idx);
                     46:        environ_set(env, "TMUX", var);
1.10      nicm       47: }
                     48:
                     49: void
1.1       nicm       50: server_write_client(
1.11      nicm       51:     struct client *c, enum msgtype type, const void *buf, size_t len)
1.1       nicm       52: {
1.14      nicm       53:        struct imsgbuf  *ibuf = &c->ibuf;
1.1       nicm       54:
1.12      nicm       55:        if (c->flags & CLIENT_BAD)
                     56:                return;
1.14      nicm       57:        log_debug("writing %d to client %d", type, c->ibuf.fd);
                     58:        imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, -1, (void *) buf, len);
1.31      nicm       59:        server_update_event(c);
1.1       nicm       60: }
                     61:
                     62: void
                     63: server_write_session(
1.11      nicm       64:     struct session *s, enum msgtype type, const void *buf, size_t len)
1.1       nicm       65: {
                     66:        struct client   *c;
                     67:        u_int            i;
                     68:
                     69:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     70:                c = ARRAY_ITEM(&clients, i);
                     71:                if (c == NULL || c->session == NULL)
                     72:                        continue;
                     73:                if (c->session == s)
                     74:                        server_write_client(c, type, buf, len);
                     75:        }
                     76: }
                     77:
                     78: void
                     79: server_redraw_client(struct client *c)
                     80: {
                     81:        c->flags |= CLIENT_REDRAW;
                     82: }
                     83:
                     84: void
                     85: server_status_client(struct client *c)
                     86: {
                     87:        c->flags |= CLIENT_STATUS;
                     88: }
                     89:
                     90: void
                     91: server_redraw_session(struct session *s)
                     92: {
                     93:        struct client   *c;
                     94:        u_int            i;
                     95:
                     96:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     97:                c = ARRAY_ITEM(&clients, i);
                     98:                if (c == NULL || c->session == NULL)
                     99:                        continue;
                    100:                if (c->session == s)
                    101:                        server_redraw_client(c);
                    102:        }
                    103: }
                    104:
                    105: void
1.25      nicm      106: server_redraw_session_group(struct session *s)
                    107: {
                    108:        struct session_group    *sg;
                    109:
                    110:        if ((sg = session_group_find(s)) == NULL)
                    111:                server_redraw_session(s);
                    112:        else {
                    113:                TAILQ_FOREACH(s, &sg->sessions, gentry)
                    114:                        server_redraw_session(s);
                    115:        }
                    116: }
                    117:
                    118: void
1.1       nicm      119: server_status_session(struct session *s)
                    120: {
                    121:        struct client   *c;
                    122:        u_int            i;
                    123:
                    124:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    125:                c = ARRAY_ITEM(&clients, i);
                    126:                if (c == NULL || c->session == NULL)
                    127:                        continue;
                    128:                if (c->session == s)
                    129:                        server_status_client(c);
                    130:        }
                    131: }
                    132:
                    133: void
1.25      nicm      134: server_status_session_group(struct session *s)
                    135: {
                    136:        struct session_group    *sg;
                    137:
                    138:        if ((sg = session_group_find(s)) == NULL)
                    139:                server_status_session(s);
                    140:        else {
                    141:                TAILQ_FOREACH(s, &sg->sessions, gentry)
                    142:                        server_status_session(s);
                    143:        }
                    144: }
                    145:
                    146: void
1.1       nicm      147: server_redraw_window(struct window *w)
                    148: {
                    149:        struct client   *c;
                    150:        u_int            i;
                    151:
                    152:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    153:                c = ARRAY_ITEM(&clients, i);
                    154:                if (c == NULL || c->session == NULL)
                    155:                        continue;
                    156:                if (c->session->curw->window == w)
                    157:                        server_redraw_client(c);
                    158:        }
                    159:        w->flags |= WINDOW_REDRAW;
1.33      nicm      160: }
                    161:
                    162: void
                    163: server_redraw_window_borders(struct window *w)
                    164: {
                    165:        struct client   *c;
                    166:        u_int            i;
                    167:
                    168:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    169:                c = ARRAY_ITEM(&clients, i);
                    170:                if (c == NULL || c->session == NULL)
                    171:                        continue;
                    172:                if (c->session->curw->window == w)
                    173:                        c->flags |= CLIENT_BORDERS;
                    174:        }
1.1       nicm      175: }
                    176:
                    177: void
                    178: server_status_window(struct window *w)
                    179: {
                    180:        struct session  *s;
                    181:
                    182:        /*
                    183:         * This is slightly different. We want to redraw the status line of any
                    184:         * clients containing this window rather than any where it is the
                    185:         * current window.
                    186:         */
                    187:
1.47      nicm      188:        RB_FOREACH(s, sessions, &sessions) {
                    189:                if (session_has(s, w) != NULL)
1.1       nicm      190:                        server_status_session(s);
                    191:        }
                    192: }
                    193:
                    194: void
                    195: server_lock(void)
                    196: {
1.23      nicm      197:        struct client   *c;
                    198:        u_int            i;
1.17      nicm      199:
1.1       nicm      200:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    201:                c = ARRAY_ITEM(&clients, i);
                    202:                if (c == NULL || c->session == NULL)
                    203:                        continue;
1.23      nicm      204:                server_lock_client(c);
                    205:        }
                    206: }
1.1       nicm      207:
1.23      nicm      208: void
                    209: server_lock_session(struct session *s)
                    210: {
                    211:        struct client   *c;
                    212:        u_int            i;
                    213:
                    214:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    215:                c = ARRAY_ITEM(&clients, i);
                    216:                if (c == NULL || c->session == NULL || c->session != s)
                    217:                        continue;
                    218:                server_lock_client(c);
1.31      nicm      219:        }
1.23      nicm      220: }
1.1       nicm      221:
1.23      nicm      222: void
                    223: server_lock_client(struct client *c)
                    224: {
                    225:        const char              *cmd;
                    226:        size_t                   cmdlen;
                    227:        struct msg_lock_data     lockdata;
1.24      nicm      228:
                    229:        if (c->flags & CLIENT_SUSPENDED)
                    230:                return;
1.1       nicm      231:
1.23      nicm      232:        cmd = options_get_string(&c->session->options, "lock-command");
                    233:        cmdlen = strlcpy(lockdata.cmd, cmd, sizeof lockdata.cmd);
                    234:        if (cmdlen >= sizeof lockdata.cmd)
                    235:                return;
1.31      nicm      236:
1.23      nicm      237:        tty_stop_tty(&c->tty);
                    238:        tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
                    239:        tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
                    240:
                    241:        c->flags |= CLIENT_SUSPENDED;
                    242:        server_write_client(c, MSG_LOCK, &lockdata, sizeof lockdata);
1.8       nicm      243: }
                    244:
                    245: void
                    246: server_kill_window(struct window *w)
                    247: {
1.48      nicm      248:        struct session  *s, *next_s;
1.8       nicm      249:        struct winlink  *wl;
1.31      nicm      250:
1.48      nicm      251:        next_s = RB_MIN(sessions, &sessions);
                    252:        while (next_s != NULL) {
                    253:                s = next_s;
                    254:                next_s = RB_NEXT(sessions, &sessions, s);
                    255:
1.47      nicm      256:                if (session_has(s, w) == NULL)
1.8       nicm      257:                        continue;
1.34      nicm      258:                while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
                    259:                        if (session_detach(s, wl)) {
                    260:                                server_destroy_session_group(s);
                    261:                                break;
                    262:                        } else
                    263:                                server_redraw_session_group(s);
                    264:                }
1.8       nicm      265:        }
1.21      nicm      266: }
                    267:
                    268: int
1.25      nicm      269: server_link_window(struct session *src, struct winlink *srcwl,
                    270:     struct session *dst, int dstidx, int killflag, int selectflag, char **cause)
1.21      nicm      271: {
1.25      nicm      272:        struct winlink          *dstwl;
                    273:        struct session_group    *srcsg, *dstsg;
                    274:
                    275:        srcsg = session_group_find(src);
                    276:        dstsg = session_group_find(dst);
                    277:        if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
                    278:                xasprintf(cause, "sessions are grouped");
                    279:                return (-1);
                    280:        }
1.21      nicm      281:
                    282:        dstwl = NULL;
                    283:        if (dstidx != -1)
                    284:                dstwl = winlink_find_by_index(&dst->windows, dstidx);
                    285:        if (dstwl != NULL) {
1.44      nicm      286:                if (dstwl->window == srcwl->window) {
                    287:                        xasprintf(cause, "same index: %d", dstidx);
1.41      nicm      288:                        return (-1);
1.44      nicm      289:                }
1.21      nicm      290:                if (killflag) {
                    291:                        /*
                    292:                         * Can't use session_detach as it will destroy session
                    293:                         * if this makes it empty.
                    294:                         */
1.37      nicm      295:                        dstwl->flags &= ~WINLINK_ALERTFLAGS;
1.21      nicm      296:                        winlink_stack_remove(&dst->lastw, dstwl);
                    297:                        winlink_remove(&dst->windows, dstwl);
                    298:
                    299:                        /* Force select/redraw if current. */
1.26      nicm      300:                        if (dstwl == dst->curw) {
1.21      nicm      301:                                selectflag = 1;
1.26      nicm      302:                                dst->curw = NULL;
                    303:                        }
1.21      nicm      304:                }
                    305:        }
                    306:
                    307:        if (dstidx == -1)
                    308:                dstidx = -1 - options_get_number(&dst->options, "base-index");
                    309:        dstwl = session_attach(dst, srcwl->window, dstidx, cause);
                    310:        if (dstwl == NULL)
                    311:                return (-1);
                    312:
1.25      nicm      313:        if (selectflag)
1.21      nicm      314:                session_select(dst, dstwl->idx);
1.25      nicm      315:        server_redraw_session_group(dst);
1.21      nicm      316:
                    317:        return (0);
                    318: }
                    319:
                    320: void
                    321: server_unlink_window(struct session *s, struct winlink *wl)
                    322: {
                    323:        if (session_detach(s, wl))
1.25      nicm      324:                server_destroy_session_group(s);
                    325:        else
                    326:                server_redraw_session_group(s);
1.29      nicm      327: }
                    328:
                    329: void
                    330: server_destroy_pane(struct window_pane *wp)
                    331: {
1.51    ! nicm      332:        struct window           *w = wp->window;
        !           333:        int                      old_fd;
        !           334:        struct screen_write_ctx  ctx;
        !           335:        struct grid_cell         gc;
1.29      nicm      336:
1.51    ! nicm      337:        old_fd = wp->fd;
1.36      nicm      338:        if (wp->fd != -1) {
                    339:                close(wp->fd);
                    340:                bufferevent_free(wp->event);
                    341:                wp->fd = -1;
                    342:        }
1.29      nicm      343:
1.51    ! nicm      344:        if (options_get_number(&w->options, "remain-on-exit")) {
        !           345:                if (old_fd == -1)
        !           346:                        return;
        !           347:                screen_write_start(&ctx, wp, &wp->base);
        !           348:                screen_write_scrollregion(&ctx, 0, screen_size_y(ctx.s) - 1);
        !           349:                screen_write_cursormove(&ctx, 0, screen_size_y(ctx.s) - 1);
        !           350:                screen_write_linefeed(&ctx, 1);
        !           351:                memcpy(&gc, &grid_default_cell, sizeof gc);
        !           352:                gc.attr |= GRID_ATTR_BRIGHT;
        !           353:                screen_write_puts(&ctx, &gc, "Pane is dead");
        !           354:                screen_write_stop(&ctx);
        !           355:                wp->flags |= PANE_REDRAW;
1.29      nicm      356:                return;
1.51    ! nicm      357:        }
1.29      nicm      358:
                    359:        layout_close_pane(wp);
                    360:        window_remove_pane(w, wp);
                    361:
                    362:        if (TAILQ_EMPTY(&w->panes))
                    363:                server_kill_window(w);
                    364:        else
                    365:                server_redraw_window(w);
1.25      nicm      366: }
                    367:
                    368: void
                    369: server_destroy_session_group(struct session *s)
                    370: {
                    371:        struct session_group    *sg;
                    372:
                    373:        if ((sg = session_group_find(s)) == NULL)
1.21      nicm      374:                server_destroy_session(s);
1.25      nicm      375:        else {
                    376:                TAILQ_FOREACH(s, &sg->sessions, gentry)
                    377:                        server_destroy_session(s);
                    378:                TAILQ_REMOVE(&session_groups, sg, entry);
                    379:                xfree(sg);
                    380:        }
1.19      nicm      381: }
                    382:
1.39      nicm      383: struct session *
                    384: server_next_session(struct session *s)
                    385: {
                    386:        struct session *s_loop, *s_out;
                    387:
                    388:        s_out = NULL;
1.47      nicm      389:        RB_FOREACH(s_loop, sessions, &sessions) {
                    390:                if (s_loop == s)
1.39      nicm      391:                        continue;
                    392:                if (s_out == NULL ||
                    393:                    timercmp(&s_loop->activity_time, &s_out->activity_time, <))
                    394:                        s_out = s_loop;
                    395:        }
                    396:        return (s_out);
                    397: }
                    398:
1.19      nicm      399: void
                    400: server_destroy_session(struct session *s)
                    401: {
                    402:        struct client   *c;
1.39      nicm      403:        struct session  *s_new;
1.19      nicm      404:        u_int            i;
1.31      nicm      405:
1.39      nicm      406:        if (!options_get_number(&s->options, "detach-on-destroy"))
                    407:                s_new = server_next_session(s);
                    408:        else
                    409:                s_new = NULL;
                    410:
1.19      nicm      411:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    412:                c = ARRAY_ITEM(&clients, i);
                    413:                if (c == NULL || c->session != s)
                    414:                        continue;
1.39      nicm      415:                if (s_new == NULL) {
                    416:                        c->session = NULL;
1.40      nicm      417:                        c->flags |= CLIENT_EXIT;
1.39      nicm      418:                } else {
1.46      nicm      419:                        c->last_session = NULL;
1.39      nicm      420:                        c->session = s_new;
1.49      nicm      421:                        session_update_activity(s_new);
1.39      nicm      422:                        server_redraw_client(c);
                    423:                }
1.19      nicm      424:        }
1.38      nicm      425:        recalculate_sizes();
1.42      nicm      426: }
                    427:
                    428: void
                    429: server_check_unattached (void)
                    430: {
                    431:        struct session  *s;
                    432:
                    433:        /*
                    434:         * If any sessions are no longer attached and have destroy-unattached
                    435:         * set, collect them.
                    436:         */
1.47      nicm      437:        RB_FOREACH(s, sessions, &sessions) {
                    438:                if (!(s->flags & SESSION_UNATTACHED))
1.42      nicm      439:                        continue;
                    440:                if (options_get_number (&s->options, "destroy-unattached"))
                    441:                        session_destroy(s);
                    442:        }
1.15      nicm      443: }
                    444:
                    445: void
                    446: server_set_identify(struct client *c)
                    447: {
                    448:        struct timeval  tv;
                    449:        int             delay;
                    450:
                    451:        delay = options_get_number(&c->session->options, "display-panes-time");
                    452:        tv.tv_sec = delay / 1000;
                    453:        tv.tv_usec = (delay % 1000) * 1000L;
1.31      nicm      454:
1.28      nicm      455:        evtimer_del(&c->identify_timer);
                    456:        evtimer_set(&c->identify_timer, server_callback_identify, c);
                    457:        evtimer_add(&c->identify_timer, &tv);
1.15      nicm      458:
                    459:        c->flags |= CLIENT_IDENTIFY;
                    460:        c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
                    461:        server_redraw_client(c);
                    462: }
                    463:
                    464: void
                    465: server_clear_identify(struct client *c)
                    466: {
                    467:        if (c->flags & CLIENT_IDENTIFY) {
                    468:                c->flags &= ~CLIENT_IDENTIFY;
                    469:                c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
                    470:                server_redraw_client(c);
                    471:        }
1.28      nicm      472: }
                    473:
1.30      nicm      474: /* ARGSUSED */
1.28      nicm      475: void
                    476: server_callback_identify(unused int fd, unused short events, void *data)
                    477: {
                    478:        struct client   *c = data;
                    479:
                    480:        server_clear_identify(c);
1.27      nicm      481: }
                    482:
                    483: void
                    484: server_update_event(struct client *c)
                    485: {
                    486:        short   events;
                    487:
                    488:        events = 0;
                    489:        if (!(c->flags & CLIENT_BAD))
                    490:                events |= EV_READ;
                    491:        if (c->ibuf.w.queued > 0)
                    492:                events |= EV_WRITE;
                    493:        event_del(&c->event);
                    494:        event_set(&c->event, c->ibuf.fd, events, server_client_callback, c);
1.31      nicm      495:        event_add(&c->event, NULL);
1.1       nicm      496: }