[login] Add "login" command and UI
[people/asdlkf/gpxe.git] / 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., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 /** @file
20  *
21  * Login UI
22  *
23  */
24
25 #include <string.h>
26 #include <errno.h>
27 #include <curses.h>
28 #include <console.h>
29 #include <gpxe/settings.h>
30 #include <gpxe/editbox.h>
31 #include <gpxe/keys.h>
32 #include <gpxe/login_ui.h>
33
34 /* Colour pairs */
35 #define CPAIR_NORMAL            1
36 #define CPAIR_LABEL             2
37 #define CPAIR_EDITBOX           3
38
39 /* Screen layout */
40 #define USERNAME_LABEL_ROW      8
41 #define USERNAME_ROW            10
42 #define PASSWORD_LABEL_ROW      14
43 #define PASSWORD_ROW            16
44 #define LABEL_COL               36
45 #define EDITBOX_COL             30
46 #define EDITBOX_WIDTH           20
47
48 int login_ui ( void ) {
49         char username[64];
50         char password[64];
51         struct edit_box username_box;
52         struct edit_box password_box;
53         struct edit_box *current_box = &username_box;
54         int key;
55         int rc = -EINPROGRESS;
56
57         /* Fetch current setting values */
58         fetch_string_setting ( NULL, &username_setting, username,
59                                sizeof ( username ) );
60         fetch_string_setting ( NULL, &password_setting, password,
61                                sizeof ( password ) );
62
63         /* Initialise UI */
64         initscr();
65         start_color();
66         init_pair ( CPAIR_NORMAL, COLOR_WHITE, COLOR_BLACK );
67         init_pair ( CPAIR_LABEL, COLOR_WHITE, COLOR_BLACK );
68         init_pair ( CPAIR_EDITBOX, COLOR_WHITE, COLOR_BLUE );
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         erase();
77         color_set ( CPAIR_LABEL, NULL );
78         mvprintw ( USERNAME_LABEL_ROW, LABEL_COL, "Username:" );
79         mvprintw ( PASSWORD_LABEL_ROW, LABEL_COL, "Password:" );
80         color_set ( CPAIR_EDITBOX, NULL );
81         draw_editbox ( &username_box );
82         draw_editbox ( &password_box );
83
84         /* Main loop */
85         while ( rc == -EINPROGRESS ) {
86
87                 draw_editbox ( current_box );
88
89                 key = getkey();
90                 switch ( key ) {
91                 case KEY_DOWN:
92                         current_box = &password_box;
93                         break;
94                 case KEY_UP:
95                         current_box = &username_box;
96                         break;
97                 case TAB:
98                         current_box = ( ( current_box == &username_box ) ?
99                                         &password_box : &username_box );
100                         break;
101                 case KEY_ENTER:
102                         if ( current_box == &username_box ) {
103                                 current_box = &password_box;
104                         } else {
105                                 rc = 0;
106                         }
107                         break;
108                 case CTRL_C:
109                 case ESC:
110                         rc = -ECANCELED;
111                         break;
112                 default:
113                         edit_editbox ( current_box, key );
114                         break;
115                 }
116         }
117
118         /* Terminate UI */
119         color_set ( CPAIR_NORMAL, NULL );
120         erase();
121         endwin();
122
123         if ( rc != 0 )
124                 return rc;
125
126         /* Store settings */
127         if ( ( rc = store_setting ( NULL, &username_setting, username,
128                                     strlen ( username ) ) ) != 0 )
129                 return rc;
130         if ( ( rc = store_setting ( NULL, &password_setting, password,
131                                     strlen ( password ) ) ) != 0 )
132                 return rc;
133
134         return 0;
135 }