1
0
mirror of https://github.com/tmux/tmux.git synced 2025-04-01 21:41:44 +00:00

Run alert hooks based on the options rather than unconditionally, from

Brad Town.
This commit is contained in:
nicm 2017-08-23 09:14:21 +00:00
parent bbe9da063e
commit e1b3dc89d2
2 changed files with 48 additions and 44 deletions

View File

@ -30,12 +30,13 @@ static int alerts_enabled(struct window *, int);
static void alerts_callback(int, short, void *); static void alerts_callback(int, short, void *);
static void alerts_reset(struct window *); static void alerts_reset(struct window *);
static int alerts_action_applies(struct winlink *, const char *);
static int alerts_check_all(struct window *); static int alerts_check_all(struct window *);
static int alerts_check_bell(struct window *); static int alerts_check_bell(struct window *);
static int alerts_check_activity(struct window *); static int alerts_check_activity(struct window *);
static int alerts_check_silence(struct window *); static int alerts_check_silence(struct window *);
static void alerts_set_message(struct session *, struct window *, static void alerts_set_message(struct winlink *, const char *,
struct winlink *, const char *, int, int); const char *);
static TAILQ_HEAD(, window) alerts_list = TAILQ_HEAD_INITIALIZER(alerts_list); static TAILQ_HEAD(, window) alerts_list = TAILQ_HEAD_INITIALIZER(alerts_list);
@ -67,6 +68,27 @@ alerts_callback(__unused int fd, __unused short events, __unused void *arg)
alerts_fired = 0; alerts_fired = 0;
} }
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);
}
static int static int
alerts_check_all(struct window *w) alerts_check_all(struct window *w)
{ {
@ -176,18 +198,17 @@ alerts_check_bell(struct window *w)
if (wl->flags & WINLINK_BELL) if (wl->flags & WINLINK_BELL)
continue; continue;
s = wl->session; s = wl->session;
if (s->curw != wl) { if (s->curw != wl)
wl->flags |= WINLINK_BELL; wl->flags |= WINLINK_BELL;
if (!alerts_action_applies(wl, "bell-action"))
continue;
notify_winlink("alert-bell", wl); notify_winlink("alert-bell", wl);
}
if (s->flags & SESSION_ALERTED) if (s->flags & SESSION_ALERTED)
continue; continue;
s->flags |= SESSION_ALERTED; s->flags |= SESSION_ALERTED;
alerts_set_message(s, w, wl, "Bell", alerts_set_message(wl, "Bell", "visual-bell");
options_get_number(s->options, "bell-action"),
options_get_number(s->options, "visual-bell"));
} }
return (WINDOW_BELL); return (WINDOW_BELL);
@ -211,18 +232,17 @@ alerts_check_activity(struct window *w)
if (wl->flags & WINLINK_ACTIVITY) if (wl->flags & WINLINK_ACTIVITY)
continue; continue;
s = wl->session; s = wl->session;
if (s->curw != wl) { if (s->curw != wl)
wl->flags |= WINLINK_ACTIVITY; wl->flags |= WINLINK_ACTIVITY;
if (!alerts_action_applies(wl, "activity-action"))
continue;
notify_winlink("alert-activity", wl); notify_winlink("alert-activity", wl);
}
if (s->flags & SESSION_ALERTED) if (s->flags & SESSION_ALERTED)
continue; continue;
s->flags |= SESSION_ALERTED; s->flags |= SESSION_ALERTED;
alerts_set_message(s, w, wl, "Activity", alerts_set_message(wl, "Activity", "visual-activity");
options_get_number(s->options, "activity-action"),
options_get_number(s->options, "visual-activity"));
} }
return (WINDOW_ACTIVITY); return (WINDOW_ACTIVITY);
@ -246,64 +266,48 @@ alerts_check_silence(struct window *w)
if (wl->flags & WINLINK_SILENCE) if (wl->flags & WINLINK_SILENCE)
continue; continue;
s = wl->session; s = wl->session;
if (s->curw != wl) { if (s->curw != wl)
wl->flags |= WINLINK_SILENCE; wl->flags |= WINLINK_SILENCE;
if (!alerts_action_applies(wl, "silence-action"))
continue;
notify_winlink("alert-silence", wl); notify_winlink("alert-silence", wl);
}
if (s->flags & SESSION_ALERTED) if (s->flags & SESSION_ALERTED)
continue; continue;
s->flags |= SESSION_ALERTED; s->flags |= SESSION_ALERTED;
alerts_set_message(s, w, wl, "Silence", alerts_set_message(wl, "Silence", "visual-silence");
options_get_number(s->options, "silence-action"),
options_get_number(s->options, "visual-silence"));
} }
return (WINDOW_SILENCE); return (WINDOW_SILENCE);
} }
static void static void
alerts_set_message(struct session *s, struct window *w, struct winlink *wl, alerts_set_message(struct winlink *wl, const char *type, const char *option)
const char *type, int action, int visual)
{ {
struct client *c; struct client *c;
int flag; int visual;
/* /*
* We have found an alert (bell, activity or silence), so we need to * 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, * pass it on to the user. For each client attached to this session,
* decide whether a bell (or visual message) is needed. * decide whether a bell, message or both is needed.
*
* {bell,activity,silence}-action determines when we alert: none means
* nothing happens, current means we only do something for the current
* window and other means only for windows other than the current.
* *
* If visual-{bell,activity,silence} is on, then a message is * 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 * substituted for a bell; if it is off, a bell is sent as normal; both
* mean both a bell and visual message is sent. * mean both a bell and message is sent.
*/ */
if (action == ALERT_NONE) visual = options_get_number(wl->session->options, option);
return;
TAILQ_FOREACH(c, &clients, entry) { TAILQ_FOREACH(c, &clients, entry) {
if (c->session != s || c->flags & CLIENT_CONTROL) if (c->session != wl->session || c->flags & CLIENT_CONTROL)
continue;
flag = 0;
if (action == ALERT_ANY)
flag = 1;
else if (action == ALERT_CURRENT)
flag = (c->session->curw->window == w);
else if (action == ALERT_OTHER)
flag = (c->session->curw->window != w);
if (!flag)
continue; continue;
if (visual == VISUAL_OFF || visual == VISUAL_BOTH) if (visual == VISUAL_OFF || visual == VISUAL_BOTH)
tty_putcode(&c->tty, TTYC_BEL); tty_putcode(&c->tty, TTYC_BEL);
if (visual == VISUAL_OFF) if (visual == VISUAL_OFF)
continue; continue;
if (c->session->curw->window == w) if (c->session->curw == wl)
status_message_set(c, "%s in current window", type); status_message_set(c, "%s in current window", type);
else else
status_message_set(c, "%s in window %d", type, wl->idx); status_message_set(c, "%s in window %d", type, wl->idx);

View File

@ -191,7 +191,7 @@ args_print(struct args *args)
int int
args_has(struct args *args, u_char ch) args_has(struct args *args, u_char ch)
{ {
return (args_find(args, ch) == NULL ? 0 : 1); return (args_find(args, ch) != NULL);
} }
/* Set argument value in the arguments tree. */ /* Set argument value in the arguments tree. */