Home | History | Annotate | Download | only in tui
      1 /*
      2  * Copyright (C) 2009 Michael Brown <mbrown (at) 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_LICENCE ( GPL2_OR_LATER );
     20 
     21 /** @file
     22  *
     23  * Login UI
     24  *
     25  */
     26 
     27 #include <string.h>
     28 #include <errno.h>
     29 #include <curses.h>
     30 #include <console.h>
     31 #include <gpxe/settings.h>
     32 #include <gpxe/editbox.h>
     33 #include <gpxe/keys.h>
     34 #include <gpxe/login_ui.h>
     35 
     36 /* Colour pairs */
     37 #define CPAIR_NORMAL		1
     38 #define CPAIR_LABEL		2
     39 #define CPAIR_EDITBOX		3
     40 
     41 /* Screen layout */
     42 #define USERNAME_LABEL_ROW	8
     43 #define USERNAME_ROW		10
     44 #define PASSWORD_LABEL_ROW	14
     45 #define PASSWORD_ROW		16
     46 #define LABEL_COL		36
     47 #define EDITBOX_COL		30
     48 #define EDITBOX_WIDTH		20
     49 
     50 int login_ui ( void ) {
     51 	char username[64];
     52 	char password[64];
     53 	struct edit_box username_box;
     54 	struct edit_box password_box;
     55 	struct edit_box *current_box = &username_box;
     56 	int key;
     57 	int rc = -EINPROGRESS;
     58 
     59 	/* Fetch current setting values */
     60 	fetch_string_setting ( NULL, &username_setting, username,
     61 			       sizeof ( username ) );
     62 	fetch_string_setting ( NULL, &password_setting, password,
     63 			       sizeof ( password ) );
     64 
     65 	/* Initialise UI */
     66 	initscr();
     67 	start_color();
     68 	init_pair ( CPAIR_NORMAL, COLOR_WHITE, COLOR_BLACK );
     69 	init_pair ( CPAIR_LABEL, COLOR_WHITE, COLOR_BLACK );
     70 	init_pair ( CPAIR_EDITBOX, COLOR_WHITE, COLOR_BLUE );
     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 	erase();
     79 	color_set ( CPAIR_LABEL, NULL );
     80 	mvprintw ( USERNAME_LABEL_ROW, LABEL_COL, "Username:" );
     81 	mvprintw ( PASSWORD_LABEL_ROW, LABEL_COL, "Password:" );
     82 	color_set ( CPAIR_EDITBOX, 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();
     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 }
    138