forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.c
292 lines (260 loc) · 6.18 KB
/
color.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* @file
* Color and attribute parsing
*
* @authors
* Copyright (C) 1996-2002,2012 Michael R. Elkins <[email protected]>
* Copyright (C) 2020 R Primus <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* 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/>.
*/
/**
* @page color_color Color and attribute parsing
*
* Color and attribute parsing
*/
#include "config.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "lib.h"
#include "pattern/lib.h"
#include "context.h"
#include "init.h"
#include "mutt_globals.h"
#include "options.h"
struct ColorList *UserColors; ///< Array of user colours
int NumUserColors; ///< Number of user colours
/**
* struct ColorList - A set of colors
*/
struct ColorList
{
/* TrueColor uses 24bit. Use fixed-width integer type to make sure it fits.
* Use the upper 8 bits to store flags. */
uint32_t fg; ///< Foreground colour
uint32_t bg; ///< Background colour
short index; ///< Index number
short ref_count; ///< Number of users
struct ColorList *next; ///< Linked list
};
const struct Mapping ColorNames[] = {
// clang-format off
{ "black", COLOR_BLACK },
{ "blue", COLOR_BLUE },
{ "cyan", COLOR_CYAN },
{ "green", COLOR_GREEN },
{ "magenta", COLOR_MAGENTA },
{ "red", COLOR_RED },
{ "white", COLOR_WHITE },
{ "yellow", COLOR_YELLOW },
{ "default", COLOR_DEFAULT },
{ 0, 0 },
// clang-format on
};
/**
* color_list_free - Free the list of curses colours
* @param ptr Colours
*/
static void color_list_free(struct ColorList **ptr)
{
if (!ptr || !*ptr)
return;
struct ColorList *cl = *ptr;
struct ColorList *next = NULL;
while (cl)
{
next = cl->next;
FREE(&cl);
cl = next;
}
*ptr = NULL;
}
/**
* mutt_color_free - Free a colour
* @param fg Foreground colour ID
* @param bg Background colour ID
*
* If there are no more users, the resource will be freed.
*/
void mutt_color_free(uint32_t fg, uint32_t bg)
{
struct ColorList *q = NULL;
struct ColorList *p = UserColors;
while (p)
{
if ((p->fg == fg) && (p->bg == bg))
{
(p->ref_count)--;
if (p->ref_count > 0)
return;
NumUserColors--;
mutt_debug(LL_DEBUG1, "Color pairs used so far: %d\n", NumUserColors);
if (p == UserColors)
{
UserColors = UserColors->next;
FREE(&p);
return;
}
q = UserColors;
while (q)
{
if (q->next == p)
{
q->next = p->next;
FREE(&p);
return;
}
q = q->next;
}
/* can't get here */
}
p = p->next;
}
}
/**
* colors_clear - Reset all the colours
*/
void colors_clear(void)
{
simple_colors_clear();
quoted_colors_clear();
regex_colors_clear();
color_list_free(&UserColors);
}
/**
* mutt_colors_cleanup - Cleanup all the colours
*/
void mutt_colors_cleanup(void)
{
colors_clear();
color_notify_free();
}
/**
* mutt_colors_init - Initialize colours
*/
void mutt_colors_init(void)
{
color_notify_init();
simple_colors_init();
quoted_colors_init();
regex_colors_init();
start_color();
}
/**
* mutt_color_alloc - Allocate a colour pair
* @param fg Foreground colour ID
* @param bg Background colour ID
* @retval num Combined colour pair
*/
int mutt_color_alloc(uint32_t fg, uint32_t bg)
{
struct ColorList *p = UserColors;
/* check to see if this color is already allocated to save space */
while (p)
{
if ((p->fg == fg) && (p->bg == bg))
{
(p->ref_count)++;
return COLOR_PAIR(p->index);
}
p = p->next;
}
/* check to see if there are colors left */
if (++NumUserColors > COLOR_PAIRS)
return A_NORMAL;
/* find the smallest available index (object) */
int i = 1;
while (true)
{
p = UserColors;
while (p)
{
if (p->index == i)
break;
p = p->next;
}
if (!p)
break;
i++;
}
/* Check for pair overflow too.
* We are currently using init_pair(), which only accepts size short. */
if (i > SHRT_MAX)
return (0);
p = mutt_mem_malloc(sizeof(struct ColorList));
p->next = UserColors;
UserColors = p;
p->index = i;
p->ref_count = 1;
p->bg = bg;
p->fg = fg;
if (fg == COLOR_DEFAULT)
fg = COLOR_UNSET;
if (bg == COLOR_DEFAULT)
bg = COLOR_UNSET;
init_pair(i, fg, bg);
mutt_debug(LL_DEBUG3, "Color pairs used so far: %d\n", NumUserColors);
return COLOR_PAIR(p->index);
}
/**
* mutt_lookup_color - Get the colours from a colour pair
* @param[in] pair Colour pair
* @param[out] fg Foreground colour (OPTIONAL)
* @param[out] bg Background colour (OPTIONAL)
* @retval 0 Success
* @retval -1 Error
*/
static int mutt_lookup_color(short pair, uint32_t *fg, uint32_t *bg)
{
struct ColorList *p = UserColors;
while (p)
{
if (COLOR_PAIR(p->index) == pair)
{
if (fg)
*fg = p->fg;
if (bg)
*bg = p->bg;
return 0;
}
p = p->next;
}
return -1;
}
/**
* mutt_color_combine - Combine two colours
* @param fg_attr Colour pair of foreground to use
* @param bg_attr Colour pair of background to use
* @retval num Colour pair of combined colour
*/
int mutt_color_combine(uint32_t fg_attr, uint32_t bg_attr)
{
uint32_t fg = COLOR_DEFAULT;
uint32_t bg = COLOR_DEFAULT;
mutt_lookup_color(fg_attr, &fg, NULL);
mutt_lookup_color(bg_attr, NULL, &bg);
if ((fg == COLOR_DEFAULT) && (bg == COLOR_DEFAULT))
return A_NORMAL;
return mutt_color_alloc(fg, bg);
}