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.2KB

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