NeoMutt  2023-05-17-56-ga67199
Teaching an old dog new tricks
DOXYGEN
curses.c File Reference

Curses Colour. More...

#include "config.h"
#include <stddef.h>
#include <stdbool.h>
#include "mutt/lib.h"
#include "gui/lib.h"
#include "lib.h"
+ Include dependency graph for curses.c:

Go to the source code of this file.

Functions

void curses_colors_init (void)
 Initialise the Curses colours. More...
 
struct CursesColorcurses_colors_find (int fg, int bg)
 Find a Curses colour by foreground/background. More...
 
static int curses_color_init (int fg, int bg)
 Initialise a new Curses colour. More...
 
void curses_color_free (struct CursesColor **ptr)
 Free a CursesColor. More...
 
struct CursesColorcurses_color_new (int fg, int bg)
 Create a new CursesColor. More...
 

Variables

struct CursesColorList CursesColors
 List of all Curses colours. More...
 
int NumCursesColors
 Number of ncurses colours left to allocate. More...
 

Detailed Description

Curses Colour.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file curses.c.

Function Documentation

◆ curses_colors_init()

void curses_colors_init ( void  )

Initialise the Curses colours.

Definition at line 42 of file curses.c.

43{
44 color_debug(LL_DEBUG5, "init CursesColors\n");
47}
int NumCursesColors
Number of ncurses colours left to allocate.
Definition: curses.c:37
struct CursesColorList CursesColors
List of all Curses colours.
Definition: curses.c:36
int color_debug(enum LogLevel level, const char *format,...)
Write to the log file.
Definition: debug.c:44
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
#define TAILQ_INIT(head)
Definition: queue.h:765
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ curses_colors_find()

struct CursesColor * curses_colors_find ( int  fg,
int  bg 
)

Find a Curses colour by foreground/background.

Parameters
fgForeground colour
bgBackground colour
Return values
ptrCurses colour

Definition at line 55 of file curses.c.

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}
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
Colour in the ncurses palette.
Definition: curses2.h:38
uint32_t fg
Foreground colour.
Definition: curses2.h:41
uint32_t bg
Background colour.
Definition: curses2.h:42
+ Here is the caller graph for this function:

◆ curses_color_init()

static int curses_color_init ( int  fg,
int  bg 
)
static

Initialise a new Curses colour.

Parameters
fgForeground colour
bgBackground colour
Return values
numIndex of Curses colour

Definition at line 75 of file curses.c.

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}
#define COLOR_DEFAULT
Definition: color.h:102
#define COLOR_UNSET
Definition: color.h:103
#define mutt_error(...)
Definition: logging2.h:90
#define _(a)
Definition: message.h:28
short index
Index number.
Definition: curses2.h:43
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ curses_color_free()

void curses_color_free ( struct CursesColor **  ptr)

Free a CursesColor.

Parameters
ptrCursesColor to be freed

Definition at line 119 of file curses.c.

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}
void curses_color_dump(struct CursesColor *cc, const char *prefix)
Log one Curses colour.
Definition: debug.c:254
#define FREE(x)
Definition: memory.h:43
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:841
short ref_count
Number of users.
Definition: curses2.h:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ curses_color_new()

struct CursesColor * curses_color_new ( int  fg,
int  bg 
)

Create a new CursesColor.

Parameters
fgForeground colour
bgBackground colour
Return values
ptrNew CursesColor

If the colour already exists, this function will return a pointer to the object (and increase its ref-count).

Definition at line 149 of file curses.c.

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}
static int curses_color_init(int fg, int bg)
Initialise a new Curses colour.
Definition: curses.c:75
struct CursesColor * curses_colors_find(int fg, int bg)
Find a Curses colour by foreground/background.
Definition: curses.c:55
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:809
#define TAILQ_INSERT_BEFORE(listelm, elm, field)
Definition: queue.h:786
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ CursesColors

struct CursesColorList CursesColors

List of all Curses colours.

Definition at line 36 of file curses.c.

◆ NumCursesColors

int NumCursesColors

Number of ncurses colours left to allocate.

Definition at line 37 of file curses.c.