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.

console_cmd.c 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * Console management commands
  23. *
  24. */
  25. #include <string.h>
  26. #include <getopt.h>
  27. #include <ipxe/command.h>
  28. #include <ipxe/parseopt.h>
  29. #include <ipxe/console.h>
  30. #include <ipxe/image.h>
  31. #include <ipxe/pixbuf.h>
  32. #include <ipxe/ansiesc.h>
  33. #include <ipxe/ansicol.h>
  34. #include <usr/imgmgmt.h>
  35. /** "console" options */
  36. struct console_options {
  37. /** Console configuration */
  38. struct console_configuration config;
  39. /** Picture URI */
  40. char *picture;
  41. /** Keep picture after configuration */
  42. int keep;
  43. };
  44. /** "console" option list */
  45. static struct option_descriptor console_opts[] = {
  46. OPTION_DESC ( "x", 'x', required_argument,
  47. struct console_options, config.width, parse_integer ),
  48. OPTION_DESC ( "y", 'y', required_argument,
  49. struct console_options, config.height, parse_integer ),
  50. OPTION_DESC ( "left", 'l', required_argument,
  51. struct console_options, config.left, parse_integer ),
  52. OPTION_DESC ( "right", 'r', required_argument,
  53. struct console_options, config.right, parse_integer ),
  54. OPTION_DESC ( "top", 't', required_argument,
  55. struct console_options, config.top, parse_integer ),
  56. OPTION_DESC ( "bottom", 'b', required_argument,
  57. struct console_options, config.bottom, parse_integer ),
  58. OPTION_DESC ( "depth", 'd', required_argument,
  59. struct console_options, config.depth, parse_integer ),
  60. OPTION_DESC ( "picture", 'p', required_argument,
  61. struct console_options, picture, parse_string ),
  62. OPTION_DESC ( "keep", 'k', no_argument,
  63. struct console_options, keep, parse_flag ),
  64. };
  65. /** "console" command descriptor */
  66. static struct command_descriptor console_cmd =
  67. COMMAND_DESC ( struct console_options, console_opts, 0, 0, NULL );
  68. /**
  69. * "console" command
  70. *
  71. * @v argc Argument count
  72. * @v argv Argument list
  73. * @ret rc Return status code
  74. */
  75. static int console_exec ( int argc, char **argv ) {
  76. struct console_options opts;
  77. struct image *image = NULL;
  78. int rc;
  79. /* Parse options */
  80. if ( ( rc = parse_options ( argc, argv, &console_cmd, &opts ) ) != 0 )
  81. goto err_parse;
  82. /* Handle background picture, if applicable */
  83. if ( opts.picture ) {
  84. /* Acquire image */
  85. if ( ( rc = imgacquire ( opts.picture, &image ) ) != 0 )
  86. goto err_acquire;
  87. /* Convert to pixel buffer */
  88. if ( ( rc = image_pixbuf ( image, &opts.config.pixbuf ) ) != 0){
  89. printf ( "Could not use picture: %s\n",
  90. strerror ( rc ) );
  91. goto err_pixbuf;
  92. }
  93. /* Apply image's width and height if none specified */
  94. if ( ! opts.config.width )
  95. opts.config.width = opts.config.pixbuf->width;
  96. if ( ! opts.config.height )
  97. opts.config.height = opts.config.pixbuf->height;
  98. }
  99. /* Configure console */
  100. if ( ( rc = console_configure ( &opts.config ) ) != 0 ) {
  101. printf ( "Could not configure console: %s\n", strerror ( rc ) );
  102. goto err_configure;
  103. }
  104. /* Reapply default colour pair and clear screen */
  105. ansicol_set_pair ( CPAIR_DEFAULT );
  106. printf ( CSI "2J" CSI "H" );
  107. err_configure:
  108. pixbuf_put ( opts.config.pixbuf );
  109. err_pixbuf:
  110. /* Discard image unless --keep was specified */
  111. if ( image && ( ! opts.keep ) )
  112. unregister_image ( image );
  113. err_acquire:
  114. err_parse:
  115. return rc;
  116. }
  117. /** "colour" options */
  118. struct colour_options {
  119. /** Basic colour */
  120. unsigned int basic;
  121. /** 24-bit RGB value */
  122. unsigned int rgb;
  123. };
  124. /** "colour" option list */
  125. static struct option_descriptor colour_opts[] = {
  126. OPTION_DESC ( "basic", 'b', required_argument,
  127. struct colour_options, basic, parse_integer ),
  128. OPTION_DESC ( "rgb", 'r', required_argument,
  129. struct colour_options, rgb, parse_integer ),
  130. };
  131. /** "colour" command descriptor */
  132. static struct command_descriptor colour_cmd =
  133. COMMAND_DESC ( struct colour_options, colour_opts, 1, 1, "<colour>" );
  134. /**
  135. * "colour" command
  136. *
  137. * @v argc Argument count
  138. * @v argv Argument list
  139. * @ret rc Return status code
  140. */
  141. static int colour_exec ( int argc, char **argv ) {
  142. struct colour_options opts;
  143. unsigned int colour;
  144. int rc;
  145. /* Initialise options */
  146. memset ( &opts, 0, sizeof ( opts ) );
  147. opts.basic = COLOUR_DEFAULT;
  148. opts.rgb = ANSICOL_NO_RGB;
  149. /* Parse options */
  150. if ( ( rc = reparse_options ( argc, argv, &colour_cmd, &opts ) ) != 0 )
  151. return rc;
  152. /* Parse colour index */
  153. if ( ( rc = parse_integer ( argv[optind], &colour ) ) != 0 )
  154. return rc;
  155. /* Define colour */
  156. if ( ( rc = ansicol_define ( colour, opts.basic, opts.rgb ) ) != 0 ) {
  157. printf ( "Could not define colour: %s\n", strerror ( rc ) );
  158. return rc;
  159. }
  160. /* Reapply default colour pair, in case definition has changed */
  161. ansicol_set_pair ( CPAIR_DEFAULT );
  162. return 0;
  163. }
  164. /** "cpair" options */
  165. struct cpair_options {
  166. /** Foreground colour */
  167. unsigned int foreground;
  168. /** Background colour */
  169. unsigned int background;
  170. };
  171. /** "cpair" option list */
  172. static struct option_descriptor cpair_opts[] = {
  173. OPTION_DESC ( "foreground", 'f', required_argument,
  174. struct cpair_options, foreground, parse_integer ),
  175. OPTION_DESC ( "background", 'b', required_argument,
  176. struct cpair_options, background, parse_integer ),
  177. };
  178. /** "cpair" command descriptor */
  179. static struct command_descriptor cpair_cmd =
  180. COMMAND_DESC ( struct cpair_options, cpair_opts, 1, 1, "<cpair>" );
  181. /**
  182. * "cpair" command
  183. *
  184. * @v argc Argument count
  185. * @v argv Argument list
  186. * @ret rc Return status code
  187. */
  188. static int cpair_exec ( int argc, char **argv ) {
  189. struct cpair_options opts;
  190. unsigned int cpair;
  191. int rc;
  192. /* Initialise options */
  193. memset ( &opts, 0, sizeof ( opts ) );
  194. opts.foreground = COLOUR_DEFAULT;
  195. opts.background = COLOUR_DEFAULT;
  196. /* Parse options */
  197. if ( ( rc = reparse_options ( argc, argv, &cpair_cmd, &opts ) ) != 0 )
  198. return rc;
  199. /* Parse colour pair index */
  200. if ( ( rc = parse_integer ( argv[optind], &cpair ) ) != 0 )
  201. return rc;
  202. /* Define colour pair */
  203. if ( ( rc = ansicol_define_pair ( cpair, opts.foreground,
  204. opts.background ) ) != 0 ) {
  205. printf ( "Could not define colour pair: %s\n",
  206. strerror ( rc ) );
  207. return rc;
  208. }
  209. /* Reapply default colour pair, in case definition has changed */
  210. ansicol_set_pair ( CPAIR_DEFAULT );
  211. return 0;
  212. }
  213. /** Console management commands */
  214. struct command console_commands[] __command = {
  215. {
  216. .name = "console",
  217. .exec = console_exec,
  218. },
  219. {
  220. .name = "colour",
  221. .exec = colour_exec,
  222. },
  223. {
  224. .name = "cpair",
  225. .exec = cpair_exec,
  226. },
  227. };