1 /*	$OpenBSD: win.c,v 1.7 2003/06/03 02:56:23 millert Exp $	*/
2 /*	$NetBSD: win.c,v 1.8 1996/02/08 21:07:57 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Edward Wang at The University of California, Berkeley.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)win.c	8.1 (Berkeley) 6/6/93";
39 #else
40 static char rcsid[] = "$OpenBSD: win.c,v 1.7 2003/06/03 02:56:23 millert Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include "defs.h"
45 #include "char.h"
46 #include <string.h>
47 
48 /*
49  * Higher level routines for dealing with windows.
50  *
51  * There are two types of windows: user window, and information window.
52  * User windows are the ones with a pty and shell.  Information windows
53  * are for displaying error messages, and other information.
54  *
55  * The windows are doubly linked in overlapping order and divided into
56  * two groups: foreground and normal.  Information
57  * windows are always foreground.  User windows can be either.
58  * Addwin() adds a window to the list at the top of one of the two groups.
59  * Deletewin() deletes a window.  Front() moves a window to the front
60  * of its group.  Wwopen(), wwadd(), and wwdelete() should never be called
61  * directly.
62  */
63 
64 /*
65  * Open a user window.
66  */
67 struct ww *
openwin(id,row,col,nrow,ncol,nline,label,type,uflags,shf,sh)68 openwin(id, row, col, nrow, ncol, nline, label, type, uflags, shf, sh)
69 char *label;
70 char *shf, **sh;
71 {
72 	struct ww *w;
73 
74 	if (id < 0 && (id = findid()) < 0)
75 		return 0;
76 	if (row + nrow <= 0 || row > wwnrow - 1
77 	    || col + ncol <= 0 || col > wwncol - 1) {
78 		error("Illegal window position.");
79 		return 0;
80 	}
81 	w = wwopen(type, 0, nrow, ncol, row, col, nline);
82 	if (w == 0) {
83 		error("Can't open window: %s.", wwerror());
84 		return 0;
85 	}
86 	w->ww_id = id;
87 	window[id] = w;
88 	CLR(w->ww_uflags, WWU_ALLFLAGS);
89 	SET(w->ww_uflags, uflags);
90 	w->ww_alt = w->ww_w;
91 	if (label != 0 && setlabel(w, label) < 0)
92 		error("No memory for label.");
93 	wwcursor(w, 1);
94 	/*
95 	 * We have to do this little maneuver to make sure
96 	 * addwin() puts w at the top, so we don't waste an
97 	 * insert and delete operation.
98 	 */
99 	setselwin((struct ww *)0);
100 	addwin(w, 0);
101 	setselwin(w);
102 	if (wwspawn(w, shf, sh) < 0) {
103 		error("Can't execute %s: %s.", shf, wwerror());
104 		closewin(w);
105 		return 0;
106 	}
107 	return w;
108 }
109 
findid()110 findid()
111 {
112 	int i;
113 
114 	for (i = 0; i < NWINDOW && window[i] != 0; i++)
115 		;
116 	if (i >= NWINDOW) {
117 		error("Too many windows.");
118 		return -1;
119 	}
120 	return i;
121 }
122 
123 struct ww *
findselwin()124 findselwin()
125 {
126 	struct ww *w, *s = 0;
127 	int i;
128 
129 	for (i = 0; i < NWINDOW; i++)
130 		if ((w = window[i]) != 0 && w != selwin &&
131 		    (s == 0 ||
132 		     !isfg(w) && (w->ww_order < s->ww_order || isfg(s))))
133 			s = w;
134 	return s;
135 }
136 
137 /*
138  * Close a user window.  Close all if w == 0.
139  */
140 closewin(w)
141 struct ww *w;
142 {
143 	char didit = 0;
144 	int i;
145 
146 	if (w != 0) {
147 		closewin1(w);
148 		didit++;
149 	} else
150 		for (i = 0; i < NWINDOW; i++) {
151 			if ((w = window[i]) == 0)
152 				continue;
153 			closewin1(w);
154 			didit++;
155 		}
156 	if (didit) {
157 		if (selwin == 0)
158 			if (lastselwin != 0) {
159 				setselwin(lastselwin);
160 				lastselwin = 0;
161 			} else if (w = findselwin())
162 				setselwin(w);
163 		if (lastselwin == 0 && selwin)
164 			if (w = findselwin())
165 				lastselwin = w;
166 		reframe();
167 	}
168 }
169 
170 /*
171  * Open an information (display) window.
172  */
173 struct ww *
openiwin(nrow,label)174 openiwin(nrow, label)
175 char *label;
176 {
177 	struct ww *w;
178 
179 	if ((w = wwopen(WWT_INTERNAL, 0, nrow, wwncol, 2, 0, 0)) == 0)
180 		return 0;
181 	SET(w->ww_wflags, WWW_MAPNL | WWW_NOINTR | WWW_NOUPDATE | WWW_UNCTRL);
182 	SET(w->ww_uflags, WWU_HASFRAME | WWU_CENTER);
183 	w->ww_id = -1;
184 	(void) setlabel(w, label);
185 	addwin(w, 1);
186 	reframe();
187 	return w;
188 }
189 
190 /*
191  * Close an information window.
192  */
193 closeiwin(w)
194 struct ww *w;
195 {
196 	closewin1(w);
197 	reframe();
198 }
199 
200 closewin1(w)
201 struct ww *w;
202 {
203 	if (w == selwin)
204 		selwin = 0;
205 	if (w == lastselwin)
206 		lastselwin = 0;
207 	if (w->ww_id >= 0 && w->ww_id < NWINDOW)
208 		window[w->ww_id] = 0;
209 	if (w->ww_label)
210 		str_free(w->ww_label);
211 	deletewin(w);
212 	wwclose(w);
213 }
214 
215 /*
216  * Move the window to the top of its group.
217  * Don't do it if already fully visible.
218  * Wwvisible() doesn't work for tinted windows.
219  * But anything to make it faster.
220  * Always reframe() if doreframe is true.
221  */
222 front(w, doreframe)
223 struct ww *w;
224 char doreframe;
225 {
226 	if (w->ww_back != (isfg(w) ? framewin : fgwin) && !wwvisible(w)) {
227 		deletewin(w);
228 		addwin(w, isfg(w));
229 		doreframe = 1;
230 	}
231 	if (doreframe)
232 		reframe();
233 }
234 
235 /*
236  * Add a window at the top of normal windows or foreground windows.
237  * For normal windows, we put it behind the current window.
238  */
239 addwin(w, fg)
240 struct ww *w;
241 char fg;
242 {
243 	if (fg) {
244 		wwadd(w, framewin);
245 		if (fgwin == framewin)
246 			fgwin = w;
247 	} else
248 		wwadd(w, selwin != 0 && selwin != w && !isfg(selwin)
249 				? selwin : fgwin);
250 }
251 
252 /*
253  * Delete a window.
254  */
255 deletewin(w)
256 struct ww *w;
257 {
258 	if (fgwin == w)
259 		fgwin = w->ww_back;
260 	wwdelete(w);
261 }
262 
reframe()263 reframe()
264 {
265 	struct ww *w;
266 
267 	wwunframe(framewin);
268 	for (w = wwhead.ww_back; w != &wwhead; w = w->ww_back)
269 		if (ISSET(w->ww_uflags, WWU_HASFRAME)) {
270 			wwframe(w, framewin);
271 			labelwin(w);
272 		}
273 }
274 
275 labelwin(w)
276 struct ww *w;
277 {
278 	int mode = w == selwin ? WWM_REV : 0;
279 
280 	if (!ISSET(w->ww_uflags, WWU_HASFRAME))
281 		return;
282 	if (w->ww_id >= 0) {
283 		char buf[2];
284 
285 		buf[0] = w->ww_id + '1';
286 		buf[1] = 0;
287 		wwlabel(w, framewin, 1, buf, mode);
288 	}
289 	if (w->ww_label) {
290 		int col;
291 
292 		if (ISSET(w->ww_uflags, WWU_CENTER)) {
293 			col = (w->ww_w.nc - strlen(w->ww_label)) / 2;
294 			col = MAX(3, col);
295 		} else
296 			col = 3;
297 		wwlabel(w, framewin, col, w->ww_label, mode);
298 	}
299 }
300 
301 stopwin(w)
302 	struct ww *w;
303 {
304 	if (w->ww_pty >= 0 && w->ww_type == WWT_PTY && wwstoptty(w->ww_pty) < 0)
305 		error("Can't stop output: %s.", wwerror());
306 	else
307 		SET(w->ww_pflags, WWP_STOPPED);
308 }
309 
310 startwin(w)
311 	struct ww *w;
312 {
313 	if (w->ww_pty >= 0 && w->ww_type == WWT_PTY && wwstarttty(w->ww_pty) < 0)
314 		error("Can't start output: %s.", wwerror());
315 	else
316 		CLR(w->ww_pflags, WWP_STOPPED);
317 }
318 
319 sizewin(w, nrow, ncol)
320 struct ww *w;
321 {
322 	struct ww *back = w->ww_back;
323 
324 	w->ww_alt.nr = w->ww_w.nr;
325 	w->ww_alt.nc = w->ww_w.nc;
326 	wwdelete(w);
327 	if (wwsize(w, nrow, ncol) < 0)
328 		error("Can't resize window: %s.", wwerror());
329 	wwadd(w, back);
330 	reframe();
331 }
332 
333 waitnl(w)
334 struct ww *w;
335 {
336 	(void) waitnl1(w, "[Type any key to continue]");
337 }
338 
339 more(w, always)
340 struct ww *w;
341 char always;
342 {
343 	int c;
344 	int uc = ISSET(w->ww_wflags, WWW_UNCTRL);
345 
346 	if (!always && w->ww_cur.r < w->ww_w.b - 2)
347 		return 0;
348 	c = waitnl1(w, "[Type escape to abort, any other key to continue]");
349 	CLR(w->ww_wflags, WWW_UNCTRL);
350 	wwputs("\033E", w);
351 	SET(w->ww_wflags, uc);
352 	return c == ctrl('[') ? 2 : 1;
353 }
354 
355 waitnl1(w, prompt)
356 struct ww *w;
357 char *prompt;
358 {
359 	int uc = ISSET(w->ww_wflags, WWW_UNCTRL);
360 
361 	CLR(w->ww_wflags, WWW_UNCTRL);
362 	front(w, 0);
363 	wwprintf(w, "\033Y%c%c\033sA%s\033rA ",
364 		w->ww_w.nr - 1 + ' ', ' ', prompt);	/* print on last line */
365 	wwcurtowin(w);
366 	while (wwpeekc() < 0)
367 		wwiomux();
368 	SET(w->ww_wflags, uc);
369 	return wwgetc();
370 }
371