1 /****************************************************************************
2 * Copyright 2018-2020,2021 Thomas E. Dickey *
3 * Copyright 2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Thomas E. Dickey *
32 ****************************************************************************/
36 * New color-pair functions, alloc_pair and free_pair
39 #define NEW_PAIR_INTERNAL 1
40 #include <curses.priv.h>
43 #define CUR SP_TERMTYPE
46 #ifdef USE_TERM_DRIVER
47 #define MaxColors InfoOf(SP_PARM).maxcolors
49 #define MaxColors max_colors
52 #if NCURSES_EXT_COLORS
54 /* fix redefinition versys tic.h */
56 #define entry my_entry
58 #define ENTRY my_ENTRY
64 MODULE_ID("$Id: new_pair.c,v 1.23 2021/08/16 22:11:26 tom Exp $")
66 #if NCURSES_EXT_COLORS
71 prev_len(SCREEN *sp, int pair)
75 colorpair_t *list = sp->_color_pairs;
76 while (list[pair].prev != base) {
78 pair = list[pair].prev;
84 next_len(SCREEN *sp, int pair)
88 colorpair_t *list = sp->_color_pairs;
89 while (list[pair].next != base) {
91 pair = list[pair].next;
97 * Trace the contents of LRU color-pairs.
100 dumpit(SCREEN *sp, int pair, const char *tag)
102 colorpair_t *list = sp->_color_pairs;
103 char bigbuf[256 * 20];
106 size_t have = sizeof(bigbuf);
108 _nc_STRCPY(p, tag, have);
109 for (n = 0; n < sp->_pair_alloc; ++n) {
110 if (list[n].mode != cpFREE) {
112 if ((size_t) (p - bigbuf) + 50 > have)
114 _nc_SPRINTF(p, _nc_SLIMIT(have - (p - bigbuf))
116 n, n == pair ? '@' : ':', list[n].next, list[n].prev);
119 T(("(%d/%d) %ld - %s",
122 strlen(bigbuf), bigbuf));
124 if (next_len(sp, 0) != prev_len(sp, 0)) {
126 ExitProgram(EXIT_FAILURE);
130 #define dumpit(sp, pair, tag) /* nothing */
134 compare_data(const void *a, const void *b)
136 const colorpair_t *p = (const colorpair_t *) a;
137 const colorpair_t *q = (const colorpair_t *) b;
138 return ((p->fg == q->fg)
144 _nc_find_color_pair(SCREEN *sp, int fg, int bg)
153 if ((pp = tfind(&find, &sp->_ordered_pairs, compare_data)) != 0) {
154 colorpair_t *temp = *(colorpair_t **) pp;
155 result = (int) (temp - sp->_color_pairs);
162 delink_color_pair(SCREEN *sp, int pair)
164 colorpair_t *list = sp->_color_pairs;
165 int prev = list[pair].prev;
166 int next = list[pair].next;
168 /* delink this from its current location */
169 if (list[prev].next == pair &&
170 list[next].prev == pair) {
171 list[prev].next = next;
172 list[next].prev = prev;
173 dumpit(sp, pair, "delinked");
178 * Discard all nodes in the fast-index.
181 _nc_free_ordered_pairs(SCREEN *sp)
183 if (sp && sp->_ordered_pairs && sp->_pair_alloc) {
185 for (n = 0; n < sp->_pair_alloc; ++n) {
186 tdelete(&sp->_color_pairs[n], &sp->_ordered_pairs, compare_data);
192 * Use this call to update the fast-index when modifying an entry in the color
196 _nc_reset_color_pair(SCREEN *sp, int pair, colorpair_t * next)
200 if (ValidPair(sp, pair)) {
203 ReservePairs(sp, pair);
204 last = &(sp->_color_pairs[pair]);
205 delink_color_pair(sp, pair);
206 if (last->mode > cpFREE &&
207 (last->fg != next->fg || last->bg != next->bg)) {
208 /* remove the old entry from fast index */
209 tdelete(last, &sp->_ordered_pairs, compare_data);
212 used = (last->mode != cpFREE);
215 /* create a new entry in fast index */
217 tsearch(last, &sp->_ordered_pairs, compare_data);
223 * Use this call to relink the newest pair to the front of the list, keeping
227 _nc_set_color_pair(SCREEN *sp, int pair, int mode)
229 if (ValidPair(sp, pair)) {
230 colorpair_t *list = sp->_color_pairs;
231 dumpit(sp, pair, "SET_PAIR");
232 list[0].mode = cpKEEP;
233 if (list[pair].mode <= cpFREE)
235 list[pair].mode = mode;
236 if (list[0].next != pair) {
237 /* link it at the front of the list */
238 list[pair].next = list[0].next;
239 list[list[pair].next].prev = pair;
243 dumpit(sp, pair, "...after");
248 * If we reallocate the color-pair array, we have to adjust the fast-index.
251 _nc_copy_pairs(SCREEN *sp, colorpair_t * target, colorpair_t * source, int length)
254 for (n = 0; n < length; ++n) {
255 void *find = tfind(source + n, &sp->_ordered_pairs, compare_data);
257 tdelete(source + n, &sp->_ordered_pairs, compare_data);
258 tsearch(target + n, &sp->_ordered_pairs, compare_data);
264 NCURSES_SP_NAME(alloc_pair) (NCURSES_SP_DCLx int fg, int bg)
268 T((T_CALLED("alloc_pair(%d,%d)"), fg, bg));
271 } else if ((pair = _nc_find_color_pair(SP_PARM, fg, bg)) < 0) {
273 * Check if all of the slots have been used. If not, find one and
276 if (SP_PARM->_pairs_used + 1 < SP_PARM->_pair_limit) {
278 int hint = SP_PARM->_recent_pair;
281 * The linear search is done to allow mixing calls to init_pair()
282 * and alloc_pair(). The former can make gaps...
284 for (pair = hint + 1; pair < SP_PARM->_pair_alloc; pair++) {
285 if (SP_PARM->_color_pairs[pair].mode == cpFREE) {
286 T(("found gap %d", pair));
291 if (!found && (SP_PARM->_pair_alloc < SP_PARM->_pair_limit)) {
292 pair = SP_PARM->_pair_alloc;
293 ReservePairs(SP_PARM, pair);
294 if (SP_PARM->_color_pairs == 0) {
300 if (!found && SP_PARM->_color_pairs != NULL) {
301 for (pair = 1; pair <= hint; pair++) {
302 if (SP_PARM->_color_pairs[pair].mode == cpFREE) {
303 T(("found gap %d", pair));
310 SP_PARM->_recent_pair = pair;
315 /* reuse the oldest one */
316 pair = SP_PARM->_color_pairs[0].prev;
317 T(("reusing %d", pair));
320 if (_nc_init_pair(SP_PARM, pair, fg, bg) == ERR)
327 NCURSES_SP_NAME(find_pair) (NCURSES_SP_DCLx int fg, int bg)
331 T((T_CALLED("find_pair(%d,%d)"), fg, bg));
332 pair = _nc_find_color_pair(SP_PARM, fg, bg);
337 NCURSES_SP_NAME(free_pair) (NCURSES_SP_DCLx int pair)
340 T((T_CALLED("free_pair(%d)"), pair));
341 if (ValidPair(SP_PARM, pair) && pair < SP_PARM->_pair_alloc) {
342 colorpair_t *cp = &(SP_PARM->_color_pairs[pair]);
344 _nc_change_pair(SP_PARM, pair);
345 delink_color_pair(SP_PARM, pair);
346 tdelete(cp, &SP_PARM->_ordered_pairs, compare_data);
349 SP_PARM->_pairs_used--;
357 alloc_pair(int f, int b)
359 return NCURSES_SP_NAME(alloc_pair) (CURRENT_SCREEN, f, b);
363 find_pair(int f, int b)
365 return NCURSES_SP_NAME(find_pair) (CURRENT_SCREEN, f, b);
371 return NCURSES_SP_NAME(free_pair) (CURRENT_SCREEN, pair);
377 _nc_new_pair_leaks(SCREEN *sp)
379 if (sp->_color_pairs) {
380 while (sp->_color_pairs[0].next) {
381 free_pair(sp->_color_pairs[0].next);
388 void _nc_new_pair(void);
393 #endif /* NCURSES_EXT_COLORS */