iPXE
ansicol.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 <assert.h>
00029 #include <ipxe/ansiesc.h>
00030 #include <ipxe/ansicol.h>
00031 #include <config/colour.h>
00032 
00033 /** @file
00034  *
00035  * ANSI colours
00036  *
00037  */
00038 
00039 /** ANSI colour pair definitions */
00040 static struct ansicol_pair ansicol_pairs[] = {
00041         [CPAIR_DEFAULT] = { COLOR_DEFAULT, COLOR_DEFAULT },
00042         [CPAIR_NORMAL] = { COLOR_NORMAL_FG, COLOR_NORMAL_BG },
00043         [CPAIR_SELECT] = { COLOR_SELECT_FG, COLOR_SELECT_BG },
00044         [CPAIR_SEPARATOR] = { COLOR_SEPARATOR_FG, COLOR_SEPARATOR_BG },
00045         [CPAIR_EDIT] = { COLOR_EDIT_FG, COLOR_EDIT_BG },
00046         [CPAIR_ALERT] = { COLOR_ALERT_FG, COLOR_ALERT_BG },
00047         [CPAIR_URL] = { COLOR_URL_FG, COLOR_URL_BG },
00048         [CPAIR_PXE] = { COLOR_PXE_FG, COLOR_PXE_BG },
00049 };
00050 
00051 /**
00052  * Set ANSI colour (when no colour definition support is present)
00053  *
00054  * @v colour            Colour index
00055  * @v which             Foreground/background selector
00056  */
00057 __weak void ansicol_set ( unsigned int colour, unsigned int which ) {
00058 
00059         /* Colour indices are hardcoded and should never be out of range */
00060         assert ( colour < 10 );
00061 
00062         /* Set basic colour */
00063         printf ( CSI "%c%dm", which, colour );
00064 }
00065 
00066 /**
00067  * Set ANSI foreground colour
00068  *
00069  * @v colour            Colour index
00070  */
00071 static void ansicol_foreground ( unsigned int colour ) {
00072         ansicol_set ( colour, '3' );
00073 }
00074 
00075 /**
00076  * Set ANSI background colour
00077  *
00078  * @v colour            Colour index
00079  */
00080 static void ansicol_background ( unsigned int colour ) {
00081         ansicol_set ( colour, '4' );
00082 }
00083 
00084 /**
00085  * Set ANSI foreground and background colour
00086  *
00087  * @v cpair             Colour pair index
00088  */
00089 void ansicol_set_pair ( unsigned int cpair ) {
00090         struct ansicol_pair *pair;
00091 
00092         /* Colour pair indices are hardcoded and should never be out of range */
00093         assert ( cpair < ( sizeof ( ansicol_pairs ) /
00094                            sizeof ( ansicol_pairs[0] ) ) );
00095 
00096         /* Set both foreground and background colours */
00097         pair = &ansicol_pairs[cpair];
00098         ansicol_foreground ( pair->foreground );
00099         ansicol_background ( pair->background );
00100 }
00101 
00102 /**
00103  * Define ANSI colour pair
00104  *
00105  * @v cpair             Colour pair index
00106  * @v foreground        Foreground colour index
00107  * @v background        Background colour index
00108  * @ret rc              Return status code
00109  */
00110 int ansicol_define_pair ( unsigned int cpair, unsigned int foreground,
00111                           unsigned int background ) {
00112         struct ansicol_pair *pair;
00113 
00114         /* Fail if colour index is out of range */
00115         if ( cpair >= ( sizeof ( ansicol_pairs ) / sizeof ( ansicol_pairs[0] )))
00116                 return -EINVAL;
00117 
00118         /* Update colour pair definition */
00119         pair = &ansicol_pairs[cpair];
00120         pair->foreground = foreground;
00121         pair->background = background;
00122         DBGC ( &ansicol_pairs[0], "ANSICOL redefined colour pair %d as "
00123                "foreground %d background %d\n", cpair, foreground, background );
00124 
00125         return 0;
00126 }