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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_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 ) ( ( (basic) << 28 ) | (rgb) )
47 
48 /**
49  * Extract basic colour from ANSI colour definition
50  *
51  * @v ansicol ANSI colour definition
52  * @ret basic Basic colour
53  */
54 #define ANSICOL_BASIC( ansicol ) ( (ansicol) >> 28 )
55 
56 /**
57  * Extract 24-bit RGB value from ANSI colour definition
58  *
59  * @v ansicol ANSI colour definition
60  * @ret rgb 24-bit RGB value
61  */
62 #define ANSICOL_RGB( ansicol ) ( ( (ansicol) >> 0 ) & 0xffffffUL )
63 
64 /**
65  * Extract 24-bit RGB value red component from ANSI colour definition
66  *
67  * @v ansicol ANSI colour definition
68  * @ret red Red component
69  */
70 #define ANSICOL_RED( ansicol ) ( ( (ansicol) >> 16 ) & 0xff )
71 
72 /**
73  * Extract 24-bit RGB value green component from ANSI colour definition
74  *
75  * @v ansicol ANSI colour definition
76  * @ret green Green component
77  */
78 #define ANSICOL_GREEN( ansicol ) ( ( (ansicol) >> 8 ) & 0xff )
79 
80 /**
81  * Extract 24-bit RGB value blue component from ANSI colour definition
82  *
83  * @v ansicol ANSI colour definition
84  * @ret blue Blue component
85  */
86 #define ANSICOL_BLUE( ansicol ) ( ( (ansicol) >> 0 ) & 0xff )
87 
88 /**
89  * Construct default ANSI colour definition
90  *
91  * @v basic Basic colour
92  * @ret ansicol ANSI colour definition
93  *
94  * Colours default to being just a basic colour. If the colour
95  * matches the normal UI text background colour, then its basic colour
96  * value is set to @c ANSICOL_MAGIC.
97  */
98 #define ANSICOL_DEFAULT( basic ) \
99  ANSICOL_DEFINE ( ( ( (basic) == COLOR_NORMAL_BG ) ? \
100  ANSICOL_MAGIC : (basic) ), \
101  ANSICOL_NO_RGB )
102 
103 /** ANSI colour definitions */
104 static uint32_t ansicols[] = {
113 };
114 
115 /** Magic basic colour */
117 
118 /**
119  * Define ANSI colour
120  *
121  * @v colour Colour index
122  * @v basic Basic colour
123  * @v rgb 24-bit RGB value (or ANSICOL_NO_RGB)
124  * @ret rc Return status code
125  */
126 int ansicol_define ( unsigned int colour, unsigned int basic, uint32_t rgb ) {
127  uint32_t ansicol;
128 
129  /* Fail if colour index is out of range */
130  if ( colour >= ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) )
131  return -EINVAL;
132 
133  /* Update colour definition */
134  ansicol = ANSICOL_DEFINE ( basic, rgb );
135  ansicols[colour] = ansicol;
136  DBGC ( &ansicols[0], "ANSICOL redefined colour %d as basic %d RGB "
137  "%#06lx%s\n", colour, ANSICOL_BASIC ( ansicol ),
138  ANSICOL_RGB ( ansicol ),
139  ( ( ansicol & ANSICOL_NO_RGB ) ? " [norgb]" : "" ) );
140 
141  return 0;
142 }
143 
144 /**
145  * Set ANSI colour (using colour definitions)
146  *
147  * @v colour Colour index
148  * @v which Foreground/background selector
149  */
150 void ansicol_set ( unsigned int colour, unsigned int which ) {
151  uint32_t ansicol;
152  unsigned int basic;
153 
154  /* Use default colour if colour index is out of range */
155  if ( colour < ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) ) {
156  ansicol = ansicols[colour];
157  } else {
159  }
160 
161  /* If basic colour is out of range, use the magic colour */
162  basic = ANSICOL_BASIC ( ansicol );
163  if ( basic >= 10 )
164  basic = ansicol_magic;
165 
166  /* Set basic colour first */
167  printf ( CSI "%c%dm", which, basic );
168 
169  /* Set 24-bit RGB colour, if applicable */
170  if ( ! ( ansicol & ANSICOL_NO_RGB ) ) {
171  printf ( CSI "%c8;2;%d;%d;%dm", which, ANSICOL_RED ( ansicol ),
172  ANSICOL_GREEN ( ansicol ), ANSICOL_BLUE ( ansicol ) );
173  }
174 }
175 
176 /**
177  * Reset magic colour
178  *
179  */
180 void ansicol_reset_magic ( void ) {
181 
182  /* Set to the compile-time default background colour */
184 }
185 
186 /**
187  * Set magic colour to transparent
188  *
189  */
191 
192  /* Set to the console default colour (which will give a
193  * transparent background on the framebuffer console).
194  */
196 }
#define ANSICOL_GREEN(ansicol)
Extract 24-bit RGB value green component from ANSI colour definition.
Definition: ansicoldef.c:78
#define EINVAL
Invalid argument.
Definition: errno.h:429
#define COLOR_DEFAULT
Definition: ansicol.h:18
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:465
#define COLOR_YELLOW
Definition: curses.h:206
#define COLOR_BLACK
Definition: curses.h:200
#define ANSICOL_RGB(ansicol)
Extract 24-bit RGB value from ANSI colour definition.
Definition: ansicoldef.c:62
void ansicol_set(unsigned int colour, unsigned int which)
Set ANSI colour (using colour definitions)
Definition: ansicoldef.c:150
Error codes.
#define DBGC(...)
Definition: compiler.h:505
#define ANSICOL_RED(ansicol)
Extract 24-bit RGB value red component from ANSI colour definition.
Definition: ansicoldef.c:70
#define COLOR_BLUE
Definition: curses.h:201
Display colour configuration.
#define COLOR_WHITE
Definition: curses.h:207
#define CSI
Control Sequence Introducer.
Definition: ansiesc.h:96
FILE_SECBOOT(PERMITTED)
#define COLOR_NORMAL_BG
Definition: colour.h:14
#define colour
Colour for debug messages.
Definition: acpi.c:42
int ansicol_define(unsigned int colour, unsigned int basic, uint32_t rgb)
Define ANSI colour.
Definition: ansicoldef.c:126
#define ANSICOL_BASIC(ansicol)
Extract basic colour from ANSI colour definition.
Definition: ansicoldef.c:54
ANSI escape sequences.
#define ANSICOL_BLUE(ansicol)
Extract 24-bit RGB value blue component from ANSI colour definition.
Definition: ansicoldef.c:86
#define COLOR_GREEN
Definition: curses.h:202
#define COLOR_CYAN
Definition: curses.h:203
#define COLOUR_DEFAULT
Default colour (usually white foreground, black background)
Definition: ansicol.h:17
unsigned char uint8_t
Definition: stdint.h:10
void ansicol_set_magic_transparent(void)
Set magic colour to transparent.
Definition: ansicoldef.c:190
unsigned int uint32_t
Definition: stdint.h:12
static uint8_t ansicol_magic
Magic basic colour.
Definition: ansicoldef.c:116
#define ANSICOL_NO_RGB
RGB value for "not defined".
Definition: ansicol.h:30
static uint32_t ansicols[]
ANSI colour definitions.
Definition: ansicoldef.c:104
void ansicol_reset_magic(void)
Reset magic colour.
Definition: ansicoldef.c:180
#define ANSICOL_DEFINE(basic, rgb)
Construct ANSI colour definition.
Definition: ansicoldef.c:46
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define COLOR_RED
Definition: curses.h:204
#define COLOR_MAGENTA
Definition: curses.h:205
ANSI colours.
#define ANSICOL_DEFAULT(basic)
Construct default ANSI colour definition.
Definition: ansicoldef.c:98