2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
24 * Console management commands
30 #include <ipxe/command.h>
31 #include <ipxe/parseopt.h>
32 #include <ipxe/console.h>
33 #include <ipxe/image.h>
34 #include <ipxe/pixbuf.h>
35 #include <ipxe/ansiesc.h>
36 #include <ipxe/ansicol.h>
37 #include <usr/imgmgmt.h>
39 /** "console" options */
40 struct console_options {
41 /** Console configuration */
42 struct console_configuration config;
45 /** Keep picture after configuration */
49 /** "console" option list */
50 static struct option_descriptor console_opts[] = {
51 OPTION_DESC ( "x", 'x', required_argument,
52 struct console_options, config.width, parse_integer ),
53 OPTION_DESC ( "y", 'y', required_argument,
54 struct console_options, config.height, parse_integer ),
55 OPTION_DESC ( "left", 'l', required_argument,
56 struct console_options, config.left, parse_integer ),
57 OPTION_DESC ( "right", 'r', required_argument,
58 struct console_options, config.right, parse_integer ),
59 OPTION_DESC ( "top", 't', required_argument,
60 struct console_options, config.top, parse_integer ),
61 OPTION_DESC ( "bottom", 'b', required_argument,
62 struct console_options, config.bottom, parse_integer ),
63 OPTION_DESC ( "depth", 'd', required_argument,
64 struct console_options, config.depth, parse_integer ),
65 OPTION_DESC ( "picture", 'p', required_argument,
66 struct console_options, picture, parse_string ),
67 OPTION_DESC ( "keep", 'k', no_argument,
68 struct console_options, keep, parse_flag ),
71 /** "console" command descriptor */
72 static struct command_descriptor console_cmd =
73 COMMAND_DESC ( struct console_options, console_opts, 0, 0, NULL );
78 * @v argc Argument count
79 * @v argv Argument list
80 * @ret rc Return status code
82 static int console_exec ( int argc, char **argv ) {
83 struct console_options opts;
84 struct image *image = NULL;
88 if ( ( rc = parse_options ( argc, argv, &console_cmd, &opts ) ) != 0 )
91 /* Handle background picture, if applicable */
95 if ( ( rc = imgacquire ( opts.picture, 0, &image ) ) != 0 )
98 /* Convert to pixel buffer */
99 if ( ( rc = image_pixbuf ( image, &opts.config.pixbuf ) ) != 0){
100 printf ( "Could not use picture: %s\n",
105 /* Apply image's width and height if none specified */
106 if ( ! opts.config.width )
107 opts.config.width = opts.config.pixbuf->width;
108 if ( ! opts.config.height )
109 opts.config.height = opts.config.pixbuf->height;
112 /* Configure console */
113 if ( ( rc = console_configure ( &opts.config ) ) != 0 ) {
114 printf ( "Could not configure console: %s\n", strerror ( rc ) );
118 /* Reapply default colour pair and clear screen */
119 ansicol_set_pair ( CPAIR_DEFAULT );
120 printf ( CSI "2J" CSI "H" );
123 pixbuf_put ( opts.config.pixbuf );
125 /* Discard image unless --keep was specified */
126 if ( image && ( ! opts.keep ) )
127 unregister_image ( image );
133 /** "colour" options */
134 struct colour_options {
137 /** 24-bit RGB value */
141 /** "colour" option list */
142 static struct option_descriptor colour_opts[] = {
143 OPTION_DESC ( "basic", 'b', required_argument,
144 struct colour_options, basic, parse_integer ),
145 OPTION_DESC ( "rgb", 'r', required_argument,
146 struct colour_options, rgb, parse_integer ),
149 /** "colour" command descriptor */
150 static struct command_descriptor colour_cmd =
151 COMMAND_DESC ( struct colour_options, colour_opts, 1, 1, "<colour>" );
156 * @v argc Argument count
157 * @v argv Argument list
158 * @ret rc Return status code
160 static int colour_exec ( int argc, char **argv ) {
161 struct colour_options opts;
165 /* Initialise options */
166 memset ( &opts, 0, sizeof ( opts ) );
167 opts.basic = COLOUR_DEFAULT;
168 opts.rgb = ANSICOL_NO_RGB;
171 if ( ( rc = reparse_options ( argc, argv, &colour_cmd, &opts ) ) != 0 )
174 /* Parse colour index */
175 if ( ( rc = parse_integer ( argv[optind], &colour ) ) != 0 )
179 if ( ( rc = ansicol_define ( colour, opts.basic, opts.rgb ) ) != 0 ) {
180 printf ( "Could not define colour: %s\n", strerror ( rc ) );
184 /* Reapply default colour pair, in case definition has changed */
185 ansicol_set_pair ( CPAIR_DEFAULT );
190 /** "cpair" options */
191 struct cpair_options {
192 /** Foreground colour */
193 unsigned int foreground;
194 /** Background colour */
195 unsigned int background;
198 /** "cpair" option list */
199 static struct option_descriptor cpair_opts[] = {
200 OPTION_DESC ( "foreground", 'f', required_argument,
201 struct cpair_options, foreground, parse_integer ),
202 OPTION_DESC ( "background", 'b', required_argument,
203 struct cpair_options, background, parse_integer ),
206 /** "cpair" command descriptor */
207 static struct command_descriptor cpair_cmd =
208 COMMAND_DESC ( struct cpair_options, cpair_opts, 1, 1, "<cpair>" );
213 * @v argc Argument count
214 * @v argv Argument list
215 * @ret rc Return status code
217 static int cpair_exec ( int argc, char **argv ) {
218 struct cpair_options opts;
222 /* Initialise options */
223 memset ( &opts, 0, sizeof ( opts ) );
224 opts.foreground = COLOUR_DEFAULT;
225 opts.background = COLOUR_DEFAULT;
228 if ( ( rc = reparse_options ( argc, argv, &cpair_cmd, &opts ) ) != 0 )
231 /* Parse colour pair index */
232 if ( ( rc = parse_integer ( argv[optind], &cpair ) ) != 0 )
235 /* Define colour pair */
236 if ( ( rc = ansicol_define_pair ( cpair, opts.foreground,
237 opts.background ) ) != 0 ) {
238 printf ( "Could not define colour pair: %s\n",
243 /* Reapply default colour pair, in case definition has changed */
244 ansicol_set_pair ( CPAIR_DEFAULT );
249 /** Console management commands */
250 struct command console_commands[] __command = {
253 .exec = console_exec,