Use format_get_command() and some spacing tweaks.
[tmux-openbsd.git] / server-window.c
blobe82f0e36ac5b5ef7927452d16a5a9c357600ab8f
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <event.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 #include "tmux.h"
27 int server_window_check_bell(struct session *, struct winlink *);
28 int server_window_check_activity(struct session *, struct winlink *);
29 int server_window_check_silence(struct session *, struct winlink *);
30 int server_window_check_content(
31 struct session *, struct winlink *, struct window_pane *);
32 void ring_bell(struct session *);
34 /* Window functions that need to happen every loop. */
35 void
36 server_window_loop(void)
38 struct window *w;
39 struct winlink *wl;
40 struct window_pane *wp;
41 struct session *s;
42 u_int i;
44 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
45 w = ARRAY_ITEM(&windows, i);
46 if (w == NULL)
47 continue;
49 RB_FOREACH(s, sessions, &sessions) {
50 wl = session_has(s, w);
51 if (wl == NULL)
52 continue;
54 if (server_window_check_bell(s, wl) ||
55 server_window_check_activity(s, wl) ||
56 server_window_check_silence(s, wl))
57 server_status_session(s);
58 TAILQ_FOREACH(wp, &w->panes, entry)
59 server_window_check_content(s, wl, wp);
64 /* Check for bell in window. */
65 int
66 server_window_check_bell(struct session *s, struct winlink *wl)
68 struct client *c;
69 struct window *w = wl->window;
70 u_int i;
71 int action, visual;
73 if (!(w->flags & WINDOW_BELL) || wl->flags & WINLINK_BELL)
74 return (0);
75 if (s->curw != wl || s->flags & SESSION_UNATTACHED)
76 wl->flags |= WINLINK_BELL;
77 if (s->flags & SESSION_UNATTACHED)
78 return (0);
79 if (s->curw->window == wl->window)
80 w->flags &= ~WINDOW_BELL;
82 visual = options_get_number(&s->options, "visual-bell");
83 action = options_get_number(&s->options, "bell-action");
84 if (action == BELL_NONE)
85 return (0);
86 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
87 c = ARRAY_ITEM(&clients, i);
88 if (c == NULL || c->session != s || (c->flags & CLIENT_CONTROL))
89 continue;
90 if (!visual) {
91 tty_bell(&c->tty);
92 continue;
94 if (c->session->curw->window == w)
95 status_message_set(c, "Bell in current window");
96 else if (action == BELL_ANY) {
97 status_message_set(c, "Bell in window %u",
98 winlink_find_by_window(&s->windows, w)->idx);
102 return (1);
105 /* Check for activity in window. */
107 server_window_check_activity(struct session *s, struct winlink *wl)
109 struct client *c;
110 struct window *w = wl->window;
111 u_int i;
113 if (s->curw->window == wl->window)
114 w->flags &= ~WINDOW_ACTIVITY;
116 if (!(w->flags & WINDOW_ACTIVITY) || wl->flags & WINLINK_ACTIVITY)
117 return (0);
118 if (s->curw == wl && !(s->flags & SESSION_UNATTACHED))
119 return (0);
121 if (!options_get_number(&w->options, "monitor-activity"))
122 return (0);
124 if (options_get_number(&s->options, "bell-on-alert"))
125 ring_bell(s);
126 wl->flags |= WINLINK_ACTIVITY;
128 if (options_get_number(&s->options, "visual-activity")) {
129 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
130 c = ARRAY_ITEM(&clients, i);
131 if (c == NULL || c->session != s)
132 continue;
133 status_message_set(c, "Activity in window %u",
134 winlink_find_by_window(&s->windows, w)->idx);
138 return (1);
141 /* Check for silence in window. */
143 server_window_check_silence(struct session *s, struct winlink *wl)
145 struct client *c;
146 struct window *w = wl->window;
147 struct timeval timer;
148 u_int i;
149 int silence_interval, timer_difference;
151 if (!(w->flags & WINDOW_SILENCE) || wl->flags & WINLINK_SILENCE)
152 return (0);
154 if (s->curw == wl && !(s->flags & SESSION_UNATTACHED)) {
156 * Reset the timer for this window if we've focused it. We
157 * don't want the timer tripping as soon as we've switched away
158 * from this window.
160 if (gettimeofday(&w->silence_timer, NULL) != 0)
161 fatal("gettimeofday failed.");
163 return (0);
166 silence_interval = options_get_number(&w->options, "monitor-silence");
167 if (silence_interval == 0)
168 return (0);
170 if (gettimeofday(&timer, NULL) != 0)
171 fatal("gettimeofday");
172 timer_difference = timer.tv_sec - w->silence_timer.tv_sec;
173 if (timer_difference <= silence_interval)
174 return (0);
176 if (options_get_number(&s->options, "bell-on-alert"))
177 ring_bell(s);
178 wl->flags |= WINLINK_SILENCE;
180 if (options_get_number(&s->options, "visual-silence")) {
181 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
182 c = ARRAY_ITEM(&clients, i);
183 if (c == NULL || c->session != s)
184 continue;
185 status_message_set(c, "Silence in window %u",
186 winlink_find_by_window(&s->windows, w)->idx);
190 return (1);
193 /* Check for content change in window. */
195 server_window_check_content(
196 struct session *s, struct winlink *wl, struct window_pane *wp)
198 struct client *c;
199 struct window *w = wl->window;
200 u_int i;
201 char *found, *ptr;
203 /* Activity flag must be set for new content. */
204 if (s->curw->window == w)
205 w->flags &= ~WINDOW_ACTIVITY;
207 if (!(w->flags & WINDOW_ACTIVITY) || wl->flags & WINLINK_CONTENT)
208 return (0);
209 if (s->curw == wl && !(s->flags & SESSION_UNATTACHED))
210 return (0);
212 ptr = options_get_string(&w->options, "monitor-content");
213 if (ptr == NULL || *ptr == '\0')
214 return (0);
215 if ((found = window_pane_search(wp, ptr, NULL)) == NULL)
216 return (0);
217 free(found);
219 if (options_get_number(&s->options, "bell-on-alert"))
220 ring_bell(s);
221 wl->flags |= WINLINK_CONTENT;
223 if (options_get_number(&s->options, "visual-content")) {
224 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
225 c = ARRAY_ITEM(&clients, i);
226 if (c == NULL || c->session != s)
227 continue;
228 status_message_set(c, "Content in window %u",
229 winlink_find_by_window(&s->windows, w)->idx);
233 return (1);
236 /* Ring terminal bell. */
237 void
238 ring_bell(struct session *s)
240 struct client *c;
241 u_int i;
243 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
244 c = ARRAY_ITEM(&clients, i);
245 if (c != NULL && c->session == s && !(c->flags & CLIENT_CONTROL))
246 tty_bell(&c->tty);