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