xref: /openbsd/lib/libcurses/new_pair.h (revision c7ef0cfc)
1 /****************************************************************************
2  * Copyright 2018-2020,2021 Thomas E. Dickey                                *
3  * Copyright 2017 Free Software Foundation, Inc.                            *
4  *                                                                          *
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:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
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.                               *
23  *                                                                          *
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       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *  Author: Thomas E. Dickey                                                *
32  ****************************************************************************/
33 
34 /*
35  * Common type definitions and macros for new_pair.c, lib_color.c
36  *
37  * $Id: new_pair.h,v 1.1 2023/10/17 09:52:08 nicm Exp $
38  */
39 
40 #ifndef NEW_PAIR_H
41 #define NEW_PAIR_H 1
42 /* *INDENT-OFF* */
43 
44 #include <ncurses_cfg.h>
45 #include <ncurses_dll.h>
46 
47 #include <sys/types.h>
48 
49 #undef SCREEN
50 #define SCREEN struct screen
51 SCREEN;
52 
53 #define LIMIT_TYPED(n,t) \
54 	(t)(((n) > MAX_OF_TYPE(t)) \
55 	    ? MAX_OF_TYPE(t) \
56 	    : ((n) < -MAX_OF_TYPE(t)) \
57 	       ? -MAX_OF_TYPE(t) \
58 	       : (n))
59 
60 #define limit_COLOR(n) LIMIT_TYPED(n,NCURSES_COLOR_T)
61 #define limit_PAIRS(n) LIMIT_TYPED(n,NCURSES_PAIRS_T)
62 
63 #define MAX_XCURSES_PAIR MAX_OF_TYPE(NCURSES_PAIRS_T)
64 
65 #if NCURSES_EXT_COLORS
66 #define OPTIONAL_PAIR	GCC_UNUSED
67 #define get_extended_pair(opts, color_pair) \
68 	if ((opts) != NULL) { \
69 	    *(int*)(opts) = color_pair; \
70 	}
71 #define set_extended_pair(opts, color_pair) \
72 	if ((opts) != NULL) { \
73 	    color_pair = *(const int*)(opts); \
74 	}
75 #else
76 #define OPTIONAL_PAIR	/* nothing */
77 #define get_extended_pair(opts, color_pair) /* nothing */
78 #define set_extended_pair(opts, color_pair) \
79 	if ((opts) != NULL) { \
80 	    color_pair = -1; \
81 	}
82 #endif
83 
84 #ifdef NEW_PAIR_INTERNAL
85 
86 typedef enum {
87     cpKEEP = -1,		/* color pair 0 */
88     cpFREE = 0,			/* free for use */
89     cpINIT = 1			/* initialized */
90 } CPMODE;
91 
92 typedef struct _color_pairs
93 {
94     int fg;
95     int bg;
96 #if NCURSES_EXT_COLORS
97     int mode;			/* tells if the entry is allocated or free */
98     int prev;			/* index of previous item */
99     int next;			/* index of next item */
100 #endif
101 }
102 colorpair_t;
103 
104 #define MakeColorPair(target,f,b) target.fg = f, target.bg = b
105 #define isSamePair(a,b)		((a).fg == (b).fg && (a).bg == (b).bg)
106 #define FORE_OF(c)		(c).fg
107 #define BACK_OF(c)		(c).bg
108 
109 /*
110  * Ensure that we use color pairs only when colors have been started, and also
111  * that the index is within the limits of the table which we allocated.
112  */
113 #define ValidPair(sp,pair) \
114     ((sp != 0) && (pair >= 0) && (pair < sp->_pair_limit) && sp->_coloron)
115 
116 #if NCURSES_EXT_COLORS
117 extern NCURSES_EXPORT(void)     _nc_copy_pairs(SCREEN*, colorpair_t*, colorpair_t*, int);
118 extern NCURSES_EXPORT(void)     _nc_free_ordered_pairs(SCREEN*);
119 extern NCURSES_EXPORT(void)     _nc_reset_color_pair(SCREEN*, int, colorpair_t*);
120 extern NCURSES_EXPORT(void)     _nc_set_color_pair(SCREEN*, int, int);
121 #else
122 #define _nc_free_ordered_pairs(sp) /* nothing */
123 #define _nc_reset_color_pair(sp, pair, data) /* nothing */
124 #define _nc_set_color_pair(sp, pair, mode) /* nothing */
125 #endif
126 
127 #else
128 
129 typedef struct _color_pairs colorpair_t;
130 
131 #endif /* NEW_PAIR_INTERNAL */
132 
133 #if NO_LEAKS
134 extern NCURSES_EXPORT(void)     _nc_new_pair_leaks(SCREEN*);
135 #endif
136 
137 /* *INDENT-ON* */
138 
139 #endif /* NEW_PAIR_H */
140