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