[process] Include process name in debug messages
[ipxe.git] / src / core / ansicoldef.c
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[] = {
104 [COLOR_BLACK] = ANSICOL_DEFAULT ( COLOR_BLACK ),
105 [COLOR_RED] = ANSICOL_DEFAULT ( COLOR_RED ),
106 [COLOR_GREEN] = ANSICOL_DEFAULT ( COLOR_GREEN ),
107 [COLOR_YELLOW] = ANSICOL_DEFAULT ( COLOR_YELLOW ),
108 [COLOR_BLUE] = ANSICOL_DEFAULT ( COLOR_BLUE ),
109 [COLOR_MAGENTA] = ANSICOL_DEFAULT ( COLOR_MAGENTA ),
110 [COLOR_CYAN] = ANSICOL_DEFAULT ( COLOR_CYAN ),
111 [COLOR_WHITE] = ANSICOL_DEFAULT ( COLOR_WHITE ),
112 };
113
114 /** Magic basic colour */
115 static uint8_t ansicol_magic = COLOR_NORMAL_BG;
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 {
157 ansicol = ANSICOL_DEFINE ( COLOUR_DEFAULT, ANSICOL_NO_RGB );
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 */
182 ansicol_magic = COLOR_NORMAL_BG;
183 }
184
185 /**
186 * Set magic colour to transparent
187 *
188 */
189 void ansicol_set_magic_transparent ( void ) {
190
191 /* Set to the console default colour (which will give a
192 * transparent background on the framebuffer console).
193 */
194 ansicol_magic = COLOR_DEFAULT;
195 }