summaryrefslogtreecommitdiff
path: root/usr/src/lib/libeti/panel/common/misc.c
blob: 825a7ae569f48e8d04ad9090796ce27d3dc1eafe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved  	*/


/*
 *      Copyright (c) 1997, by Sun Microsystems, Inc.
 *      All rights reserved.
 */

/* A panels subsystem built on curses--Miscellaneous routines */

#pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.4	*/

/*LINTLIBRARY*/

#include <sys/types.h>
#include <stdlib.h>
#include <curses.h>
#include "private.h"

PANEL	*_Bottom_panel;
PANEL	*_Top_panel;
int	_Panel_cnt;

static	_obscured_list	*_Free_list;
static int	_Free_list_cnt;


/* panel_window - Return the window pointer */
WINDOW *
panel_window(PANEL *panel)
{
	return (panel ? panel -> win : 0);
}

/* panel_userptr - Return the user pointer */
char *
panel_userptr(PANEL *panel)
{
	return (panel ? panel -> user : 0);
}

/* set_panel_userptr - set the user pointer */
int
set_panel_userptr(PANEL *panel, char *ptr)
{
	if (panel) {
		panel -> user = ptr;
		return (OK);
	} else
		return (ERR);
}

/*
 * panel_above - Return the panel above the
 * given panel (or the bottom panel in 0)
 */
PANEL *
panel_above(PANEL *panel)
{

	if (!panel)
		return (_Bottom_panel);

	return ((panel == panel -> below) ? ((PANEL *) 0) : panel -> above);
}


/*
 * panel_below - Return the panel below the
 * given panel (or the top panel in 0)
 */
PANEL *
panel_below(PANEL *panel)
{

	if (!panel)
		return (_Top_panel);

	return ((panel == panel -> below) ? ((PANEL *) 0) : panel -> below);
}

/* panel_hidden - Return TRUE if the panel is hidden, FALSE if not.  */
int
panel_hidden(PANEL *panel)
{
	return ((!panel || (panel != panel -> below)) ? FALSE : TRUE);
}

/* _get_overlap - Get an overlap node from the free list. */
static _obscured_list *
_get_overlap(void)
{
	_obscured_list	*overlap;

	if (_Free_list_cnt-- > 0) {
		overlap = _Free_list;
		_Free_list = _Free_list -> next;
	} else {
		_Free_list_cnt = 0;
		overlap = 0;
	}

	return (overlap);
}


/*
 * _unlink_obs - Find the obscured node, if any,
 * in the first panel which refers the second panel.
 */
_obscured_list *
_unlink_obs(PANEL *pnl, PANEL *panel)
{
	_obscured_list	*obs;
	_obscured_list	*prev_obs;

	if (!pnl -> obscured || !_panels_intersect(pnl, panel))
		return ((_obscured_list *) 0);

	obs = pnl -> obscured;
	do {
		prev_obs = obs;
		obs = obs -> next;
	}
	while (obs->panel_p != panel && obs != pnl->obscured);
	if (obs -> panel_p != panel) {
#ifdef DEBUG
		fprintf(stderr, "_unlink_obs:  Obscured panel lost\n");
#endif
		return ((_obscured_list *) 0);
	}

	if (obs == prev_obs)
		pnl -> obscured = 0;
	else {
		prev_obs -> next = obs -> next;
		if (obs == pnl -> obscured)
			pnl -> obscured = prev_obs;
	}
	return (obs);
}

/*
 * add_obs - Add an obscured node to a panel, ensuring
 * that the obscured list is ordered from top to bottom.
 */
static void
add_obs(PANEL *panel, _obscured_list *obs)
{
	PANEL		*pnl;
	_obscured_list	*curr_obs;
	_obscured_list	*prev_obs;

	if ((prev_obs = panel -> obscured) == 0) {
		panel -> obscured = obs -> next = obs;
		return;
	}

	curr_obs = prev_obs -> next;

	for (pnl = _Top_panel; pnl != panel; pnl = pnl->below) {
		if (curr_obs -> panel_p == pnl) {
			prev_obs = curr_obs;
			curr_obs = curr_obs -> next;
			if (prev_obs == panel -> obscured) {
				panel -> obscured = obs;
				break;
			}
		}
	}

	obs -> next = curr_obs;
	prev_obs -> next = obs;
}


/*
 *  _intersect_panel
 * Create an obscured node for each panel that the given panel intersects.
 * The overlap record is always attached to the panel which is covered up.
 *
 * This routine assumes that _alloc_overlap() has been called to ensure
 * that there are enough overlap nodes to satisfy the requests.
 */
void
_intersect_panel(PANEL *panel)
{
	PANEL		*pnl;
	_obscured_list	*obs;
	int		above_panel;

	above_panel = FALSE;

	for (pnl = _Bottom_panel; pnl; pnl = pnl -> above) {
		if (pnl == panel) {
			above_panel = TRUE;
			continue;
		}

		if (!_panels_intersect(pnl, panel))
			continue;	/* no overlap */

		obs = _get_overlap();
		obs->start = (panel->wstarty >= pnl->wstarty) ?
				panel->wstarty : pnl->wstarty;
		obs->end = (panel->wendy <= pnl->wendy) ?
				panel->wendy : pnl->wendy;

		if (above_panel) {
			obs -> panel_p = pnl;
			if (panel -> obscured) {
				obs -> next = panel -> obscured -> next;
				panel -> obscured -> next = obs;
			} else
				obs -> next = panel -> obscured = obs;
		} else {
			obs -> panel_p = panel;
			add_obs(pnl, obs);
		}

	}
}

/*
 *  _alloc_overlap
 * Create enough obscured nodes to record all overlaps of a given
 * panel.  The obscured nodes must be pre-allocated by this routine
 * to preserve the integrity of the pile during move.
 * If the move operation fails, the pile is supposed to remain
 * unchanged.  If the obscured nodes are not allocated in advance,
 * then an allocation failure in the middle of a move could
 * leave the pile in a corrupted state with possibly no way to
 * restore the pile to its original state.
 *
 * The cnt parameter is the(worst case) number of overlap nodes which
 * are required to satisfy any request.  Return 0 on error, else non-zero
 */
int
_alloc_overlap(int cnt)
{
	_obscured_list	*overlap;
	int		i;

	for (i = cnt-_Free_list_cnt; i > 0; i--) {
		if (!(overlap = (_obscured_list *)
		    malloc(sizeof (_obscured_list))))
			return (0);

		overlap -> next = _Free_list;
		_Free_list = overlap;
		_Free_list_cnt++;
	}

	return (1);
}


/*
 * _free_overlap - Free a single overlap node.  Don't
 * really free it; just save it on a list.
 */
void
_free_overlap(_obscured_list *overlap)
{
	overlap -> next = _Free_list;
	_Free_list = overlap;
	_Free_list_cnt++;
}