alerts.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2015 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <event.h>
  19. #include "tmux.h"
  20. int alerts_fired;
  21. void alerts_timer(int, short, void *);
  22. int alerts_enabled(struct window *, int);
  23. void alerts_callback(int, short, void *);
  24. void alerts_reset(struct window *);
  25. void alerts_run_hook(struct session *, struct winlink *, int);
  26. int alerts_check_all(struct session *, struct winlink *);
  27. int alerts_check_bell(struct session *, struct winlink *);
  28. int alerts_check_activity(struct session *, struct winlink *);
  29. int alerts_check_silence(struct session *, struct winlink *);
  30. void alerts_ring_bell(struct session *);
  31. void
  32. alerts_timer(__unused int fd, __unused short events, void *arg)
  33. {
  34. struct window *w = arg;
  35. log_debug("@%u alerts timer expired", w->id);
  36. alerts_reset(w);
  37. alerts_queue(w, WINDOW_SILENCE);
  38. }
  39. void
  40. alerts_callback(__unused int fd, __unused short events, __unused void *arg)
  41. {
  42. struct window *w;
  43. struct session *s;
  44. struct winlink *wl;
  45. int flags, alerts;
  46. RB_FOREACH(w, windows, &windows) {
  47. RB_FOREACH(s, sessions, &sessions) {
  48. RB_FOREACH(wl, winlinks, &s->windows) {
  49. if (wl->window != w)
  50. continue;
  51. flags = w->flags;
  52. alerts = alerts_check_all(s, wl);
  53. log_debug("%s:%d @%u alerts check, alerts %#x, "
  54. "flags %#x", s->name, wl->idx, w->id,
  55. alerts, flags);
  56. }
  57. }
  58. }
  59. alerts_fired = 0;
  60. }
  61. void
  62. alerts_run_hook(struct session *s, struct winlink *wl, int flags)
  63. {
  64. struct cmd_find_state fs;
  65. if (cmd_find_from_winlink(&fs, s, wl) != 0)
  66. return;
  67. if (flags & WINDOW_BELL)
  68. hooks_run(s->hooks, NULL, &fs, "alert-bell");
  69. if (flags & WINDOW_SILENCE)
  70. hooks_run(s->hooks, NULL, &fs, "alert-silence");
  71. if (flags & WINDOW_ACTIVITY)
  72. hooks_run(s->hooks, NULL, &fs, "alert-activity");
  73. }
  74. int
  75. alerts_check_all(struct session *s, struct winlink *wl)
  76. {
  77. int alerts;
  78. alerts = alerts_check_bell(s, wl);
  79. alerts |= alerts_check_activity(s, wl);
  80. alerts |= alerts_check_silence(s, wl);
  81. if (alerts != 0) {
  82. alerts_run_hook(s, wl, alerts);
  83. server_status_session(s);
  84. }
  85. return (alerts);
  86. }
  87. void
  88. alerts_check_session(struct session *s)
  89. {
  90. struct winlink *wl;
  91. RB_FOREACH(wl, winlinks, &s->windows)
  92. alerts_check_all(s, wl);
  93. }
  94. int
  95. alerts_enabled(struct window *w, int flags)
  96. {
  97. if (flags & WINDOW_BELL)
  98. return (1);
  99. if (flags & WINDOW_ACTIVITY) {
  100. if (options_get_number(w->options, "monitor-activity"))
  101. return (1);
  102. }
  103. if (flags & WINDOW_SILENCE) {
  104. if (options_get_number(w->options, "monitor-silence") != 0)
  105. return (1);
  106. }
  107. return (0);
  108. }
  109. void
  110. alerts_reset_all(void)
  111. {
  112. struct window *w;
  113. RB_FOREACH(w, windows, &windows)
  114. alerts_reset(w);
  115. }
  116. void
  117. alerts_reset(struct window *w)
  118. {
  119. struct timeval tv;
  120. w->flags &= ~WINDOW_SILENCE;
  121. event_del(&w->alerts_timer);
  122. timerclear(&tv);
  123. tv.tv_sec = options_get_number(w->options, "monitor-silence");
  124. log_debug("@%u alerts timer reset %u", w->id, (u_int)tv.tv_sec);
  125. if (tv.tv_sec != 0)
  126. event_add(&w->alerts_timer, &tv);
  127. }
  128. void
  129. alerts_queue(struct window *w, int flags)
  130. {
  131. if (w->flags & WINDOW_ACTIVITY)
  132. alerts_reset(w);
  133. if (!event_initialized(&w->alerts_timer))
  134. evtimer_set(&w->alerts_timer, alerts_timer, w);
  135. if (!alerts_fired) {
  136. w->flags |= flags;
  137. log_debug("@%u alerts flags added %#x", w->id, flags);
  138. if (alerts_enabled(w, flags)) {
  139. log_debug("alerts check queued (by @%u)", w->id);
  140. event_once(-1, EV_TIMEOUT, alerts_callback, NULL, NULL);
  141. alerts_fired = 1;
  142. }
  143. }
  144. }
  145. int
  146. alerts_check_bell(struct session *s, struct winlink *wl)
  147. {
  148. struct client *c;
  149. struct window *w = wl->window;
  150. int action, visual;
  151. if (!(w->flags & WINDOW_BELL))
  152. return (0);
  153. if (s->curw != wl) {
  154. wl->flags |= WINLINK_BELL;
  155. w->flags &= ~WINDOW_BELL;
  156. }
  157. if (s->curw->window == w)
  158. w->flags &= ~WINDOW_BELL;
  159. action = options_get_number(s->options, "bell-action");
  160. if (action == BELL_NONE)
  161. return (0);
  162. visual = options_get_number(s->options, "visual-bell");
  163. TAILQ_FOREACH(c, &clients, entry) {
  164. if (c->session != s || c->flags & CLIENT_CONTROL)
  165. continue;
  166. if (!visual) {
  167. if ((action == BELL_CURRENT &&
  168. c->session->curw->window == w) ||
  169. (action == BELL_OTHER &&
  170. c->session->curw->window != w) ||
  171. action == BELL_ANY)
  172. tty_putcode(&c->tty, TTYC_BEL);
  173. continue;
  174. }
  175. if (action == BELL_CURRENT && c->session->curw->window == w)
  176. status_message_set(c, "Bell in current window");
  177. else if (action == BELL_ANY || (action == BELL_OTHER &&
  178. c->session->curw->window != w))
  179. status_message_set(c, "Bell in window %d", wl->idx);
  180. }
  181. return (WINDOW_BELL);
  182. }
  183. int
  184. alerts_check_activity(struct session *s, struct winlink *wl)
  185. {
  186. struct client *c;
  187. struct window *w = wl->window;
  188. if (s->curw->window == w)
  189. w->flags &= ~WINDOW_ACTIVITY;
  190. if (!(w->flags & WINDOW_ACTIVITY) || wl->flags & WINLINK_ACTIVITY)
  191. return (0);
  192. if (s->curw == wl)
  193. return (0);
  194. if (!options_get_number(w->options, "monitor-activity"))
  195. return (0);
  196. if (options_get_number(s->options, "bell-on-alert"))
  197. alerts_ring_bell(s);
  198. wl->flags |= WINLINK_ACTIVITY;
  199. if (options_get_number(s->options, "visual-activity")) {
  200. TAILQ_FOREACH(c, &clients, entry) {
  201. if (c->session != s)
  202. continue;
  203. status_message_set(c, "Activity in window %d", wl->idx);
  204. }
  205. }
  206. return (WINDOW_ACTIVITY);
  207. }
  208. int
  209. alerts_check_silence(struct session *s, struct winlink *wl)
  210. {
  211. struct client *c;
  212. struct window *w = wl->window;
  213. if (s->curw->window == w)
  214. w->flags &= ~WINDOW_SILENCE;
  215. if (!(w->flags & WINDOW_SILENCE) || wl->flags & WINLINK_SILENCE)
  216. return (0);
  217. if (s->curw == wl)
  218. return (0);
  219. if (options_get_number(w->options, "monitor-silence") == 0)
  220. return (0);
  221. if (options_get_number(s->options, "bell-on-alert"))
  222. alerts_ring_bell(s);
  223. wl->flags |= WINLINK_SILENCE;
  224. if (options_get_number(s->options, "visual-silence")) {
  225. TAILQ_FOREACH(c, &clients, entry) {
  226. if (c->session != s)
  227. continue;
  228. status_message_set(c, "Silence in window %d", wl->idx);
  229. }
  230. }
  231. return (WINDOW_SILENCE);
  232. }
  233. void
  234. alerts_ring_bell(struct session *s)
  235. {
  236. struct client *c;
  237. TAILQ_FOREACH(c, &clients, entry) {
  238. if (c->session == s && !(c->flags & CLIENT_CONTROL))
  239. tty_putcode(&c->tty, TTYC_BEL);
  240. }
  241. }