diff --git a/src/config/colour.h b/src/config/colour.h index d32d46dc..c75f65e6 100644 --- a/src/config/colour.h +++ b/src/config/colour.h @@ -27,6 +27,9 @@ FILE_LICENCE ( GPL2_OR_LATER ); #define COLOR_URL_FG COLOR_CYAN #define COLOR_URL_BG COLOR_BLUE +#define COLOR_PXE_FG COLOR_BLACK +#define COLOR_PXE_BG COLOR_WHITE + #include #endif /* CONFIG_COLOUR_H */ diff --git a/src/core/ansicol.c b/src/core/ansicol.c new file mode 100644 index 00000000..142a00f8 --- /dev/null +++ b/src/core/ansicol.c @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2013 Michael Brown . + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +#include +#include +#include +#include +#include +#include + +/** @file + * + * ANSI colours + * + */ + +/** ANSI colour pair definitions */ +static struct ansicol_pair ansicol_pairs[] = { + [CPAIR_DEFAULT] = { COLOR_DEFAULT, COLOR_DEFAULT }, + [CPAIR_NORMAL] = { COLOR_NORMAL_FG, COLOR_NORMAL_BG }, + [CPAIR_SELECT] = { COLOR_SELECT_FG, COLOR_SELECT_BG }, + [CPAIR_SEPARATOR] = { COLOR_SEPARATOR_FG, COLOR_SEPARATOR_BG }, + [CPAIR_EDIT] = { COLOR_EDIT_FG, COLOR_EDIT_BG }, + [CPAIR_ALERT] = { COLOR_ALERT_FG, COLOR_ALERT_BG }, + [CPAIR_URL] = { COLOR_URL_FG, COLOR_URL_BG }, + [CPAIR_PXE] = { COLOR_PXE_FG, COLOR_PXE_BG }, +}; + +/** + * Set ANSI colour (when no colour definition support is present) + * + * @v colour Colour index + * @v which Foreground/background selector + */ +__weak void ansicol_set ( unsigned int colour, unsigned int which ) { + + /* Colour indices are hardcoded and should never be out of range */ + assert ( colour < 10 ); + + /* Set basic colour */ + printf ( CSI "%c%dm", which, colour ); +} + +/** + * Set ANSI foreground colour + * + * @v colour Colour index + */ +static void ansicol_foreground ( unsigned int colour ) { + ansicol_set ( colour, '3' ); +} + +/** + * Set ANSI background colour + * + * @v colour Colour index + */ +static void ansicol_background ( unsigned int colour ) { + ansicol_set ( colour, '4' ); +} + +/** + * Set ANSI foreground and background colour + * + * @v cpair Colour pair index + */ +void ansicol_set_pair ( unsigned int cpair ) { + struct ansicol_pair *pair; + + /* Colour pair indices are hardcoded and should never be out of range */ + assert ( cpair < ( sizeof ( ansicol_pairs ) / + sizeof ( ansicol_pairs[0] ) ) ); + + /* Set both foreground and background colours */ + pair = &ansicol_pairs[cpair]; + ansicol_foreground ( pair->foreground ); + ansicol_background ( pair->background ); +} + +/** + * Define ANSI colour pair + * + * @v cpair Colour pair index + * @v foreground Foreground colour index + * @v background Background colour index + * @ret rc Return status code + */ +int ansicol_define_pair ( unsigned int cpair, unsigned int foreground, + unsigned int background ) { + struct ansicol_pair *pair; + + /* Fail if colour index is out of range */ + if ( cpair >= ( sizeof ( ansicol_pairs ) / sizeof ( ansicol_pairs[0] ))) + return -EINVAL; + + /* Update colour pair definition */ + pair = &ansicol_pairs[cpair]; + pair->foreground = foreground; + pair->background = background; + DBGC ( &ansicol_pairs[0], "ANSICOL redefined colour pair %d as " + "foreground %d background %d\n", cpair, foreground, background ); + + return 0; +} diff --git a/src/core/ansicoldef.c b/src/core/ansicoldef.c new file mode 100644 index 00000000..56135611 --- /dev/null +++ b/src/core/ansicoldef.c @@ -0,0 +1,161 @@ +/* + * Copyright (C) 2013 Michael Brown . + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +#include +#include +#include +#include +#include + +/** @file + * + * ANSI colour definitions + * + */ + +/** + * Construct ANSI colour definition + * + * @v basic Basic colour + * @v rgb 24-bit RGB value (or ANSICOL_NO_RGB) + * @ret ansicol ANSI colour definition + */ +#define ANSICOL_DEFINE( basic, rgb ) ( ( (basic) << 28 ) | (rgb) ) + +/** + * Extract basic colour from ANSI colour definition + * + * @v ansicol ANSI colour definition + * @ret basic Basic colour + */ +#define ANSICOL_BASIC( ansicol ) ( (ansicol) >> 28 ) + +/** + * Extract 24-bit RGB value from ANSI colour definition + * + * @v ansicol ANSI colour definition + * @ret rgb 24-bit RGB value + */ +#define ANSICOL_RGB( ansicol ) ( ( (ansicol) >> 0 ) & 0xffffffUL ) + +/** + * Extract 24-bit RGB value red component from ANSI colour definition + * + * @v ansicol ANSI colour definition + * @ret red Red component + */ +#define ANSICOL_RED( ansicol ) ( ( (ansicol) >> 16 ) & 0xff ) + +/** + * Extract 24-bit RGB value green component from ANSI colour definition + * + * @v ansicol ANSI colour definition + * @ret green Green component + */ +#define ANSICOL_GREEN( ansicol ) ( ( (ansicol) >> 8 ) & 0xff ) + +/** + * Extract 24-bit RGB value blue component from ANSI colour definition + * + * @v ansicol ANSI colour definition + * @ret blue Blue component + */ +#define ANSICOL_BLUE( ansicol ) ( ( (ansicol) >> 0 ) & 0xff ) + +/** + * Construct default ANSI colour definition + * + * @v basic Basic colour + * @ret ansicol ANSI colour definition + * + * Colours default to being just a basic colour. + */ +#define ANSICOL_DEFAULT( basic ) ANSICOL_DEFINE ( (basic), ANSICOL_NO_RGB ) + +/** ANSI colour definitions */ +static uint32_t ansicols[] = { + [COLOR_BLACK] = ANSICOL_DEFAULT ( COLOR_BLACK ), + [COLOR_RED] = ANSICOL_DEFAULT ( COLOR_RED ), + [COLOR_GREEN] = ANSICOL_DEFAULT ( COLOR_GREEN ), + [COLOR_YELLOW] = ANSICOL_DEFAULT ( COLOR_YELLOW ), + [COLOR_BLUE] = ANSICOL_DEFAULT ( COLOR_BLUE ), + [COLOR_MAGENTA] = ANSICOL_DEFAULT ( COLOR_MAGENTA ), + [COLOR_CYAN] = ANSICOL_DEFAULT ( COLOR_CYAN ), + [COLOR_WHITE] = ANSICOL_DEFAULT ( COLOR_WHITE ), +}; + +/** + * Define ANSI colour + * + * @v colour Colour index + * @v basic Basic colour + * @v rgb 24-bit RGB value (or ANSICOL_NO_RGB) + * @ret rc Return status code + */ +int ansicol_define ( unsigned int colour, unsigned int basic, uint32_t rgb ) { + uint32_t ansicol; + + /* Fail if colour index is out of range */ + if ( colour >= ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) ) + return -EINVAL; + + /* Update colour definition */ + ansicol = ANSICOL_DEFINE ( basic, rgb ); + ansicols[colour] = ansicol; + DBGC ( &ansicols[0], "ANSICOL redefined colour %d as basic %d RGB " + "%#06lx%s\n", colour, ANSICOL_BASIC ( ansicol ), + ANSICOL_RGB ( ansicol ), + ( ( ansicol & ANSICOL_NO_RGB ) ? " [norgb]" : "" ) ); + + return 0; +} + +/** + * Set ANSI colour (using colour definitions) + * + * @v colour Colour index + * @v which Foreground/background selector + */ +void ansicol_set ( unsigned int colour, unsigned int which ) { + uint32_t ansicol; + unsigned int basic; + + /* Use default colour if colour index is out of range */ + if ( colour < ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) ) { + ansicol = ansicols[colour]; + } else { + ansicol = ANSICOL_DEFINE ( COLOUR_DEFAULT, ANSICOL_NO_RGB ); + } + + /* If basic colour is out of range, use the default colour */ + basic = ANSICOL_BASIC ( ansicol ); + if ( basic >= 10 ) + basic = COLOR_DEFAULT; + + /* Set basic colour first */ + printf ( CSI "%c%dm", which, basic ); + + /* Set 24-bit RGB colour, if applicable */ + if ( ! ( ansicol & ANSICOL_NO_RGB ) ) { + printf ( CSI "%c8;2;%d;%d;%dm", which, ANSICOL_RED ( ansicol ), + ANSICOL_GREEN ( ansicol ), ANSICOL_BLUE ( ansicol ) ); + } +} diff --git a/src/include/ipxe/ansicol.h b/src/include/ipxe/ansicol.h new file mode 100644 index 00000000..2fa08462 --- /dev/null +++ b/src/include/ipxe/ansicol.h @@ -0,0 +1,73 @@ +#ifndef _IPXE_ANSICOL_H +#define _IPXE_ANSICOL_H + +/** @file + * + * ANSI colours + * + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +#include +#include /* For COLOR_RED etc. */ + +/** Default colour (usually white foreground, black background) */ +#define COLOUR_DEFAULT 9 +#define COLOR_DEFAULT COLOUR_DEFAULT + +/** RGB value for "not defined" */ +#define ANSICOL_NO_RGB 0x01000000 + +/** + * @defgroup ansicolpairs ANSI colour pairs + * @{ + */ + +/** Default colour pair */ +#define CPAIR_DEFAULT 0 + +/** Normal text */ +#define CPAIR_NORMAL 1 + +/** Highlighted text */ +#define CPAIR_SELECT 2 + +/** Unselectable text (e.g. continuation ellipses, menu separators) */ +#define CPAIR_SEPARATOR 3 + +/** Editable text */ +#define CPAIR_EDIT 4 + +/** Error text */ +#define CPAIR_ALERT 5 + +/** URL text */ +#define CPAIR_URL 6 + +/** PXE selected menu entry */ +#define CPAIR_PXE 7 + +/** @} */ + +/** An ANSI colour pair definition */ +struct ansicol_pair { + /** Foreground colour index */ + uint8_t foreground; + /** Background colour index */ + uint8_t background; +} __attribute__ (( packed )); + +/* ansicol.c */ +extern void ansicol_set_pair ( unsigned int cpair ); +extern int ansicol_define_pair ( unsigned int cpair, unsigned int foreground, + unsigned int background ); + +/* ansicoldef.c */ +extern int ansicol_define ( unsigned int colour, unsigned int ansi, + uint32_t rgb ); + +/* Function provided by ansicol.c but overridden by ansicoldef.c, if present */ +extern void ansicol_set ( unsigned int colour, unsigned int which ); + +#endif /* _IPXE_ANSICOL_H */ diff --git a/src/include/ipxe/errfile.h b/src/include/ipxe/errfile.h index b2476480..32c596ff 100644 --- a/src/include/ipxe/errfile.h +++ b/src/include/ipxe/errfile.h @@ -66,6 +66,8 @@ FILE_LICENCE ( GPL2_OR_LATER ); #define ERRFILE_null_reboot ( ERRFILE_CORE | 0x001a0000 ) #define ERRFILE_pinger ( ERRFILE_CORE | 0x001b0000 ) #define ERRFILE_fbcon ( ERRFILE_CORE | 0x001c0000 ) +#define ERRFILE_ansicol ( ERRFILE_CORE | 0x001d0000 ) +#define ERRFILE_ansicoldef ( ERRFILE_CORE | 0x001e0000 ) #define ERRFILE_eisa ( ERRFILE_DRIVER | 0x00000000 ) #define ERRFILE_isa ( ERRFILE_DRIVER | 0x00010000 )