You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ansicoldef.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #include <ipxe/ansiesc.h>
  27. #include <ipxe/ansicol.h>
  28. #include <config/colour.h>
  29. /** @file
  30. *
  31. * ANSI colour definitions
  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. * Extract basic colour from ANSI colour definition
  44. *
  45. * @v ansicol ANSI colour definition
  46. * @ret basic Basic colour
  47. */
  48. #define ANSICOL_BASIC( ansicol ) ( (ansicol) >> 28 )
  49. /**
  50. * Extract 24-bit RGB value from ANSI colour definition
  51. *
  52. * @v ansicol ANSI colour definition
  53. * @ret rgb 24-bit RGB value
  54. */
  55. #define ANSICOL_RGB( ansicol ) ( ( (ansicol) >> 0 ) & 0xffffffUL )
  56. /**
  57. * Extract 24-bit RGB value red component from ANSI colour definition
  58. *
  59. * @v ansicol ANSI colour definition
  60. * @ret red Red component
  61. */
  62. #define ANSICOL_RED( ansicol ) ( ( (ansicol) >> 16 ) & 0xff )
  63. /**
  64. * Extract 24-bit RGB value green component from ANSI colour definition
  65. *
  66. * @v ansicol ANSI colour definition
  67. * @ret green Green component
  68. */
  69. #define ANSICOL_GREEN( ansicol ) ( ( (ansicol) >> 8 ) & 0xff )
  70. /**
  71. * Extract 24-bit RGB value blue component from ANSI colour definition
  72. *
  73. * @v ansicol ANSI colour definition
  74. * @ret blue Blue component
  75. */
  76. #define ANSICOL_BLUE( ansicol ) ( ( (ansicol) >> 0 ) & 0xff )
  77. /**
  78. * Construct default ANSI colour definition
  79. *
  80. * @v basic Basic colour
  81. * @ret ansicol ANSI colour definition
  82. *
  83. * Colours default to being just a basic colour. If the colour
  84. * matches the normal UI text background colour, then its basic colour
  85. * value is set to @c ANSICOL_MAGIC.
  86. */
  87. #define ANSICOL_DEFAULT( basic ) \
  88. ANSICOL_DEFINE ( ( ( (basic) == COLOR_NORMAL_BG ) ? \
  89. ANSICOL_MAGIC : (basic) ), \
  90. ANSICOL_NO_RGB )
  91. /** ANSI colour definitions */
  92. static uint32_t ansicols[] = {
  93. [COLOR_BLACK] = ANSICOL_DEFAULT ( COLOR_BLACK ),
  94. [COLOR_RED] = ANSICOL_DEFAULT ( COLOR_RED ),
  95. [COLOR_GREEN] = ANSICOL_DEFAULT ( COLOR_GREEN ),
  96. [COLOR_YELLOW] = ANSICOL_DEFAULT ( COLOR_YELLOW ),
  97. [COLOR_BLUE] = ANSICOL_DEFAULT ( COLOR_BLUE ),
  98. [COLOR_MAGENTA] = ANSICOL_DEFAULT ( COLOR_MAGENTA ),
  99. [COLOR_CYAN] = ANSICOL_DEFAULT ( COLOR_CYAN ),
  100. [COLOR_WHITE] = ANSICOL_DEFAULT ( COLOR_WHITE ),
  101. };
  102. /** Magic basic colour */
  103. static uint8_t ansicol_magic = COLOR_NORMAL_BG;
  104. /**
  105. * Define ANSI colour
  106. *
  107. * @v colour Colour index
  108. * @v basic Basic colour
  109. * @v rgb 24-bit RGB value (or ANSICOL_NO_RGB)
  110. * @ret rc Return status code
  111. */
  112. int ansicol_define ( unsigned int colour, unsigned int basic, uint32_t rgb ) {
  113. uint32_t ansicol;
  114. /* Fail if colour index is out of range */
  115. if ( colour >= ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) )
  116. return -EINVAL;
  117. /* Update colour definition */
  118. ansicol = ANSICOL_DEFINE ( basic, rgb );
  119. ansicols[colour] = ansicol;
  120. DBGC ( &ansicols[0], "ANSICOL redefined colour %d as basic %d RGB "
  121. "%#06lx%s\n", colour, ANSICOL_BASIC ( ansicol ),
  122. ANSICOL_RGB ( ansicol ),
  123. ( ( ansicol & ANSICOL_NO_RGB ) ? " [norgb]" : "" ) );
  124. return 0;
  125. }
  126. /**
  127. * Set ANSI colour (using colour definitions)
  128. *
  129. * @v colour Colour index
  130. * @v which Foreground/background selector
  131. */
  132. void ansicol_set ( unsigned int colour, unsigned int which ) {
  133. uint32_t ansicol;
  134. unsigned int basic;
  135. /* Use default colour if colour index is out of range */
  136. if ( colour < ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) ) {
  137. ansicol = ansicols[colour];
  138. } else {
  139. ansicol = ANSICOL_DEFINE ( COLOUR_DEFAULT, ANSICOL_NO_RGB );
  140. }
  141. /* If basic colour is out of range, use the magic colour */
  142. basic = ANSICOL_BASIC ( ansicol );
  143. if ( basic >= 10 )
  144. basic = ansicol_magic;
  145. /* Set basic colour first */
  146. printf ( CSI "%c%dm", which, basic );
  147. /* Set 24-bit RGB colour, if applicable */
  148. if ( ! ( ansicol & ANSICOL_NO_RGB ) ) {
  149. printf ( CSI "%c8;2;%d;%d;%dm", which, ANSICOL_RED ( ansicol ),
  150. ANSICOL_GREEN ( ansicol ), ANSICOL_BLUE ( ansicol ) );
  151. }
  152. }
  153. /**
  154. * Reset magic colour
  155. *
  156. */
  157. void ansicol_reset_magic ( void ) {
  158. /* Set to the compile-time default background colour */
  159. ansicol_magic = COLOR_NORMAL_BG;
  160. }
  161. /**
  162. * Set magic colour to transparent
  163. *
  164. */
  165. void ansicol_set_magic_transparent ( void ) {
  166. /* Set to the console default colour (which will give a
  167. * transparent background on the framebuffer console).
  168. */
  169. ansicol_magic = COLOR_DEFAULT;
  170. }