This commit was manufactured by cvs2svn to create tag 'TMUX_1_2'.
[tmux.git] / server-window.c
blob2a933cc3c4d1afb0ec842404775a4f187319b007
1 /* $Id: server-window.c,v 1.14 2010-02-26 13:26:44 tcunha Exp $ */
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 <unistd.h>
24 #include "tmux.h"
26 int server_window_backoff(struct window_pane *);
27 int server_window_check_bell(struct session *, struct window *);
28 int server_window_check_activity(struct session *, struct window *);
29 int server_window_check_content(
30 struct session *, struct window *, struct window_pane *);
32 /* Check if this window should suspend reading. */
33 int
34 server_window_backoff(struct window_pane *wp)
36 struct client *c;
37 u_int i;
39 if (!window_pane_visible(wp))
40 return (0);
42 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
43 c = ARRAY_ITEM(&clients, i);
44 if (c == NULL || c->session == NULL)
45 continue;
46 if ((c->flags & (CLIENT_SUSPENDED|CLIENT_DEAD)) != 0)
47 continue;
48 if (c->session->curw->window != wp->window)
49 continue;
51 if (EVBUFFER_LENGTH(c->tty.event->output) > BACKOFF_THRESHOLD)
52 return (1);
54 return (0);
57 /* Window functions that need to happen every loop. */
58 void
59 server_window_loop(void)
61 struct window *w;
62 struct window_pane *wp;
63 struct session *s;
64 u_int i, j;
66 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
67 w = ARRAY_ITEM(&windows, i);
68 if (w == NULL)
69 continue;
71 TAILQ_FOREACH(wp, &w->panes, entry) {
72 if (wp->fd == -1)
73 continue;
74 if (!(wp->flags & PANE_FREEZE)) {
75 if (server_window_backoff(wp))
76 bufferevent_disable(wp->event, EV_READ);
77 else
78 bufferevent_enable(wp->event, EV_READ);
82 for (j = 0; j < ARRAY_LENGTH(&sessions); j++) {
83 s = ARRAY_ITEM(&sessions, j);
84 if (s == NULL || !session_has(s, w))
85 continue;
87 if (server_window_check_bell(s, w) ||
88 server_window_check_activity(s, w))
89 server_status_session(s);
90 TAILQ_FOREACH(wp, &w->panes, entry)
91 server_window_check_content(s, w, wp);
93 w->flags &= ~(WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT);
97 /* Check for bell in window. */
98 int
99 server_window_check_bell(struct session *s, struct window *w)
101 struct client *c;
102 u_int i;
103 int action, visual;
105 if (!(w->flags & WINDOW_BELL))
106 return (0);
108 if (session_alert_has_window(s, w, WINDOW_BELL))
109 return (0);
110 session_alert_add(s, w, WINDOW_BELL);
112 action = options_get_number(&s->options, "bell-action");
113 switch (action) {
114 case BELL_ANY:
115 if (s->flags & SESSION_UNATTACHED)
116 break;
117 visual = options_get_number(&s->options, "visual-bell");
118 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
119 c = ARRAY_ITEM(&clients, i);
120 if (c == NULL || c->session != s)
121 continue;
122 if (!visual) {
123 tty_putcode(&c->tty, TTYC_BEL);
124 continue;
126 if (c->session->curw->window == w) {
127 status_message_set(c, "Bell in current window");
128 continue;
130 status_message_set(c, "Bell in window %u",
131 winlink_find_by_window(&s->windows, w)->idx);
133 break;
134 case BELL_CURRENT:
135 if (s->flags & SESSION_UNATTACHED)
136 break;
137 visual = options_get_number(&s->options, "visual-bell");
138 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
139 c = ARRAY_ITEM(&clients, i);
140 if (c == NULL || c->session != s)
141 continue;
142 if (c->session->curw->window != w)
143 continue;
144 if (!visual) {
145 tty_putcode(&c->tty, TTYC_BEL);
146 continue;
148 status_message_set(c, "Bell in current window");
150 break;
153 return (1);
156 /* Check for activity in window. */
158 server_window_check_activity(struct session *s, struct window *w)
160 struct client *c;
161 u_int i;
163 if (!(w->flags & WINDOW_ACTIVITY))
164 return (0);
165 if (s->curw->window == w)
166 return (0);
168 if (!options_get_number(&w->options, "monitor-activity"))
169 return (0);
171 if (session_alert_has_window(s, w, WINDOW_ACTIVITY))
172 return (0);
173 session_alert_add(s, w, WINDOW_ACTIVITY);
175 if (s->flags & SESSION_UNATTACHED)
176 return (0);
177 if (options_get_number(&s->options, "visual-activity")) {
178 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
179 c = ARRAY_ITEM(&clients, i);
180 if (c == NULL || c->session != s)
181 continue;
182 status_message_set(c, "Activity in window %u",
183 winlink_find_by_window(&s->windows, w)->idx);
187 return (1);
190 /* Check for content change in window. */
192 server_window_check_content(
193 struct session *s, struct window *w, struct window_pane *wp)
195 struct client *c;
196 u_int i;
197 char *found, *ptr;
199 if (!(w->flags & WINDOW_ACTIVITY)) /* activity for new content */
200 return (0);
201 if (s->curw->window == w)
202 return (0);
204 ptr = options_get_string(&w->options, "monitor-content");
205 if (ptr == NULL || *ptr == '\0')
206 return (0);
208 if (session_alert_has_window(s, w, WINDOW_CONTENT))
209 return (0);
211 if ((found = window_pane_search(wp, ptr, NULL)) == NULL)
212 return (0);
213 xfree(found);
215 session_alert_add(s, w, WINDOW_CONTENT);
216 if (s->flags & SESSION_UNATTACHED)
217 return (0);
218 if (options_get_number(&s->options, "visual-content")) {
219 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
220 c = ARRAY_ITEM(&clients, i);
221 if (c == NULL || c->session != s)
222 continue;
223 status_message_set(c, "Content in window %u",
224 winlink_find_by_window(&s->windows, w)->idx);
228 return (1);