Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / tui / login_ui.c
1 /*
2  * Copyright (C) 2009 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  * Login UI
25  *
26  */
27
28 #include <string.h>
29 #include <errno.h>
30 #include <curses.h>
31 #include <ipxe/console.h>
32 #include <ipxe/settings.h>
33 #include <ipxe/editbox.h>
34 #include <ipxe/keys.h>
35 #include <ipxe/ansicol.h>
36 #include <ipxe/login_ui.h>
37
38 /* Screen layout */
39 #define USERNAME_LABEL_ROW      ( ( LINES / 2U ) - 4U )
40 #define USERNAME_ROW            ( ( LINES / 2U ) - 2U )
41 #define PASSWORD_LABEL_ROW      ( ( LINES / 2U ) + 2U )
42 #define PASSWORD_ROW            ( ( LINES / 2U ) + 4U )
43 #define LABEL_COL               ( ( COLS / 2U ) - 4U )
44 #define EDITBOX_COL             ( ( COLS / 2U ) - 10U )
45 #define EDITBOX_WIDTH           20U
46
47 int login_ui ( void ) {
48         char username[64];
49         char password[64];
50         struct edit_box username_box;
51         struct edit_box password_box;
52         struct edit_box *current_box = &username_box;
53         int key;
54         int rc = -EINPROGRESS;
55
56         /* Fetch current setting values */
57         fetch_string_setting ( NULL, &username_setting, username,
58                                sizeof ( username ) );
59         fetch_string_setting ( NULL, &password_setting, password,
60                                sizeof ( password ) );
61
62         /* Initialise UI */
63         initscr();
64         start_color();
65         init_editbox ( &username_box, username, sizeof ( username ), NULL,
66                        USERNAME_ROW, EDITBOX_COL, EDITBOX_WIDTH, 0 );
67         init_editbox ( &password_box, password, sizeof ( password ), NULL,
68                        PASSWORD_ROW, EDITBOX_COL, EDITBOX_WIDTH,
69                        EDITBOX_STARS );
70
71         /* Draw initial UI */
72         color_set ( CPAIR_NORMAL, NULL );
73         erase();
74         attron ( A_BOLD );
75         mvprintw ( USERNAME_LABEL_ROW, LABEL_COL, "Username:" );
76         mvprintw ( PASSWORD_LABEL_ROW, LABEL_COL, "Password:" );
77         attroff ( A_BOLD );
78         color_set ( CPAIR_EDIT, NULL );
79         draw_editbox ( &username_box );
80         draw_editbox ( &password_box );
81
82         /* Main loop */
83         while ( rc == -EINPROGRESS ) {
84
85                 draw_editbox ( current_box );
86
87                 key = getkey ( 0 );
88                 switch ( key ) {
89                 case KEY_DOWN:
90                         current_box = &password_box;
91                         break;
92                 case KEY_UP:
93                         current_box = &username_box;
94                         break;
95                 case TAB:
96                         current_box = ( ( current_box == &username_box ) ?
97                                         &password_box : &username_box );
98                         break;
99                 case KEY_ENTER:
100                         if ( current_box == &username_box ) {
101                                 current_box = &password_box;
102                         } else {
103                                 rc = 0;
104                         }
105                         break;
106                 case CTRL_C:
107                 case ESC:
108                         rc = -ECANCELED;
109                         break;
110                 default:
111                         edit_editbox ( current_box, key );
112                         break;
113                 }
114         }
115
116         /* Terminate UI */
117         color_set ( CPAIR_NORMAL, NULL );
118         erase();
119         endwin();
120
121         if ( rc != 0 )
122                 return rc;
123
124         /* Store settings */
125         if ( ( rc = store_setting ( NULL, &username_setting, username,
126                                     strlen ( username ) ) ) != 0 )
127                 return rc;
128         if ( ( rc = store_setting ( NULL, &password_setting, password,
129                                     strlen ( password ) ) ) != 0 )
130                 return rc;
131
132         return 0;
133 }