1 /****************************************************************************
2 * Copyright (c) 2017 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Thomas E. Dickey *
31 ****************************************************************************/
35 * New color-pair functions, alloc_pair and free_pair
38 #define NEW_PAIR_INTERNAL 1
39 #include <curses.priv.h>
42 #define CUR SP_TERMTYPE
45 #ifdef USE_TERM_DRIVER
46 #define MaxColors InfoOf(SP_PARM).maxcolors
48 #define MaxColors max_colors
53 /* fix redefinition versys tic.h */
55 #define entry my_entry
57 #define ENTRY my_ENTRY
63 MODULE_ID("$Id: new_pair.c,v 1.8 2017/03/10 09:22:50 tom Exp $")
70 prev_len(SCREEN *sp, int pair)
74 colorpair_t *list = sp->_color_pairs;
75 while (list[pair].prev != base) {
77 pair = list[pair].prev;
83 next_len(SCREEN *sp, int pair)
87 colorpair_t *list = sp->_color_pairs;
88 while (list[pair].next != base) {
90 pair = list[pair].next;
96 * Trace the contents of LRU color-pairs.
99 dumpit(SCREEN *sp, int pair, const char *tag)
101 colorpair_t *list = sp->_color_pairs;
102 char bigbuf[256 * 20];
105 sprintf(p, "%s", tag);
107 for (n = 0; n < sp->_pair_limit; ++n) {
108 if (list[n].mode != cpFREE) {
109 sprintf(p, " %d%c(%d,%d)",
110 n, n == pair ? '@' : ':', list[n].next, list[n].prev);
114 T(("(%d/%d) %ld - %s",
117 strlen(bigbuf), bigbuf));
119 if (next_len(sp, 0) != prev_len(sp, 0)) {
125 #define dumpit(sp, pair, tag) /* nothing */
129 compare_data(const void *a, const void *b)
131 const colorpair_t *p = (const colorpair_t *) a;
132 const colorpair_t *q = (const colorpair_t *) b;
133 return ((p->fg == q->fg)
139 _nc_find_color_pair(SCREEN *sp, int fg, int bg)
147 if ((pp = tfind(&find, &sp->_ordered_pairs, compare_data)) != 0) {
148 colorpair_t *temp = *(colorpair_t **) pp;
149 result = (int) (temp - sp->_color_pairs);
157 delink_color_pair(SCREEN *sp, int pair)
159 colorpair_t *list = sp->_color_pairs;
160 int prev = list[pair].prev;
161 int next = list[pair].next;
163 /* delink this from its current location */
164 if (list[prev].next == pair &&
165 list[next].prev == pair) {
166 list[prev].next = next;
167 list[next].prev = prev;
168 dumpit(sp, pair, "delinked");
173 * Use this call to update the fast-index when modifying an entry in the color
177 _nc_reset_color_pair(SCREEN *sp, int pair, colorpair_t * next)
179 if (ValidPair(sp, pair)) {
180 colorpair_t *last = &(sp->_color_pairs[pair]);
181 delink_color_pair(sp, pair);
182 if (last->mode > cpFREE &&
183 (last->fg != next->fg || last->bg != next->bg)) {
184 /* remove the old entry from fast index */
185 tdelete(last, &sp->_ordered_pairs, compare_data);
186 /* create a new entry in fast index */
188 tsearch(last, &sp->_ordered_pairs, compare_data);
194 * Use this call to relink the newest pair to the front of the list, keeping
198 _nc_set_color_pair(SCREEN *sp, int pair, int mode)
200 if (ValidPair(sp, pair)) {
201 colorpair_t *list = sp->_color_pairs;
202 dumpit(sp, pair, "SET_PAIR");
203 list[0].mode = cpKEEP;
204 if (list[pair].mode <= cpFREE)
206 list[pair].mode = mode;
207 if (list[0].next != pair) {
208 /* link it at the front of the list */
209 list[pair].next = list[0].next;
210 list[list[pair].next].prev = pair;
214 dumpit(sp, pair, "...after");
219 NCURSES_SP_NAME(alloc_pair) (NCURSES_SP_DCLx int fg, int bg)
223 T((T_CALLED("alloc_pair(%d,%d)"), fg, bg));
224 if ((pair = _nc_find_color_pair(SP_PARM, fg, bg)) < 0) {
226 * Check if all of the slots have been used. If not, find one and
229 if (SP_PARM->_pairs_used + 1 < SP_PARM->_pair_limit) {
231 int hint = SP_PARM->_recent_pair;
234 * The linear search is done to allow mixing calls to init_pair()
235 * and alloc_pair(). The former can make gaps...
237 for (pair = hint + 1; pair < SP_PARM->_pair_limit; pair++) {
238 if (SP_PARM->_color_pairs[pair].mode == cpFREE) {
239 T(("found gap %d", pair));
245 for (pair = 1; pair <= hint; pair++) {
246 if (SP_PARM->_color_pairs[pair].mode == cpFREE) {
247 T(("found gap %d", pair));
254 SP_PARM->_recent_pair = pair;
259 /* reuse the oldest one */
260 pair = SP_PARM->_color_pairs[0].prev;
261 T(("reusing %d", pair));
264 if (pair > 0 && pair <= MAX_XCURSES_PAIR) {
265 IGNORE_RC(init_pair((short)pair, (short)fg, (short)bg));
274 NCURSES_SP_NAME(find_pair) (NCURSES_SP_DCLx int fg, int bg)
278 T((T_CALLED("find_pair(%d,%d)"), fg, bg));
279 pair = _nc_find_color_pair(SP_PARM, fg, bg);
284 NCURSES_SP_NAME(free_pair) (NCURSES_SP_DCLx int pair)
287 T((T_CALLED("free_pair(%d)"), pair));
288 if (ValidPair(SP_PARM, pair)) {
289 colorpair_t *cp = &(SP_PARM->_color_pairs[pair]);
291 _nc_change_pair(SP_PARM, pair);
292 delink_color_pair(SP_PARM, pair);
293 tdelete(cp, &SP_PARM->_ordered_pairs, compare_data);
296 SP_PARM->_pairs_used--;
304 alloc_pair(int f, int b)
306 return NCURSES_SP_NAME(alloc_pair) (CURRENT_SCREEN, f, b);
310 find_pair(int f, int b)
312 return NCURSES_SP_NAME(find_pair) (CURRENT_SCREEN, f, b);
318 return NCURSES_SP_NAME(free_pair) (CURRENT_SCREEN, pair);
324 _nc_new_pair_leaks(SCREEN *sp)
326 if (sp->_color_pairs) {
327 while (sp->_color_pairs[0].next) {
328 free_pair(sp->_color_pairs[0].next);
335 void _nc_new_pair(void);
340 #endif /* USE_NEW_PAIR */