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

1.42    ! nicm        1: /* $OpenBSD: server-fn.c,v 1.41 2010/08/11 07:36:23 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.13      nicm       33:        char             tmuxvar[MAXPATHLEN], *term;
                     34:        u_int            idx;
1.1       nicm       35:
                     36:        if (session_index(s, &idx) != 0)
                     37:                fatalx("session not found");
                     38:        xsnprintf(tmuxvar, sizeof tmuxvar,
1.13      nicm       39:            "%s,%ld,%u", socket_path, (long) getpid(), idx);
                     40:        environ_set(env, "TMUX", tmuxvar);
1.1       nicm       41:
1.13      nicm       42:        term = options_get_string(&s->options, "default-terminal");
                     43:        environ_set(env, "TERM", term);
1.10      nicm       44: }
                     45:
                     46: void
1.1       nicm       47: server_write_client(
1.11      nicm       48:     struct client *c, enum msgtype type, const void *buf, size_t len)
1.1       nicm       49: {
1.14      nicm       50:        struct imsgbuf  *ibuf = &c->ibuf;
1.1       nicm       51:
1.12      nicm       52:        if (c->flags & CLIENT_BAD)
                     53:                return;
1.14      nicm       54:        log_debug("writing %d to client %d", type, c->ibuf.fd);
                     55:        imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, -1, (void *) buf, len);
1.31      nicm       56:        server_update_event(c);
1.1       nicm       57: }
                     58:
                     59: void
                     60: server_write_session(
1.11      nicm       61:     struct session *s, enum msgtype type, const void *buf, size_t len)
1.1       nicm       62: {
                     63:        struct client   *c;
                     64:        u_int            i;
                     65:
                     66:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     67:                c = ARRAY_ITEM(&clients, i);
                     68:                if (c == NULL || c->session == NULL)
                     69:                        continue;
                     70:                if (c->session == s)
                     71:                        server_write_client(c, type, buf, len);
                     72:        }
                     73: }
                     74:
                     75: void
                     76: server_redraw_client(struct client *c)
                     77: {
                     78:        c->flags |= CLIENT_REDRAW;
                     79: }
                     80:
                     81: void
                     82: server_status_client(struct client *c)
                     83: {
                     84:        c->flags |= CLIENT_STATUS;
                     85: }
                     86:
                     87: void
                     88: server_redraw_session(struct session *s)
                     89: {
                     90:        struct client   *c;
                     91:        u_int            i;
                     92:
                     93:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     94:                c = ARRAY_ITEM(&clients, i);
                     95:                if (c == NULL || c->session == NULL)
                     96:                        continue;
                     97:                if (c->session == s)
                     98:                        server_redraw_client(c);
                     99:        }
                    100: }
                    101:
                    102: void
1.25      nicm      103: server_redraw_session_group(struct session *s)
                    104: {
                    105:        struct session_group    *sg;
                    106:
                    107:        if ((sg = session_group_find(s)) == NULL)
                    108:                server_redraw_session(s);
                    109:        else {
                    110:                TAILQ_FOREACH(s, &sg->sessions, gentry)
                    111:                        server_redraw_session(s);
                    112:        }
                    113: }
                    114:
                    115: void
1.1       nicm      116: server_status_session(struct session *s)
                    117: {
                    118:        struct client   *c;
                    119:        u_int            i;
                    120:
                    121:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    122:                c = ARRAY_ITEM(&clients, i);
                    123:                if (c == NULL || c->session == NULL)
                    124:                        continue;
                    125:                if (c->session == s)
                    126:                        server_status_client(c);
                    127:        }
                    128: }
                    129:
                    130: void
1.25      nicm      131: server_status_session_group(struct session *s)
                    132: {
                    133:        struct session_group    *sg;
                    134:
                    135:        if ((sg = session_group_find(s)) == NULL)
                    136:                server_status_session(s);
                    137:        else {
                    138:                TAILQ_FOREACH(s, &sg->sessions, gentry)
                    139:                        server_status_session(s);
                    140:        }
                    141: }
                    142:
                    143: void
1.1       nicm      144: server_redraw_window(struct window *w)
                    145: {
                    146:        struct client   *c;
                    147:        u_int            i;
                    148:
                    149:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    150:                c = ARRAY_ITEM(&clients, i);
                    151:                if (c == NULL || c->session == NULL)
                    152:                        continue;
                    153:                if (c->session->curw->window == w)
                    154:                        server_redraw_client(c);
                    155:        }
                    156:        w->flags |= WINDOW_REDRAW;
1.33      nicm      157: }
                    158:
                    159: void
                    160: server_redraw_window_borders(struct window *w)
                    161: {
                    162:        struct client   *c;
                    163:        u_int            i;
                    164:
                    165:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    166:                c = ARRAY_ITEM(&clients, i);
                    167:                if (c == NULL || c->session == NULL)
                    168:                        continue;
                    169:                if (c->session->curw->window == w)
                    170:                        c->flags |= CLIENT_BORDERS;
                    171:        }
1.1       nicm      172: }
                    173:
                    174: void
                    175: server_status_window(struct window *w)
                    176: {
                    177:        struct session  *s;
                    178:        u_int            i;
                    179:
                    180:        /*
                    181:         * This is slightly different. We want to redraw the status line of any
                    182:         * clients containing this window rather than any where it is the
                    183:         * current window.
                    184:         */
                    185:
                    186:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    187:                s = ARRAY_ITEM(&sessions, i);
1.37      nicm      188:                if (s != NULL && session_has(s, w) != NULL)
1.1       nicm      189:                        server_status_session(s);
                    190:        }
                    191: }
                    192:
                    193: void
                    194: server_lock(void)
                    195: {
1.23      nicm      196:        struct client   *c;
                    197:        u_int            i;
1.17      nicm      198:
1.1       nicm      199:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    200:                c = ARRAY_ITEM(&clients, i);
                    201:                if (c == NULL || c->session == NULL)
                    202:                        continue;
1.23      nicm      203:                server_lock_client(c);
                    204:        }
                    205: }
1.1       nicm      206:
1.23      nicm      207: void
                    208: server_lock_session(struct session *s)
                    209: {
                    210:        struct client   *c;
                    211:        u_int            i;
                    212:
                    213:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    214:                c = ARRAY_ITEM(&clients, i);
                    215:                if (c == NULL || c->session == NULL || c->session != s)
                    216:                        continue;
                    217:                server_lock_client(c);
1.31      nicm      218:        }
1.23      nicm      219: }
1.1       nicm      220:
1.23      nicm      221: void
                    222: server_lock_client(struct client *c)
                    223: {
                    224:        const char              *cmd;
                    225:        size_t                   cmdlen;
                    226:        struct msg_lock_data     lockdata;
1.24      nicm      227:
                    228:        if (c->flags & CLIENT_SUSPENDED)
                    229:                return;
1.1       nicm      230:
1.23      nicm      231:        cmd = options_get_string(&c->session->options, "lock-command");
                    232:        cmdlen = strlcpy(lockdata.cmd, cmd, sizeof lockdata.cmd);
                    233:        if (cmdlen >= sizeof lockdata.cmd)
                    234:                return;
1.31      nicm      235:
1.23      nicm      236:        tty_stop_tty(&c->tty);
                    237:        tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
                    238:        tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
                    239:
                    240:        c->flags |= CLIENT_SUSPENDED;
                    241:        server_write_client(c, MSG_LOCK, &lockdata, sizeof lockdata);
1.8       nicm      242: }
                    243:
                    244: void
                    245: server_kill_window(struct window *w)
                    246: {
                    247:        struct session  *s;
                    248:        struct winlink  *wl;
1.19      nicm      249:        u_int            i;
1.31      nicm      250:
1.8       nicm      251:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    252:                s = ARRAY_ITEM(&sessions, i);
1.37      nicm      253:                if (s == NULL || session_has(s, w) == NULL)
1.8       nicm      254:                        continue;
1.34      nicm      255:                while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
                    256:                        if (session_detach(s, wl)) {
                    257:                                server_destroy_session_group(s);
                    258:                                break;
                    259:                        } else
                    260:                                server_redraw_session_group(s);
                    261:                }
1.8       nicm      262:        }
1.21      nicm      263: }
                    264:
                    265: int
1.25      nicm      266: server_link_window(struct session *src, struct winlink *srcwl,
                    267:     struct session *dst, int dstidx, int killflag, int selectflag, char **cause)
1.21      nicm      268: {
1.25      nicm      269:        struct winlink          *dstwl;
                    270:        struct session_group    *srcsg, *dstsg;
                    271:
                    272:        srcsg = session_group_find(src);
                    273:        dstsg = session_group_find(dst);
                    274:        if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
                    275:                xasprintf(cause, "sessions are grouped");
                    276:                return (-1);
                    277:        }
1.21      nicm      278:
                    279:        dstwl = NULL;
                    280:        if (dstidx != -1)
                    281:                dstwl = winlink_find_by_index(&dst->windows, dstidx);
                    282:        if (dstwl != NULL) {
                    283:                if (dstwl->window == srcwl->window)
1.41      nicm      284:                        return (-1);
1.21      nicm      285:                if (killflag) {
                    286:                        /*
                    287:                         * Can't use session_detach as it will destroy session
                    288:                         * if this makes it empty.
                    289:                         */
1.37      nicm      290:                        dstwl->flags &= ~WINLINK_ALERTFLAGS;
1.21      nicm      291:                        winlink_stack_remove(&dst->lastw, dstwl);
                    292:                        winlink_remove(&dst->windows, dstwl);
                    293:
                    294:                        /* Force select/redraw if current. */
1.26      nicm      295:                        if (dstwl == dst->curw) {
1.21      nicm      296:                                selectflag = 1;
1.26      nicm      297:                                dst->curw = NULL;
                    298:                        }
1.21      nicm      299:                }
                    300:        }
                    301:
                    302:        if (dstidx == -1)
                    303:                dstidx = -1 - options_get_number(&dst->options, "base-index");
                    304:        dstwl = session_attach(dst, srcwl->window, dstidx, cause);
                    305:        if (dstwl == NULL)
                    306:                return (-1);
                    307:
1.25      nicm      308:        if (selectflag)
1.21      nicm      309:                session_select(dst, dstwl->idx);
1.25      nicm      310:        server_redraw_session_group(dst);
1.21      nicm      311:
                    312:        return (0);
                    313: }
                    314:
                    315: void
                    316: server_unlink_window(struct session *s, struct winlink *wl)
                    317: {
                    318:        if (session_detach(s, wl))
1.25      nicm      319:                server_destroy_session_group(s);
                    320:        else
                    321:                server_redraw_session_group(s);
1.29      nicm      322: }
                    323:
                    324: void
                    325: server_destroy_pane(struct window_pane *wp)
                    326: {
                    327:        struct window   *w = wp->window;
                    328:
1.36      nicm      329:        if (wp->fd != -1) {
                    330:                close(wp->fd);
                    331:                bufferevent_free(wp->event);
                    332:                wp->fd = -1;
                    333:        }
1.29      nicm      334:
                    335:        if (options_get_number(&w->options, "remain-on-exit"))
                    336:                return;
                    337:
                    338:        layout_close_pane(wp);
                    339:        window_remove_pane(w, wp);
                    340:
                    341:        if (TAILQ_EMPTY(&w->panes))
                    342:                server_kill_window(w);
                    343:        else
                    344:                server_redraw_window(w);
1.25      nicm      345: }
                    346:
                    347: void
                    348: server_destroy_session_group(struct session *s)
                    349: {
                    350:        struct session_group    *sg;
                    351:
                    352:        if ((sg = session_group_find(s)) == NULL)
1.21      nicm      353:                server_destroy_session(s);
1.25      nicm      354:        else {
                    355:                TAILQ_FOREACH(s, &sg->sessions, gentry)
                    356:                        server_destroy_session(s);
                    357:                TAILQ_REMOVE(&session_groups, sg, entry);
                    358:                xfree(sg);
                    359:        }
1.19      nicm      360: }
                    361:
1.39      nicm      362: struct session *
                    363: server_next_session(struct session *s)
                    364: {
                    365:        struct session *s_loop, *s_out;
                    366:        u_int           i;
                    367:
                    368:        s_out = NULL;
                    369:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    370:                s_loop = ARRAY_ITEM(&sessions, i);
                    371:                if (s_loop == s)
                    372:                        continue;
                    373:                if (s_out == NULL ||
                    374:                    timercmp(&s_loop->activity_time, &s_out->activity_time, <))
                    375:                        s_out = s_loop;
                    376:        }
                    377:        return (s_out);
                    378: }
                    379:
1.19      nicm      380: void
                    381: server_destroy_session(struct session *s)
                    382: {
                    383:        struct client   *c;
1.39      nicm      384:        struct session  *s_new;
1.19      nicm      385:        u_int            i;
1.31      nicm      386:
1.39      nicm      387:        if (!options_get_number(&s->options, "detach-on-destroy"))
                    388:                s_new = server_next_session(s);
                    389:        else
                    390:                s_new = NULL;
                    391:
1.19      nicm      392:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    393:                c = ARRAY_ITEM(&clients, i);
                    394:                if (c == NULL || c->session != s)
                    395:                        continue;
1.39      nicm      396:                if (s_new == NULL) {
                    397:                        c->session = NULL;
1.40      nicm      398:                        c->flags |= CLIENT_EXIT;
1.39      nicm      399:                } else {
                    400:                        c->session = s_new;
                    401:                        server_redraw_client(c);
                    402:                }
1.19      nicm      403:        }
1.38      nicm      404:        recalculate_sizes();
1.42    ! nicm      405: }
        !           406:
        !           407: void
        !           408: server_check_unattached (void)
        !           409: {
        !           410:        struct session  *s;
        !           411:        u_int            i;
        !           412:
        !           413:        /*
        !           414:         * If any sessions are no longer attached and have destroy-unattached
        !           415:         * set, collect them.
        !           416:         */
        !           417:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
        !           418:                s = ARRAY_ITEM(&sessions, i);
        !           419:                if (s == NULL || !(s->flags & SESSION_UNATTACHED))
        !           420:                        continue;
        !           421:                if (options_get_number (&s->options, "destroy-unattached"))
        !           422:                        session_destroy(s);
        !           423:        }
1.15      nicm      424: }
                    425:
                    426: void
                    427: server_set_identify(struct client *c)
                    428: {
                    429:        struct timeval  tv;
                    430:        int             delay;
                    431:
                    432:        delay = options_get_number(&c->session->options, "display-panes-time");
                    433:        tv.tv_sec = delay / 1000;
                    434:        tv.tv_usec = (delay % 1000) * 1000L;
1.31      nicm      435:
1.28      nicm      436:        evtimer_del(&c->identify_timer);
                    437:        evtimer_set(&c->identify_timer, server_callback_identify, c);
                    438:        evtimer_add(&c->identify_timer, &tv);
1.15      nicm      439:
                    440:        c->flags |= CLIENT_IDENTIFY;
                    441:        c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
                    442:        server_redraw_client(c);
                    443: }
                    444:
                    445: void
                    446: server_clear_identify(struct client *c)
                    447: {
                    448:        if (c->flags & CLIENT_IDENTIFY) {
                    449:                c->flags &= ~CLIENT_IDENTIFY;
                    450:                c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
                    451:                server_redraw_client(c);
                    452:        }
1.28      nicm      453: }
                    454:
1.30      nicm      455: /* ARGSUSED */
1.28      nicm      456: void
                    457: server_callback_identify(unused int fd, unused short events, void *data)
                    458: {
                    459:        struct client   *c = data;
                    460:
                    461:        server_clear_identify(c);
1.27      nicm      462: }
                    463:
                    464: void
                    465: server_update_event(struct client *c)
                    466: {
                    467:        short   events;
                    468:
                    469:        events = 0;
                    470:        if (!(c->flags & CLIENT_BAD))
                    471:                events |= EV_READ;
                    472:        if (c->ibuf.w.queued > 0)
                    473:                events |= EV_WRITE;
                    474:        event_del(&c->event);
                    475:        event_set(&c->event, c->ibuf.fd, events, server_client_callback, c);
1.31      nicm      476:        event_add(&c->event, NULL);
1.1       nicm      477: }