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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
28 * Console management commands
34 #include <ipxe/command.h>
35 #include <ipxe/parseopt.h>
36 #include <ipxe/console.h>
37 #include <ipxe/image.h>
38 #include <ipxe/pixbuf.h>
39 #include <ipxe/ansiesc.h>
40 #include <ipxe/ansicol.h>
41 #include <usr/imgmgmt.h>
43 /** "console" options */
44 struct console_options
{
45 /** Console configuration */
46 struct console_configuration config
;
49 /** Keep picture after configuration */
53 /** "console" option list */
54 static struct option_descriptor console_opts
[] = {
55 OPTION_DESC ( "x", 'x', required_argument
,
56 struct console_options
, config
.width
, parse_integer
),
57 OPTION_DESC ( "y", 'y', required_argument
,
58 struct console_options
, config
.height
, parse_integer
),
59 OPTION_DESC ( "left", 'l', required_argument
,
60 struct console_options
, config
.left
, parse_integer
),
61 OPTION_DESC ( "right", 'r', required_argument
,
62 struct console_options
, config
.right
, parse_integer
),
63 OPTION_DESC ( "top", 't', required_argument
,
64 struct console_options
, config
.top
, parse_integer
),
65 OPTION_DESC ( "bottom", 'b', required_argument
,
66 struct console_options
, config
.bottom
, parse_integer
),
67 OPTION_DESC ( "depth", 'd', required_argument
,
68 struct console_options
, config
.depth
, parse_integer
),
69 OPTION_DESC ( "picture", 'p', required_argument
,
70 struct console_options
, picture
, parse_string
),
71 OPTION_DESC ( "keep", 'k', no_argument
,
72 struct console_options
, keep
, parse_flag
),
75 /** "console" command descriptor */
76 static struct command_descriptor console_cmd
=
77 COMMAND_DESC ( struct console_options
, console_opts
, 0, 0, NULL
);
82 * @v argc Argument count
83 * @v argv Argument list
84 * @ret rc Return status code
86 static int console_exec ( int argc
, char **argv
) {
87 struct console_options opts
;
88 struct image
*image
= NULL
;
92 if ( ( rc
= parse_options ( argc
, argv
, &console_cmd
, &opts
) ) != 0 )
95 /* Handle background picture, if applicable */
99 if ( ( rc
= imgacquire ( opts
.picture
, 0, &image
) ) != 0 )
102 /* Convert to pixel buffer */
103 if ( ( rc
= image_pixbuf ( image
, &opts
.config
.pixbuf
) ) != 0){
104 printf ( "Could not use picture: %s\n",
109 /* Apply image's width and height if none specified */
110 if ( ! opts
.config
.width
)
111 opts
.config
.width
= opts
.config
.pixbuf
->width
;
112 if ( ! opts
.config
.height
)
113 opts
.config
.height
= opts
.config
.pixbuf
->height
;
116 /* Configure console */
117 if ( ( rc
= console_configure ( &opts
.config
) ) != 0 ) {
118 printf ( "Could not configure console: %s\n", strerror ( rc
) );
122 /* Reapply default colour pair and clear screen */
123 ansicol_set_pair ( CPAIR_DEFAULT
);
124 printf ( CSI
"2J" CSI
"H" );
127 pixbuf_put ( opts
.config
.pixbuf
);
129 /* Discard image unless --keep was specified */
130 if ( image
&& ( ! opts
.keep
) )
131 unregister_image ( image
);
137 /** "colour" options */
138 struct colour_options
{
141 /** 24-bit RGB value */
145 /** "colour" option list */
146 static struct option_descriptor colour_opts
[] = {
147 OPTION_DESC ( "basic", 'b', required_argument
,
148 struct colour_options
, basic
, parse_integer
),
149 OPTION_DESC ( "rgb", 'r', required_argument
,
150 struct colour_options
, rgb
, parse_integer
),
153 /** "colour" command descriptor */
154 static struct command_descriptor colour_cmd
=
155 COMMAND_DESC ( struct colour_options
, colour_opts
, 1, 1, "<colour>" );
160 * @v argc Argument count
161 * @v argv Argument list
162 * @ret rc Return status code
164 static int colour_exec ( int argc
, char **argv
) {
165 struct colour_options opts
;
169 /* Initialise options */
170 memset ( &opts
, 0, sizeof ( opts
) );
171 opts
.basic
= COLOUR_DEFAULT
;
172 opts
.rgb
= ANSICOL_NO_RGB
;
175 if ( ( rc
= reparse_options ( argc
, argv
, &colour_cmd
, &opts
) ) != 0 )
178 /* Parse colour index */
179 if ( ( rc
= parse_integer ( argv
[optind
], &colour
) ) != 0 )
183 if ( ( rc
= ansicol_define ( colour
, opts
.basic
, opts
.rgb
) ) != 0 ) {
184 printf ( "Could not define colour: %s\n", strerror ( rc
) );
188 /* Reapply default colour pair, in case definition has changed */
189 ansicol_set_pair ( CPAIR_DEFAULT
);
194 /** "cpair" options */
195 struct cpair_options
{
196 /** Foreground colour */
197 unsigned int foreground
;
198 /** Background colour */
199 unsigned int background
;
202 /** "cpair" option list */
203 static struct option_descriptor cpair_opts
[] = {
204 OPTION_DESC ( "foreground", 'f', required_argument
,
205 struct cpair_options
, foreground
, parse_integer
),
206 OPTION_DESC ( "background", 'b', required_argument
,
207 struct cpair_options
, background
, parse_integer
),
210 /** "cpair" command descriptor */
211 static struct command_descriptor cpair_cmd
=
212 COMMAND_DESC ( struct cpair_options
, cpair_opts
, 1, 1, "<cpair>" );
217 * @v argc Argument count
218 * @v argv Argument list
219 * @ret rc Return status code
221 static int cpair_exec ( int argc
, char **argv
) {
222 struct cpair_options opts
;
226 /* Initialise options */
227 memset ( &opts
, 0, sizeof ( opts
) );
228 opts
.foreground
= COLOUR_DEFAULT
;
229 opts
.background
= COLOUR_DEFAULT
;
232 if ( ( rc
= reparse_options ( argc
, argv
, &cpair_cmd
, &opts
) ) != 0 )
235 /* Parse colour pair index */
236 if ( ( rc
= parse_integer ( argv
[optind
], &cpair
) ) != 0 )
239 /* Define colour pair */
240 if ( ( rc
= ansicol_define_pair ( cpair
, opts
.foreground
,
241 opts
.background
) ) != 0 ) {
242 printf ( "Could not define colour pair: %s\n",
247 /* Reapply default colour pair, in case definition has changed */
248 ansicol_set_pair ( CPAIR_DEFAULT
);
253 /** Console management commands */
254 struct command console_commands
[] __command
= {
257 .exec
= console_exec
,