Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / commands / console_cmd.c
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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23  *
24  * Console management commands
25  *
26  */
27
28 #include <string.h>
29 #include <getopt.h>
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>
38
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
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 ),
69 };
70
71 /** "console" command descriptor */
72 static struct command_descriptor console_cmd =
73         COMMAND_DESC ( struct console_options, console_opts, 0, 0, NULL );
74
75 /**
76  * "console" command
77  *
78  * @v argc              Argument count
79  * @v argv              Argument list
80  * @ret rc              Return status code
81  */
82 static int console_exec ( int argc, char **argv ) {
83         struct console_options opts;
84         struct image *image = NULL;
85         int rc;
86
87         /* Parse options */
88         if ( ( rc = parse_options ( argc, argv, &console_cmd, &opts ) ) != 0 )
89                 goto err_parse;
90
91         /* Handle background picture, if applicable */
92         if ( opts.picture ) {
93
94                 /* Acquire image */
95                 if ( ( rc = imgacquire ( opts.picture, 0, &image ) ) != 0 )
96                         goto err_acquire;
97
98                 /* Convert to pixel buffer */
99                 if ( ( rc = image_pixbuf ( image, &opts.config.pixbuf ) ) != 0){
100                         printf ( "Could not use picture: %s\n",
101                                  strerror ( rc ) );
102                         goto err_pixbuf;
103                 }
104
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;
110         }
111
112         /* Configure console */
113         if ( ( rc = console_configure ( &opts.config ) ) != 0 ) {
114                 printf ( "Could not configure console: %s\n", strerror ( rc ) );
115                 goto err_configure;
116         }
117
118         /* Reapply default colour pair and clear screen */
119         ansicol_set_pair ( CPAIR_DEFAULT );
120         printf ( CSI "2J" CSI "H" );
121
122  err_configure:
123         pixbuf_put ( opts.config.pixbuf );
124  err_pixbuf:
125         /* Discard image unless --keep was specified */
126         if ( image && ( ! opts.keep ) )
127                 unregister_image ( image );
128  err_acquire:
129  err_parse:
130         return rc;
131 }
132
133 /** "colour" options */
134 struct colour_options {
135         /** Basic colour */
136         unsigned int basic;
137         /** 24-bit RGB value */
138         unsigned int rgb;
139 };
140
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 ),
147 };
148
149 /** "colour" command descriptor */
150 static struct command_descriptor colour_cmd =
151         COMMAND_DESC ( struct colour_options, colour_opts, 1, 1, "<colour>" );
152
153 /**
154  * "colour" command
155  *
156  * @v argc              Argument count
157  * @v argv              Argument list
158  * @ret rc              Return status code
159  */
160 static int colour_exec ( int argc, char **argv ) {
161         struct colour_options opts;
162         unsigned int colour;
163         int rc;
164
165         /* Initialise options */
166         memset ( &opts, 0, sizeof ( opts ) );
167         opts.basic = COLOUR_DEFAULT;
168         opts.rgb = ANSICOL_NO_RGB;
169
170         /* Parse options */
171         if ( ( rc = reparse_options ( argc, argv, &colour_cmd, &opts ) ) != 0 )
172                 return rc;
173
174         /* Parse colour index */
175         if ( ( rc = parse_integer ( argv[optind], &colour ) ) != 0 )
176                 return rc;
177
178         /* Define colour */
179         if ( ( rc = ansicol_define ( colour, opts.basic, opts.rgb ) ) != 0 ) {
180                 printf ( "Could not define colour: %s\n", strerror ( rc ) );
181                 return rc;
182         }
183
184         /* Reapply default colour pair, in case definition has changed */
185         ansicol_set_pair ( CPAIR_DEFAULT );
186
187         return 0;
188 }
189
190 /** "cpair" options */
191 struct cpair_options {
192         /** Foreground colour */
193         unsigned int foreground;
194         /** Background colour */
195         unsigned int background;
196 };
197
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 ),
204 };
205
206 /** "cpair" command descriptor */
207 static struct command_descriptor cpair_cmd =
208         COMMAND_DESC ( struct cpair_options, cpair_opts, 1, 1, "<cpair>" );
209
210 /**
211  * "cpair" command
212  *
213  * @v argc              Argument count
214  * @v argv              Argument list
215  * @ret rc              Return status code
216  */
217 static int cpair_exec ( int argc, char **argv ) {
218         struct cpair_options opts;
219         unsigned int cpair;
220         int rc;
221
222         /* Initialise options */
223         memset ( &opts, 0, sizeof ( opts ) );
224         opts.foreground = COLOUR_DEFAULT;
225         opts.background = COLOUR_DEFAULT;
226
227         /* Parse options */
228         if ( ( rc = reparse_options ( argc, argv, &cpair_cmd, &opts ) ) != 0 )
229                 return rc;
230
231         /* Parse colour pair index */
232         if ( ( rc = parse_integer ( argv[optind], &cpair ) ) != 0 )
233                 return rc;
234
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",
239                          strerror ( rc ) );
240                 return rc;
241         }
242
243         /* Reapply default colour pair, in case definition has changed */
244         ansicol_set_pair ( CPAIR_DEFAULT );
245
246         return 0;
247 }
248
249 /** Console management commands */
250 struct command console_commands[] __command = {
251         {
252                 .name = "console",
253                 .exec = console_exec,
254         },
255         {
256                 .name = "colour",
257                 .exec = colour_exec,
258         },
259         {
260                 .name = "cpair",
261                 .exec = cpair_exec,
262         },
263 };