iPXE
ansicoldef.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdio.h>
00027 #include <errno.h>
00028 #include <ipxe/ansiesc.h>
00029 #include <ipxe/ansicol.h>
00030 #include <config/colour.h>
00031 
00032 /** @file
00033  *
00034  * ANSI colour definitions
00035  *
00036  */
00037 
00038 /**
00039  * Construct ANSI colour definition
00040  *
00041  * @v basic             Basic colour
00042  * @v rgb               24-bit RGB value (or ANSICOL_NO_RGB)
00043  * @ret ansicol         ANSI colour definition
00044  */
00045 #define ANSICOL_DEFINE( basic, rgb ) ( ( (basic) << 28 ) | (rgb) )
00046 
00047 /**
00048  * Extract basic colour from ANSI colour definition
00049  *
00050  * @v ansicol           ANSI colour definition
00051  * @ret basic           Basic colour
00052  */
00053 #define ANSICOL_BASIC( ansicol ) ( (ansicol) >> 28 )
00054 
00055 /**
00056  * Extract 24-bit RGB value from ANSI colour definition
00057  *
00058  * @v ansicol           ANSI colour definition
00059  * @ret rgb             24-bit RGB value
00060  */
00061 #define ANSICOL_RGB( ansicol ) ( ( (ansicol) >> 0 ) & 0xffffffUL )
00062 
00063 /**
00064  * Extract 24-bit RGB value red component from ANSI colour definition
00065  *
00066  * @v ansicol           ANSI colour definition
00067  * @ret red             Red component
00068  */
00069 #define ANSICOL_RED( ansicol ) ( ( (ansicol) >> 16 ) & 0xff )
00070 
00071 /**
00072  * Extract 24-bit RGB value green component from ANSI colour definition
00073  *
00074  * @v ansicol           ANSI colour definition
00075  * @ret green           Green component
00076  */
00077 #define ANSICOL_GREEN( ansicol ) ( ( (ansicol) >> 8 ) & 0xff )
00078 
00079 /**
00080  * Extract 24-bit RGB value blue component from ANSI colour definition
00081  *
00082  * @v ansicol           ANSI colour definition
00083  * @ret blue            Blue component
00084  */
00085 #define ANSICOL_BLUE( ansicol ) ( ( (ansicol) >> 0 ) & 0xff )
00086 
00087 /**
00088  * Construct default ANSI colour definition
00089  *
00090  * @v basic             Basic colour
00091  * @ret ansicol         ANSI colour definition
00092  *
00093  * Colours default to being just a basic colour.  If the colour
00094  * matches the normal UI text background colour, then its basic colour
00095  * value is set to @c ANSICOL_MAGIC.
00096  */
00097 #define ANSICOL_DEFAULT( basic )                                        \
00098         ANSICOL_DEFINE ( ( ( (basic) == COLOR_NORMAL_BG ) ?             \
00099                            ANSICOL_MAGIC : (basic) ),                   \
00100                          ANSICOL_NO_RGB )
00101 
00102 /** ANSI colour definitions */
00103 static uint32_t ansicols[] = {
00104         [COLOR_BLACK]   = ANSICOL_DEFAULT ( COLOR_BLACK ),
00105         [COLOR_RED]     = ANSICOL_DEFAULT ( COLOR_RED ),
00106         [COLOR_GREEN]   = ANSICOL_DEFAULT ( COLOR_GREEN ),
00107         [COLOR_YELLOW]  = ANSICOL_DEFAULT ( COLOR_YELLOW ),
00108         [COLOR_BLUE]    = ANSICOL_DEFAULT ( COLOR_BLUE ),
00109         [COLOR_MAGENTA] = ANSICOL_DEFAULT ( COLOR_MAGENTA ),
00110         [COLOR_CYAN]    = ANSICOL_DEFAULT ( COLOR_CYAN ),
00111         [COLOR_WHITE]   = ANSICOL_DEFAULT ( COLOR_WHITE ),
00112 };
00113 
00114 /** Magic basic colour */
00115 static uint8_t ansicol_magic = COLOR_NORMAL_BG;
00116 
00117 /**
00118  * Define ANSI colour
00119  *
00120  * @v colour            Colour index
00121  * @v basic             Basic colour
00122  * @v rgb               24-bit RGB value (or ANSICOL_NO_RGB)
00123  * @ret rc              Return status code
00124  */
00125 int ansicol_define ( unsigned int colour, unsigned int basic, uint32_t rgb ) {
00126         uint32_t ansicol;
00127 
00128         /* Fail if colour index is out of range */
00129         if ( colour >= ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) )
00130                 return -EINVAL;
00131 
00132         /* Update colour definition */
00133         ansicol = ANSICOL_DEFINE ( basic, rgb );
00134         ansicols[colour] = ansicol;
00135         DBGC ( &ansicols[0], "ANSICOL redefined colour %d as basic %d RGB "
00136                "%#06lx%s\n", colour, ANSICOL_BASIC ( ansicol ),
00137                ANSICOL_RGB ( ansicol ),
00138                ( ( ansicol & ANSICOL_NO_RGB ) ? " [norgb]" : "" ) );
00139 
00140         return 0;
00141 }
00142 
00143 /**
00144  * Set ANSI colour (using colour definitions)
00145  *
00146  * @v colour            Colour index
00147  * @v which             Foreground/background selector
00148  */
00149 void ansicol_set ( unsigned int colour, unsigned int which ) {
00150         uint32_t ansicol;
00151         unsigned int basic;
00152 
00153         /* Use default colour if colour index is out of range */
00154         if ( colour < ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) ) {
00155                 ansicol = ansicols[colour];
00156         } else {
00157                 ansicol = ANSICOL_DEFINE ( COLOUR_DEFAULT, ANSICOL_NO_RGB );
00158         }
00159 
00160         /* If basic colour is out of range, use the magic colour */
00161         basic = ANSICOL_BASIC ( ansicol );
00162         if ( basic >= 10 )
00163                 basic = ansicol_magic;
00164 
00165         /* Set basic colour first */
00166         printf ( CSI "%c%dm", which, basic );
00167 
00168         /* Set 24-bit RGB colour, if applicable */
00169         if ( ! ( ansicol & ANSICOL_NO_RGB ) ) {
00170                 printf ( CSI "%c8;2;%d;%d;%dm", which, ANSICOL_RED ( ansicol ),
00171                          ANSICOL_GREEN ( ansicol ), ANSICOL_BLUE ( ansicol ) );
00172         }
00173 }
00174 
00175 /**
00176  * Reset magic colour
00177  *
00178  */
00179 void ansicol_reset_magic ( void ) {
00180 
00181         /* Set to the compile-time default background colour */
00182         ansicol_magic = COLOR_NORMAL_BG;
00183 }
00184 
00185 /**
00186  * Set magic colour to transparent
00187  *
00188  */
00189 void ansicol_set_magic_transparent ( void ) {
00190 
00191         /* Set to the console default colour (which will give a
00192          * transparent background on the framebuffer console).
00193          */
00194         ansicol_magic = COLOR_DEFAULT;
00195 }