NeoMutt  2023-05-17-56-ga67199
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"
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 if (fg == COLOR_DEFAULT)
100 fg = COLOR_UNSET;
101 if (bg == COLOR_DEFAULT)
102 bg = COLOR_UNSET;
103
104#ifdef NEOMUTT_DIRECT_COLORS
105 int rc = init_extended_pair(index, fg, bg);
106 color_debug(LL_DEBUG5, "init_extended_pair(%d,%d,%d) -> %d\n", index, fg, bg, rc);
107#else
108 int rc = init_pair(index, fg, bg);
109 color_debug(LL_DEBUG5, "init_pair(%d,%d,%d) -> %d\n", index, fg, bg, rc);
110#endif
111
112 return index;
113}
114
120{
121 if (!ptr || !*ptr)
122 return;
123
124 struct CursesColor *cc = *ptr;
125 if (cc->ref_count > 1)
126 {
127 cc->ref_count--;
128 curses_color_dump(cc, "CursesColor rc--: ");
129 *ptr = NULL;
130 return;
131 }
132
133 curses_color_dump(cc, "free: ");
134 TAILQ_REMOVE(&CursesColors, cc, entries);
136 color_debug(LL_DEBUG5, "CursesColors: %d\n", NumCursesColors);
137 FREE(ptr);
138}
139
150{
151 color_debug(LL_DEBUG5, "fg %d, bg %d\n", fg, bg);
152 if (((fg == COLOR_UNSET) && (bg == COLOR_UNSET)) ||
153 ((fg == COLOR_DEFAULT) && (bg == COLOR_DEFAULT)))
154 {
155 color_debug(LL_DEBUG5, "both unset\n");
156 return NULL;
157 }
158
159 struct CursesColor *cc = curses_colors_find(fg, bg);
160 if (cc)
161 {
162 cc->ref_count++;
163 curses_color_dump(cc, "rc++: ");
164 return cc;
165 }
166
167 color_debug(LL_DEBUG5, "new curses\n");
169 if (index < 0)
170 return NULL;
171
172 struct CursesColor *cc_new = mutt_mem_calloc(1, sizeof(*cc_new));
174 color_debug(LL_DEBUG5, "CursesColor %p\n", cc_new);
175 cc_new->fg = fg;
176 cc_new->bg = bg;
177 cc_new->ref_count = 1;
178 cc_new->index = index;
179
180 // insert curses colour
181 TAILQ_FOREACH(cc, &CursesColors, entries)
182 {
183 if (cc->index > index)
184 {
185 color_debug(LL_DEBUG5, "insert\n");
186 TAILQ_INSERT_BEFORE(cc, cc_new, entries);
187 goto done;
188 }
189 }
190
191 TAILQ_INSERT_TAIL(&CursesColors, cc_new, entries);
192 color_debug(LL_DEBUG5, "tail\n");
193
194done:
195 curses_color_dump(cc_new, "CursesColor new: ");
196 color_debug(LL_DEBUG5, "CursesColors: %d\n", NumCursesColors);
197 return cc_new;
198}
#define COLOR_DEFAULT
Definition: color.h:102
#define COLOR_UNSET
Definition: color.h:103
int NumCursesColors
Number of ncurses colours left to allocate.
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:119
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:149
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: logging2.h:90
Convenience wrapper for the gui headers.
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
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