NeoMutt  2022-04-29-215-gc12b98
Teaching an old dog new tricks
DOXYGEN
curses.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stddef.h>
31#include <stdbool.h>
32#include "mutt/lib.h"
33#include "gui/lib.h" // IWYU pragma: keep
34#include "lib.h"
35
36struct CursesColorList CursesColors;
38
43{
44 color_debug(LL_DEBUG5, "init CursesColors\n");
47}
48
56{
57 struct CursesColor *cc = NULL;
58 TAILQ_FOREACH(cc, &CursesColors, entries)
59 {
60 if ((cc->fg == fg) && (cc->bg == bg))
61 {
62 return cc;
63 }
64 }
65
66 return NULL;
67}
68
75static int curses_color_init(int fg, int bg)
76{
77 color_debug(LL_DEBUG5, "find lowest index\n");
78 int index = 16;
79 struct CursesColor *cc = NULL;
80 TAILQ_FOREACH(cc, &CursesColors, entries)
81 {
82 if (cc->index == index)
83 index++;
84 else
85 break;
86 }
87 color_debug(LL_DEBUG5, "lowest index = %d\n", index);
88 if (index >= COLOR_PAIRS)
89 {
90 static bool warned = false;
91 if (!warned)
92 {
93 mutt_error(_("Too many colors: %d / %d"), index, COLOR_PAIRS);
94 warned = true;
95 }
96 return 0;
97 }
98
99 // const char *color = color_debug_log_color(fg, bg);
100 // printf("%s\n", color);
101 if (fg == COLOR_DEFAULT)
102 fg = COLOR_UNSET;
103 if (bg == COLOR_DEFAULT)
104 bg = COLOR_UNSET;
105
106 color_debug(LL_DEBUG5, "init_pair(%d,%d,%d) -> %d\n", index, fg, bg);
107 init_pair(index, fg, bg);
108
109 return index;
110}
111
117{
118 if (!ptr || !*ptr)
119 return;
120
121 struct CursesColor *cc = *ptr;
122 if (cc->ref_count > 1)
123 {
124 cc->ref_count--;
125 curses_color_dump(cc, "CursesColor rc--: ");
126 *ptr = NULL;
127 // curses_colors_dump();
128 return;
129 }
130
131 curses_color_dump(cc, "free: ");
132 TAILQ_REMOVE(&CursesColors, cc, entries);
134 color_debug(LL_DEBUG5, "CursesColors: %d\n", NumCursesColors);
135 // curses_colors_dump();
136 FREE(ptr);
137}
138
149{
150 color_debug(LL_DEBUG5, "fg %d, bg %d\n", fg, bg);
151 if (((fg == COLOR_UNSET) && (bg == COLOR_UNSET)) ||
152 ((fg == COLOR_DEFAULT) && (bg == COLOR_DEFAULT)))
153 {
154 color_debug(LL_DEBUG5, "both unset\n");
155 return NULL;
156 }
157
158 struct CursesColor *cc = curses_colors_find(fg, bg);
159 if (cc)
160 {
161 cc->ref_count++;
162 curses_color_dump(cc, "rc++: ");
163 return cc;
164 }
165
166 color_debug(LL_DEBUG5, "new curses\n");
168 if (index < 0)
169 return NULL;
170
171 struct CursesColor *cc_new = mutt_mem_calloc(1, sizeof(*cc_new));
173 color_debug(LL_DEBUG5, "CursesColor %p\n", cc_new);
174 cc_new->fg = fg;
175 cc_new->bg = bg;
176 cc_new->ref_count = 1;
177 cc_new->index = index;
178
179 // insert curses colour
180 TAILQ_FOREACH(cc, &CursesColors, entries)
181 {
182 if (cc->index > index)
183 {
184 color_debug(LL_DEBUG5, "insert\n");
185 TAILQ_INSERT_BEFORE(cc, cc_new, entries);
186 goto done;
187 }
188 }
189
190 TAILQ_INSERT_TAIL(&CursesColors, cc_new, entries);
191 color_debug(LL_DEBUG5, "tail\n");
192
193done:
194 curses_color_dump(cc_new, "CursesColor new: ");
195 color_debug(LL_DEBUG5, "CursesColors: %d\n", NumCursesColors);
196 return cc_new;
197}
#define COLOR_DEFAULT
Definition: color.h:100
#define COLOR_UNSET
Definition: color.h:101
int NumCursesColors
Definition: curses.c:37
static int curses_color_init(int fg, int bg)
Initialise a new Curses colour.
Definition: curses.c:75
void curses_color_free(struct CursesColor **ptr)
Free a CursesColor.
Definition: curses.c:116
struct CursesColor * curses_colors_find(int fg, int bg)
Find a Curses colour by foreground/background.
Definition: curses.c:55
struct CursesColor * curses_color_new(int fg, int bg)
Create a new CursesColor.
Definition: curses.c:148
struct CursesColorList CursesColors
List of all Curses colours.
Definition: curses.c:36
void curses_colors_init(void)
Initialise the Curses colours.
Definition: curses.c:42
int color_debug(enum LogLevel level, const char *format,...)
Write to the log file.
Definition: debug.c:44
void curses_color_dump(struct CursesColor *cc, const char *prefix)
Log one Curses colour.
Definition: debug.c:254
#define mutt_error(...)
Definition: logging.h:87
Convenience wrapper for the gui headers.
@ LL_DEBUG5
Log at debug level 5.
Definition: logging.h:44
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:43
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
#define TAILQ_INIT(head)
Definition: queue.h:765
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:809
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:841
#define TAILQ_INSERT_BEFORE(listelm, elm, field)
Definition: queue.h:786
Key value store.
Colour in the ncurses palette.
Definition: curses2.h:38
short index
Index number.
Definition: curses2.h:43
uint32_t fg
Foreground colour.
Definition: curses2.h:41
uint32_t bg
Background colour.
Definition: curses2.h:42
short ref_count
Number of users.
Definition: curses2.h:44