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>
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. */
36 server_window_loop(void)
40 struct window_pane
*wp
;
44 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
45 w
= ARRAY_ITEM(&windows
, i
);
49 RB_FOREACH(s
, sessions
, &sessions
) {
50 wl
= session_has(s
, w
);
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. */
66 server_window_check_bell(struct session
*s
, struct winlink
*wl
)
69 struct window
*w
= wl
->window
;
73 if (!(w
->flags
& WINDOW_BELL
) || wl
->flags
& WINLINK_BELL
)
75 if (s
->curw
!= wl
|| s
->flags
& SESSION_UNATTACHED
)
76 wl
->flags
|= WINLINK_BELL
;
77 if (s
->flags
& SESSION_UNATTACHED
)
79 if (s
->curw
->window
== w
)
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
)
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
))
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", wl
->idx
);
103 /* Check for activity in window. */
105 server_window_check_activity(struct session
*s
, struct winlink
*wl
)
108 struct window
*w
= wl
->window
;
111 if (s
->curw
->window
== w
)
112 w
->flags
&= ~WINDOW_ACTIVITY
;
114 if (!(w
->flags
& WINDOW_ACTIVITY
) || wl
->flags
& WINLINK_ACTIVITY
)
116 if (s
->curw
== wl
&& !(s
->flags
& SESSION_UNATTACHED
))
119 if (!options_get_number(&w
->options
, "monitor-activity"))
122 if (options_get_number(&s
->options
, "bell-on-alert"))
124 wl
->flags
|= WINLINK_ACTIVITY
;
126 if (options_get_number(&s
->options
, "visual-activity")) {
127 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
128 c
= ARRAY_ITEM(&clients
, i
);
129 if (c
== NULL
|| c
->session
!= s
)
131 status_message_set(c
, "Activity in window %u", wl
->idx
);
138 /* Check for silence in window. */
140 server_window_check_silence(struct session
*s
, struct winlink
*wl
)
143 struct window
*w
= wl
->window
;
144 struct timeval timer
;
146 int silence_interval
, timer_difference
;
148 if (!(w
->flags
& WINDOW_SILENCE
) || wl
->flags
& WINLINK_SILENCE
)
151 if (s
->curw
== wl
&& !(s
->flags
& SESSION_UNATTACHED
)) {
153 * Reset the timer for this window if we've focused it. We
154 * don't want the timer tripping as soon as we've switched away
157 if (gettimeofday(&w
->silence_timer
, NULL
) != 0)
158 fatal("gettimeofday failed.");
163 silence_interval
= options_get_number(&w
->options
, "monitor-silence");
164 if (silence_interval
== 0)
167 if (gettimeofday(&timer
, NULL
) != 0)
168 fatal("gettimeofday");
169 timer_difference
= timer
.tv_sec
- w
->silence_timer
.tv_sec
;
170 if (timer_difference
<= silence_interval
)
173 if (options_get_number(&s
->options
, "bell-on-alert"))
175 wl
->flags
|= WINLINK_SILENCE
;
177 if (options_get_number(&s
->options
, "visual-silence")) {
178 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
179 c
= ARRAY_ITEM(&clients
, i
);
180 if (c
== NULL
|| c
->session
!= s
)
182 status_message_set(c
, "Silence in window %u", wl
->idx
);
189 /* Check for content change in window. */
191 server_window_check_content(
192 struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
195 struct window
*w
= wl
->window
;
199 /* Activity flag must be set for new content. */
200 if (s
->curw
->window
== w
)
201 w
->flags
&= ~WINDOW_ACTIVITY
;
203 if (!(w
->flags
& WINDOW_ACTIVITY
) || wl
->flags
& WINLINK_CONTENT
)
205 if (s
->curw
== wl
&& !(s
->flags
& SESSION_UNATTACHED
))
208 ptr
= options_get_string(&w
->options
, "monitor-content");
209 if (ptr
== NULL
|| *ptr
== '\0')
211 if ((found
= window_pane_search(wp
, ptr
, NULL
)) == NULL
)
215 if (options_get_number(&s
->options
, "bell-on-alert"))
217 wl
->flags
|= WINLINK_CONTENT
;
219 if (options_get_number(&s
->options
, "visual-content")) {
220 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
221 c
= ARRAY_ITEM(&clients
, i
);
222 if (c
== NULL
|| c
->session
!= s
)
224 status_message_set(c
, "Content in window %u", wl
->idx
);
231 /* Ring terminal bell. */
233 ring_bell(struct session
*s
)
238 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
239 c
= ARRAY_ITEM(&clients
, i
);
240 if (c
!= NULL
&& c
->session
== s
&& !(c
->flags
& CLIENT_CONTROL
))