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