xref: /titanic_50/usr/src/lib/libeti/panel/common/misc.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  *      Copyright (c) 1997, by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  *      All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /* A panels subsystem built on curses--Miscellaneous routines */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.4	*/
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
38*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
39*7c478bd9Sstevel@tonic-gate #include <curses.h>
40*7c478bd9Sstevel@tonic-gate #include "private.h"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate PANEL	*_Bottom_panel;
43*7c478bd9Sstevel@tonic-gate PANEL	*_Top_panel;
44*7c478bd9Sstevel@tonic-gate int	_Panel_cnt;
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate static	_obscured_list	*_Free_list;
47*7c478bd9Sstevel@tonic-gate static int	_Free_list_cnt;
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /* panel_window - Return the window pointer */
51*7c478bd9Sstevel@tonic-gate WINDOW *
panel_window(PANEL * panel)52*7c478bd9Sstevel@tonic-gate panel_window(PANEL *panel)
53*7c478bd9Sstevel@tonic-gate {
54*7c478bd9Sstevel@tonic-gate 	return (panel ? panel -> win : 0);
55*7c478bd9Sstevel@tonic-gate }
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate /* panel_userptr - Return the user pointer */
58*7c478bd9Sstevel@tonic-gate char *
panel_userptr(PANEL * panel)59*7c478bd9Sstevel@tonic-gate panel_userptr(PANEL *panel)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate 	return (panel ? panel -> user : 0);
62*7c478bd9Sstevel@tonic-gate }
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /* set_panel_userptr - set the user pointer */
65*7c478bd9Sstevel@tonic-gate int
set_panel_userptr(PANEL * panel,char * ptr)66*7c478bd9Sstevel@tonic-gate set_panel_userptr(PANEL *panel, char *ptr)
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate 	if (panel) {
69*7c478bd9Sstevel@tonic-gate 		panel -> user = ptr;
70*7c478bd9Sstevel@tonic-gate 		return (OK);
71*7c478bd9Sstevel@tonic-gate 	} else
72*7c478bd9Sstevel@tonic-gate 		return (ERR);
73*7c478bd9Sstevel@tonic-gate }
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate /*
76*7c478bd9Sstevel@tonic-gate  * panel_above - Return the panel above the
77*7c478bd9Sstevel@tonic-gate  * given panel (or the bottom panel in 0)
78*7c478bd9Sstevel@tonic-gate  */
79*7c478bd9Sstevel@tonic-gate PANEL *
panel_above(PANEL * panel)80*7c478bd9Sstevel@tonic-gate panel_above(PANEL *panel)
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	if (!panel)
84*7c478bd9Sstevel@tonic-gate 		return (_Bottom_panel);
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	return ((panel == panel -> below) ? ((PANEL *) 0) : panel -> above);
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate /*
91*7c478bd9Sstevel@tonic-gate  * panel_below - Return the panel below the
92*7c478bd9Sstevel@tonic-gate  * given panel (or the top panel in 0)
93*7c478bd9Sstevel@tonic-gate  */
94*7c478bd9Sstevel@tonic-gate PANEL *
panel_below(PANEL * panel)95*7c478bd9Sstevel@tonic-gate panel_below(PANEL *panel)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	if (!panel)
99*7c478bd9Sstevel@tonic-gate 		return (_Top_panel);
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	return ((panel == panel -> below) ? ((PANEL *) 0) : panel -> below);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /* panel_hidden - Return TRUE if the panel is hidden, FALSE if not.  */
105*7c478bd9Sstevel@tonic-gate int
panel_hidden(PANEL * panel)106*7c478bd9Sstevel@tonic-gate panel_hidden(PANEL *panel)
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate 	return ((!panel || (panel != panel -> below)) ? FALSE : TRUE);
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate /* _get_overlap - Get an overlap node from the free list. */
112*7c478bd9Sstevel@tonic-gate static _obscured_list *
_get_overlap(void)113*7c478bd9Sstevel@tonic-gate _get_overlap(void)
114*7c478bd9Sstevel@tonic-gate {
115*7c478bd9Sstevel@tonic-gate 	_obscured_list	*overlap;
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	if (_Free_list_cnt-- > 0) {
118*7c478bd9Sstevel@tonic-gate 		overlap = _Free_list;
119*7c478bd9Sstevel@tonic-gate 		_Free_list = _Free_list -> next;
120*7c478bd9Sstevel@tonic-gate 	} else {
121*7c478bd9Sstevel@tonic-gate 		_Free_list_cnt = 0;
122*7c478bd9Sstevel@tonic-gate 		overlap = 0;
123*7c478bd9Sstevel@tonic-gate 	}
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	return (overlap);
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate /*
130*7c478bd9Sstevel@tonic-gate  * _unlink_obs - Find the obscured node, if any,
131*7c478bd9Sstevel@tonic-gate  * in the first panel which refers the second panel.
132*7c478bd9Sstevel@tonic-gate  */
133*7c478bd9Sstevel@tonic-gate _obscured_list *
_unlink_obs(PANEL * pnl,PANEL * panel)134*7c478bd9Sstevel@tonic-gate _unlink_obs(PANEL *pnl, PANEL *panel)
135*7c478bd9Sstevel@tonic-gate {
136*7c478bd9Sstevel@tonic-gate 	_obscured_list	*obs;
137*7c478bd9Sstevel@tonic-gate 	_obscured_list	*prev_obs;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	if (!pnl -> obscured || !_panels_intersect(pnl, panel))
140*7c478bd9Sstevel@tonic-gate 		return ((_obscured_list *) 0);
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	obs = pnl -> obscured;
143*7c478bd9Sstevel@tonic-gate 	do {
144*7c478bd9Sstevel@tonic-gate 		prev_obs = obs;
145*7c478bd9Sstevel@tonic-gate 		obs = obs -> next;
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 	while (obs->panel_p != panel && obs != pnl->obscured);
148*7c478bd9Sstevel@tonic-gate 	if (obs -> panel_p != panel) {
149*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
150*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "_unlink_obs:  Obscured panel lost\n");
151*7c478bd9Sstevel@tonic-gate #endif
152*7c478bd9Sstevel@tonic-gate 		return ((_obscured_list *) 0);
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	if (obs == prev_obs)
156*7c478bd9Sstevel@tonic-gate 		pnl -> obscured = 0;
157*7c478bd9Sstevel@tonic-gate 	else {
158*7c478bd9Sstevel@tonic-gate 		prev_obs -> next = obs -> next;
159*7c478bd9Sstevel@tonic-gate 		if (obs == pnl -> obscured)
160*7c478bd9Sstevel@tonic-gate 			pnl -> obscured = prev_obs;
161*7c478bd9Sstevel@tonic-gate 	}
162*7c478bd9Sstevel@tonic-gate 	return (obs);
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate /*
166*7c478bd9Sstevel@tonic-gate  * add_obs - Add an obscured node to a panel, ensuring
167*7c478bd9Sstevel@tonic-gate  * that the obscured list is ordered from top to bottom.
168*7c478bd9Sstevel@tonic-gate  */
169*7c478bd9Sstevel@tonic-gate static void
add_obs(PANEL * panel,_obscured_list * obs)170*7c478bd9Sstevel@tonic-gate add_obs(PANEL *panel, _obscured_list *obs)
171*7c478bd9Sstevel@tonic-gate {
172*7c478bd9Sstevel@tonic-gate 	PANEL		*pnl;
173*7c478bd9Sstevel@tonic-gate 	_obscured_list	*curr_obs;
174*7c478bd9Sstevel@tonic-gate 	_obscured_list	*prev_obs;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	if ((prev_obs = panel -> obscured) == 0) {
177*7c478bd9Sstevel@tonic-gate 		panel -> obscured = obs -> next = obs;
178*7c478bd9Sstevel@tonic-gate 		return;
179*7c478bd9Sstevel@tonic-gate 	}
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	curr_obs = prev_obs -> next;
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	for (pnl = _Top_panel; pnl != panel; pnl = pnl->below) {
184*7c478bd9Sstevel@tonic-gate 		if (curr_obs -> panel_p == pnl) {
185*7c478bd9Sstevel@tonic-gate 			prev_obs = curr_obs;
186*7c478bd9Sstevel@tonic-gate 			curr_obs = curr_obs -> next;
187*7c478bd9Sstevel@tonic-gate 			if (prev_obs == panel -> obscured) {
188*7c478bd9Sstevel@tonic-gate 				panel -> obscured = obs;
189*7c478bd9Sstevel@tonic-gate 				break;
190*7c478bd9Sstevel@tonic-gate 			}
191*7c478bd9Sstevel@tonic-gate 		}
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	obs -> next = curr_obs;
195*7c478bd9Sstevel@tonic-gate 	prev_obs -> next = obs;
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate /*
200*7c478bd9Sstevel@tonic-gate  *  _intersect_panel
201*7c478bd9Sstevel@tonic-gate  * Create an obscured node for each panel that the given panel intersects.
202*7c478bd9Sstevel@tonic-gate  * The overlap record is always attached to the panel which is covered up.
203*7c478bd9Sstevel@tonic-gate  *
204*7c478bd9Sstevel@tonic-gate  * This routine assumes that _alloc_overlap() has been called to ensure
205*7c478bd9Sstevel@tonic-gate  * that there are enough overlap nodes to satisfy the requests.
206*7c478bd9Sstevel@tonic-gate  */
207*7c478bd9Sstevel@tonic-gate void
_intersect_panel(PANEL * panel)208*7c478bd9Sstevel@tonic-gate _intersect_panel(PANEL *panel)
209*7c478bd9Sstevel@tonic-gate {
210*7c478bd9Sstevel@tonic-gate 	PANEL		*pnl;
211*7c478bd9Sstevel@tonic-gate 	_obscured_list	*obs;
212*7c478bd9Sstevel@tonic-gate 	int		above_panel;
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	above_panel = FALSE;
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	for (pnl = _Bottom_panel; pnl; pnl = pnl -> above) {
217*7c478bd9Sstevel@tonic-gate 		if (pnl == panel) {
218*7c478bd9Sstevel@tonic-gate 			above_panel = TRUE;
219*7c478bd9Sstevel@tonic-gate 			continue;
220*7c478bd9Sstevel@tonic-gate 		}
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 		if (!_panels_intersect(pnl, panel))
223*7c478bd9Sstevel@tonic-gate 			continue;	/* no overlap */
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 		obs = _get_overlap();
226*7c478bd9Sstevel@tonic-gate 		obs->start = (panel->wstarty >= pnl->wstarty) ?
227*7c478bd9Sstevel@tonic-gate 				panel->wstarty : pnl->wstarty;
228*7c478bd9Sstevel@tonic-gate 		obs->end = (panel->wendy <= pnl->wendy) ?
229*7c478bd9Sstevel@tonic-gate 				panel->wendy : pnl->wendy;
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 		if (above_panel) {
232*7c478bd9Sstevel@tonic-gate 			obs -> panel_p = pnl;
233*7c478bd9Sstevel@tonic-gate 			if (panel -> obscured) {
234*7c478bd9Sstevel@tonic-gate 				obs -> next = panel -> obscured -> next;
235*7c478bd9Sstevel@tonic-gate 				panel -> obscured -> next = obs;
236*7c478bd9Sstevel@tonic-gate 			} else
237*7c478bd9Sstevel@tonic-gate 				obs -> next = panel -> obscured = obs;
238*7c478bd9Sstevel@tonic-gate 		} else {
239*7c478bd9Sstevel@tonic-gate 			obs -> panel_p = panel;
240*7c478bd9Sstevel@tonic-gate 			add_obs(pnl, obs);
241*7c478bd9Sstevel@tonic-gate 		}
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	}
244*7c478bd9Sstevel@tonic-gate }
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate /*
247*7c478bd9Sstevel@tonic-gate  *  _alloc_overlap
248*7c478bd9Sstevel@tonic-gate  * Create enough obscured nodes to record all overlaps of a given
249*7c478bd9Sstevel@tonic-gate  * panel.  The obscured nodes must be pre-allocated by this routine
250*7c478bd9Sstevel@tonic-gate  * to preserve the integrity of the pile during move.
251*7c478bd9Sstevel@tonic-gate  * If the move operation fails, the pile is supposed to remain
252*7c478bd9Sstevel@tonic-gate  * unchanged.  If the obscured nodes are not allocated in advance,
253*7c478bd9Sstevel@tonic-gate  * then an allocation failure in the middle of a move could
254*7c478bd9Sstevel@tonic-gate  * leave the pile in a corrupted state with possibly no way to
255*7c478bd9Sstevel@tonic-gate  * restore the pile to its original state.
256*7c478bd9Sstevel@tonic-gate  *
257*7c478bd9Sstevel@tonic-gate  * The cnt parameter is the(worst case) number of overlap nodes which
258*7c478bd9Sstevel@tonic-gate  * are required to satisfy any request.  Return 0 on error, else non-zero
259*7c478bd9Sstevel@tonic-gate  */
260*7c478bd9Sstevel@tonic-gate int
_alloc_overlap(int cnt)261*7c478bd9Sstevel@tonic-gate _alloc_overlap(int cnt)
262*7c478bd9Sstevel@tonic-gate {
263*7c478bd9Sstevel@tonic-gate 	_obscured_list	*overlap;
264*7c478bd9Sstevel@tonic-gate 	int		i;
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	for (i = cnt-_Free_list_cnt; i > 0; i--) {
267*7c478bd9Sstevel@tonic-gate 		if (!(overlap = (_obscured_list *)
268*7c478bd9Sstevel@tonic-gate 		    malloc(sizeof (_obscured_list))))
269*7c478bd9Sstevel@tonic-gate 			return (0);
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 		overlap -> next = _Free_list;
272*7c478bd9Sstevel@tonic-gate 		_Free_list = overlap;
273*7c478bd9Sstevel@tonic-gate 		_Free_list_cnt++;
274*7c478bd9Sstevel@tonic-gate 	}
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 	return (1);
277*7c478bd9Sstevel@tonic-gate }
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate /*
281*7c478bd9Sstevel@tonic-gate  * _free_overlap - Free a single overlap node.  Don't
282*7c478bd9Sstevel@tonic-gate  * really free it; just save it on a list.
283*7c478bd9Sstevel@tonic-gate  */
284*7c478bd9Sstevel@tonic-gate void
_free_overlap(_obscured_list * overlap)285*7c478bd9Sstevel@tonic-gate _free_overlap(_obscured_list *overlap)
286*7c478bd9Sstevel@tonic-gate {
287*7c478bd9Sstevel@tonic-gate 	overlap -> next = _Free_list;
288*7c478bd9Sstevel@tonic-gate 	_Free_list = overlap;
289*7c478bd9Sstevel@tonic-gate 	_Free_list_cnt++;
290*7c478bd9Sstevel@tonic-gate }
291