iPXE
ansicoldef.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stdio.h>
28#include <errno.h>
29#include <ipxe/ansiesc.h>
30#include <ipxe/ansicol.h>
31#include <config/colour.h>
32
33/** @file
34 *
35 * ANSI colour definitions
36 *
37 */
38
39/**
40 * Construct ANSI colour definition
41 *
42 * @v basic Basic colour
43 * @v rgb 24-bit RGB value (or ANSICOL_NO_RGB)
44 * @ret ansicol ANSI colour definition
45 */
46#define ANSICOL_DEFINE( basic, rgb ) \
47 ( ( ( ( unsigned int ) (basic) ) << 28 ) | (rgb) )
48
49/**
50 * Extract basic colour from ANSI colour definition
51 *
52 * @v ansicol ANSI colour definition
53 * @ret basic Basic colour
54 */
55#define ANSICOL_BASIC( ansicol ) ( (ansicol) >> 28 )
56
57/**
58 * Extract 24-bit RGB value from ANSI colour definition
59 *
60 * @v ansicol ANSI colour definition
61 * @ret rgb 24-bit RGB value
62 */
63#define ANSICOL_RGB( ansicol ) ( ( (ansicol) >> 0 ) & 0xffffffUL )
64
65/**
66 * Extract 24-bit RGB value red component from ANSI colour definition
67 *
68 * @v ansicol ANSI colour definition
69 * @ret red Red component
70 */
71#define ANSICOL_RED( ansicol ) ( ( (ansicol) >> 16 ) & 0xff )
72
73/**
74 * Extract 24-bit RGB value green component from ANSI colour definition
75 *
76 * @v ansicol ANSI colour definition
77 * @ret green Green component
78 */
79#define ANSICOL_GREEN( ansicol ) ( ( (ansicol) >> 8 ) & 0xff )
80
81/**
82 * Extract 24-bit RGB value blue component from ANSI colour definition
83 *
84 * @v ansicol ANSI colour definition
85 * @ret blue Blue component
86 */
87#define ANSICOL_BLUE( ansicol ) ( ( (ansicol) >> 0 ) & 0xff )
88
89/**
90 * Construct default ANSI colour definition
91 *
92 * @v basic Basic colour
93 * @ret ansicol ANSI colour definition
94 *
95 * Colours default to being just a basic colour. If the colour
96 * matches the normal UI text background colour, then its basic colour
97 * value is set to @c ANSICOL_MAGIC.
98 */
99#define ANSICOL_DEFAULT( basic ) \
100 ANSICOL_DEFINE ( ( ( (basic) == COLOR_NORMAL_BG ) ? \
101 ANSICOL_MAGIC : (basic) ), \
102 ANSICOL_NO_RGB )
103
104/** ANSI colour definitions */
115
116/** Magic basic colour */
118
119/**
120 * Define ANSI colour
121 *
122 * @v colour Colour index
123 * @v basic Basic colour
124 * @v rgb 24-bit RGB value (or ANSICOL_NO_RGB)
125 * @ret rc Return status code
126 */
127int ansicol_define ( unsigned int colour, unsigned int basic, uint32_t rgb ) {
128 uint32_t ansicol;
129
130 /* Fail if colour index is out of range */
131 if ( colour >= ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) )
132 return -EINVAL;
133
134 /* Update colour definition */
135 ansicol = ANSICOL_DEFINE ( basic, rgb );
136 ansicols[colour] = ansicol;
137 DBGC ( &ansicols[0], "ANSICOL redefined colour %d as basic %d RGB "
138 "%#06lx%s\n", colour, ANSICOL_BASIC ( ansicol ),
139 ANSICOL_RGB ( ansicol ),
140 ( ( ansicol & ANSICOL_NO_RGB ) ? " [norgb]" : "" ) );
141
142 return 0;
143}
144
145/**
146 * Set ANSI colour (using colour definitions)
147 *
148 * @v colour Colour index
149 * @v which Foreground/background selector
150 */
151void ansicol_set ( unsigned int colour, unsigned int which ) {
152 uint32_t ansicol;
153 unsigned int basic;
154
155 /* Use default colour if colour index is out of range */
156 if ( colour < ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) ) {
157 ansicol = ansicols[colour];
158 } else {
160 }
161
162 /* If basic colour is out of range, use the magic colour */
163 basic = ANSICOL_BASIC ( ansicol );
164 if ( basic >= 10 )
165 basic = ansicol_magic;
166
167 /* Set basic colour first */
168 printf ( CSI "%c%dm", which, basic );
169
170 /* Set 24-bit RGB colour, if applicable */
171 if ( ! ( ansicol & ANSICOL_NO_RGB ) ) {
172 printf ( CSI "%c8;2;%d;%d;%dm", which, ANSICOL_RED ( ansicol ),
173 ANSICOL_GREEN ( ansicol ), ANSICOL_BLUE ( ansicol ) );
174 }
175}
176
177/**
178 * Reset magic colour
179 *
180 */
181void ansicol_reset_magic ( void ) {
182
183 /* Set to the compile-time default background colour */
185}
186
187/**
188 * Set magic colour to transparent
189 *
190 */
192
193 /* Set to the console default colour (which will give a
194 * transparent background on the framebuffer console).
195 */
197}
#define colour
Colour for debug messages.
Definition acpi.c:42
ANSI colours.
#define COLOR_DEFAULT
Definition ansicol.h:18
#define ANSICOL_NO_RGB
RGB value for "not defined".
Definition ansicol.h:30
#define COLOUR_DEFAULT
Default colour (usually white foreground, black background).
Definition ansicol.h:17
#define ANSICOL_RED(ansicol)
Extract 24-bit RGB value red component from ANSI colour definition.
Definition ansicoldef.c:71
#define ANSICOL_DEFINE(basic, rgb)
Construct ANSI colour definition.
Definition ansicoldef.c:46
#define ANSICOL_GREEN(ansicol)
Extract 24-bit RGB value green component from ANSI colour definition.
Definition ansicoldef.c:79
void ansicol_set_magic_transparent(void)
Set magic colour to transparent.
Definition ansicoldef.c:191
void ansicol_reset_magic(void)
Reset magic colour.
Definition ansicoldef.c:181
#define ANSICOL_BASIC(ansicol)
Extract basic colour from ANSI colour definition.
Definition ansicoldef.c:55
void ansicol_set(unsigned int colour, unsigned int which)
Set ANSI colour (using colour definitions).
Definition ansicoldef.c:151
#define ANSICOL_BLUE(ansicol)
Extract 24-bit RGB value blue component from ANSI colour definition.
Definition ansicoldef.c:87
static uint32_t ansicols[]
ANSI colour definitions.
Definition ansicoldef.c:105
static uint8_t ansicol_magic
Magic basic colour.
Definition ansicoldef.c:117
#define ANSICOL_DEFAULT(basic)
Construct default ANSI colour definition.
Definition ansicoldef.c:99
int ansicol_define(unsigned int colour, unsigned int basic, uint32_t rgb)
Define ANSI colour.
Definition ansicoldef.c:127
#define ANSICOL_RGB(ansicol)
Extract 24-bit RGB value from ANSI colour definition.
Definition ansicoldef.c:63
ANSI escape sequences.
#define CSI
Control Sequence Introducer.
Definition ansiesc.h:96
unsigned int uint32_t
Definition stdint.h:12
unsigned char uint8_t
Definition stdint.h:10
Display colour configuration.
#define COLOR_NORMAL_BG
Definition colour.h:14
#define COLOR_BLUE
Definition curses.h:201
#define COLOR_YELLOW
Definition curses.h:206
#define COLOR_CYAN
Definition curses.h:203
#define COLOR_MAGENTA
Definition curses.h:205
#define COLOR_WHITE
Definition curses.h:207
#define COLOR_BLACK
Definition curses.h:200
#define COLOR_RED
Definition curses.h:204
#define COLOR_GREEN
Definition curses.h:202
Error codes.
#define DBGC(...)
Definition compiler.h:530
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define EINVAL
Invalid argument.
Definition errno.h:472
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition vsprintf.c:485