NeoMutt  2022-04-29-215-gc12b98
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
 

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
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: logging.h:44
#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 // const char *color = color_debug_log_color(fg, bg);
100 // printf("%s\n", color);
101 if (fg == COLOR_DEFAULT)
102 fg = COLOR_UNSET;
103 if (bg == COLOR_DEFAULT)
104 bg = COLOR_UNSET;
105
106 color_debug(LL_DEBUG5, "init_pair(%d,%d,%d) -> %d\n", index, fg, bg);
107 init_pair(index, fg, bg);
108
109 return index;
110}
#define COLOR_DEFAULT
Definition: color.h:100
#define COLOR_UNSET
Definition: color.h:101
#define mutt_error(...)
Definition: logging.h:87
#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 116 of file curses.c.

117{
118 if (!ptr || !*ptr)
119 return;
120
121 struct CursesColor *cc = *ptr;
122 if (cc->ref_count > 1)
123 {
124 cc->ref_count--;
125 curses_color_dump(cc, "CursesColor rc--: ");
126 *ptr = NULL;
127 // curses_colors_dump();
128 return;
129 }
130
131 curses_color_dump(cc, "free: ");
132 TAILQ_REMOVE(&CursesColors, cc, entries);
134 color_debug(LL_DEBUG5, "CursesColors: %d\n", NumCursesColors);
135 // curses_colors_dump();
136 FREE(ptr);
137}
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 148 of file curses.c.

149{
150 color_debug(LL_DEBUG5, "fg %d, bg %d\n", fg, bg);
151 if (((fg == COLOR_UNSET) && (bg == COLOR_UNSET)) ||
152 ((fg == COLOR_DEFAULT) && (bg == COLOR_DEFAULT)))
153 {
154 color_debug(LL_DEBUG5, "both unset\n");
155 return NULL;
156 }
157
158 struct CursesColor *cc = curses_colors_find(fg, bg);
159 if (cc)
160 {
161 cc->ref_count++;
162 curses_color_dump(cc, "rc++: ");
163 return cc;
164 }
165
166 color_debug(LL_DEBUG5, "new curses\n");
168 if (index < 0)
169 return NULL;
170
171 struct CursesColor *cc_new = mutt_mem_calloc(1, sizeof(*cc_new));
173 color_debug(LL_DEBUG5, "CursesColor %p\n", cc_new);
174 cc_new->fg = fg;
175 cc_new->bg = bg;
176 cc_new->ref_count = 1;
177 cc_new->index = index;
178
179 // insert curses colour
180 TAILQ_FOREACH(cc, &CursesColors, entries)
181 {
182 if (cc->index > index)
183 {
184 color_debug(LL_DEBUG5, "insert\n");
185 TAILQ_INSERT_BEFORE(cc, cc_new, entries);
186 goto done;
187 }
188 }
189
190 TAILQ_INSERT_TAIL(&CursesColors, cc_new, entries);
191 color_debug(LL_DEBUG5, "tail\n");
192
193done:
194 curses_color_dump(cc_new, "CursesColor new: ");
195 color_debug(LL_DEBUG5, "CursesColors: %d\n", NumCursesColors);
196 return cc_new;
197}
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

Definition at line 37 of file curses.c.