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

1.44    ! nicm        1: /* $OpenBSD: server-fn.c,v 1.43 2010/10/05 17:15:21 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) {
1.44    ! nicm      283:                if (dstwl->window == srcwl->window) {
        !           284:                        xasprintf(cause, "same index: %d", dstidx);
1.41      nicm      285:                        return (-1);
1.44    ! nicm      286:                }
1.21      nicm      287:                if (killflag) {
                    288:                        /*
                    289:                         * Can't use session_detach as it will destroy session
                    290:                         * if this makes it empty.
                    291:                         */
1.37      nicm      292:                        dstwl->flags &= ~WINLINK_ALERTFLAGS;
1.21      nicm      293:                        winlink_stack_remove(&dst->lastw, dstwl);
                    294:                        winlink_remove(&dst->windows, dstwl);
                    295:
                    296:                        /* Force select/redraw if current. */
1.26      nicm      297:                        if (dstwl == dst->curw) {
1.21      nicm      298:                                selectflag = 1;
1.26      nicm      299:                                dst->curw = NULL;
                    300:                        }
1.21      nicm      301:                }
                    302:        }
                    303:
                    304:        if (dstidx == -1)
                    305:                dstidx = -1 - options_get_number(&dst->options, "base-index");
                    306:        dstwl = session_attach(dst, srcwl->window, dstidx, cause);
                    307:        if (dstwl == NULL)
                    308:                return (-1);
                    309:
1.25      nicm      310:        if (selectflag)
1.21      nicm      311:                session_select(dst, dstwl->idx);
1.25      nicm      312:        server_redraw_session_group(dst);
1.21      nicm      313:
                    314:        return (0);
                    315: }
                    316:
                    317: void
                    318: server_unlink_window(struct session *s, struct winlink *wl)
                    319: {
                    320:        if (session_detach(s, wl))
1.25      nicm      321:                server_destroy_session_group(s);
                    322:        else
                    323:                server_redraw_session_group(s);
1.29      nicm      324: }
                    325:
                    326: void
                    327: server_destroy_pane(struct window_pane *wp)
                    328: {
                    329:        struct window   *w = wp->window;
                    330:
1.36      nicm      331:        if (wp->fd != -1) {
                    332:                close(wp->fd);
                    333:                bufferevent_free(wp->event);
                    334:                wp->fd = -1;
                    335:        }
1.29      nicm      336:
                    337:        if (options_get_number(&w->options, "remain-on-exit"))
                    338:                return;
                    339:
                    340:        layout_close_pane(wp);
                    341:        window_remove_pane(w, wp);
                    342:
                    343:        if (TAILQ_EMPTY(&w->panes))
                    344:                server_kill_window(w);
                    345:        else
                    346:                server_redraw_window(w);
1.25      nicm      347: }
                    348:
                    349: void
                    350: server_destroy_session_group(struct session *s)
                    351: {
                    352:        struct session_group    *sg;
                    353:
                    354:        if ((sg = session_group_find(s)) == NULL)
1.21      nicm      355:                server_destroy_session(s);
1.25      nicm      356:        else {
                    357:                TAILQ_FOREACH(s, &sg->sessions, gentry)
                    358:                        server_destroy_session(s);
                    359:                TAILQ_REMOVE(&session_groups, sg, entry);
                    360:                xfree(sg);
                    361:        }
1.19      nicm      362: }
                    363:
1.39      nicm      364: struct session *
                    365: server_next_session(struct session *s)
                    366: {
                    367:        struct session *s_loop, *s_out;
                    368:        u_int           i;
                    369:
                    370:        s_out = NULL;
                    371:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    372:                s_loop = ARRAY_ITEM(&sessions, i);
1.43      nicm      373:                if (s_loop == NULL || s_loop == s)
1.39      nicm      374:                        continue;
                    375:                if (s_out == NULL ||
                    376:                    timercmp(&s_loop->activity_time, &s_out->activity_time, <))
                    377:                        s_out = s_loop;
                    378:        }
                    379:        return (s_out);
                    380: }
                    381:
1.19      nicm      382: void
                    383: server_destroy_session(struct session *s)
                    384: {
                    385:        struct client   *c;
1.39      nicm      386:        struct session  *s_new;
1.19      nicm      387:        u_int            i;
1.31      nicm      388:
1.39      nicm      389:        if (!options_get_number(&s->options, "detach-on-destroy"))
                    390:                s_new = server_next_session(s);
                    391:        else
                    392:                s_new = NULL;
                    393:
1.19      nicm      394:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    395:                c = ARRAY_ITEM(&clients, i);
                    396:                if (c == NULL || c->session != s)
                    397:                        continue;
1.39      nicm      398:                if (s_new == NULL) {
                    399:                        c->session = NULL;
1.40      nicm      400:                        c->flags |= CLIENT_EXIT;
1.39      nicm      401:                } else {
                    402:                        c->session = s_new;
                    403:                        server_redraw_client(c);
                    404:                }
1.19      nicm      405:        }
1.38      nicm      406:        recalculate_sizes();
1.42      nicm      407: }
                    408:
                    409: void
                    410: server_check_unattached (void)
                    411: {
                    412:        struct session  *s;
                    413:        u_int            i;
                    414:
                    415:        /*
                    416:         * If any sessions are no longer attached and have destroy-unattached
                    417:         * set, collect them.
                    418:         */
                    419:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    420:                s = ARRAY_ITEM(&sessions, i);
                    421:                if (s == NULL || !(s->flags & SESSION_UNATTACHED))
                    422:                        continue;
                    423:                if (options_get_number (&s->options, "destroy-unattached"))
                    424:                        session_destroy(s);
                    425:        }
1.15      nicm      426: }
                    427:
                    428: void
                    429: server_set_identify(struct client *c)
                    430: {
                    431:        struct timeval  tv;
                    432:        int             delay;
                    433:
                    434:        delay = options_get_number(&c->session->options, "display-panes-time");
                    435:        tv.tv_sec = delay / 1000;
                    436:        tv.tv_usec = (delay % 1000) * 1000L;
1.31      nicm      437:
1.28      nicm      438:        evtimer_del(&c->identify_timer);
                    439:        evtimer_set(&c->identify_timer, server_callback_identify, c);
                    440:        evtimer_add(&c->identify_timer, &tv);
1.15      nicm      441:
                    442:        c->flags |= CLIENT_IDENTIFY;
                    443:        c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
                    444:        server_redraw_client(c);
                    445: }
                    446:
                    447: void
                    448: server_clear_identify(struct client *c)
                    449: {
                    450:        if (c->flags & CLIENT_IDENTIFY) {
                    451:                c->flags &= ~CLIENT_IDENTIFY;
                    452:                c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
                    453:                server_redraw_client(c);
                    454:        }
1.28      nicm      455: }
                    456:
1.30      nicm      457: /* ARGSUSED */
1.28      nicm      458: void
                    459: server_callback_identify(unused int fd, unused short events, void *data)
                    460: {
                    461:        struct client   *c = data;
                    462:
                    463:        server_clear_identify(c);
1.27      nicm      464: }
                    465:
                    466: void
                    467: server_update_event(struct client *c)
                    468: {
                    469:        short   events;
                    470:
                    471:        events = 0;
                    472:        if (!(c->flags & CLIENT_BAD))
                    473:                events |= EV_READ;
                    474:        if (c->ibuf.w.queued > 0)
                    475:                events |= EV_WRITE;
                    476:        event_del(&c->event);
                    477:        event_set(&c->event, c->ibuf.fd, events, server_client_callback, c);
1.31      nicm      478:        event_add(&c->event, NULL);
1.1       nicm      479: }