NeoMutt  2025-12-11-769-g906513
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
curses.c
Go to the documentation of this file.
1
22
28
29#include "config.h"
30#include <stdbool.h>
31#include <stddef.h>
32#include "mutt/lib.h"
33#include "core/lib.h"
34#include "gui/lib.h"
35#include "color.h"
36#include "curses2.h"
37#include "debug.h"
38#include "module_data.h"
39
44{
46 color_debug(LL_DEBUG5, "init CursesColors\n");
47 TAILQ_INIT(&mod_data->curses_colors);
48 mod_data->num_curses_colors = 0;
49}
50
58{
60 struct CursesColor *cc = NULL;
61 TAILQ_FOREACH(cc, &mod_data->curses_colors, entries)
62 {
63 if ((cc->fg == fg) && (cc->bg == bg))
64 {
65 curses_color_dump(cc, "find");
66 return cc;
67 }
68 }
69
70 return NULL;
71}
72
80{
82 color_debug(LL_DEBUG5, "find lowest index\n");
83 int index = 16;
84 struct CursesColor *cc = NULL;
85 TAILQ_FOREACH(cc, &mod_data->curses_colors, entries)
86 {
87 if (cc->index == index)
88 index++;
89 else
90 break;
91 }
92 color_debug(LL_DEBUG5, "lowest index = %d\n", index);
93 if (index >= COLOR_PAIRS)
94 {
95 if (COLOR_PAIRS > 0)
96 {
97 static bool warned = false;
98 if (!warned)
99 {
100 mutt_error(_("Too many colors: %d / %d"), index, COLOR_PAIRS);
101 warned = true;
102 }
103 }
104 return 0;
105 }
106
107#ifdef NEOMUTT_DIRECT_COLORS
108 int rc = init_extended_pair(index, fg, bg);
109 color_debug(LL_DEBUG5, "init_extended_pair(%d,%d,%d) -> %d\n", index, fg, bg, rc);
110#else
111 int rc = init_pair(index, fg, bg);
112 color_debug(LL_DEBUG5, "init_pair(%d,%d,%d) -> %d\n", index, fg, bg, rc);
113#endif
114
115 return index;
116}
117
123{
124 if (!ptr || !*ptr)
125 return;
126
127 struct CursesColor *cc = *ptr;
128
129 cc->ref_count--;
130 if (cc->ref_count > 0)
131 {
132 curses_color_dump(cc, "curses rc--");
133 *ptr = NULL;
134 return;
135 }
136
137 curses_color_dump(cc, "curses free");
139 TAILQ_REMOVE(&mod_data->curses_colors, cc, entries);
140 mod_data->num_curses_colors--;
141 color_debug(LL_DEBUG5, "CursesColors: %d\n", mod_data->num_curses_colors);
142 FREE(ptr);
143}
144
155{
156 color_debug(LL_DEBUG5, "fg %d, bg %d\n", fg, bg);
157 if ((fg == COLOR_DEFAULT) && (bg == COLOR_DEFAULT))
158 {
159 color_debug(LL_DEBUG5, "both unset\n");
160 return NULL;
161 }
162
163 struct CursesColor *cc = curses_colors_find(fg, bg);
164 if (cc)
165 {
166 cc->ref_count++;
167 curses_color_dump(cc, "curses rc++");
168 return cc;
169 }
170
171 color_debug(LL_DEBUG5, "new curses\n");
173 if (index == 0)
174 return NULL;
175
177 struct CursesColor *cc_new = MUTT_MEM_CALLOC(1, struct CursesColor);
178 mod_data->num_curses_colors++;
179 color_debug(LL_DEBUG5, "CursesColor %p\n", (void *) cc_new);
180 cc_new->fg = fg;
181 cc_new->bg = bg;
182 cc_new->ref_count = 1;
183 cc_new->index = index;
184
185 // insert curses colour
186 TAILQ_FOREACH(cc, &mod_data->curses_colors, entries)
187 {
188 if (cc->index > index)
189 {
190 color_debug(LL_DEBUG5, "insert\n");
191 TAILQ_INSERT_BEFORE(cc, cc_new, entries);
192 goto done;
193 }
194 }
195
196 TAILQ_INSERT_TAIL(&mod_data->curses_colors, cc_new, entries);
197 color_debug(LL_DEBUG5, "tail\n");
198
199done:
200 curses_color_dump(cc_new, "curses new");
201 color_debug(LL_DEBUG5, "CursesColors: %d\n", mod_data->num_curses_colors);
202 return cc_new;
203}
Color private Module data.
Color and attribute parsing.
#define COLOR_DEFAULT
Definition color.h:102
Convenience wrapper for the core headers.
Curses Colour.
int32_t color_t
Type for 24-bit colour value.
Definition curses2.h:30
struct CursesColor * curses_color_new(color_t fg, color_t bg)
Create a new CursesColor.
Definition curses.c:154
static int curses_color_init(color_t fg, color_t bg)
Initialise a new Curses colour.
Definition curses.c:79
void curses_color_free(struct CursesColor **ptr)
Free a CursesColor.
Definition curses.c:122
void curses_colors_init(void)
Initialise the Curses colours.
Definition curses.c:43
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:120
Colour Debugging.
static int color_debug(enum LogLevel level, const char *format,...)
Definition debug.h:51
#define mutt_error(...)
Definition logging2.h:94
Convenience wrapper for the gui headers.
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:49
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
@ MODULE_ID_COLOR
ModuleColor, Color
Definition module_api.h:53
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:665
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:782
#define TAILQ_INIT(head)
Definition queue.h:822
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:866
#define TAILQ_REMOVE(head, elm, field)
Definition queue.h:901
#define TAILQ_INSERT_BEFORE(listelm, elm, field)
Definition queue.h:843
Color private Module data.
Definition module_data.h:35
int num_curses_colors
Number of ncurses colours left to allocate.
Definition module_data.h:38
struct CursesColorList curses_colors
List of all Curses colours.
Definition module_data.h:37
Colour in the ncurses palette.
Definition curses2.h:40
color_t fg
Foreground colour.
Definition curses2.h:41
color_t bg
Background colour.
Definition curses2.h:42
short index
Index number.
Definition curses2.h:43
short ref_count
Number of users.
Definition curses2.h:44
Container for Accounts, Notifications.
Definition neomutt.h:41