iPXE
login_ui.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * Login UI
00029  *
00030  */
00031 
00032 #include <string.h>
00033 #include <errno.h>
00034 #include <curses.h>
00035 #include <ipxe/console.h>
00036 #include <ipxe/settings.h>
00037 #include <ipxe/editbox.h>
00038 #include <ipxe/keys.h>
00039 #include <ipxe/ansicol.h>
00040 #include <ipxe/login_ui.h>
00041 
00042 /* Screen layout */
00043 #define USERNAME_LABEL_ROW      ( ( LINES / 2U ) - 4U )
00044 #define USERNAME_ROW            ( ( LINES / 2U ) - 2U )
00045 #define PASSWORD_LABEL_ROW      ( ( LINES / 2U ) + 2U )
00046 #define PASSWORD_ROW            ( ( LINES / 2U ) + 4U )
00047 #define LABEL_COL               ( ( COLS / 2U ) - 4U )
00048 #define EDITBOX_COL             ( ( COLS / 2U ) - 10U )
00049 #define EDITBOX_WIDTH           20U
00050 
00051 int login_ui ( void ) {
00052         char username[64];
00053         char password[64];
00054         struct edit_box username_box;
00055         struct edit_box password_box;
00056         struct edit_box *current_box = &username_box;
00057         int key;
00058         int rc = -EINPROGRESS;
00059 
00060         /* Fetch current setting values */
00061         fetch_string_setting ( NULL, &username_setting, username,
00062                                sizeof ( username ) );
00063         fetch_string_setting ( NULL, &password_setting, password,
00064                                sizeof ( password ) );
00065 
00066         /* Initialise UI */
00067         initscr();
00068         start_color();
00069         init_editbox ( &username_box, username, sizeof ( username ), NULL,
00070                        USERNAME_ROW, EDITBOX_COL, EDITBOX_WIDTH, 0 );
00071         init_editbox ( &password_box, password, sizeof ( password ), NULL,
00072                        PASSWORD_ROW, EDITBOX_COL, EDITBOX_WIDTH,
00073                        EDITBOX_STARS );
00074 
00075         /* Draw initial UI */
00076         color_set ( CPAIR_NORMAL, NULL );
00077         erase();
00078         attron ( A_BOLD );
00079         mvprintw ( USERNAME_LABEL_ROW, LABEL_COL, "Username:" );
00080         mvprintw ( PASSWORD_LABEL_ROW, LABEL_COL, "Password:" );
00081         attroff ( A_BOLD );
00082         color_set ( CPAIR_EDIT, NULL );
00083         draw_editbox ( &username_box );
00084         draw_editbox ( &password_box );
00085 
00086         /* Main loop */
00087         while ( rc == -EINPROGRESS ) {
00088 
00089                 draw_editbox ( current_box );
00090 
00091                 key = getkey ( 0 );
00092                 switch ( key ) {
00093                 case KEY_DOWN:
00094                         current_box = &password_box;
00095                         break;
00096                 case KEY_UP:
00097                         current_box = &username_box;
00098                         break;
00099                 case TAB:
00100                         current_box = ( ( current_box == &username_box ) ?
00101                                         &password_box : &username_box );
00102                         break;
00103                 case KEY_ENTER:
00104                         if ( current_box == &username_box ) {
00105                                 current_box = &password_box;
00106                         } else {
00107                                 rc = 0;
00108                         }
00109                         break;
00110                 case CTRL_C:
00111                 case ESC:
00112                         rc = -ECANCELED;
00113                         break;
00114                 default:
00115                         edit_editbox ( current_box, key );
00116                         break;
00117                 }
00118         }
00119 
00120         /* Terminate UI */
00121         color_set ( CPAIR_NORMAL, NULL );
00122         erase();
00123         endwin();
00124 
00125         if ( rc != 0 )
00126                 return rc;
00127 
00128         /* Store settings */
00129         if ( ( rc = store_setting ( NULL, &username_setting, username,
00130                                     strlen ( username ) ) ) != 0 )
00131                 return rc;
00132         if ( ( rc = store_setting ( NULL, &password_setting, password,
00133                                     strlen ( password ) ) ) != 0 )
00134                 return rc;
00135 
00136         return 0;
00137 }