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

Annotation of src/usr.bin/tmux/server-window.c, Revision 1.9

1.9     ! nicm        1: /* $OpenBSD: server-window.c,v 1.8 2009/11/04 22:02:38 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 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:
1.5       nicm       21: #include <event.h>
1.1       nicm       22: #include <unistd.h>
                     23:
                     24: #include "tmux.h"
                     25:
1.3       nicm       26: int    server_window_backoff(struct window_pane *);
1.1       nicm       27: int    server_window_check_bell(struct session *, struct window *);
                     28: int    server_window_check_activity(struct session *, struct window *);
                     29: int    server_window_check_content(
                     30:            struct session *, struct window *, struct window_pane *);
                     31: void   server_window_check_alive(struct window *);
1.2       nicm       32:
1.3       nicm       33: /* Check if this window should suspend reading. */
                     34: int
                     35: server_window_backoff(struct window_pane *wp)
                     36: {
                     37:        struct client   *c;
                     38:        u_int            i;
                     39:
                     40:        if (!window_pane_visible(wp))
                     41:                return (0);
                     42:
                     43:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     44:                c = ARRAY_ITEM(&clients, i);
                     45:                if (c == NULL || c->session == NULL)
1.4       nicm       46:                        continue;
                     47:                if ((c->flags & (CLIENT_SUSPENDED|CLIENT_DEAD)) != 0)
1.3       nicm       48:                        continue;
                     49:                if (c->session->curw->window != wp->window)
                     50:                        continue;
1.7       nicm       51:
                     52:                if (EVBUFFER_LENGTH(c->tty.event->output) > BACKOFF_THRESHOLD)
1.3       nicm       53:                        return (1);
                     54:        }
                     55:        return (0);
1.2       nicm       56: }
1.1       nicm       57:
                     58: /* Window functions that need to happen every loop. */
                     59: void
                     60: server_window_loop(void)
                     61: {
                     62:        struct window           *w;
                     63:        struct window_pane      *wp;
                     64:        struct session          *s;
                     65:        u_int                    i, j;
                     66:
                     67:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                     68:                w = ARRAY_ITEM(&windows, i);
                     69:                if (w == NULL)
                     70:                        continue;
1.9     ! nicm       71:
        !            72:                TAILQ_FOREACH(wp, &w->panes, entry) {
        !            73:                        if (server_window_backoff(wp))
        !            74:                                bufferevent_disable(wp->event, EV_READ);
        !            75:                        else
        !            76:                                bufferevent_enable(wp->event, EV_READ);
        !            77:                }
1.1       nicm       78:
                     79:                for (j = 0; j < ARRAY_LENGTH(&sessions); j++) {
                     80:                        s = ARRAY_ITEM(&sessions, j);
                     81:                        if (s == NULL || !session_has(s, w))
                     82:                                continue;
                     83:
                     84:                        if (server_window_check_bell(s, w) ||
                     85:                            server_window_check_activity(s, w))
                     86:                                server_status_session(s);
                     87:                        TAILQ_FOREACH(wp, &w->panes, entry)
                     88:                                server_window_check_content(s, w, wp);
                     89:                }
                     90:                w->flags &= ~(WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT);
                     91:
                     92:                server_window_check_alive(w);
                     93:        }
                     94:
                     95:        set_window_names();
                     96: }
                     97:
                     98: /* Check for bell in window. */
                     99: int
                    100: server_window_check_bell(struct session *s, struct window *w)
                    101: {
                    102:        struct client   *c;
                    103:        u_int            i;
                    104:        int              action, visual;
                    105:
                    106:        if (!(w->flags & WINDOW_BELL))
                    107:                return (0);
                    108:
                    109:        if (session_alert_has_window(s, w, WINDOW_BELL))
                    110:                return (0);
                    111:        session_alert_add(s, w, WINDOW_BELL);
                    112:
                    113:        action = options_get_number(&s->options, "bell-action");
                    114:        switch (action) {
                    115:        case BELL_ANY:
                    116:                if (s->flags & SESSION_UNATTACHED)
                    117:                        break;
                    118:                visual = options_get_number(&s->options, "visual-bell");
                    119:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    120:                        c = ARRAY_ITEM(&clients, i);
                    121:                        if (c == NULL || c->session != s)
                    122:                                continue;
                    123:                        if (!visual) {
                    124:                                tty_putcode(&c->tty, TTYC_BEL);
                    125:                                continue;
                    126:                        }
                    127:                        if (c->session->curw->window == w) {
                    128:                                status_message_set(c, "Bell in current window");
                    129:                                continue;
                    130:                        }
                    131:                        status_message_set(c, "Bell in window %u",
                    132:                            winlink_find_by_window(&s->windows, w)->idx);
                    133:                }
                    134:                break;
                    135:        case BELL_CURRENT:
                    136:                if (s->flags & SESSION_UNATTACHED)
                    137:                        break;
                    138:                visual = options_get_number(&s->options, "visual-bell");
                    139:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    140:                        c = ARRAY_ITEM(&clients, i);
                    141:                        if (c == NULL || c->session != s)
                    142:                                continue;
                    143:                        if (c->session->curw->window != w)
                    144:                                continue;
                    145:                        if (!visual) {
                    146:                                tty_putcode(&c->tty, TTYC_BEL);
                    147:                                continue;
                    148:                        }
                    149:                        status_message_set(c, "Bell in current window");
                    150:                }
                    151:                break;
                    152:        }
                    153:
                    154:        return (1);
                    155: }
                    156:
                    157: /* Check for activity in window. */
                    158: int
                    159: server_window_check_activity(struct session *s, struct window *w)
                    160: {
                    161:        struct client   *c;
                    162:        u_int            i;
                    163:
                    164:        if (!(w->flags & WINDOW_ACTIVITY))
                    165:                return (0);
                    166:        if (s->curw->window == w)
                    167:                return (0);
                    168:
                    169:        if (!options_get_number(&w->options, "monitor-activity"))
                    170:                return (0);
                    171:
                    172:        if (session_alert_has_window(s, w, WINDOW_ACTIVITY))
                    173:                return (0);
                    174:        session_alert_add(s, w, WINDOW_ACTIVITY);
                    175:
                    176:        if (s->flags & SESSION_UNATTACHED)
                    177:                return (0);
                    178:        if (options_get_number(&s->options, "visual-activity")) {
                    179:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    180:                        c = ARRAY_ITEM(&clients, i);
                    181:                        if (c == NULL || c->session != s)
                    182:                                continue;
                    183:                        status_message_set(c, "Activity in window %u",
                    184:                            winlink_find_by_window(&s->windows, w)->idx);
                    185:                }
                    186:        }
                    187:
                    188:        return (1);
                    189: }
                    190:
                    191: /* Check for content change in window. */
                    192: int
                    193: server_window_check_content(
                    194:     struct session *s, struct window *w, struct window_pane *wp)
                    195: {
                    196:        struct client   *c;
                    197:        u_int            i;
                    198:        char            *found, *ptr;
                    199:
                    200:        if (!(w->flags & WINDOW_ACTIVITY))      /* activity for new content */
                    201:                return (0);
                    202:        if (s->curw->window == w)
                    203:                return (0);
                    204:
                    205:        ptr = options_get_string(&w->options, "monitor-content");
                    206:        if (ptr == NULL || *ptr == '\0')
                    207:                return (0);
                    208:
                    209:        if (session_alert_has_window(s, w, WINDOW_CONTENT))
                    210:                return (0);
                    211:
                    212:        if ((found = window_pane_search(wp, ptr, NULL)) == NULL)
                    213:                return (0);
                    214:        xfree(found);
                    215:
                    216:        session_alert_add(s, w, WINDOW_CONTENT);
                    217:        if (s->flags & SESSION_UNATTACHED)
                    218:                return (0);
                    219:        if (options_get_number(&s->options, "visual-content")) {
                    220:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    221:                        c = ARRAY_ITEM(&clients, i);
                    222:                        if (c == NULL || c->session != s)
                    223:                                continue;
                    224:                        status_message_set(c, "Content in window %u",
                    225:                            winlink_find_by_window(&s->windows, w)->idx);
                    226:                }
                    227:        }
                    228:
                    229:        return (1);
                    230: }
                    231:
                    232: /* Check if window still exists. */
                    233: void
                    234: server_window_check_alive(struct window *w)
                    235: {
                    236:        struct window_pane      *wp, *wq;
                    237:        struct options          *oo = &w->options;
                    238:        struct session          *s;
                    239:        struct winlink          *wl;
                    240:        u_int                    i;
                    241:        int                      destroyed;
                    242:
                    243:        destroyed = 1;
                    244:
                    245:        wp = TAILQ_FIRST(&w->panes);
                    246:        while (wp != NULL) {
                    247:                wq = TAILQ_NEXT(wp, entry);
                    248:                /*
                    249:                 * If the pane has died and the remain-on-exit flag is not set,
                    250:                 * remove the pane; otherwise, if the flag is set, don't allow
                    251:                 * the window to be destroyed (or it'll close when the last
                    252:                 * pane dies).
                    253:                 */
                    254:                if (wp->fd == -1 && !options_get_number(oo, "remain-on-exit")) {
                    255:                        layout_close_pane(wp);
                    256:                        window_remove_pane(w, wp);
                    257:                        server_redraw_window(w);
                    258:                } else
                    259:                        destroyed = 0;
                    260:                wp = wq;
                    261:        }
                    262:
                    263:        if (!destroyed)
                    264:                return;
                    265:
                    266:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    267:                s = ARRAY_ITEM(&sessions, i);
                    268:                if (s == NULL)
                    269:                        continue;
                    270:                if (!session_has(s, w))
                    271:                        continue;
                    272:
                    273:        restart:
                    274:                /* Detach window and either redraw or kill clients. */
                    275:                RB_FOREACH(wl, winlinks, &s->windows) {
                    276:                        if (wl->window != w)
                    277:                                continue;
                    278:                        if (session_detach(s, wl)) {
                    279:                                server_destroy_session_group(s);
                    280:                                break;
                    281:                        }
                    282:                        server_redraw_session(s);
                    283:                        server_status_session_group(s);
                    284:                        goto restart;
                    285:                }
                    286:        }
                    287:
                    288:        recalculate_sizes();
                    289: }