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

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