]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/base/new_pair.c
ncurses 6.2 - patch 20210821
[ncurses.git] / ncurses / base / new_pair.c
index 271d494d8f884f3ad21102643b377541bfa5364b..70866c6368123dcc042fca77390d8679e1449f81 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 2017 Free Software Foundation, Inc.                        *
+ * Copyright 2018-2020,2021 Thomas E. Dickey                                *
+ * Copyright 2017 Free Software Foundation, Inc.                            *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -48,7 +49,7 @@
 #define MaxColors      max_colors
 #endif
 
-#if USE_NEW_PAIR
+#if NCURSES_EXT_COLORS
 
 /* fix redefinition versys tic.h */
 #undef entry
@@ -60,9 +61,9 @@
 
 #endif
 
-MODULE_ID("$Id: new_pair.c,v 1.8 2017/03/10 09:22:50 tom Exp $")
+MODULE_ID("$Id: new_pair.c,v 1.23 2021/08/16 22:11:26 tom Exp $")
 
-#if USE_NEW_PAIR
+#if NCURSES_EXT_COLORS
 
 #ifdef NEW_PAIR_DEBUG
 
@@ -102,13 +103,17 @@ dumpit(SCREEN *sp, int pair, const char *tag)
     char bigbuf[256 * 20];
     char *p = bigbuf;
     int n;
-    sprintf(p, "%s", tag);
-    p += strlen(p);
-    for (n = 0; n < sp->_pair_limit; ++n) {
+    size_t have = sizeof(bigbuf);
+
+    _nc_STRCPY(p, tag, have);
+    for (n = 0; n < sp->_pair_alloc; ++n) {
        if (list[n].mode != cpFREE) {
-           sprintf(p, " %d%c(%d,%d)",
-                   n, n == pair ? '@' : ':', list[n].next, list[n].prev);
            p += strlen(p);
+           if ((size_t) (p - bigbuf) + 50 > have)
+               break;
+           _nc_SPRINTF(p, _nc_SLIMIT(have - (p - bigbuf))
+                       " %d%c(%d,%d)",
+                       n, n == pair ? '@' : ':', list[n].next, list[n].prev);
        }
     }
     T(("(%d/%d) %ld - %s",
@@ -118,7 +123,7 @@ dumpit(SCREEN *sp, int pair, const char *tag)
 
     if (next_len(sp, 0) != prev_len(sp, 0)) {
        endwin();
-       exit(1);
+       ExitProgram(EXIT_FAILURE);
     }
 }
 #else
@@ -139,16 +144,16 @@ static int
 _nc_find_color_pair(SCREEN *sp, int fg, int bg)
 {
     colorpair_t find;
-    int result;
-    void *pp;
+    int result = -1;
 
     find.fg = fg;
     find.bg = bg;
-    if ((pp = tfind(&find, &sp->_ordered_pairs, compare_data)) != 0) {
-       colorpair_t *temp = *(colorpair_t **) pp;
-       result = (int) (temp - sp->_color_pairs);
-    } else {
-       result = -1;
+    if (sp != 0) {
+       void *pp;
+       if ((pp = tfind(&find, &sp->_ordered_pairs, compare_data)) != 0) {
+           colorpair_t *temp = *(colorpair_t **) pp;
+           result = (int) (temp - sp->_color_pairs);
+       }
     }
     return result;
 }
@@ -169,6 +174,20 @@ delink_color_pair(SCREEN *sp, int pair)
     }
 }
 
+/*
+ * Discard all nodes in the fast-index.
+ */
+NCURSES_EXPORT(void)
+_nc_free_ordered_pairs(SCREEN *sp)
+{
+    if (sp && sp->_ordered_pairs && sp->_pair_alloc) {
+       int n;
+       for (n = 0; n < sp->_pair_alloc; ++n) {
+           tdelete(&sp->_color_pairs[n], &sp->_ordered_pairs, compare_data);
+       }
+    }
+}
+
 /*
  * Use this call to update the fast-index when modifying an entry in the color
  * pair table.
@@ -176,13 +195,23 @@ delink_color_pair(SCREEN *sp, int pair)
 NCURSES_EXPORT(void)
 _nc_reset_color_pair(SCREEN *sp, int pair, colorpair_t * next)
 {
+    colorpair_t *last;
+
     if (ValidPair(sp, pair)) {
-       colorpair_t *last = &(sp->_color_pairs[pair]);
+       bool used;
+
+       ReservePairs(sp, pair);
+       last = &(sp->_color_pairs[pair]);
        delink_color_pair(sp, pair);
        if (last->mode > cpFREE &&
            (last->fg != next->fg || last->bg != next->bg)) {
            /* remove the old entry from fast index */
            tdelete(last, &sp->_ordered_pairs, compare_data);
+           used = FALSE;
+       } else {
+           used = (last->mode != cpFREE);
+       }
+       if (!used) {
            /* create a new entry in fast index */
            *last = *next;
            tsearch(last, &sp->_ordered_pairs, compare_data);
@@ -215,13 +244,31 @@ _nc_set_color_pair(SCREEN *sp, int pair, int mode)
     }
 }
 
+/*
+ * If we reallocate the color-pair array, we have to adjust the fast-index.
+ */
+NCURSES_EXPORT(void)
+_nc_copy_pairs(SCREEN *sp, colorpair_t * target, colorpair_t * source, int length)
+{
+    int n;
+    for (n = 0; n < length; ++n) {
+       void *find = tfind(source + n, &sp->_ordered_pairs, compare_data);
+       if (find != 0) {
+           tdelete(source + n, &sp->_ordered_pairs, compare_data);
+           tsearch(target + n, &sp->_ordered_pairs, compare_data);
+       }
+    }
+}
+
 NCURSES_EXPORT(int)
 NCURSES_SP_NAME(alloc_pair) (NCURSES_SP_DCLx int fg, int bg)
 {
     int pair;
 
     T((T_CALLED("alloc_pair(%d,%d)"), fg, bg));
-    if ((pair = _nc_find_color_pair(SP_PARM, fg, bg)) < 0) {
+    if (SP_PARM == 0) {
+       pair = -1;
+    } else if ((pair = _nc_find_color_pair(SP_PARM, fg, bg)) < 0) {
        /*
         * Check if all of the slots have been used.  If not, find one and
         * use that.
@@ -234,14 +281,23 @@ NCURSES_SP_NAME(alloc_pair) (NCURSES_SP_DCLx int fg, int bg)
             * The linear search is done to allow mixing calls to init_pair()
             * and alloc_pair().  The former can make gaps...
             */
-           for (pair = hint + 1; pair < SP_PARM->_pair_limit; pair++) {
+           for (pair = hint + 1; pair < SP_PARM->_pair_alloc; pair++) {
                if (SP_PARM->_color_pairs[pair].mode == cpFREE) {
                    T(("found gap %d", pair));
                    found = TRUE;
                    break;
                }
            }
-           if (!found) {
+           if (!found && (SP_PARM->_pair_alloc < SP_PARM->_pair_limit)) {
+               pair = SP_PARM->_pair_alloc;
+               ReservePairs(SP_PARM, pair);
+               if (SP_PARM->_color_pairs == 0) {
+                   pair = -1;
+               } else {
+                   found = TRUE;
+               }
+           }
+           if (!found && SP_PARM->_color_pairs != NULL) {
                for (pair = 1; pair <= hint; pair++) {
                    if (SP_PARM->_color_pairs[pair].mode == cpFREE) {
                        T(("found gap %d", pair));
@@ -261,11 +317,8 @@ NCURSES_SP_NAME(alloc_pair) (NCURSES_SP_DCLx int fg, int bg)
            T(("reusing %d", pair));
        }
 
-       if (pair > 0 && pair <= MAX_XCURSES_PAIR) {
-           IGNORE_RC(init_pair((short)pair, (short)fg, (short)bg));
-       } else {
+       if (_nc_init_pair(SP_PARM, pair, fg, bg) == ERR)
            pair = ERR;
-       }
     }
     returnCode(pair);
 }
@@ -285,7 +338,7 @@ NCURSES_SP_NAME(free_pair) (NCURSES_SP_DCLx int pair)
 {
     int result = ERR;
     T((T_CALLED("free_pair(%d)"), pair));
-    if (ValidPair(SP_PARM, pair)) {
+    if (ValidPair(SP_PARM, pair) && pair < SP_PARM->_pair_alloc) {
        colorpair_t *cp = &(SP_PARM->_color_pairs[pair]);
        if (pair != 0) {
            _nc_change_pair(SP_PARM, pair);
@@ -336,5 +389,5 @@ void _nc_new_pair(void);
 void
 _nc_new_pair(void)
 {
-};
-#endif /* USE_NEW_PAIR */
+}
+#endif /* NCURSES_EXT_COLORS */