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

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