Annotation of src/usr.bin/tmux/server-window.c, Revision 1.19
1.19 ! nicm 1: /* $OpenBSD: server-window.c,v 1.18 2010/12/06 22:51:02 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
5: *
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.
9: *
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.
17: */
18:
19: #include <sys/types.h>
20:
1.5 nicm 21: #include <event.h>
1.1 nicm 22: #include <unistd.h>
23:
24: #include "tmux.h"
25:
1.3 nicm 26: int server_window_backoff(struct window_pane *);
1.15 nicm 27: int server_window_check_bell(struct session *, struct winlink *);
28: int server_window_check_activity(struct session *, struct winlink *);
1.18 nicm 29: int server_window_check_silence(struct session *, struct winlink *);
1.1 nicm 30: int server_window_check_content(
1.15 nicm 31: struct session *, struct winlink *, struct window_pane *);
1.2 nicm 32:
1.1 nicm 33: /* Window functions that need to happen every loop. */
34: void
35: server_window_loop(void)
36: {
37: struct window *w;
1.15 nicm 38: struct winlink *wl;
1.1 nicm 39: struct window_pane *wp;
40: struct session *s;
1.19 ! nicm 41: u_int i;
1.1 nicm 42:
43: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
44: w = ARRAY_ITEM(&windows, i);
45: if (w == NULL)
46: continue;
47:
1.19 ! nicm 48: RB_FOREACH(s, sessions, &sessions) {
1.15 nicm 49: wl = session_has(s, w);
50: if (wl == NULL)
1.1 nicm 51: continue;
1.13 nicm 52:
1.15 nicm 53: if (server_window_check_bell(s, wl) ||
1.18 nicm 54: server_window_check_activity(s, wl) ||
55: server_window_check_silence(s, wl))
1.1 nicm 56: server_status_session(s);
57: TAILQ_FOREACH(wp, &w->panes, entry)
1.15 nicm 58: server_window_check_content(s, wl, wp);
1.1 nicm 59: }
1.15 nicm 60: w->flags &= ~(WINDOW_BELL|WINDOW_ACTIVITY);
1.1 nicm 61: }
62: }
63:
64: /* Check for bell in window. */
65: int
1.15 nicm 66: server_window_check_bell(struct session *s, struct winlink *wl)
1.1 nicm 67: {
68: struct client *c;
1.15 nicm 69: struct window *w = wl->window;
1.1 nicm 70: u_int i;
71: int action, visual;
72:
1.15 nicm 73: if (!(w->flags & WINDOW_BELL) || wl->flags & WINLINK_BELL)
74: return (0);
1.16 nicm 75: if (s->curw != wl)
76: wl->flags |= WINLINK_BELL;
1.1 nicm 77:
78: action = options_get_number(&s->options, "bell-action");
79: switch (action) {
80: case BELL_ANY:
81: if (s->flags & SESSION_UNATTACHED)
82: break;
83: visual = options_get_number(&s->options, "visual-bell");
84: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
85: c = ARRAY_ITEM(&clients, i);
86: if (c == NULL || c->session != s)
87: continue;
88: if (!visual) {
89: tty_putcode(&c->tty, TTYC_BEL);
90: continue;
91: }
1.13 nicm 92: if (c->session->curw->window == w) {
1.1 nicm 93: status_message_set(c, "Bell in current window");
94: continue;
95: }
96: status_message_set(c, "Bell in window %u",
97: winlink_find_by_window(&s->windows, w)->idx);
98: }
99: break;
100: case BELL_CURRENT:
101: if (s->flags & SESSION_UNATTACHED)
102: break;
103: visual = options_get_number(&s->options, "visual-bell");
104: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
105: c = ARRAY_ITEM(&clients, i);
106: if (c == NULL || c->session != s)
107: continue;
1.13 nicm 108: if (c->session->curw->window != w)
1.1 nicm 109: continue;
110: if (!visual) {
111: tty_putcode(&c->tty, TTYC_BEL);
112: continue;
113: }
114: status_message_set(c, "Bell in current window");
115: }
116: break;
117: }
118:
119: return (1);
120: }
121:
122: /* Check for activity in window. */
123: int
1.15 nicm 124: server_window_check_activity(struct session *s, struct winlink *wl)
1.1 nicm 125: {
126: struct client *c;
1.15 nicm 127: struct window *w = wl->window;
1.1 nicm 128: u_int i;
129:
1.15 nicm 130: if (!(w->flags & WINDOW_ACTIVITY) || wl->flags & WINLINK_ACTIVITY)
1.1 nicm 131: return (0);
1.15 nicm 132: if (s->curw == wl)
1.1 nicm 133: return (0);
134:
1.13 nicm 135: if (!options_get_number(&w->options, "monitor-activity"))
1.1 nicm 136: return (0);
137:
1.15 nicm 138: wl->flags |= WINLINK_ACTIVITY;
1.1 nicm 139:
1.13 nicm 140: if (options_get_number(&s->options, "visual-activity")) {
1.1 nicm 141: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
142: c = ARRAY_ITEM(&clients, i);
143: if (c == NULL || c->session != s)
144: continue;
145: status_message_set(c, "Activity in window %u",
1.18 nicm 146: winlink_find_by_window(&s->windows, w)->idx);
147: }
148: }
149:
150: return (1);
151: }
152:
153: /* Check for silence in window. */
154: int
155: server_window_check_silence(struct session *s, struct winlink *wl)
156: {
157: struct client *c;
158: struct window *w = wl->window;
159: struct timeval timer;
160: u_int i;
161: int silence_interval, timer_difference;
162:
163: if (!(w->flags & WINDOW_SILENCE) || wl->flags & WINLINK_SILENCE)
164: return (0);
165:
166: if (s->curw == wl) {
167: /*
168: * Reset the timer for this window if we've focused it. We
169: * don't want the timer tripping as soon as we've switched away
170: * from this window.
171: */
172: if (gettimeofday(&w->silence_timer, NULL) != 0)
173: fatal("gettimeofday failed.");
174:
175: return (0);
176: }
177:
178: silence_interval = options_get_number(&w->options, "monitor-silence");
179: if (silence_interval == 0)
180: return (0);
181:
182: if (gettimeofday(&timer, NULL) != 0)
183: fatal("gettimeofday");
184: timer_difference = timer.tv_sec - w->silence_timer.tv_sec;
185: if (timer_difference <= silence_interval)
186: return (0);
187: wl->flags |= WINLINK_SILENCE;
188:
189: if (options_get_number(&s->options, "visual-silence")) {
190: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
191: c = ARRAY_ITEM(&clients, i);
192: if (c == NULL || c->session != s)
193: continue;
194: status_message_set(c, "Silence in window %u",
1.1 nicm 195: winlink_find_by_window(&s->windows, w)->idx);
196: }
197: }
198:
199: return (1);
200: }
201:
202: /* Check for content change in window. */
203: int
204: server_window_check_content(
1.15 nicm 205: struct session *s, struct winlink *wl, struct window_pane *wp)
1.1 nicm 206: {
207: struct client *c;
1.15 nicm 208: struct window *w = wl->window;
1.1 nicm 209: u_int i;
210: char *found, *ptr;
1.13 nicm 211:
1.15 nicm 212: /* Activity flag must be set for new content. */
213: if (!(w->flags & WINDOW_ACTIVITY) || wl->flags & WINLINK_CONTENT)
1.1 nicm 214: return (0);
1.15 nicm 215: if (s->curw == wl)
1.1 nicm 216: return (0);
217:
218: ptr = options_get_string(&w->options, "monitor-content");
219: if (ptr == NULL || *ptr == '\0')
220: return (0);
221: if ((found = window_pane_search(wp, ptr, NULL)) == NULL)
222: return (0);
1.13 nicm 223: xfree(found);
1.1 nicm 224:
1.15 nicm 225: wl->flags |= WINLINK_CONTENT;
226:
1.13 nicm 227: if (options_get_number(&s->options, "visual-content")) {
1.1 nicm 228: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
229: c = ARRAY_ITEM(&clients, i);
230: if (c == NULL || c->session != s)
231: continue;
232: status_message_set(c, "Content in window %u",
233: winlink_find_by_window(&s->windows, w)->idx);
234: }
235: }
236:
237: return (1);
238: }