login_ui.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020
00021
00022
00023
00024
00025
00026
00027 #include <string.h>
00028 #include <errno.h>
00029 #include <curses.h>
00030 #include <console.h>
00031 #include <gpxe/settings.h>
00032 #include <gpxe/editbox.h>
00033 #include <gpxe/keys.h>
00034 #include <gpxe/login_ui.h>
00035
00036
00037 #define CPAIR_NORMAL 1
00038 #define CPAIR_LABEL 2
00039 #define CPAIR_EDITBOX 3
00040
00041
00042 #define USERNAME_LABEL_ROW 8
00043 #define USERNAME_ROW 10
00044 #define PASSWORD_LABEL_ROW 14
00045 #define PASSWORD_ROW 16
00046 #define LABEL_COL 36
00047 #define EDITBOX_COL 30
00048 #define EDITBOX_WIDTH 20
00049
00050 int login_ui ( void ) {
00051 char username[64];
00052 char password[64];
00053 struct edit_box username_box;
00054 struct edit_box password_box;
00055 struct edit_box *current_box = &username_box;
00056 int key;
00057 int rc = -EINPROGRESS;
00058
00059
00060 fetch_string_setting ( NULL, &username_setting, username,
00061 sizeof ( username ) );
00062 fetch_string_setting ( NULL, &password_setting, password,
00063 sizeof ( password ) );
00064
00065
00066 initscr();
00067 start_color();
00068 init_pair ( CPAIR_NORMAL, COLOR_WHITE, COLOR_BLACK );
00069 init_pair ( CPAIR_LABEL, COLOR_WHITE, COLOR_BLACK );
00070 init_pair ( CPAIR_EDITBOX, COLOR_WHITE, COLOR_BLUE );
00071 init_editbox ( &username_box, username, sizeof ( username ), NULL,
00072 USERNAME_ROW, EDITBOX_COL, EDITBOX_WIDTH, 0 );
00073 init_editbox ( &password_box, password, sizeof ( password ), NULL,
00074 PASSWORD_ROW, EDITBOX_COL, EDITBOX_WIDTH,
00075 EDITBOX_STARS );
00076
00077
00078 erase();
00079 color_set ( CPAIR_LABEL, NULL );
00080 mvprintw ( USERNAME_LABEL_ROW, LABEL_COL, "Username:" );
00081 mvprintw ( PASSWORD_LABEL_ROW, LABEL_COL, "Password:" );
00082 color_set ( CPAIR_EDITBOX, NULL );
00083 draw_editbox ( &username_box );
00084 draw_editbox ( &password_box );
00085
00086
00087 while ( rc == -EINPROGRESS ) {
00088
00089 draw_editbox ( current_box );
00090
00091 key = getkey();
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
00121 color_set ( CPAIR_NORMAL, NULL );
00122 erase();
00123 endwin();
00124
00125 if ( rc != 0 )
00126 return rc;
00127
00128
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 }