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

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