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

1.24    ! nicm        1: /* $OpenBSD: server-window.c,v 1.23 2012/01/21 06:13:16 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.15      nicm       26: int    server_window_check_bell(struct session *, struct winlink *);
                     27: int    server_window_check_activity(struct session *, struct winlink *);
1.18      nicm       28: int    server_window_check_silence(struct session *, struct winlink *);
1.1       nicm       29: int    server_window_check_content(
1.15      nicm       30:            struct session *, struct winlink *, struct window_pane *);
1.21      nicm       31: void   ring_bell(struct session *);
1.2       nicm       32:
1.1       nicm       33: /* Window functions that need to happen every loop. */
                     34: void
                     35: server_window_loop(void)
                     36: {
                     37:        struct window           *w;
1.15      nicm       38:        struct winlink          *wl;
1.1       nicm       39:        struct window_pane      *wp;
                     40:        struct session          *s;
1.19      nicm       41:        u_int                    i;
1.1       nicm       42:
                     43:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                     44:                w = ARRAY_ITEM(&windows, i);
                     45:                if (w == NULL)
                     46:                        continue;
                     47:
1.19      nicm       48:                RB_FOREACH(s, sessions, &sessions) {
1.15      nicm       49:                        wl = session_has(s, w);
                     50:                        if (wl == NULL)
1.1       nicm       51:                                continue;
1.13      nicm       52:
1.15      nicm       53:                        if (server_window_check_bell(s, wl) ||
1.18      nicm       54:                            server_window_check_activity(s, wl) ||
                     55:                            server_window_check_silence(s, wl))
1.1       nicm       56:                                server_status_session(s);
                     57:                        TAILQ_FOREACH(wp, &w->panes, entry)
1.15      nicm       58:                                server_window_check_content(s, wl, wp);
1.23      nicm       59:
                     60:                        if (!(s->flags & SESSION_UNATTACHED))
                     61:                                w->flags &= ~(WINDOW_BELL|WINDOW_ACTIVITY);
1.1       nicm       62:                }
                     63:        }
                     64: }
                     65:
                     66: /* Check for bell in window. */
                     67: int
1.15      nicm       68: server_window_check_bell(struct session *s, struct winlink *wl)
1.1       nicm       69: {
                     70:        struct client   *c;
1.15      nicm       71:        struct window   *w = wl->window;
1.1       nicm       72:        u_int            i;
                     73:        int              action, visual;
                     74:
1.15      nicm       75:        if (!(w->flags & WINDOW_BELL) || wl->flags & WINLINK_BELL)
                     76:                return (0);
1.23      nicm       77:        if (s->curw != wl || s->flags & SESSION_UNATTACHED)
1.16      nicm       78:                wl->flags |= WINLINK_BELL;
1.24    ! nicm       79:        if (s->flags & SESSION_UNATTACHED)
        !            80:                return (1);
1.1       nicm       81:
1.24    ! nicm       82:        visual = options_get_number(&s->options, "visual-bell");
1.1       nicm       83:        action = options_get_number(&s->options, "bell-action");
1.24    ! nicm       84:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
        !            85:                c = ARRAY_ITEM(&clients, i);
        !            86:                if (c == NULL || c->session != s)
        !            87:                        continue;
        !            88:                if (!visual) {
        !            89:                        tty_bell(&c->tty);
        !            90:                        continue;
1.1       nicm       91:                }
1.24    ! nicm       92:                if (c->session->curw->window == w)
1.1       nicm       93:                        status_message_set(c, "Bell in current window");
1.24    ! nicm       94:                else if (action == BELL_ANY) {
        !            95:                        status_message_set(c, "Bell in window %u",
        !            96:                                winlink_find_by_window(&s->windows, w)->idx);
1.1       nicm       97:                }
                     98:        }
                     99:
                    100:        return (1);
                    101: }
                    102:
                    103: /* Check for activity in window. */
                    104: int
1.15      nicm      105: server_window_check_activity(struct session *s, struct winlink *wl)
1.1       nicm      106: {
                    107:        struct client   *c;
1.15      nicm      108:        struct window   *w = wl->window;
1.1       nicm      109:        u_int            i;
                    110:
1.15      nicm      111:        if (!(w->flags & WINDOW_ACTIVITY) || wl->flags & WINLINK_ACTIVITY)
1.1       nicm      112:                return (0);
1.23      nicm      113:        if (s->curw == wl && !(s->flags & SESSION_UNATTACHED))
1.1       nicm      114:                return (0);
                    115:
1.13      nicm      116:        if (!options_get_number(&w->options, "monitor-activity"))
1.1       nicm      117:                return (0);
                    118:
1.21      nicm      119:        if (options_get_number(&s->options, "bell-on-alert"))
                    120:                ring_bell(s);
1.15      nicm      121:        wl->flags |= WINLINK_ACTIVITY;
1.1       nicm      122:
1.13      nicm      123:        if (options_get_number(&s->options, "visual-activity")) {
1.1       nicm      124:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    125:                        c = ARRAY_ITEM(&clients, i);
                    126:                        if (c == NULL || c->session != s)
                    127:                                continue;
                    128:                        status_message_set(c, "Activity in window %u",
1.18      nicm      129:                            winlink_find_by_window(&s->windows, w)->idx);
                    130:                }
                    131:        }
                    132:
                    133:        return (1);
                    134: }
                    135:
                    136: /* Check for silence in window. */
                    137: int
                    138: server_window_check_silence(struct session *s, struct winlink *wl)
                    139: {
                    140:        struct client   *c;
                    141:        struct window   *w = wl->window;
                    142:        struct timeval   timer;
                    143:        u_int            i;
                    144:        int              silence_interval, timer_difference;
                    145:
                    146:        if (!(w->flags & WINDOW_SILENCE) || wl->flags & WINLINK_SILENCE)
                    147:                return (0);
                    148:
1.23      nicm      149:        if (s->curw == wl && !(s->flags & SESSION_UNATTACHED)) {
1.18      nicm      150:                /*
                    151:                 * Reset the timer for this window if we've focused it.  We
                    152:                 * don't want the timer tripping as soon as we've switched away
                    153:                 * from this window.
                    154:                 */
                    155:                if (gettimeofday(&w->silence_timer, NULL) != 0)
                    156:                        fatal("gettimeofday failed.");
                    157:
                    158:                return (0);
                    159:        }
                    160:
                    161:        silence_interval = options_get_number(&w->options, "monitor-silence");
                    162:        if (silence_interval == 0)
                    163:                return (0);
                    164:
                    165:        if (gettimeofday(&timer, NULL) != 0)
                    166:                fatal("gettimeofday");
                    167:        timer_difference = timer.tv_sec - w->silence_timer.tv_sec;
                    168:        if (timer_difference <= silence_interval)
                    169:                return (0);
1.21      nicm      170:
                    171:        if (options_get_number(&s->options, "bell-on-alert"))
                    172:                ring_bell(s);
1.18      nicm      173:        wl->flags |= WINLINK_SILENCE;
                    174:
                    175:        if (options_get_number(&s->options, "visual-silence")) {
                    176:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    177:                        c = ARRAY_ITEM(&clients, i);
                    178:                        if (c == NULL || c->session != s)
                    179:                                continue;
                    180:                        status_message_set(c, "Silence in window %u",
1.1       nicm      181:                            winlink_find_by_window(&s->windows, w)->idx);
                    182:                }
                    183:        }
                    184:
                    185:        return (1);
                    186: }
                    187:
                    188: /* Check for content change in window. */
                    189: int
                    190: server_window_check_content(
1.15      nicm      191:     struct session *s, struct winlink *wl, struct window_pane *wp)
1.1       nicm      192: {
                    193:        struct client   *c;
1.15      nicm      194:        struct window   *w = wl->window;
1.1       nicm      195:        u_int            i;
                    196:        char            *found, *ptr;
1.13      nicm      197:
1.15      nicm      198:        /* Activity flag must be set for new content. */
                    199:        if (!(w->flags & WINDOW_ACTIVITY) || wl->flags & WINLINK_CONTENT)
1.1       nicm      200:                return (0);
1.23      nicm      201:        if (s->curw == wl && !(s->flags & SESSION_UNATTACHED))
1.1       nicm      202:                return (0);
                    203:
                    204:        ptr = options_get_string(&w->options, "monitor-content");
                    205:        if (ptr == NULL || *ptr == '\0')
                    206:                return (0);
                    207:        if ((found = window_pane_search(wp, ptr, NULL)) == NULL)
                    208:                return (0);
1.13      nicm      209:        xfree(found);
1.1       nicm      210:
1.21      nicm      211:        if (options_get_number(&s->options, "bell-on-alert"))
                    212:                ring_bell(s);
1.15      nicm      213:        wl->flags |= WINLINK_CONTENT;
                    214:
1.13      nicm      215:        if (options_get_number(&s->options, "visual-content")) {
1.1       nicm      216:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    217:                        c = ARRAY_ITEM(&clients, i);
                    218:                        if (c == NULL || c->session != s)
                    219:                                continue;
                    220:                        status_message_set(c, "Content in window %u",
                    221:                            winlink_find_by_window(&s->windows, w)->idx);
                    222:                }
                    223:        }
                    224:
                    225:        return (1);
1.21      nicm      226: }
                    227:
                    228: /* Ring terminal bell. */
                    229: void
                    230: ring_bell(struct session *s)
                    231: {
                    232:        struct client   *c;
                    233:        u_int            i;
                    234:
                    235:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    236:                c = ARRAY_ITEM(&clients, i);
                    237:                if (c != NULL && c->session == s)
1.22      nicm      238:                        tty_bell(&c->tty);
1.21      nicm      239:        }
1.1       nicm      240: }