2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Edward Wang at The University of California, Berkeley.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)win.c 8.1 (Berkeley) 6/6/93
37 * $FreeBSD: src/usr.bin/window/win.c,v 1.1.1.1.14.1 2001/05/17 09:45:01 obrien Exp $
38 * $DragonFly: src/usr.bin/window/win.c,v 1.2 2003/06/17 04:29:34 dillon Exp $
45 * Higher level routines for dealing with windows.
47 * There are two types of windows: user window, and information window.
48 * User windows are the ones with a pty and shell. Information windows
49 * are for displaying error messages, and other information.
51 * The windows are doubly linked in overlapping order and divided into
52 * two groups: foreground and normal. Information
53 * windows are always foreground. User windows can be either.
54 * Addwin() adds a window to the list at the top of one of the two groups.
55 * Deletewin() deletes a window. Front() moves a window to the front
56 * of its group. Wwopen(), wwadd(), and wwdelete() should never be called
64 openwin(id
, row
, col
, nrow
, ncol
, nline
, label
, haspty
, hasframe
, shf
, sh
)
66 char haspty
, hasframe
;
69 register struct ww
*w
;
71 if (id
< 0 && (id
= findid()) < 0)
73 if (row
+ nrow
<= 0 || row
> wwnrow
- 1
74 || col
+ ncol
<= 0 || col
> wwncol
- 1) {
75 error("Illegal window position.");
78 w
= wwopen(haspty
? WWO_PTY
: WWO_SOCKET
, nrow
, ncol
, row
, col
, nline
);
80 error("Can't open window: %s.", wwerror());
85 w
->ww_hasframe
= hasframe
;
87 if (label
!= 0 && setlabel(w
, label
) < 0)
88 error("No memory for label.");
91 * We have to do this little maneuver to make sure
92 * addwin() puts w at the top, so we don't waste an
93 * insert and delete operation.
95 setselwin((struct ww
*)0);
98 if (wwspawn(w
, shf
, sh
) < 0) {
99 error("Can't execute %s: %s.", shf
, wwerror());
110 for (i
= 0; i
< NWINDOW
&& window
[i
] != 0; i
++)
113 error("Too many windows.");
122 register struct ww
*w
, *s
= 0;
125 for (i
= 0; i
< NWINDOW
; i
++)
126 if ((w
= window
[i
]) != 0 && w
!= selwin
&&
128 !isfg(w
) && (w
->ww_order
< s
->ww_order
|| isfg(s
))))
134 * Close a user window. Close all if w == 0.
137 register struct ww
*w
;
146 for (i
= 0; i
< NWINDOW
; i
++) {
147 if ((w
= window
[i
]) == 0)
154 if (lastselwin
!= 0) {
155 setselwin(lastselwin
);
157 } else if (w
= findselwin())
159 if (lastselwin
== 0 && selwin
)
160 if (w
= findselwin())
167 * Open an information (display) window.
170 openiwin(nrow
, label
)
173 register struct ww
*w
;
175 if ((w
= wwopen(0, nrow
, wwncol
, 2, 0, 0)) == 0)
184 (void) setlabel(w
, label
);
191 * Close an information window.
201 register struct ww
*w
;
207 if (w
->ww_id
>= 0 && w
->ww_id
< NWINDOW
)
208 window
[w
->ww_id
] = 0;
210 str_free(w
->ww_label
);
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.
223 register struct ww
*w
;
226 if (w
->ww_back
!= (isfg(w
) ? framewin
: fgwin
) && !wwvisible(w
)) {
236 * Add a window at the top of normal windows or foreground windows.
237 * For normal windows, we put it behind the current window.
240 register struct ww
*w
;
245 if (fgwin
== framewin
)
248 wwadd(w
, selwin
!= 0 && selwin
!= w
&& !isfg(selwin
)
256 register struct ww
*w
;
265 register struct ww
*w
;
268 for (w
= wwhead
.ww_back
; w
!= &wwhead
; w
= w
->ww_back
)
269 if (w
->ww_hasframe
) {
270 wwframe(w
, framewin
);
276 register struct ww
*w
;
278 int mode
= w
== selwin
? WWM_REV
: 0;
285 buf
[0] = w
->ww_id
+ '1';
287 wwlabel(w
, framewin
, 1, buf
, mode
);
293 col
= (w
->ww_w
.nc
- strlen(w
->ww_label
)) / 2;
297 wwlabel(w
, framewin
, col
, w
->ww_label
, mode
);
302 register struct ww
*w
;
304 if (w
->ww_pty
>= 0 && w
->ww_ispty
&& wwstoptty(w
->ww_pty
) < 0)
305 error("Can't stop output: %s.", wwerror());
311 register struct ww
*w
;
313 if (w
->ww_pty
>= 0 && w
->ww_ispty
&& wwstarttty(w
->ww_pty
) < 0)
314 error("Can't start output: %s.", wwerror());
319 sizewin(w
, nrow
, ncol
)
320 register struct ww
*w
;
322 struct ww
*back
= w
->ww_back
;
324 w
->ww_alt
.nr
= w
->ww_w
.nr
;
325 w
->ww_alt
.nc
= w
->ww_w
.nc
;
327 if (wwsize(w
, nrow
, ncol
) < 0)
328 error("Can't resize window: %s.", wwerror());
336 (void) waitnl1(w
, "[Type any key to continue]");
340 register struct ww
*w
;
344 char uc
= w
->ww_unctrl
;
346 if (!always
&& w
->ww_cur
.r
< w
->ww_w
.b
- 2)
348 c
= waitnl1(w
, "[Type escape to abort, any other key to continue]");
352 return c
== ctrl('[') ? 2 : 1;
356 register struct ww
*w
;
359 char uc
= w
->ww_unctrl
;
363 wwprintf(w
, "\033Y%c%c\033sA%s\033rA ",
364 w
->ww_w
.nr
- 1 + ' ', ' ', prompt
); /* print on last line */
366 while (wwpeekc() < 0)