NeoMutt  2025-12-11-911-gd8d604
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
45void curses_colors_init(struct CursesColorList *curses_colors, int *num_curses_colors)
46{
47 color_debug(LL_DEBUG5, "init CursesColors\n");
48 TAILQ_INIT(curses_colors);
49 *num_curses_colors = 0;
50}
51
59{
61 struct CursesColor *cc = NULL;
62 TAILQ_FOREACH(cc, &mod_data->curses_colors, entries)
63 {
64 if ((cc->fg == fg) && (cc->bg == bg))
65 {
66 curses_color_dump(cc, "find");
67 return cc;
68 }
69 }
70
71 return NULL;
72}
73
81{
83 color_debug(LL_DEBUG5, "find lowest index\n");
84 int index = 16;
85 struct CursesColor *cc = NULL;
86 TAILQ_FOREACH(cc, &mod_data->curses_colors, entries)
87 {
88 if (cc->index == index)
89 index++;
90 else
91 break;
92 }
93 color_debug(LL_DEBUG5, "lowest index = %d\n", index);
94 if (index >= COLOR_PAIRS)
95 {
96 if (COLOR_PAIRS > 0)
97 {
98 static bool warned = false;
99 if (!warned)
100 {
101 mutt_error(_("Too many colors: %d / %d"), index, COLOR_PAIRS);
102 warned = true;
103 }
104 }
105 return 0;
106 }
107
108#ifdef NEOMUTT_DIRECT_COLORS
109 int rc = init_extended_pair(index, fg, bg);
110 color_debug(LL_DEBUG5, "init_extended_pair(%d,%d,%d) -> %d\n", index, fg, bg, rc);
111#else
112 int rc = init_pair(index, fg, bg);
113 color_debug(LL_DEBUG5, "init_pair(%d,%d,%d) -> %d\n", index, fg, bg, rc);
114#endif
115
116 return index;
117}
118
124{
125 if (!ptr || !*ptr)
126 return;
127
128 struct CursesColor *cc = *ptr;
129
130 cc->ref_count--;
131 if (cc->ref_count > 0)
132 {
133 curses_color_dump(cc, "curses rc--");
134 *ptr = NULL;
135 return;
136 }
137
138 curses_color_dump(cc, "curses free");
140 TAILQ_REMOVE(&mod_data->curses_colors, cc, entries);
141 mod_data->num_curses_colors--;
142 color_debug(LL_DEBUG5, "CursesColors: %d\n", mod_data->num_curses_colors);
143 FREE(ptr);
144}
145
156{
157 color_debug(LL_DEBUG5, "fg %d, bg %d\n", fg, bg);
158 if ((fg == COLOR_DEFAULT) && (bg == COLOR_DEFAULT))
159 {
160 color_debug(LL_DEBUG5, "both unset\n");
161 return NULL;
162 }
163
164 struct CursesColor *cc = curses_colors_find(fg, bg);
165 if (cc)
166 {
167 cc->ref_count++;
168 curses_color_dump(cc, "curses rc++");
169 return cc;
170 }
171
172 color_debug(LL_DEBUG5, "new curses\n");
174 if (index == 0)
175 return NULL;
176
178 struct CursesColor *cc_new = MUTT_MEM_CALLOC(1, struct CursesColor);
179 mod_data->num_curses_colors++;
180 color_debug(LL_DEBUG5, "CursesColor %p\n", (void *) cc_new);
181 cc_new->fg = fg;
182 cc_new->bg = bg;
183 cc_new->ref_count = 1;
184 cc_new->index = index;
185
186 // insert curses colour
187 TAILQ_FOREACH(cc, &mod_data->curses_colors, entries)
188 {
189 if (cc->index > index)
190 {
191 color_debug(LL_DEBUG5, "insert\n");
192 TAILQ_INSERT_BEFORE(cc, cc_new, entries);
193 goto done;
194 }
195 }
196
197 TAILQ_INSERT_TAIL(&mod_data->curses_colors, cc_new, entries);
198 color_debug(LL_DEBUG5, "tail\n");
199
200done:
201 curses_color_dump(cc_new, "curses new");
202 color_debug(LL_DEBUG5, "CursesColors: %d\n", mod_data->num_curses_colors);
203 return cc_new;
204}
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:155
static int curses_color_init(color_t fg, color_t bg)
Initialise a new Curses colour.
Definition curses.c:80
void curses_colors_init(struct CursesColorList *curses_colors, int *num_curses_colors)
Initialise the Curses colours.
Definition curses.c:45
void curses_color_free(struct CursesColor **ptr)
Free a CursesColor.
Definition curses.c:123
struct CursesColor * curses_colors_find(color_t fg, color_t bg)
Find a Curses colour by foreground/background.
Definition curses.c:58
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:663
#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