2009-10-22 19:41:51 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2015 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-10-22 19:41:51 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2024-03-18 03:18:14 +00:00
|
|
|
|
2016-10-19 09:22:07 +00:00
|
|
|
#include <stdlib.h>
|
2024-03-18 03:20:38 +00:00
|
|
|
/*small tests*/
|
2009-10-22 19:41:51 +00:00
|
|
|
#include "tmux.h"
|
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static int alerts_fired;
|
2015-08-29 08:30:54 +00:00
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static void alerts_timer(int, short, void *);
|
|
|
|
static int alerts_enabled(struct window *, int);
|
|
|
|
static void alerts_callback(int, short, void *);
|
|
|
|
static void alerts_reset(struct window *);
|
2015-08-29 08:30:54 +00:00
|
|
|
|
2017-08-23 09:14:21 +00:00
|
|
|
static int alerts_action_applies(struct winlink *, const char *);
|
2016-10-19 09:22:07 +00:00
|
|
|
static int alerts_check_all(struct window *);
|
|
|
|
static int alerts_check_bell(struct window *);
|
|
|
|
static int alerts_check_activity(struct window *);
|
|
|
|
static int alerts_check_silence(struct window *);
|
2017-08-23 09:14:21 +00:00
|
|
|
static void alerts_set_message(struct winlink *, const char *,
|
|
|
|
const char *);
|
2015-08-29 08:30:54 +00:00
|
|
|
|
2016-10-19 09:22:07 +00:00
|
|
|
static TAILQ_HEAD(, window) alerts_list = TAILQ_HEAD_INITIALIZER(alerts_list);
|
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2015-11-18 14:27:44 +00:00
|
|
|
alerts_timer(__unused int fd, __unused short events, void *arg)
|
2015-08-29 08:30:54 +00:00
|
|
|
{
|
|
|
|
struct window *w = arg;
|
|
|
|
|
|
|
|
log_debug("@%u alerts timer expired", w->id);
|
|
|
|
alerts_queue(w, WINDOW_SILENCE);
|
|
|
|
}
|
2009-10-22 19:41:51 +00:00
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2015-11-18 14:27:44 +00:00
|
|
|
alerts_callback(__unused int fd, __unused short events, __unused void *arg)
|
2009-10-22 19:41:51 +00:00
|
|
|
{
|
2016-10-19 09:22:07 +00:00
|
|
|
struct window *w, *w1;
|
|
|
|
int alerts;
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2016-10-19 09:22:07 +00:00
|
|
|
TAILQ_FOREACH_SAFE(w, &alerts_list, alerts_entry, w1) {
|
|
|
|
alerts = alerts_check_all(w);
|
|
|
|
log_debug("@%u alerts check, alerts %#x", w->id, alerts);
|
2015-08-29 08:30:54 +00:00
|
|
|
|
2016-10-19 09:22:07 +00:00
|
|
|
w->alerts_queued = 0;
|
|
|
|
TAILQ_REMOVE(&alerts_list, w, alerts_entry);
|
2016-11-01 09:07:18 +00:00
|
|
|
|
|
|
|
w->flags &= ~WINDOW_ALERTFLAGS;
|
2017-04-28 19:13:55 +00:00
|
|
|
window_remove_ref(w, __func__);
|
2009-10-22 19:41:51 +00:00
|
|
|
}
|
2015-08-29 08:30:54 +00:00
|
|
|
alerts_fired = 0;
|
2009-10-22 19:41:51 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 09:14:21 +00:00
|
|
|
static int
|
|
|
|
alerts_action_applies(struct winlink *wl, const char *name)
|
|
|
|
{
|
|
|
|
int action;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* {bell,activity,silence}-action determines when to alert: none means
|
|
|
|
* nothing happens, current means only do something for the current
|
|
|
|
* window and other means only for windows other than the current.
|
|
|
|
*/
|
|
|
|
|
|
|
|
action = options_get_number(wl->session->options, name);
|
|
|
|
if (action == ALERT_ANY)
|
|
|
|
return (1);
|
|
|
|
if (action == ALERT_CURRENT)
|
|
|
|
return (wl == wl->session->curw);
|
|
|
|
if (action == ALERT_OTHER)
|
|
|
|
return (wl != wl->session->curw);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static int
|
2016-10-19 09:22:07 +00:00
|
|
|
alerts_check_all(struct window *w)
|
2015-12-07 09:47:41 +00:00
|
|
|
{
|
|
|
|
int alerts;
|
|
|
|
|
2017-08-23 09:14:21 +00:00
|
|
|
alerts = alerts_check_bell(w);
|
2016-10-19 09:22:07 +00:00
|
|
|
alerts |= alerts_check_activity(w);
|
|
|
|
alerts |= alerts_check_silence(w);
|
2015-12-07 09:47:41 +00:00
|
|
|
return (alerts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
alerts_check_session(struct session *s)
|
|
|
|
{
|
|
|
|
struct winlink *wl;
|
|
|
|
|
|
|
|
RB_FOREACH(wl, winlinks, &s->windows)
|
2016-10-19 09:22:07 +00:00
|
|
|
alerts_check_all(wl->window);
|
2015-12-07 09:47:41 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static int
|
2015-08-29 08:30:54 +00:00
|
|
|
alerts_enabled(struct window *w, int flags)
|
|
|
|
{
|
2017-08-17 08:37:38 +00:00
|
|
|
if (flags & WINDOW_BELL) {
|
|
|
|
if (options_get_number(w->options, "monitor-bell"))
|
|
|
|
return (1);
|
|
|
|
}
|
2015-08-29 08:30:54 +00:00
|
|
|
if (flags & WINDOW_ACTIVITY) {
|
2015-10-27 15:58:42 +00:00
|
|
|
if (options_get_number(w->options, "monitor-activity"))
|
2015-08-29 08:30:54 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (flags & WINDOW_SILENCE) {
|
2015-10-27 15:58:42 +00:00
|
|
|
if (options_get_number(w->options, "monitor-silence") != 0)
|
2015-08-29 08:30:54 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
alerts_reset_all(void)
|
|
|
|
{
|
|
|
|
struct window *w;
|
|
|
|
|
|
|
|
RB_FOREACH(w, windows, &windows)
|
|
|
|
alerts_reset(w);
|
|
|
|
}
|
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2015-08-29 08:30:54 +00:00
|
|
|
alerts_reset(struct window *w)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
2017-09-22 09:04:46 +00:00
|
|
|
if (!event_initialized(&w->alerts_timer))
|
|
|
|
evtimer_set(&w->alerts_timer, alerts_timer, w);
|
|
|
|
|
2015-08-29 08:30:54 +00:00
|
|
|
w->flags &= ~WINDOW_SILENCE;
|
2017-09-22 09:04:46 +00:00
|
|
|
event_del(&w->alerts_timer);
|
2015-08-29 08:30:54 +00:00
|
|
|
|
|
|
|
timerclear(&tv);
|
2015-10-27 15:58:42 +00:00
|
|
|
tv.tv_sec = options_get_number(w->options, "monitor-silence");
|
2015-08-29 08:30:54 +00:00
|
|
|
|
|
|
|
log_debug("@%u alerts timer reset %u", w->id, (u_int)tv.tv_sec);
|
|
|
|
if (tv.tv_sec != 0)
|
|
|
|
event_add(&w->alerts_timer, &tv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
alerts_queue(struct window *w, int flags)
|
|
|
|
{
|
2017-07-26 16:16:25 +00:00
|
|
|
alerts_reset(w);
|
2015-09-21 09:34:52 +00:00
|
|
|
|
2016-05-11 20:56:58 +00:00
|
|
|
if ((w->flags & flags) != flags) {
|
2015-11-19 14:55:25 +00:00
|
|
|
w->flags |= flags;
|
|
|
|
log_debug("@%u alerts flags added %#x", w->id, flags);
|
2016-05-11 20:56:58 +00:00
|
|
|
}
|
2015-11-19 14:55:25 +00:00
|
|
|
|
2017-04-28 19:10:48 +00:00
|
|
|
if (alerts_enabled(w, flags)) {
|
|
|
|
if (!w->alerts_queued) {
|
|
|
|
w->alerts_queued = 1;
|
|
|
|
TAILQ_INSERT_TAIL(&alerts_list, w, alerts_entry);
|
2017-04-28 19:13:55 +00:00
|
|
|
window_add_ref(w, __func__);
|
2017-04-28 19:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!alerts_fired) {
|
|
|
|
log_debug("alerts check queued (by @%u)", w->id);
|
|
|
|
event_once(-1, EV_TIMEOUT, alerts_callback, NULL, NULL);
|
|
|
|
alerts_fired = 1;
|
|
|
|
}
|
2015-08-29 08:30:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static int
|
2016-10-19 09:22:07 +00:00
|
|
|
alerts_check_bell(struct window *w)
|
2009-10-22 19:41:51 +00:00
|
|
|
{
|
2016-10-19 09:22:07 +00:00
|
|
|
struct winlink *wl;
|
|
|
|
struct session *s;
|
2009-10-22 19:41:51 +00:00
|
|
|
|
2016-10-19 09:22:07 +00:00
|
|
|
if (~w->flags & WINDOW_BELL)
|
2009-10-22 19:41:51 +00:00
|
|
|
return (0);
|
2017-08-17 08:37:38 +00:00
|
|
|
if (!options_get_number(w->options, "monitor-bell"))
|
|
|
|
return (0);
|
2009-10-22 19:41:51 +00:00
|
|
|
|
2016-10-19 09:22:07 +00:00
|
|
|
TAILQ_FOREACH(wl, &w->winlinks, wentry)
|
|
|
|
wl->session->flags &= ~SESSION_ALERTED;
|
2015-08-29 08:30:54 +00:00
|
|
|
|
2016-10-19 09:22:07 +00:00
|
|
|
TAILQ_FOREACH(wl, &w->winlinks, wentry) {
|
2017-08-23 09:16:39 +00:00
|
|
|
/*
|
|
|
|
* Bells are allowed even if there is an existing bell (so do
|
|
|
|
* not check WINLINK_BELL).
|
|
|
|
*/
|
2016-10-19 09:22:07 +00:00
|
|
|
s = wl->session;
|
2020-08-19 07:15:42 +00:00
|
|
|
if (s->curw != wl || s->attached == 0) {
|
2016-10-19 09:22:07 +00:00
|
|
|
wl->flags |= WINLINK_BELL;
|
2017-12-28 12:10:50 +00:00
|
|
|
server_status_session(s);
|
|
|
|
}
|
2017-08-23 09:14:21 +00:00
|
|
|
if (!alerts_action_applies(wl, "bell-action"))
|
|
|
|
continue;
|
|
|
|
notify_winlink("alert-bell", wl);
|
2016-10-19 09:22:07 +00:00
|
|
|
|
|
|
|
if (s->flags & SESSION_ALERTED)
|
2012-06-18 10:58:44 +00:00
|
|
|
continue;
|
2016-10-19 09:22:07 +00:00
|
|
|
s->flags |= SESSION_ALERTED;
|
|
|
|
|
2017-08-23 09:14:21 +00:00
|
|
|
alerts_set_message(wl, "Bell", "visual-bell");
|
2009-10-22 19:41:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-29 08:30:54 +00:00
|
|
|
return (WINDOW_BELL);
|
2009-10-22 19:41:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static int
|
2016-10-19 09:22:07 +00:00
|
|
|
alerts_check_activity(struct window *w)
|
2009-10-22 19:41:51 +00:00
|
|
|
{
|
2016-10-19 09:22:07 +00:00
|
|
|
struct winlink *wl;
|
|
|
|
struct session *s;
|
2012-07-08 07:27:32 +00:00
|
|
|
|
2016-10-19 09:22:07 +00:00
|
|
|
if (~w->flags & WINDOW_ACTIVITY)
|
2009-10-22 19:41:51 +00:00
|
|
|
return (0);
|
2015-10-27 15:58:42 +00:00
|
|
|
if (!options_get_number(w->options, "monitor-activity"))
|
2009-10-22 19:41:51 +00:00
|
|
|
return (0);
|
|
|
|
|
2016-10-19 09:22:07 +00:00
|
|
|
TAILQ_FOREACH(wl, &w->winlinks, wentry)
|
|
|
|
wl->session->flags &= ~SESSION_ALERTED;
|
2009-10-22 19:41:51 +00:00
|
|
|
|
2016-10-19 09:22:07 +00:00
|
|
|
TAILQ_FOREACH(wl, &w->winlinks, wentry) {
|
|
|
|
if (wl->flags & WINLINK_ACTIVITY)
|
|
|
|
continue;
|
|
|
|
s = wl->session;
|
2020-08-19 07:15:42 +00:00
|
|
|
if (s->curw != wl || s->attached == 0) {
|
2017-07-26 16:14:08 +00:00
|
|
|
wl->flags |= WINLINK_ACTIVITY;
|
2017-12-28 12:10:50 +00:00
|
|
|
server_status_session(s);
|
|
|
|
}
|
2017-08-23 09:14:21 +00:00
|
|
|
if (!alerts_action_applies(wl, "activity-action"))
|
|
|
|
continue;
|
|
|
|
notify_winlink("alert-activity", wl);
|
2016-10-19 09:22:07 +00:00
|
|
|
|
|
|
|
if (s->flags & SESSION_ALERTED)
|
|
|
|
continue;
|
|
|
|
s->flags |= SESSION_ALERTED;
|
|
|
|
|
2017-08-23 09:14:21 +00:00
|
|
|
alerts_set_message(wl, "Activity", "visual-activity");
|
2010-12-06 22:51:02 +00:00
|
|
|
}
|
|
|
|
|
2015-08-29 08:30:54 +00:00
|
|
|
return (WINDOW_ACTIVITY);
|
2010-12-06 22:51:02 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static int
|
2016-10-19 09:22:07 +00:00
|
|
|
alerts_check_silence(struct window *w)
|
2010-12-06 22:51:02 +00:00
|
|
|
{
|
2016-10-19 09:22:07 +00:00
|
|
|
struct winlink *wl;
|
|
|
|
struct session *s;
|
2010-12-06 22:51:02 +00:00
|
|
|
|
2016-10-19 09:22:07 +00:00
|
|
|
if (~w->flags & WINDOW_SILENCE)
|
2010-12-06 22:51:02 +00:00
|
|
|
return (0);
|
2017-07-26 16:14:08 +00:00
|
|
|
if (options_get_number(w->options, "monitor-silence") == 0)
|
2010-12-06 22:51:02 +00:00
|
|
|
return (0);
|
2011-07-03 19:07:54 +00:00
|
|
|
|
2016-10-19 09:22:07 +00:00
|
|
|
TAILQ_FOREACH(wl, &w->winlinks, wentry)
|
|
|
|
wl->session->flags &= ~SESSION_ALERTED;
|
2010-12-06 22:51:02 +00:00
|
|
|
|
2016-10-19 09:22:07 +00:00
|
|
|
TAILQ_FOREACH(wl, &w->winlinks, wentry) {
|
|
|
|
if (wl->flags & WINLINK_SILENCE)
|
|
|
|
continue;
|
|
|
|
s = wl->session;
|
2020-08-19 07:15:42 +00:00
|
|
|
if (s->curw != wl || s->attached == 0) {
|
2017-07-26 16:14:08 +00:00
|
|
|
wl->flags |= WINLINK_SILENCE;
|
2017-12-28 12:10:50 +00:00
|
|
|
server_status_session(s);
|
|
|
|
}
|
2017-08-23 09:14:21 +00:00
|
|
|
if (!alerts_action_applies(wl, "silence-action"))
|
|
|
|
continue;
|
|
|
|
notify_winlink("alert-silence", wl);
|
2016-10-19 09:22:07 +00:00
|
|
|
|
|
|
|
if (s->flags & SESSION_ALERTED)
|
|
|
|
continue;
|
|
|
|
s->flags |= SESSION_ALERTED;
|
|
|
|
|
2017-08-23 09:14:21 +00:00
|
|
|
alerts_set_message(wl, "Silence", "visual-silence");
|
2009-10-22 19:41:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-29 08:30:54 +00:00
|
|
|
return (WINDOW_SILENCE);
|
2009-10-22 19:41:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 09:22:07 +00:00
|
|
|
static void
|
2017-08-23 09:14:21 +00:00
|
|
|
alerts_set_message(struct winlink *wl, const char *type, const char *option)
|
2016-10-19 09:22:07 +00:00
|
|
|
{
|
|
|
|
struct client *c;
|
2017-08-23 09:14:21 +00:00
|
|
|
int visual;
|
2017-07-26 16:14:08 +00:00
|
|
|
|
|
|
|
/*
|
2017-08-16 11:46:08 +00:00
|
|
|
* We have found an alert (bell, activity or silence), so we need to
|
|
|
|
* pass it on to the user. For each client attached to this session,
|
2017-08-23 09:14:21 +00:00
|
|
|
* decide whether a bell, message or both is needed.
|
2017-07-26 16:14:08 +00:00
|
|
|
*
|
|
|
|
* If visual-{bell,activity,silence} is on, then a message is
|
|
|
|
* substituted for a bell; if it is off, a bell is sent as normal; both
|
2017-08-23 09:14:21 +00:00
|
|
|
* mean both a bell and message is sent.
|
2017-07-26 16:14:08 +00:00
|
|
|
*/
|
|
|
|
|
2017-08-23 09:14:21 +00:00
|
|
|
visual = options_get_number(wl->session->options, option);
|
2016-10-19 09:22:07 +00:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
2017-08-23 09:14:21 +00:00
|
|
|
if (c->session != wl->session || c->flags & CLIENT_CONTROL)
|
2017-07-26 16:14:08 +00:00
|
|
|
continue;
|
2011-07-03 19:07:54 +00:00
|
|
|
|
2017-07-26 16:14:08 +00:00
|
|
|
if (visual == VISUAL_OFF || visual == VISUAL_BOTH)
|
2015-09-02 17:43:25 +00:00
|
|
|
tty_putcode(&c->tty, TTYC_BEL);
|
2017-07-26 16:14:08 +00:00
|
|
|
if (visual == VISUAL_OFF)
|
|
|
|
continue;
|
2021-04-12 09:36:12 +00:00
|
|
|
if (c->session->curw == wl) {
|
|
|
|
status_message_set(c, -1, 1, 0, "%s in current window",
|
|
|
|
type);
|
|
|
|
} else {
|
|
|
|
status_message_set(c, -1, 1, 0, "%s in window %d", type,
|
2020-05-16 15:54:20 +00:00
|
|
|
wl->idx);
|
|
|
|
}
|
2011-07-03 19:07:54 +00:00
|
|
|
}
|
|
|
|
}
|