|
@@ -0,0 +1,161 @@
|
|
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
|
+
|
|
20
|
+FILE_LICENCE ( GPL2_OR_LATER );
|
|
21
|
+
|
|
22
|
+#include <stdio.h>
|
|
23
|
+#include <errno.h>
|
|
24
|
+#include <ipxe/ansiesc.h>
|
|
25
|
+#include <ipxe/ansicol.h>
|
|
26
|
+#include <config/colour.h>
|
|
27
|
+
|
|
28
|
+/** @file
|
|
29
|
+ *
|
|
30
|
+ * ANSI colour definitions
|
|
31
|
+ *
|
|
32
|
+ */
|
|
33
|
+
|
|
34
|
+/**
|
|
35
|
+ * Construct ANSI colour definition
|
|
36
|
+ *
|
|
37
|
+ * @v basic Basic colour
|
|
38
|
+ * @v rgb 24-bit RGB value (or ANSICOL_NO_RGB)
|
|
39
|
+ * @ret ansicol ANSI colour definition
|
|
40
|
+ */
|
|
41
|
+#define ANSICOL_DEFINE( basic, rgb ) ( ( (basic) << 28 ) | (rgb) )
|
|
42
|
+
|
|
43
|
+/**
|
|
44
|
+ * Extract basic colour from ANSI colour definition
|
|
45
|
+ *
|
|
46
|
+ * @v ansicol ANSI colour definition
|
|
47
|
+ * @ret basic Basic colour
|
|
48
|
+ */
|
|
49
|
+#define ANSICOL_BASIC( ansicol ) ( (ansicol) >> 28 )
|
|
50
|
+
|
|
51
|
+/**
|
|
52
|
+ * Extract 24-bit RGB value from ANSI colour definition
|
|
53
|
+ *
|
|
54
|
+ * @v ansicol ANSI colour definition
|
|
55
|
+ * @ret rgb 24-bit RGB value
|
|
56
|
+ */
|
|
57
|
+#define ANSICOL_RGB( ansicol ) ( ( (ansicol) >> 0 ) & 0xffffffUL )
|
|
58
|
+
|
|
59
|
+/**
|
|
60
|
+ * Extract 24-bit RGB value red component from ANSI colour definition
|
|
61
|
+ *
|
|
62
|
+ * @v ansicol ANSI colour definition
|
|
63
|
+ * @ret red Red component
|
|
64
|
+ */
|
|
65
|
+#define ANSICOL_RED( ansicol ) ( ( (ansicol) >> 16 ) & 0xff )
|
|
66
|
+
|
|
67
|
+/**
|
|
68
|
+ * Extract 24-bit RGB value green component from ANSI colour definition
|
|
69
|
+ *
|
|
70
|
+ * @v ansicol ANSI colour definition
|
|
71
|
+ * @ret green Green component
|
|
72
|
+ */
|
|
73
|
+#define ANSICOL_GREEN( ansicol ) ( ( (ansicol) >> 8 ) & 0xff )
|
|
74
|
+
|
|
75
|
+/**
|
|
76
|
+ * Extract 24-bit RGB value blue component from ANSI colour definition
|
|
77
|
+ *
|
|
78
|
+ * @v ansicol ANSI colour definition
|
|
79
|
+ * @ret blue Blue component
|
|
80
|
+ */
|
|
81
|
+#define ANSICOL_BLUE( ansicol ) ( ( (ansicol) >> 0 ) & 0xff )
|
|
82
|
+
|
|
83
|
+/**
|
|
84
|
+ * Construct default ANSI colour definition
|
|
85
|
+ *
|
|
86
|
+ * @v basic Basic colour
|
|
87
|
+ * @ret ansicol ANSI colour definition
|
|
88
|
+ *
|
|
89
|
+ * Colours default to being just a basic colour.
|
|
90
|
+ */
|
|
91
|
+#define ANSICOL_DEFAULT( basic ) ANSICOL_DEFINE ( (basic), ANSICOL_NO_RGB )
|
|
92
|
+
|
|
93
|
+/** ANSI colour definitions */
|
|
94
|
+static uint32_t ansicols[] = {
|
|
95
|
+ [COLOR_BLACK] = ANSICOL_DEFAULT ( COLOR_BLACK ),
|
|
96
|
+ [COLOR_RED] = ANSICOL_DEFAULT ( COLOR_RED ),
|
|
97
|
+ [COLOR_GREEN] = ANSICOL_DEFAULT ( COLOR_GREEN ),
|
|
98
|
+ [COLOR_YELLOW] = ANSICOL_DEFAULT ( COLOR_YELLOW ),
|
|
99
|
+ [COLOR_BLUE] = ANSICOL_DEFAULT ( COLOR_BLUE ),
|
|
100
|
+ [COLOR_MAGENTA] = ANSICOL_DEFAULT ( COLOR_MAGENTA ),
|
|
101
|
+ [COLOR_CYAN] = ANSICOL_DEFAULT ( COLOR_CYAN ),
|
|
102
|
+ [COLOR_WHITE] = ANSICOL_DEFAULT ( COLOR_WHITE ),
|
|
103
|
+};
|
|
104
|
+
|
|
105
|
+/**
|
|
106
|
+ * Define ANSI colour
|
|
107
|
+ *
|
|
108
|
+ * @v colour Colour index
|
|
109
|
+ * @v basic Basic colour
|
|
110
|
+ * @v rgb 24-bit RGB value (or ANSICOL_NO_RGB)
|
|
111
|
+ * @ret rc Return status code
|
|
112
|
+ */
|
|
113
|
+int ansicol_define ( unsigned int colour, unsigned int basic, uint32_t rgb ) {
|
|
114
|
+ uint32_t ansicol;
|
|
115
|
+
|
|
116
|
+ /* Fail if colour index is out of range */
|
|
117
|
+ if ( colour >= ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) )
|
|
118
|
+ return -EINVAL;
|
|
119
|
+
|
|
120
|
+ /* Update colour definition */
|
|
121
|
+ ansicol = ANSICOL_DEFINE ( basic, rgb );
|
|
122
|
+ ansicols[colour] = ansicol;
|
|
123
|
+ DBGC ( &ansicols[0], "ANSICOL redefined colour %d as basic %d RGB "
|
|
124
|
+ "%#06lx%s\n", colour, ANSICOL_BASIC ( ansicol ),
|
|
125
|
+ ANSICOL_RGB ( ansicol ),
|
|
126
|
+ ( ( ansicol & ANSICOL_NO_RGB ) ? " [norgb]" : "" ) );
|
|
127
|
+
|
|
128
|
+ return 0;
|
|
129
|
+}
|
|
130
|
+
|
|
131
|
+/**
|
|
132
|
+ * Set ANSI colour (using colour definitions)
|
|
133
|
+ *
|
|
134
|
+ * @v colour Colour index
|
|
135
|
+ * @v which Foreground/background selector
|
|
136
|
+ */
|
|
137
|
+void ansicol_set ( unsigned int colour, unsigned int which ) {
|
|
138
|
+ uint32_t ansicol;
|
|
139
|
+ unsigned int basic;
|
|
140
|
+
|
|
141
|
+ /* Use default colour if colour index is out of range */
|
|
142
|
+ if ( colour < ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) ) {
|
|
143
|
+ ansicol = ansicols[colour];
|
|
144
|
+ } else {
|
|
145
|
+ ansicol = ANSICOL_DEFINE ( COLOUR_DEFAULT, ANSICOL_NO_RGB );
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ /* If basic colour is out of range, use the default colour */
|
|
149
|
+ basic = ANSICOL_BASIC ( ansicol );
|
|
150
|
+ if ( basic >= 10 )
|
|
151
|
+ basic = COLOR_DEFAULT;
|
|
152
|
+
|
|
153
|
+ /* Set basic colour first */
|
|
154
|
+ printf ( CSI "%c%dm", which, basic );
|
|
155
|
+
|
|
156
|
+ /* Set 24-bit RGB colour, if applicable */
|
|
157
|
+ if ( ! ( ansicol & ANSICOL_NO_RGB ) ) {
|
|
158
|
+ printf ( CSI "%c8;2;%d;%d;%dm", which, ANSICOL_RED ( ansicol ),
|
|
159
|
+ ANSICOL_GREEN ( ansicol ), ANSICOL_BLUE ( ansicol ) );
|
|
160
|
+ }
|
|
161
|
+}
|