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

Annotation of src/usr.bin/tmux/alerts.c, Revision 1.25

1.25    ! nicm        1: /* $OpenBSD: alerts.c,v 1.24 2017/08/17 08:37:38 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.10      nicm        4:  * Copyright (c) 2015 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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:
                     21: #include <event.h>
1.14      nicm       22: #include <stdlib.h>
1.1       nicm       23:
                     24: #include "tmux.h"
                     25:
1.12      nicm       26: static int     alerts_fired;
1.1       nicm       27:
1.12      nicm       28: static void    alerts_timer(int, short, void *);
                     29: static int     alerts_enabled(struct window *, int);
                     30: static void    alerts_callback(int, short, void *);
                     31: static void    alerts_reset(struct window *);
                     32:
1.25    ! nicm       33: static int     alerts_action_applies(struct winlink *, const char *);
1.14      nicm       34: static int     alerts_check_all(struct window *);
                     35: static int     alerts_check_bell(struct window *);
                     36: static int     alerts_check_activity(struct window *);
                     37: static int     alerts_check_silence(struct window *);
1.25    ! nicm       38: static void    alerts_set_message(struct winlink *, const char *,
        !            39:                    const char *);
1.1       nicm       40:
1.14      nicm       41: static TAILQ_HEAD(, window) alerts_list = TAILQ_HEAD_INITIALIZER(alerts_list);
                     42:
1.12      nicm       43: static void
1.5       nicm       44: alerts_timer(__unused int fd, __unused short events, void *arg)
1.1       nicm       45: {
                     46:        struct window   *w = arg;
                     47:
                     48:        log_debug("@%u alerts timer expired", w->id);
                     49:        alerts_queue(w, WINDOW_SILENCE);
                     50: }
                     51:
1.12      nicm       52: static void
1.5       nicm       53: alerts_callback(__unused int fd, __unused short events, __unused void *arg)
1.1       nicm       54: {
1.14      nicm       55:        struct window   *w, *w1;
                     56:        int              alerts;
1.1       nicm       57:
1.14      nicm       58:        TAILQ_FOREACH_SAFE(w, &alerts_list, alerts_entry, w1) {
                     59:                alerts = alerts_check_all(w);
                     60:                log_debug("@%u alerts check, alerts %#x", w->id, alerts);
                     61:
                     62:                w->alerts_queued = 0;
                     63:                TAILQ_REMOVE(&alerts_list, w, alerts_entry);
1.16      nicm       64:
                     65:                w->flags &= ~WINDOW_ALERTFLAGS;
1.19      nicm       66:                window_remove_ref(w, __func__);
1.1       nicm       67:        }
                     68:        alerts_fired = 0;
                     69: }
                     70:
1.12      nicm       71: static int
1.25    ! nicm       72: alerts_action_applies(struct winlink *wl, const char *name)
        !            73: {
        !            74:        int     action;
        !            75:
        !            76:        /*
        !            77:         * {bell,activity,silence}-action determines when to alert: none means
        !            78:         * nothing happens, current means only do something for the current
        !            79:         * window and other means only for windows other than the current.
        !            80:         */
        !            81:
        !            82:        action = options_get_number(wl->session->options, name);
        !            83:        if (action == ALERT_ANY)
        !            84:                return (1);
        !            85:        if (action == ALERT_CURRENT)
        !            86:                return (wl == wl->session->curw);
        !            87:        if (action == ALERT_OTHER)
        !            88:                return (wl != wl->session->curw);
        !            89:        return (0);
        !            90: }
        !            91:
        !            92: static int
1.14      nicm       93: alerts_check_all(struct window *w)
1.8       nicm       94: {
                     95:        int     alerts;
                     96:
1.25    ! nicm       97:        alerts  = alerts_check_bell(w);
1.14      nicm       98:        alerts |= alerts_check_activity(w);
                     99:        alerts |= alerts_check_silence(w);
1.8       nicm      100:        return (alerts);
                    101: }
                    102:
                    103: void
                    104: alerts_check_session(struct session *s)
                    105: {
                    106:        struct winlink  *wl;
                    107:
                    108:        RB_FOREACH(wl, winlinks, &s->windows)
1.14      nicm      109:                alerts_check_all(wl->window);
1.8       nicm      110: }
                    111:
1.12      nicm      112: static int
1.1       nicm      113: alerts_enabled(struct window *w, int flags)
                    114: {
1.24      nicm      115:        if (flags & WINDOW_BELL) {
                    116:                if (options_get_number(w->options, "monitor-bell"))
                    117:                        return (1);
                    118:        }
1.1       nicm      119:        if (flags & WINDOW_ACTIVITY) {
1.4       nicm      120:                if (options_get_number(w->options, "monitor-activity"))
1.1       nicm      121:                        return (1);
                    122:        }
                    123:        if (flags & WINDOW_SILENCE) {
1.4       nicm      124:                if (options_get_number(w->options, "monitor-silence") != 0)
1.1       nicm      125:                        return (1);
                    126:        }
                    127:        return (0);
                    128: }
                    129:
                    130: void
                    131: alerts_reset_all(void)
                    132: {
                    133:        struct window   *w;
                    134:
                    135:        RB_FOREACH(w, windows, &windows)
                    136:                alerts_reset(w);
                    137: }
                    138:
1.12      nicm      139: static void
1.1       nicm      140: alerts_reset(struct window *w)
                    141: {
                    142:        struct timeval  tv;
                    143:
                    144:        w->flags &= ~WINDOW_SILENCE;
                    145:        event_del(&w->alerts_timer);
                    146:
                    147:        timerclear(&tv);
1.4       nicm      148:        tv.tv_sec = options_get_number(w->options, "monitor-silence");
1.1       nicm      149:
                    150:        log_debug("@%u alerts timer reset %u", w->id, (u_int)tv.tv_sec);
                    151:        if (tv.tv_sec != 0)
                    152:                event_add(&w->alerts_timer, &tv);
                    153: }
                    154:
                    155: void
                    156: alerts_queue(struct window *w, int flags)
                    157: {
1.22      nicm      158:        alerts_reset(w);
1.3       nicm      159:
1.1       nicm      160:        if (!event_initialized(&w->alerts_timer))
                    161:                evtimer_set(&w->alerts_timer, alerts_timer, w);
                    162:
1.11      nicm      163:        if ((w->flags & flags) != flags) {
1.6       nicm      164:                w->flags |= flags;
                    165:                log_debug("@%u alerts flags added %#x", w->id, flags);
1.11      nicm      166:        }
1.6       nicm      167:
1.18      nicm      168:        if (alerts_enabled(w, flags)) {
                    169:                if (!w->alerts_queued) {
                    170:                        w->alerts_queued = 1;
                    171:                        TAILQ_INSERT_TAIL(&alerts_list, w, alerts_entry);
1.19      nicm      172:                        window_add_ref(w, __func__);
1.18      nicm      173:                }
                    174:
                    175:                if (!alerts_fired) {
                    176:                        log_debug("alerts check queued (by @%u)", w->id);
                    177:                        event_once(-1, EV_TIMEOUT, alerts_callback, NULL, NULL);
                    178:                        alerts_fired = 1;
                    179:                }
1.1       nicm      180:        }
                    181: }
                    182:
1.12      nicm      183: static int
1.14      nicm      184: alerts_check_bell(struct window *w)
1.1       nicm      185: {
1.14      nicm      186:        struct winlink  *wl;
                    187:        struct session  *s;
1.1       nicm      188:
1.14      nicm      189:        if (~w->flags & WINDOW_BELL)
1.24      nicm      190:                return (0);
                    191:        if (!options_get_number(w->options, "monitor-bell"))
1.1       nicm      192:                return (0);
                    193:
1.14      nicm      194:        TAILQ_FOREACH(wl, &w->winlinks, wentry)
                    195:                wl->session->flags &= ~SESSION_ALERTED;
1.1       nicm      196:
1.14      nicm      197:        TAILQ_FOREACH(wl, &w->winlinks, wentry) {
                    198:                if (wl->flags & WINLINK_BELL)
1.1       nicm      199:                        continue;
1.14      nicm      200:                s = wl->session;
1.25    ! nicm      201:                if (s->curw != wl)
1.14      nicm      202:                        wl->flags |= WINLINK_BELL;
1.25    ! nicm      203:                if (!alerts_action_applies(wl, "bell-action"))
        !           204:                        continue;
        !           205:                notify_winlink("alert-bell", wl);
1.14      nicm      206:
                    207:                if (s->flags & SESSION_ALERTED)
1.1       nicm      208:                        continue;
1.14      nicm      209:                s->flags |= SESSION_ALERTED;
                    210:
1.25    ! nicm      211:                alerts_set_message(wl, "Bell", "visual-bell");
1.1       nicm      212:        }
                    213:
                    214:        return (WINDOW_BELL);
                    215: }
                    216:
1.12      nicm      217: static int
1.14      nicm      218: alerts_check_activity(struct window *w)
1.1       nicm      219: {
1.14      nicm      220:        struct winlink  *wl;
                    221:        struct session  *s;
1.1       nicm      222:
1.14      nicm      223:        if (~w->flags & WINDOW_ACTIVITY)
1.1       nicm      224:                return (0);
1.4       nicm      225:        if (!options_get_number(w->options, "monitor-activity"))
1.1       nicm      226:                return (0);
                    227:
1.14      nicm      228:        TAILQ_FOREACH(wl, &w->winlinks, wentry)
                    229:                wl->session->flags &= ~SESSION_ALERTED;
1.1       nicm      230:
1.14      nicm      231:        TAILQ_FOREACH(wl, &w->winlinks, wentry) {
                    232:                if (wl->flags & WINLINK_ACTIVITY)
                    233:                        continue;
                    234:                s = wl->session;
1.25    ! nicm      235:                if (s->curw != wl)
1.21      nicm      236:                        wl->flags |= WINLINK_ACTIVITY;
1.25    ! nicm      237:                if (!alerts_action_applies(wl, "activity-action"))
        !           238:                        continue;
        !           239:                notify_winlink("alert-activity", wl);
1.14      nicm      240:
                    241:                if (s->flags & SESSION_ALERTED)
                    242:                        continue;
                    243:                s->flags |= SESSION_ALERTED;
                    244:
1.25    ! nicm      245:                alerts_set_message(wl, "Activity", "visual-activity");
1.1       nicm      246:        }
                    247:
                    248:        return (WINDOW_ACTIVITY);
                    249: }
                    250:
1.12      nicm      251: static int
1.14      nicm      252: alerts_check_silence(struct window *w)
1.1       nicm      253: {
1.14      nicm      254:        struct winlink  *wl;
                    255:        struct session  *s;
1.1       nicm      256:
1.14      nicm      257:        if (~w->flags & WINDOW_SILENCE)
1.1       nicm      258:                return (0);
1.21      nicm      259:        if (options_get_number(w->options, "monitor-silence") == 0)
1.1       nicm      260:                return (0);
                    261:
1.14      nicm      262:        TAILQ_FOREACH(wl, &w->winlinks, wentry)
                    263:                wl->session->flags &= ~SESSION_ALERTED;
                    264:
                    265:        TAILQ_FOREACH(wl, &w->winlinks, wentry) {
                    266:                if (wl->flags & WINLINK_SILENCE)
                    267:                        continue;
                    268:                s = wl->session;
1.25    ! nicm      269:                if (s->curw != wl)
1.21      nicm      270:                        wl->flags |= WINLINK_SILENCE;
1.25    ! nicm      271:                if (!alerts_action_applies(wl, "silence-action"))
        !           272:                        continue;
        !           273:                notify_winlink("alert-silence", wl);
1.14      nicm      274:
                    275:                if (s->flags & SESSION_ALERTED)
                    276:                        continue;
                    277:                s->flags |= SESSION_ALERTED;
1.1       nicm      278:
1.25    ! nicm      279:                alerts_set_message(wl, "Silence", "visual-silence");
1.1       nicm      280:        }
                    281:
                    282:        return (WINDOW_SILENCE);
1.14      nicm      283: }
                    284:
                    285: static void
1.25    ! nicm      286: alerts_set_message(struct winlink *wl, const char *type, const char *option)
1.14      nicm      287: {
                    288:        struct client   *c;
1.25    ! nicm      289:        int              visual;
1.14      nicm      290:
1.21      nicm      291:        /*
1.23      nicm      292:         * We have found an alert (bell, activity or silence), so we need to
                    293:         * pass it on to the user. For each client attached to this session,
1.25    ! nicm      294:         * decide whether a bell, message or both is needed.
1.21      nicm      295:         *
                    296:         * If visual-{bell,activity,silence} is on, then a message is
                    297:         * substituted for a bell; if it is off, a bell is sent as normal; both
1.25    ! nicm      298:         * mean both a bell and message is sent.
1.21      nicm      299:         */
1.14      nicm      300:
1.25    ! nicm      301:        visual = options_get_number(wl->session->options, option);
1.14      nicm      302:        TAILQ_FOREACH(c, &clients, entry) {
1.25    ! nicm      303:                if (c->session != wl->session || c->flags & CLIENT_CONTROL)
1.21      nicm      304:                        continue;
1.1       nicm      305:
1.21      nicm      306:                if (visual == VISUAL_OFF || visual == VISUAL_BOTH)
1.2       nicm      307:                        tty_putcode(&c->tty, TTYC_BEL);
1.21      nicm      308:                if (visual == VISUAL_OFF)
                    309:                        continue;
1.25    ! nicm      310:                if (c->session->curw == wl)
1.21      nicm      311:                        status_message_set(c, "%s in current window", type);
                    312:                else
                    313:                        status_message_set(c, "%s in window %d", type, wl->idx);
1.1       nicm      314:        }
                    315: }