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>
|
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
#include <event.h>
|
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
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static void alerts_run_hook(struct session *, struct winlink *, int);
|
|
|
|
static int alerts_check_all(struct session *, struct winlink *);
|
|
|
|
static int alerts_check_bell(struct session *, struct winlink *);
|
|
|
|
static int alerts_check_activity(struct session *, struct winlink *);
|
|
|
|
static int alerts_check_silence(struct session *, struct winlink *);
|
|
|
|
static void alerts_ring_bell(struct session *);
|
2015-08-29 08:30:54 +00:00
|
|
|
|
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_reset(w);
|
|
|
|
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
|
|
|
{
|
2014-04-17 07:36:45 +00:00
|
|
|
struct window *w;
|
|
|
|
struct session *s;
|
2015-04-22 15:30:11 +00:00
|
|
|
struct winlink *wl;
|
2015-08-29 08:30:54 +00:00
|
|
|
int flags, alerts;
|
2009-10-22 19:41:51 +00:00
|
|
|
|
2015-04-22 15:30:11 +00:00
|
|
|
RB_FOREACH(w, windows, &windows) {
|
2010-12-21 22:37:59 +00:00
|
|
|
RB_FOREACH(s, sessions, &sessions) {
|
2015-04-22 15:30:11 +00:00
|
|
|
RB_FOREACH(wl, winlinks, &s->windows) {
|
|
|
|
if (wl->window != w)
|
|
|
|
continue;
|
2015-08-29 08:30:54 +00:00
|
|
|
flags = w->flags;
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2015-12-07 09:47:41 +00:00
|
|
|
alerts = alerts_check_all(s, wl);
|
2015-08-29 08:30:54 +00:00
|
|
|
|
|
|
|
log_debug("%s:%d @%u alerts check, alerts %#x, "
|
|
|
|
"flags %#x", s->name, wl->idx, w->id,
|
|
|
|
alerts, flags);
|
2015-04-22 15:30:11 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2016-01-16 00:36:53 +00:00
|
|
|
alerts_run_hook(struct session *s, struct winlink *wl, int flags)
|
|
|
|
{
|
|
|
|
if (flags & WINDOW_BELL)
|
2016-10-16 22:18:04 +00:00
|
|
|
notify_winlink("alert-bell", s, wl);
|
2016-01-16 00:36:53 +00:00
|
|
|
if (flags & WINDOW_SILENCE)
|
2016-10-16 22:18:04 +00:00
|
|
|
notify_winlink("alert-silence", s, wl);
|
2016-01-16 00:36:53 +00:00
|
|
|
if (flags & WINDOW_ACTIVITY)
|
2016-10-16 22:18:04 +00:00
|
|
|
notify_winlink("alert-activity", s, wl);
|
2016-01-16 00:36:53 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static int
|
2015-12-07 09:47:41 +00:00
|
|
|
alerts_check_all(struct session *s, struct winlink *wl)
|
|
|
|
{
|
|
|
|
int alerts;
|
|
|
|
|
|
|
|
alerts = alerts_check_bell(s, wl);
|
|
|
|
alerts |= alerts_check_activity(s, wl);
|
|
|
|
alerts |= alerts_check_silence(s, wl);
|
2016-01-16 00:36:53 +00:00
|
|
|
if (alerts != 0) {
|
|
|
|
alerts_run_hook(s, wl, alerts);
|
2015-12-07 09:47:41 +00:00
|
|
|
server_status_session(s);
|
2016-01-16 00:36:53 +00:00
|
|
|
}
|
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)
|
|
|
|
alerts_check_all(s, wl);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2015-11-20 16:33:46 +00:00
|
|
|
if (flags & WINDOW_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;
|
|
|
|
|
|
|
|
w->flags &= ~WINDOW_SILENCE;
|
|
|
|
event_del(&w->alerts_timer);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2015-09-21 09:34:52 +00:00
|
|
|
if (w->flags & WINDOW_ACTIVITY)
|
|
|
|
alerts_reset(w);
|
|
|
|
|
2015-08-29 08:30:54 +00:00
|
|
|
if (!event_initialized(&w->alerts_timer))
|
|
|
|
evtimer_set(&w->alerts_timer, alerts_timer, w);
|
|
|
|
|
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
|
|
|
|
2016-05-11 20:56:58 +00:00
|
|
|
if (!alerts_fired && alerts_enabled(w, flags)) {
|
|
|
|
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
|
2015-08-29 08:30:54 +00:00
|
|
|
alerts_check_bell(struct session *s, struct winlink *wl)
|
2009-10-22 19:41:51 +00:00
|
|
|
{
|
|
|
|
struct client *c;
|
2010-06-21 01:27:46 +00:00
|
|
|
struct window *w = wl->window;
|
2009-10-22 19:41:51 +00:00
|
|
|
int action, visual;
|
|
|
|
|
2015-12-07 09:47:41 +00:00
|
|
|
if (!(w->flags & WINDOW_BELL))
|
2009-10-22 19:41:51 +00:00
|
|
|
return (0);
|
2015-12-07 09:47:41 +00:00
|
|
|
if (s->curw != wl) {
|
2010-07-19 21:13:03 +00:00
|
|
|
wl->flags |= WINLINK_BELL;
|
2015-12-07 09:47:41 +00:00
|
|
|
w->flags &= ~WINDOW_BELL;
|
|
|
|
}
|
2014-02-14 12:44:45 +00:00
|
|
|
if (s->curw->window == w)
|
2012-07-08 07:27:32 +00:00
|
|
|
w->flags &= ~WINDOW_BELL;
|
2009-10-22 19:41:51 +00:00
|
|
|
|
2015-10-27 15:58:42 +00:00
|
|
|
action = options_get_number(s->options, "bell-action");
|
2012-10-25 11:16:53 +00:00
|
|
|
if (action == BELL_NONE)
|
|
|
|
return (0);
|
2015-08-29 08:30:54 +00:00
|
|
|
|
2015-10-27 15:58:42 +00:00
|
|
|
visual = options_get_number(s->options, "visual-bell");
|
2015-04-24 23:17:11 +00:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->session != s || c->flags & CLIENT_CONTROL)
|
2012-06-18 10:58:44 +00:00
|
|
|
continue;
|
|
|
|
if (!visual) {
|
2015-05-12 15:27:46 +00:00
|
|
|
if ((action == BELL_CURRENT &&
|
|
|
|
c->session->curw->window == w) ||
|
|
|
|
(action == BELL_OTHER &&
|
|
|
|
c->session->curw->window != w) ||
|
|
|
|
action == BELL_ANY)
|
2015-09-02 17:43:25 +00:00
|
|
|
tty_putcode(&c->tty, TTYC_BEL);
|
2012-06-18 10:58:44 +00:00
|
|
|
continue;
|
2009-10-22 19:41:51 +00:00
|
|
|
}
|
2015-05-12 15:27:46 +00:00
|
|
|
if (action == BELL_CURRENT && c->session->curw->window == w)
|
2009-10-22 19:41:51 +00:00
|
|
|
status_message_set(c, "Bell in current window");
|
2015-05-12 15:27:46 +00:00
|
|
|
else if (action == BELL_ANY || (action == BELL_OTHER &&
|
|
|
|
c->session->curw->window != w))
|
2015-03-31 17:45:10 +00:00
|
|
|
status_message_set(c, "Bell in window %d", wl->idx);
|
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
|
2015-08-29 08:30:54 +00:00
|
|
|
alerts_check_activity(struct session *s, struct winlink *wl)
|
2009-10-22 19:41:51 +00:00
|
|
|
{
|
|
|
|
struct client *c;
|
2010-06-21 01:27:46 +00:00
|
|
|
struct window *w = wl->window;
|
2009-10-22 19:41:51 +00:00
|
|
|
|
2014-02-14 12:44:45 +00:00
|
|
|
if (s->curw->window == w)
|
2012-07-08 07:27:32 +00:00
|
|
|
w->flags &= ~WINDOW_ACTIVITY;
|
|
|
|
|
2010-06-21 01:27:46 +00:00
|
|
|
if (!(w->flags & WINDOW_ACTIVITY) || wl->flags & WINLINK_ACTIVITY)
|
2009-10-22 19:41:51 +00:00
|
|
|
return (0);
|
2015-12-07 09:47:41 +00:00
|
|
|
if (s->curw == wl)
|
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);
|
|
|
|
|
2015-10-27 15:58:42 +00:00
|
|
|
if (options_get_number(s->options, "bell-on-alert"))
|
2015-08-29 08:30:54 +00:00
|
|
|
alerts_ring_bell(s);
|
2010-06-21 01:27:46 +00:00
|
|
|
wl->flags |= WINLINK_ACTIVITY;
|
2009-10-22 19:41:51 +00:00
|
|
|
|
2015-10-27 15:58:42 +00:00
|
|
|
if (options_get_number(s->options, "visual-activity")) {
|
2015-04-24 23:17:11 +00:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->session != s)
|
2009-10-22 19:41:51 +00:00
|
|
|
continue;
|
2015-03-31 17:45:10 +00:00
|
|
|
status_message_set(c, "Activity in window %d", wl->idx);
|
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
|
2015-08-29 08:30:54 +00:00
|
|
|
alerts_check_silence(struct session *s, struct winlink *wl)
|
2010-12-06 22:51:02 +00:00
|
|
|
{
|
|
|
|
struct client *c;
|
|
|
|
struct window *w = wl->window;
|
|
|
|
|
2015-08-29 08:30:54 +00:00
|
|
|
if (s->curw->window == w)
|
|
|
|
w->flags &= ~WINDOW_SILENCE;
|
2010-12-06 22:51:02 +00:00
|
|
|
|
2015-08-29 08:30:54 +00:00
|
|
|
if (!(w->flags & WINDOW_SILENCE) || wl->flags & WINLINK_SILENCE)
|
2010-12-06 22:51:02 +00:00
|
|
|
return (0);
|
2015-12-07 09:47:41 +00:00
|
|
|
if (s->curw == wl)
|
2010-12-06 22:51:02 +00:00
|
|
|
return (0);
|
|
|
|
|
2015-10-27 15:58:42 +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
|
|
|
|
2015-10-27 15:58:42 +00:00
|
|
|
if (options_get_number(s->options, "bell-on-alert"))
|
2015-08-29 08:30:54 +00:00
|
|
|
alerts_ring_bell(s);
|
2010-12-06 22:51:02 +00:00
|
|
|
wl->flags |= WINLINK_SILENCE;
|
|
|
|
|
2015-10-27 15:58:42 +00:00
|
|
|
if (options_get_number(s->options, "visual-silence")) {
|
2015-04-24 23:17:11 +00:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->session != s)
|
2010-12-06 22:51:02 +00:00
|
|
|
continue;
|
2015-03-31 17:45:10 +00:00
|
|
|
status_message_set(c, "Silence in window %d", wl->idx);
|
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-03 22:52:11 +00:00
|
|
|
static void
|
2015-08-29 08:30:54 +00:00
|
|
|
alerts_ring_bell(struct session *s)
|
2011-07-03 19:07:54 +00:00
|
|
|
{
|
|
|
|
struct client *c;
|
|
|
|
|
2015-04-24 23:17:11 +00:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->session == s && !(c->flags & CLIENT_CONTROL))
|
2015-09-02 17:43:25 +00:00
|
|
|
tty_putcode(&c->tty, TTYC_BEL);
|
2011-07-03 19:07:54 +00:00
|
|
|
}
|
|
|
|
}
|