浏览代码

[console] Add centralised concept of colours and colour pairs

Add a centralised concept of colours and colour pairs (using the
default colour pairs as configured via config/colour.h).  A colour
pair consists of a pair of colour indices.

Add the ability to redefine both a colour pair and an individual
colour index, with minimal overhead if this feature is not required
(e.g. because the relevant shell commands are not present in the
build).

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 11 年前
父节点
当前提交
7025f5c648
共有 5 个文件被更改,包括 361 次插入0 次删除
  1. 3
    0
      src/config/colour.h
  2. 122
    0
      src/core/ansicol.c
  3. 161
    0
      src/core/ansicoldef.c
  4. 73
    0
      src/include/ipxe/ansicol.h
  5. 2
    0
      src/include/ipxe/errfile.h

+ 3
- 0
src/config/colour.h 查看文件

@@ -27,6 +27,9 @@ FILE_LICENCE ( GPL2_OR_LATER );
27 27
 #define COLOR_URL_FG		COLOR_CYAN
28 28
 #define COLOR_URL_BG		COLOR_BLUE
29 29
 
30
+#define COLOR_PXE_FG		COLOR_BLACK
31
+#define COLOR_PXE_BG		COLOR_WHITE
32
+
30 33
 #include <config/local/colour.h>
31 34
 
32 35
 #endif /* CONFIG_COLOUR_H */

+ 122
- 0
src/core/ansicol.c 查看文件

@@ -0,0 +1,122 @@
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 <assert.h>
25
+#include <ipxe/ansiesc.h>
26
+#include <ipxe/ansicol.h>
27
+#include <config/colour.h>
28
+
29
+/** @file
30
+ *
31
+ * ANSI colours
32
+ *
33
+ */
34
+
35
+/** ANSI colour pair definitions */
36
+static struct ansicol_pair ansicol_pairs[] = {
37
+	[CPAIR_DEFAULT] = { COLOR_DEFAULT, COLOR_DEFAULT },
38
+	[CPAIR_NORMAL] = { COLOR_NORMAL_FG, COLOR_NORMAL_BG },
39
+	[CPAIR_SELECT] = { COLOR_SELECT_FG, COLOR_SELECT_BG },
40
+	[CPAIR_SEPARATOR] = { COLOR_SEPARATOR_FG, COLOR_SEPARATOR_BG },
41
+	[CPAIR_EDIT] = { COLOR_EDIT_FG, COLOR_EDIT_BG },
42
+	[CPAIR_ALERT] = { COLOR_ALERT_FG, COLOR_ALERT_BG },
43
+	[CPAIR_URL] = { COLOR_URL_FG, COLOR_URL_BG },
44
+	[CPAIR_PXE] = { COLOR_PXE_FG, COLOR_PXE_BG },
45
+};
46
+
47
+/**
48
+ * Set ANSI colour (when no colour definition support is present)
49
+ *
50
+ * @v colour		Colour index
51
+ * @v which		Foreground/background selector
52
+ */
53
+__weak void ansicol_set ( unsigned int colour, unsigned int which ) {
54
+
55
+	/* Colour indices are hardcoded and should never be out of range */
56
+	assert ( colour < 10 );
57
+
58
+	/* Set basic colour */
59
+	printf ( CSI "%c%dm", which, colour );
60
+}
61
+
62
+/**
63
+ * Set ANSI foreground colour
64
+ *
65
+ * @v colour		Colour index
66
+ */
67
+static void ansicol_foreground ( unsigned int colour ) {
68
+	ansicol_set ( colour, '3' );
69
+}
70
+
71
+/**
72
+ * Set ANSI background colour
73
+ *
74
+ * @v colour		Colour index
75
+ */
76
+static void ansicol_background ( unsigned int colour ) {
77
+	ansicol_set ( colour, '4' );
78
+}
79
+
80
+/**
81
+ * Set ANSI foreground and background colour
82
+ *
83
+ * @v cpair		Colour pair index
84
+ */
85
+void ansicol_set_pair ( unsigned int cpair ) {
86
+	struct ansicol_pair *pair;
87
+
88
+	/* Colour pair indices are hardcoded and should never be out of range */
89
+	assert ( cpair < ( sizeof ( ansicol_pairs ) /
90
+			   sizeof ( ansicol_pairs[0] ) ) );
91
+
92
+	/* Set both foreground and background colours */
93
+	pair = &ansicol_pairs[cpair];
94
+	ansicol_foreground ( pair->foreground );
95
+	ansicol_background ( pair->background );
96
+}
97
+
98
+/**
99
+ * Define ANSI colour pair
100
+ *
101
+ * @v cpair		Colour pair index
102
+ * @v foreground	Foreground colour index
103
+ * @v background	Background colour index
104
+ * @ret rc		Return status code
105
+ */
106
+int ansicol_define_pair ( unsigned int cpair, unsigned int foreground,
107
+			  unsigned int background ) {
108
+	struct ansicol_pair *pair;
109
+
110
+	/* Fail if colour index is out of range */
111
+	if ( cpair >= ( sizeof ( ansicol_pairs ) / sizeof ( ansicol_pairs[0] )))
112
+		return -EINVAL;
113
+
114
+	/* Update colour pair definition */
115
+	pair = &ansicol_pairs[cpair];
116
+	pair->foreground = foreground;
117
+	pair->background = background;
118
+	DBGC ( &ansicol_pairs[0], "ANSICOL redefined colour pair %d as "
119
+	       "foreground %d background %d\n", cpair, foreground, background );
120
+
121
+	return 0;
122
+}

+ 161
- 0
src/core/ansicoldef.c 查看文件

@@ -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
+}

+ 73
- 0
src/include/ipxe/ansicol.h 查看文件

@@ -0,0 +1,73 @@
1
+#ifndef _IPXE_ANSICOL_H
2
+#define _IPXE_ANSICOL_H
3
+
4
+/** @file
5
+ *
6
+ * ANSI colours
7
+ *
8
+ */
9
+
10
+FILE_LICENCE ( GPL2_OR_LATER );
11
+
12
+#include <stdint.h>
13
+#include <curses.h> /* For COLOR_RED etc. */
14
+
15
+/** Default colour (usually white foreground, black background) */
16
+#define COLOUR_DEFAULT 9
17
+#define COLOR_DEFAULT COLOUR_DEFAULT
18
+
19
+/** RGB value for "not defined" */
20
+#define ANSICOL_NO_RGB 0x01000000
21
+
22
+/**
23
+ * @defgroup ansicolpairs ANSI colour pairs
24
+ * @{
25
+ */
26
+
27
+/** Default colour pair */
28
+#define CPAIR_DEFAULT 0
29
+
30
+/** Normal text */
31
+#define CPAIR_NORMAL 1
32
+
33
+/** Highlighted text */
34
+#define CPAIR_SELECT 2
35
+
36
+/** Unselectable text (e.g. continuation ellipses, menu separators) */
37
+#define CPAIR_SEPARATOR 3
38
+
39
+/** Editable text */
40
+#define CPAIR_EDIT 4
41
+
42
+/** Error text */
43
+#define CPAIR_ALERT 5
44
+
45
+/** URL text */
46
+#define CPAIR_URL 6
47
+
48
+/** PXE selected menu entry */
49
+#define CPAIR_PXE 7
50
+
51
+/** @} */
52
+
53
+/** An ANSI colour pair definition */
54
+struct ansicol_pair {
55
+	/** Foreground colour index */
56
+	uint8_t foreground;
57
+	/** Background colour index */
58
+	uint8_t background;
59
+} __attribute__ (( packed ));
60
+
61
+/* ansicol.c */
62
+extern void ansicol_set_pair ( unsigned int cpair );
63
+extern int ansicol_define_pair ( unsigned int cpair, unsigned int foreground,
64
+				 unsigned int background );
65
+
66
+/* ansicoldef.c */
67
+extern int ansicol_define ( unsigned int colour, unsigned int ansi,
68
+			    uint32_t rgb );
69
+
70
+/* Function provided by ansicol.c but overridden by ansicoldef.c, if present */
71
+extern void ansicol_set ( unsigned int colour, unsigned int which );
72
+
73
+#endif /* _IPXE_ANSICOL_H */

+ 2
- 0
src/include/ipxe/errfile.h 查看文件

@@ -66,6 +66,8 @@ FILE_LICENCE ( GPL2_OR_LATER );
66 66
 #define ERRFILE_null_reboot	       ( ERRFILE_CORE | 0x001a0000 )
67 67
 #define ERRFILE_pinger		       ( ERRFILE_CORE | 0x001b0000 )
68 68
 #define ERRFILE_fbcon		       ( ERRFILE_CORE | 0x001c0000 )
69
+#define ERRFILE_ansicol		       ( ERRFILE_CORE | 0x001d0000 )
70
+#define ERRFILE_ansicoldef	       ( ERRFILE_CORE | 0x001e0000 )
69 71
 
70 72
 #define ERRFILE_eisa		     ( ERRFILE_DRIVER | 0x00000000 )
71 73
 #define ERRFILE_isa		     ( ERRFILE_DRIVER | 0x00010000 )

正在加载...
取消
保存