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

1.36    ! nicm        1: /* $OpenBSD: server-window.c,v 1.35 2015/05/12 15:27:46 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.21      nicm       30: void   ring_bell(struct session *);
1.2       nicm       31:
1.1       nicm       32: /* Window functions that need to happen every loop. */
                     33: void
                     34: server_window_loop(void)
                     35: {
1.31      nicm       36:        struct window   *w;
1.33      nicm       37:        struct session  *s;
1.31      nicm       38:        struct winlink  *wl;
1.1       nicm       39:
1.33      nicm       40:        RB_FOREACH(w, windows, &windows) {
1.19      nicm       41:                RB_FOREACH(s, sessions, &sessions) {
1.33      nicm       42:                        RB_FOREACH(wl, winlinks, &s->windows) {
                     43:                                if (wl->window != w)
                     44:                                        continue;
                     45:
                     46:                                if (server_window_check_bell(s, wl) ||
                     47:                                    server_window_check_activity(s, wl) ||
                     48:                                    server_window_check_silence(s, wl))
                     49:                                        server_status_session(s);
                     50:                        }
1.1       nicm       51:                }
                     52:        }
                     53: }
                     54:
                     55: /* Check for bell in window. */
                     56: int
1.15      nicm       57: server_window_check_bell(struct session *s, struct winlink *wl)
1.1       nicm       58: {
                     59:        struct client   *c;
1.15      nicm       60:        struct window   *w = wl->window;
1.1       nicm       61:        int              action, visual;
                     62:
1.15      nicm       63:        if (!(w->flags & WINDOW_BELL) || wl->flags & WINLINK_BELL)
                     64:                return (0);
1.23      nicm       65:        if (s->curw != wl || s->flags & SESSION_UNATTACHED)
1.16      nicm       66:                wl->flags |= WINLINK_BELL;
1.24      nicm       67:        if (s->flags & SESSION_UNATTACHED)
1.27      nicm       68:                return (0);
1.29      nicm       69:        if (s->curw->window == w)
1.25      nicm       70:                w->flags &= ~WINDOW_BELL;
1.1       nicm       71:
1.24      nicm       72:        visual = options_get_number(&s->options, "visual-bell");
1.1       nicm       73:        action = options_get_number(&s->options, "bell-action");
1.27      nicm       74:        if (action == BELL_NONE)
                     75:                return (0);
1.34      nicm       76:        TAILQ_FOREACH(c, &clients, entry) {
                     77:                if (c->session != s || c->flags & CLIENT_CONTROL)
1.24      nicm       78:                        continue;
                     79:                if (!visual) {
1.35      nicm       80:                        if ((action == BELL_CURRENT &&
                     81:                            c->session->curw->window == w) ||
                     82:                            (action == BELL_OTHER &&
                     83:                            c->session->curw->window != w) ||
                     84:                            action == BELL_ANY)
1.30      nicm       85:                                tty_bell(&c->tty);
1.24      nicm       86:                        continue;
1.1       nicm       87:                }
1.35      nicm       88:                if (action == BELL_CURRENT && c->session->curw->window == w)
1.1       nicm       89:                        status_message_set(c, "Bell in current window");
1.35      nicm       90:                else if (action == BELL_ANY || (action == BELL_OTHER &&
                     91:                    c->session->curw->window != w))
1.32      nicm       92:                        status_message_set(c, "Bell in window %d", wl->idx);
1.1       nicm       93:        }
                     94:
                     95:        return (1);
                     96: }
                     97:
                     98: /* Check for activity in window. */
                     99: int
1.15      nicm      100: server_window_check_activity(struct session *s, struct winlink *wl)
1.1       nicm      101: {
                    102:        struct client   *c;
1.15      nicm      103:        struct window   *w = wl->window;
1.1       nicm      104:
1.29      nicm      105:        if (s->curw->window == w)
1.25      nicm      106:                w->flags &= ~WINDOW_ACTIVITY;
                    107:
1.15      nicm      108:        if (!(w->flags & WINDOW_ACTIVITY) || wl->flags & WINLINK_ACTIVITY)
1.1       nicm      109:                return (0);
1.23      nicm      110:        if (s->curw == wl && !(s->flags & SESSION_UNATTACHED))
1.1       nicm      111:                return (0);
                    112:
1.13      nicm      113:        if (!options_get_number(&w->options, "monitor-activity"))
1.1       nicm      114:                return (0);
                    115:
1.21      nicm      116:        if (options_get_number(&s->options, "bell-on-alert"))
                    117:                ring_bell(s);
1.15      nicm      118:        wl->flags |= WINLINK_ACTIVITY;
1.1       nicm      119:
1.13      nicm      120:        if (options_get_number(&s->options, "visual-activity")) {
1.34      nicm      121:                TAILQ_FOREACH(c, &clients, entry) {
                    122:                        if (c->session != s)
1.1       nicm      123:                                continue;
1.32      nicm      124:                        status_message_set(c, "Activity in window %d", wl->idx);
1.18      nicm      125:                }
                    126:        }
                    127:
                    128:        return (1);
                    129: }
                    130:
                    131: /* Check for silence in window. */
                    132: int
                    133: server_window_check_silence(struct session *s, struct winlink *wl)
                    134: {
                    135:        struct client   *c;
                    136:        struct window   *w = wl->window;
                    137:        struct timeval   timer;
                    138:        int              silence_interval, timer_difference;
                    139:
                    140:        if (!(w->flags & WINDOW_SILENCE) || wl->flags & WINLINK_SILENCE)
                    141:                return (0);
                    142:
1.23      nicm      143:        if (s->curw == wl && !(s->flags & SESSION_UNATTACHED)) {
1.18      nicm      144:                /*
                    145:                 * Reset the timer for this window if we've focused it.  We
                    146:                 * don't want the timer tripping as soon as we've switched away
                    147:                 * from this window.
                    148:                 */
                    149:                if (gettimeofday(&w->silence_timer, NULL) != 0)
1.36    ! nicm      150:                        fatal("gettimeofday failed");
1.18      nicm      151:
                    152:                return (0);
                    153:        }
                    154:
                    155:        silence_interval = options_get_number(&w->options, "monitor-silence");
                    156:        if (silence_interval == 0)
                    157:                return (0);
                    158:
                    159:        if (gettimeofday(&timer, NULL) != 0)
                    160:                fatal("gettimeofday");
                    161:        timer_difference = timer.tv_sec - w->silence_timer.tv_sec;
                    162:        if (timer_difference <= silence_interval)
                    163:                return (0);
1.21      nicm      164:
                    165:        if (options_get_number(&s->options, "bell-on-alert"))
                    166:                ring_bell(s);
1.18      nicm      167:        wl->flags |= WINLINK_SILENCE;
                    168:
                    169:        if (options_get_number(&s->options, "visual-silence")) {
1.34      nicm      170:                TAILQ_FOREACH(c, &clients, entry) {
                    171:                        if (c->session != s)
1.18      nicm      172:                                continue;
1.32      nicm      173:                        status_message_set(c, "Silence in window %d", wl->idx);
1.1       nicm      174:                }
                    175:        }
                    176:
                    177:        return (1);
1.21      nicm      178: }
                    179:
                    180: /* Ring terminal bell. */
                    181: void
                    182: ring_bell(struct session *s)
                    183: {
                    184:        struct client   *c;
                    185:
1.34      nicm      186:        TAILQ_FOREACH(c, &clients, entry) {
                    187:                if (c->session == s && !(c->flags & CLIENT_CONTROL))
1.22      nicm      188:                        tty_bell(&c->tty);
1.21      nicm      189:        }
1.1       nicm      190: }