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