xref: /illumos-gate/usr/src/lib/libeti/panel/common/misc.c (revision 1da57d55)
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 /*LINTLIBRARY*/
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
36*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
37*7c478bd9Sstevel@tonic-gate #include <curses.h>
38*7c478bd9Sstevel@tonic-gate #include "private.h"
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate PANEL	*_Bottom_panel;
41*7c478bd9Sstevel@tonic-gate PANEL	*_Top_panel;
42*7c478bd9Sstevel@tonic-gate int	_Panel_cnt;
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate static	_obscured_list	*_Free_list;
45*7c478bd9Sstevel@tonic-gate static int	_Free_list_cnt;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /* panel_window - Return the window pointer */
49*7c478bd9Sstevel@tonic-gate WINDOW *
panel_window(PANEL * panel)50*7c478bd9Sstevel@tonic-gate panel_window(PANEL *panel)
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate 	return (panel ? panel -> win : 0);
53*7c478bd9Sstevel@tonic-gate }
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate /* panel_userptr - Return the user pointer */
56*7c478bd9Sstevel@tonic-gate char *
panel_userptr(PANEL * panel)57*7c478bd9Sstevel@tonic-gate panel_userptr(PANEL *panel)
58*7c478bd9Sstevel@tonic-gate {
59*7c478bd9Sstevel@tonic-gate 	return (panel ? panel -> user : 0);
60*7c478bd9Sstevel@tonic-gate }
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate /* set_panel_userptr - set the user pointer */
63*7c478bd9Sstevel@tonic-gate int
set_panel_userptr(PANEL * panel,char * ptr)64*7c478bd9Sstevel@tonic-gate set_panel_userptr(PANEL *panel, char *ptr)
65*7c478bd9Sstevel@tonic-gate {
66*7c478bd9Sstevel@tonic-gate 	if (panel) {
67*7c478bd9Sstevel@tonic-gate 		panel -> user = ptr;
68*7c478bd9Sstevel@tonic-gate 		return (OK);
69*7c478bd9Sstevel@tonic-gate 	} else
70*7c478bd9Sstevel@tonic-gate 		return (ERR);
71*7c478bd9Sstevel@tonic-gate }
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate /*
74*7c478bd9Sstevel@tonic-gate  * panel_above - Return the panel above the
75*7c478bd9Sstevel@tonic-gate  * given panel (or the bottom panel in 0)
76*7c478bd9Sstevel@tonic-gate  */
77*7c478bd9Sstevel@tonic-gate PANEL *
panel_above(PANEL * panel)78*7c478bd9Sstevel@tonic-gate panel_above(PANEL *panel)
79*7c478bd9Sstevel@tonic-gate {
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	if (!panel)
82*7c478bd9Sstevel@tonic-gate 		return (_Bottom_panel);
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	return ((panel == panel -> below) ? ((PANEL *) 0) : panel -> above);
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate /*
89*7c478bd9Sstevel@tonic-gate  * panel_below - Return the panel below the
90*7c478bd9Sstevel@tonic-gate  * given panel (or the top panel in 0)
91*7c478bd9Sstevel@tonic-gate  */
92*7c478bd9Sstevel@tonic-gate PANEL *
panel_below(PANEL * panel)93*7c478bd9Sstevel@tonic-gate panel_below(PANEL *panel)
94*7c478bd9Sstevel@tonic-gate {
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	if (!panel)
97*7c478bd9Sstevel@tonic-gate 		return (_Top_panel);
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	return ((panel == panel -> below) ? ((PANEL *) 0) : panel -> below);
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate /* panel_hidden - Return TRUE if the panel is hidden, FALSE if not.  */
103*7c478bd9Sstevel@tonic-gate int
panel_hidden(PANEL * panel)104*7c478bd9Sstevel@tonic-gate panel_hidden(PANEL *panel)
105*7c478bd9Sstevel@tonic-gate {
106*7c478bd9Sstevel@tonic-gate 	return ((!panel || (panel != panel -> below)) ? FALSE : TRUE);
107*7c478bd9Sstevel@tonic-gate }
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate /* _get_overlap - Get an overlap node from the free list. */
110*7c478bd9Sstevel@tonic-gate static _obscured_list *
_get_overlap(void)111*7c478bd9Sstevel@tonic-gate _get_overlap(void)
112*7c478bd9Sstevel@tonic-gate {
113*7c478bd9Sstevel@tonic-gate 	_obscured_list	*overlap;
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	if (_Free_list_cnt-- > 0) {
116*7c478bd9Sstevel@tonic-gate 		overlap = _Free_list;
117*7c478bd9Sstevel@tonic-gate 		_Free_list = _Free_list -> next;
118*7c478bd9Sstevel@tonic-gate 	} else {
119*7c478bd9Sstevel@tonic-gate 		_Free_list_cnt = 0;
120*7c478bd9Sstevel@tonic-gate 		overlap = 0;
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	return (overlap);
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate /*
128*7c478bd9Sstevel@tonic-gate  * _unlink_obs - Find the obscured node, if any,
129*7c478bd9Sstevel@tonic-gate  * in the first panel which refers the second panel.
130*7c478bd9Sstevel@tonic-gate  */
131*7c478bd9Sstevel@tonic-gate _obscured_list *
_unlink_obs(PANEL * pnl,PANEL * panel)132*7c478bd9Sstevel@tonic-gate _unlink_obs(PANEL *pnl, PANEL *panel)
133*7c478bd9Sstevel@tonic-gate {
134*7c478bd9Sstevel@tonic-gate 	_obscured_list	*obs;
135*7c478bd9Sstevel@tonic-gate 	_obscured_list	*prev_obs;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	if (!pnl -> obscured || !_panels_intersect(pnl, panel))
138*7c478bd9Sstevel@tonic-gate 		return ((_obscured_list *) 0);
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	obs = pnl -> obscured;
141*7c478bd9Sstevel@tonic-gate 	do {
142*7c478bd9Sstevel@tonic-gate 		prev_obs = obs;
143*7c478bd9Sstevel@tonic-gate 		obs = obs -> next;
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 	while (obs->panel_p != panel && obs != pnl->obscured);
146*7c478bd9Sstevel@tonic-gate 	if (obs -> panel_p != panel) {
147*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
148*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "_unlink_obs:  Obscured panel lost\n");
149*7c478bd9Sstevel@tonic-gate #endif
150*7c478bd9Sstevel@tonic-gate 		return ((_obscured_list *) 0);
151*7c478bd9Sstevel@tonic-gate 	}
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	if (obs == prev_obs)
154*7c478bd9Sstevel@tonic-gate 		pnl -> obscured = 0;
155*7c478bd9Sstevel@tonic-gate 	else {
156*7c478bd9Sstevel@tonic-gate 		prev_obs -> next = obs -> next;
157*7c478bd9Sstevel@tonic-gate 		if (obs == pnl -> obscured)
158*7c478bd9Sstevel@tonic-gate 			pnl -> obscured = prev_obs;
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 	return (obs);
161*7c478bd9Sstevel@tonic-gate }
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate /*
164*7c478bd9Sstevel@tonic-gate  * add_obs - Add an obscured node to a panel, ensuring
165*7c478bd9Sstevel@tonic-gate  * that the obscured list is ordered from top to bottom.
166*7c478bd9Sstevel@tonic-gate  */
167*7c478bd9Sstevel@tonic-gate static void
add_obs(PANEL * panel,_obscured_list * obs)168*7c478bd9Sstevel@tonic-gate add_obs(PANEL *panel, _obscured_list *obs)
169*7c478bd9Sstevel@tonic-gate {
170*7c478bd9Sstevel@tonic-gate 	PANEL		*pnl;
171*7c478bd9Sstevel@tonic-gate 	_obscured_list	*curr_obs;
172*7c478bd9Sstevel@tonic-gate 	_obscured_list	*prev_obs;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	if ((prev_obs = panel -> obscured) == 0) {
175*7c478bd9Sstevel@tonic-gate 		panel -> obscured = obs -> next = obs;
176*7c478bd9Sstevel@tonic-gate 		return;
177*7c478bd9Sstevel@tonic-gate 	}
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	curr_obs = prev_obs -> next;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	for (pnl = _Top_panel; pnl != panel; pnl = pnl->below) {
182*7c478bd9Sstevel@tonic-gate 		if (curr_obs -> panel_p == pnl) {
183*7c478bd9Sstevel@tonic-gate 			prev_obs = curr_obs;
184*7c478bd9Sstevel@tonic-gate 			curr_obs = curr_obs -> next;
185*7c478bd9Sstevel@tonic-gate 			if (prev_obs == panel -> obscured) {
186*7c478bd9Sstevel@tonic-gate 				panel -> obscured = obs;
187*7c478bd9Sstevel@tonic-gate 				break;
188*7c478bd9Sstevel@tonic-gate 			}
189*7c478bd9Sstevel@tonic-gate 		}
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	obs -> next = curr_obs;
193*7c478bd9Sstevel@tonic-gate 	prev_obs -> next = obs;
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate /*
198*7c478bd9Sstevel@tonic-gate  *  _intersect_panel
199*7c478bd9Sstevel@tonic-gate  * Create an obscured node for each panel that the given panel intersects.
200*7c478bd9Sstevel@tonic-gate  * The overlap record is always attached to the panel which is covered up.
201*7c478bd9Sstevel@tonic-gate  *
202*7c478bd9Sstevel@tonic-gate  * This routine assumes that _alloc_overlap() has been called to ensure
203*7c478bd9Sstevel@tonic-gate  * that there are enough overlap nodes to satisfy the requests.
204*7c478bd9Sstevel@tonic-gate  */
205*7c478bd9Sstevel@tonic-gate void
_intersect_panel(PANEL * panel)206*7c478bd9Sstevel@tonic-gate _intersect_panel(PANEL *panel)
207*7c478bd9Sstevel@tonic-gate {
208*7c478bd9Sstevel@tonic-gate 	PANEL		*pnl;
209*7c478bd9Sstevel@tonic-gate 	_obscured_list	*obs;
210*7c478bd9Sstevel@tonic-gate 	int		above_panel;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	above_panel = FALSE;
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	for (pnl = _Bottom_panel; pnl; pnl = pnl -> above) {
215*7c478bd9Sstevel@tonic-gate 		if (pnl == panel) {
216*7c478bd9Sstevel@tonic-gate 			above_panel = TRUE;
217*7c478bd9Sstevel@tonic-gate 			continue;
218*7c478bd9Sstevel@tonic-gate 		}
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 		if (!_panels_intersect(pnl, panel))
221*7c478bd9Sstevel@tonic-gate 			continue;	/* no overlap */
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 		obs = _get_overlap();
224*7c478bd9Sstevel@tonic-gate 		obs->start = (panel->wstarty >= pnl->wstarty) ?
225*7c478bd9Sstevel@tonic-gate 				panel->wstarty : pnl->wstarty;
226*7c478bd9Sstevel@tonic-gate 		obs->end = (panel->wendy <= pnl->wendy) ?
227*7c478bd9Sstevel@tonic-gate 				panel->wendy : pnl->wendy;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 		if (above_panel) {
230*7c478bd9Sstevel@tonic-gate 			obs -> panel_p = pnl;
231*7c478bd9Sstevel@tonic-gate 			if (panel -> obscured) {
232*7c478bd9Sstevel@tonic-gate 				obs -> next = panel -> obscured -> next;
233*7c478bd9Sstevel@tonic-gate 				panel -> obscured -> next = obs;
234*7c478bd9Sstevel@tonic-gate 			} else
235*7c478bd9Sstevel@tonic-gate 				obs -> next = panel -> obscured = obs;
236*7c478bd9Sstevel@tonic-gate 		} else {
237*7c478bd9Sstevel@tonic-gate 			obs -> panel_p = panel;
238*7c478bd9Sstevel@tonic-gate 			add_obs(pnl, obs);
239*7c478bd9Sstevel@tonic-gate 		}
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	}
242*7c478bd9Sstevel@tonic-gate }
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate /*
245*7c478bd9Sstevel@tonic-gate  *  _alloc_overlap
246*7c478bd9Sstevel@tonic-gate  * Create enough obscured nodes to record all overlaps of a given
247*7c478bd9Sstevel@tonic-gate  * panel.  The obscured nodes must be pre-allocated by this routine
248*7c478bd9Sstevel@tonic-gate  * to preserve the integrity of the pile during move.
249*7c478bd9Sstevel@tonic-gate  * If the move operation fails, the pile is supposed to remain
250*7c478bd9Sstevel@tonic-gate  * unchanged.  If the obscured nodes are not allocated in advance,
251*7c478bd9Sstevel@tonic-gate  * then an allocation failure in the middle of a move could
252*7c478bd9Sstevel@tonic-gate  * leave the pile in a corrupted state with possibly no way to
253*7c478bd9Sstevel@tonic-gate  * restore the pile to its original state.
254*7c478bd9Sstevel@tonic-gate  *
255*7c478bd9Sstevel@tonic-gate  * The cnt parameter is the(worst case) number of overlap nodes which
256*7c478bd9Sstevel@tonic-gate  * are required to satisfy any request.  Return 0 on error, else non-zero
257*7c478bd9Sstevel@tonic-gate  */
258*7c478bd9Sstevel@tonic-gate int
_alloc_overlap(int cnt)259*7c478bd9Sstevel@tonic-gate _alloc_overlap(int cnt)
260*7c478bd9Sstevel@tonic-gate {
261*7c478bd9Sstevel@tonic-gate 	_obscured_list	*overlap;
262*7c478bd9Sstevel@tonic-gate 	int		i;
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 	for (i = cnt-_Free_list_cnt; i > 0; i--) {
265*7c478bd9Sstevel@tonic-gate 		if (!(overlap = (_obscured_list *)
266*7c478bd9Sstevel@tonic-gate 		    malloc(sizeof (_obscured_list))))
267*7c478bd9Sstevel@tonic-gate 			return (0);
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 		overlap -> next = _Free_list;
270*7c478bd9Sstevel@tonic-gate 		_Free_list = overlap;
271*7c478bd9Sstevel@tonic-gate 		_Free_list_cnt++;
272*7c478bd9Sstevel@tonic-gate 	}
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 	return (1);
275*7c478bd9Sstevel@tonic-gate }
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate /*
279*7c478bd9Sstevel@tonic-gate  * _free_overlap - Free a single overlap node.  Don't
280*7c478bd9Sstevel@tonic-gate  * really free it; just save it on a list.
281*7c478bd9Sstevel@tonic-gate  */
282*7c478bd9Sstevel@tonic-gate void
_free_overlap(_obscured_list * overlap)283*7c478bd9Sstevel@tonic-gate _free_overlap(_obscured_list *overlap)
284*7c478bd9Sstevel@tonic-gate {
285*7c478bd9Sstevel@tonic-gate 	overlap -> next = _Free_list;
286*7c478bd9Sstevel@tonic-gate 	_Free_list = overlap;
287*7c478bd9Sstevel@tonic-gate 	_Free_list_cnt++;
288*7c478bd9Sstevel@tonic-gate }
289