NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
curses.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stdbool.h>
31#include <stddef.h>
32#include "mutt/lib.h"
33#include "gui/lib.h"
34#include "color.h"
35#include "curses2.h"
36#include "debug.h"
37
38struct CursesColorList CursesColors;
40
45{
46 color_debug(LL_DEBUG5, "init CursesColors\n");
49}
50
58{
59 struct CursesColor *cc = NULL;
60 TAILQ_FOREACH(cc, &CursesColors, entries)
61 {
62 if ((cc->fg == fg) && (cc->bg == bg))
63 {
64 curses_color_dump(cc, "find");
65 return cc;
66 }
67 }
68
69 return NULL;
70}
71
79{
80 color_debug(LL_DEBUG5, "find lowest index\n");
81 int index = 16;
82 struct CursesColor *cc = NULL;
83 TAILQ_FOREACH(cc, &CursesColors, entries)
84 {
85 if (cc->index == index)
86 index++;
87 else
88 break;
89 }
90 color_debug(LL_DEBUG5, "lowest index = %d\n", index);
91 if (index >= COLOR_PAIRS)
92 {
93 if (COLOR_PAIRS > 0)
94 {
95 static bool warned = false;
96 if (!warned)
97 {
98 mutt_error(_("Too many colors: %d / %d"), index, COLOR_PAIRS);
99 warned = true;
100 }
101 }
102 return 0;
103 }
104
105#ifdef NEOMUTT_DIRECT_COLORS
106 int rc = init_extended_pair(index, fg, bg);
107 color_debug(LL_DEBUG5, "init_extended_pair(%d,%d,%d) -> %d\n", index, fg, bg, rc);
108#else
109 int rc = init_pair(index, fg, bg);
110 color_debug(LL_DEBUG5, "init_pair(%d,%d,%d) -> %d\n", index, fg, bg, rc);
111#endif
112
113 return index;
114}
115
121{
122 if (!ptr || !*ptr)
123 return;
124
125 struct CursesColor *cc = *ptr;
126
127 cc->ref_count--;
128 if (cc->ref_count > 0)
129 {
130 curses_color_dump(cc, "curses rc--");
131 *ptr = NULL;
132 return;
133 }
134
135 curses_color_dump(cc, "curses free");
136 TAILQ_REMOVE(&CursesColors, cc, entries);
138 color_debug(LL_DEBUG5, "CursesColors: %d\n", NumCursesColors);
139 FREE(ptr);
140}
141
152{
153 color_debug(LL_DEBUG5, "fg %d, bg %d\n", fg, bg);
154 if ((fg == COLOR_DEFAULT) && (bg == COLOR_DEFAULT))
155 {
156 color_debug(LL_DEBUG5, "both unset\n");
157 return NULL;
158 }
159
160 struct CursesColor *cc = curses_colors_find(fg, bg);
161 if (cc)
162 {
163 cc->ref_count++;
164 curses_color_dump(cc, "curses rc++");
165 return cc;
166 }
167
168 color_debug(LL_DEBUG5, "new curses\n");
170 if (index == 0)
171 return NULL;
172
173 struct CursesColor *cc_new = MUTT_MEM_CALLOC(1, struct CursesColor);
175 color_debug(LL_DEBUG5, "CursesColor %p\n", (void *) cc_new);
176 cc_new->fg = fg;
177 cc_new->bg = bg;
178 cc_new->ref_count = 1;
179 cc_new->index = index;
180
181 // insert curses colour
182 TAILQ_FOREACH(cc, &CursesColors, entries)
183 {
184 if (cc->index > index)
185 {
186 color_debug(LL_DEBUG5, "insert\n");
187 TAILQ_INSERT_BEFORE(cc, cc_new, entries);
188 goto done;
189 }
190 }
191
192 TAILQ_INSERT_TAIL(&CursesColors, cc_new, entries);
193 color_debug(LL_DEBUG5, "tail\n");
194
195done:
196 curses_color_dump(cc_new, "curses new");
197 color_debug(LL_DEBUG5, "CursesColors: %d\n", NumCursesColors);
198 return cc_new;
199}
Color and attribute parsing.
#define COLOR_DEFAULT
Definition: color.h:96
Curses Colour.
int32_t color_t
Type for 24-bit colour value.
Definition: curses2.h:31
struct CursesColor * curses_color_new(color_t fg, color_t bg)
Create a new CursesColor.
Definition: curses.c:151
static int curses_color_init(color_t fg, color_t bg)
Initialise a new Curses colour.
Definition: curses.c:78
int NumCursesColors
Number of ncurses colours left to allocate.
Definition: curses.c:39
void curses_color_free(struct CursesColor **ptr)
Free a CursesColor.
Definition: curses.c:120
struct CursesColorList CursesColors
List of all Curses colours.
Definition: curses.c:38
void curses_colors_init(void)
Initialise the Curses colours.
Definition: curses.c:44
struct CursesColor * curses_colors_find(color_t fg, color_t bg)
Find a Curses colour by foreground/background.
Definition: curses.c:57
void curses_color_dump(struct CursesColor *cc, const char *prefix)
Log one Curses colour.
Definition: debug.c:122
Colour Debugging.
static int color_debug(enum LogLevel level, const char *format,...)
Definition: debug.h:52
#define mutt_error(...)
Definition: logging2.h:92
Convenience wrapper for the gui headers.
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:743
#define TAILQ_INIT(head)
Definition: queue.h:783
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:827
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:862
#define TAILQ_INSERT_BEFORE(listelm, elm, field)
Definition: queue.h:804
Colour in the ncurses palette.
Definition: curses2.h:41
color_t fg
Foreground colour.
Definition: curses2.h:42
color_t bg
Background colour.
Definition: curses2.h:43
short index
Index number.
Definition: curses2.h:44
short ref_count
Number of users.
Definition: curses2.h:45