tizen 2.3.1 release
[external/qemu.git] / 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/login_ui.h>
36 #include <config/colour.h>
37
38 /* Colour pairs */
39 #define CPAIR_NORMAL            1
40 #define CPAIR_EDIT              2
41
42 /* Screen layout */
43 #define USERNAME_LABEL_ROW      8
44 #define USERNAME_ROW            10
45 #define PASSWORD_LABEL_ROW      14
46 #define PASSWORD_ROW            16
47 #define LABEL_COL               36
48 #define EDITBOX_COL             30
49 #define EDITBOX_WIDTH           20
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_pair ( CPAIR_NORMAL, COLOR_NORMAL_FG, COLOR_NORMAL_BG );
70         init_pair ( CPAIR_EDIT, COLOR_EDIT_FG, COLOR_EDIT_BG );
71         init_editbox ( &username_box, username, sizeof ( username ), NULL,
72                        USERNAME_ROW, EDITBOX_COL, EDITBOX_WIDTH, 0 );
73         init_editbox ( &password_box, password, sizeof ( password ), NULL,
74                        PASSWORD_ROW, EDITBOX_COL, EDITBOX_WIDTH,
75                        EDITBOX_STARS );
76
77         /* Draw initial UI */
78         color_set ( CPAIR_NORMAL, NULL );
79         erase();
80         attron ( A_BOLD );
81         mvprintw ( USERNAME_LABEL_ROW, LABEL_COL, "Username:" );
82         mvprintw ( PASSWORD_LABEL_ROW, LABEL_COL, "Password:" );
83         attroff ( A_BOLD );
84         color_set ( CPAIR_EDIT, NULL );
85         draw_editbox ( &username_box );
86         draw_editbox ( &password_box );
87
88         /* Main loop */
89         while ( rc == -EINPROGRESS ) {
90
91                 draw_editbox ( current_box );
92
93                 key = getkey ( 0 );
94                 switch ( key ) {
95                 case KEY_DOWN:
96                         current_box = &password_box;
97                         break;
98                 case KEY_UP:
99                         current_box = &username_box;
100                         break;
101                 case TAB:
102                         current_box = ( ( current_box == &username_box ) ?
103                                         &password_box : &username_box );
104                         break;
105                 case KEY_ENTER:
106                         if ( current_box == &username_box ) {
107                                 current_box = &password_box;
108                         } else {
109                                 rc = 0;
110                         }
111                         break;
112                 case CTRL_C:
113                 case ESC:
114                         rc = -ECANCELED;
115                         break;
116                 default:
117                         edit_editbox ( current_box, key );
118                         break;
119                 }
120         }
121
122         /* Terminate UI */
123         color_set ( CPAIR_NORMAL, NULL );
124         erase();
125         endwin();
126
127         if ( rc != 0 )
128                 return rc;
129
130         /* Store settings */
131         if ( ( rc = store_setting ( NULL, &username_setting, username,
132                                     strlen ( username ) ) ) != 0 )
133                 return rc;
134         if ( ( rc = store_setting ( NULL, &password_setting, password,
135                                     strlen ( password ) ) ) != 0 )
136                 return rc;
137
138         return 0;
139 }