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

1.24    ! nicm        1: /* $OpenBSD: server-fn.c,v 1.23 2009/09/24 14:17:09 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.13      nicm       27: void
                     28: server_fill_environ(struct session *s, struct environ *env)
1.1       nicm       29: {
1.13      nicm       30:        char             tmuxvar[MAXPATHLEN], *term;
                     31:        u_int            idx;
1.1       nicm       32:
                     33:        if (session_index(s, &idx) != 0)
                     34:                fatalx("session not found");
                     35:        xsnprintf(tmuxvar, sizeof tmuxvar,
1.13      nicm       36:            "%s,%ld,%u", socket_path, (long) getpid(), idx);
                     37:        environ_set(env, "TMUX", tmuxvar);
1.1       nicm       38:
1.13      nicm       39:        term = options_get_string(&s->options, "default-terminal");
                     40:        environ_set(env, "TERM", term);
1.1       nicm       41: }
                     42:
                     43: void
1.10      nicm       44: server_write_error(struct client *c, const char *msg)
                     45: {
                     46:        struct msg_print_data   printdata;
                     47:
                     48:        strlcpy(printdata.msg, msg, sizeof printdata.msg);
                     49:        server_write_client(c, MSG_ERROR, &printdata, sizeof printdata);
                     50: }
                     51:
                     52: void
1.1       nicm       53: server_write_client(
1.11      nicm       54:     struct client *c, enum msgtype type, const void *buf, size_t len)
1.1       nicm       55: {
1.14      nicm       56:        struct imsgbuf  *ibuf = &c->ibuf;
1.1       nicm       57:
1.12      nicm       58:        if (c->flags & CLIENT_BAD)
                     59:                return;
1.14      nicm       60:        log_debug("writing %d to client %d", type, c->ibuf.fd);
                     61:        imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, -1, (void *) buf, len);
1.1       nicm       62: }
                     63:
                     64: void
                     65: server_write_session(
1.11      nicm       66:     struct session *s, enum msgtype type, const void *buf, size_t len)
1.1       nicm       67: {
                     68:        struct client   *c;
                     69:        u_int            i;
                     70:
                     71:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     72:                c = ARRAY_ITEM(&clients, i);
                     73:                if (c == NULL || c->session == NULL)
                     74:                        continue;
                     75:                if (c->session == s)
                     76:                        server_write_client(c, type, buf, len);
                     77:        }
                     78: }
                     79:
                     80: void
                     81: server_redraw_client(struct client *c)
                     82: {
                     83:        c->flags |= CLIENT_REDRAW;
                     84: }
                     85:
                     86: void
                     87: server_status_client(struct client *c)
                     88: {
                     89:        c->flags |= CLIENT_STATUS;
                     90: }
                     91:
                     92: void
                     93: server_redraw_session(struct session *s)
                     94: {
                     95:        struct client   *c;
                     96:        u_int            i;
                     97:
                     98:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     99:                c = ARRAY_ITEM(&clients, i);
                    100:                if (c == NULL || c->session == NULL)
                    101:                        continue;
                    102:                if (c->session == s)
                    103:                        server_redraw_client(c);
                    104:        }
                    105: }
                    106:
                    107: void
                    108: server_status_session(struct session *s)
                    109: {
                    110:        struct client   *c;
                    111:        u_int            i;
                    112:
                    113:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    114:                c = ARRAY_ITEM(&clients, i);
                    115:                if (c == NULL || c->session == NULL)
                    116:                        continue;
                    117:                if (c->session == s)
                    118:                        server_status_client(c);
                    119:        }
                    120: }
                    121:
                    122: void
                    123: server_redraw_window(struct window *w)
                    124: {
                    125:        struct client   *c;
                    126:        u_int            i;
                    127:
                    128:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    129:                c = ARRAY_ITEM(&clients, i);
                    130:                if (c == NULL || c->session == NULL)
                    131:                        continue;
                    132:                if (c->session->curw->window == w)
                    133:                        server_redraw_client(c);
                    134:        }
                    135:        w->flags |= WINDOW_REDRAW;
                    136: }
                    137:
                    138: void
                    139: server_status_window(struct window *w)
                    140: {
                    141:        struct session  *s;
                    142:        u_int            i;
                    143:
                    144:        /*
                    145:         * This is slightly different. We want to redraw the status line of any
                    146:         * clients containing this window rather than any where it is the
                    147:         * current window.
                    148:         */
                    149:
                    150:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    151:                s = ARRAY_ITEM(&sessions, i);
                    152:                if (s != NULL && session_has(s, w))
                    153:                        server_status_session(s);
                    154:        }
                    155: }
                    156:
                    157: void
                    158: server_lock(void)
                    159: {
1.23      nicm      160:        struct client   *c;
                    161:        u_int            i;
1.17      nicm      162:
1.1       nicm      163:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    164:                c = ARRAY_ITEM(&clients, i);
                    165:                if (c == NULL || c->session == NULL)
                    166:                        continue;
1.23      nicm      167:                server_lock_client(c);
                    168:        }
                    169: }
1.1       nicm      170:
1.23      nicm      171: void
                    172: server_lock_session(struct session *s)
                    173: {
                    174:        struct client   *c;
                    175:        u_int            i;
                    176:
                    177:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    178:                c = ARRAY_ITEM(&clients, i);
                    179:                if (c == NULL || c->session == NULL || c->session != s)
                    180:                        continue;
                    181:                server_lock_client(c);
                    182:        }
                    183: }
1.1       nicm      184:
1.23      nicm      185: void
                    186: server_lock_client(struct client *c)
                    187: {
                    188:        const char              *cmd;
                    189:        size_t                   cmdlen;
                    190:        struct msg_lock_data     lockdata;
1.24    ! nicm      191:
        !           192:        if (c->flags & CLIENT_SUSPENDED)
        !           193:                return;
1.1       nicm      194:
1.23      nicm      195:        cmd = options_get_string(&c->session->options, "lock-command");
                    196:        cmdlen = strlcpy(lockdata.cmd, cmd, sizeof lockdata.cmd);
                    197:        if (cmdlen >= sizeof lockdata.cmd)
                    198:                return;
                    199:
                    200:        tty_stop_tty(&c->tty);
                    201:        tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
                    202:        tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
                    203:
                    204:        c->flags |= CLIENT_SUSPENDED;
                    205:        server_write_client(c, MSG_LOCK, &lockdata, sizeof lockdata);
1.8       nicm      206: }
                    207:
                    208: void
                    209: server_kill_window(struct window *w)
                    210: {
                    211:        struct session  *s;
                    212:        struct winlink  *wl;
1.19      nicm      213:        u_int            i;
1.8       nicm      214:
                    215:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    216:                s = ARRAY_ITEM(&sessions, i);
                    217:                if (s == NULL || !session_has(s, w))
                    218:                        continue;
                    219:                if ((wl = winlink_find_by_window(&s->windows, w)) == NULL)
                    220:                        continue;
                    221:
1.19      nicm      222:                if (session_detach(s, wl))
                    223:                        server_destroy_session(s);
                    224:                else
                    225:                        server_redraw_session(s);
1.8       nicm      226:        }
1.21      nicm      227: }
                    228:
                    229: int
                    230: server_link_window(
                    231:     struct winlink *srcwl, struct session *dst, int dstidx,
                    232:     int killflag, int selectflag, char **cause)
                    233: {
                    234:        struct winlink  *dstwl;
                    235:
                    236:        dstwl = NULL;
                    237:        if (dstidx != -1)
                    238:                dstwl = winlink_find_by_index(&dst->windows, dstidx);
                    239:        if (dstwl != NULL) {
                    240:                if (dstwl->window == srcwl->window)
                    241:                        return (0);
                    242:                if (killflag) {
                    243:                        /*
                    244:                         * Can't use session_detach as it will destroy session
                    245:                         * if this makes it empty.
                    246:                         */
                    247:                        session_alert_cancel(dst, dstwl);
                    248:                        winlink_stack_remove(&dst->lastw, dstwl);
                    249:                        winlink_remove(&dst->windows, dstwl);
                    250:
                    251:                        /* Force select/redraw if current. */
                    252:                        if (dstwl == dst->curw)
                    253:                                selectflag = 1;
                    254:                }
                    255:        }
                    256:
                    257:        if (dstidx == -1)
                    258:                dstidx = -1 - options_get_number(&dst->options, "base-index");
                    259:        dstwl = session_attach(dst, srcwl->window, dstidx, cause);
                    260:        if (dstwl == NULL)
                    261:                return (-1);
                    262:
                    263:        if (!selectflag)
                    264:                server_status_session(dst);
                    265:        else {
                    266:                session_select(dst, dstwl->idx);
                    267:                server_redraw_session(dst);
                    268:        }
                    269:
                    270:        return (0);
                    271: }
                    272:
                    273: void
                    274: server_unlink_window(struct session *s, struct winlink *wl)
                    275: {
                    276:        if (session_detach(s, wl))
                    277:                server_destroy_session(s);
                    278:        else
                    279:                server_redraw_session(s);
1.19      nicm      280: }
                    281:
                    282: void
                    283: server_destroy_session(struct session *s)
                    284: {
                    285:        struct client   *c;
                    286:        u_int            i;
                    287:
                    288:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    289:                c = ARRAY_ITEM(&clients, i);
                    290:                if (c == NULL || c->session != s)
                    291:                        continue;
                    292:                c->session = NULL;
                    293:                server_write_client(c, MSG_EXIT, NULL, 0);
                    294:        }
1.15      nicm      295: }
                    296:
                    297: void
                    298: server_set_identify(struct client *c)
                    299: {
                    300:        struct timeval  tv;
                    301:        int             delay;
                    302:
                    303:        delay = options_get_number(&c->session->options, "display-panes-time");
                    304:        tv.tv_sec = delay / 1000;
                    305:        tv.tv_usec = (delay % 1000) * 1000L;
                    306:
                    307:        if (gettimeofday(&c->identify_timer, NULL) != 0)
1.20      nicm      308:                fatal("gettimeofday failed");
1.15      nicm      309:        timeradd(&c->identify_timer, &tv, &c->identify_timer);
                    310:
                    311:        c->flags |= CLIENT_IDENTIFY;
                    312:        c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
                    313:        server_redraw_client(c);
                    314: }
                    315:
                    316: void
                    317: server_clear_identify(struct client *c)
                    318: {
                    319:        if (c->flags & CLIENT_IDENTIFY) {
                    320:                c->flags &= ~CLIENT_IDENTIFY;
                    321:                c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
                    322:                server_redraw_client(c);
                    323:        }
1.1       nicm      324: }