iPXE
login_ui.c
Go to the documentation of this file.
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  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * Login UI
29  *
30  */
31 
32 #include <string.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <curses.h>
36 #include <ipxe/console.h>
37 #include <ipxe/settings.h>
38 #include <ipxe/editbox.h>
39 #include <ipxe/keys.h>
40 #include <ipxe/ansicol.h>
41 #include <ipxe/login_ui.h>
42 
43 /* Screen layout */
44 #define USERNAME_LABEL_ROW ( ( LINES / 2U ) - 4U )
45 #define USERNAME_ROW ( ( LINES / 2U ) - 2U )
46 #define PASSWORD_LABEL_ROW ( ( LINES / 2U ) + 2U )
47 #define PASSWORD_ROW ( ( LINES / 2U ) + 4U )
48 #define LABEL_COL ( ( COLS / 2U ) - 4U )
49 #define EDITBOX_COL ( ( COLS / 2U ) - 10U )
50 #define EDITBOX_WIDTH 20U
51 
52 int login_ui ( void ) {
53  char *username;
54  char *password;
55  struct edit_box username_box;
56  struct edit_box password_box;
57  struct edit_box *current_box = &username_box;
58  int key;
59  int rc = -EINPROGRESS;
60 
61  /* Fetch current setting values */
62  fetchf_setting_copy ( NULL, &username_setting, NULL, NULL, &username );
63  fetchf_setting_copy ( NULL, &password_setting, NULL, NULL, &password );
64 
65  /* Initialise UI */
66  initscr();
67  start_color();
68  init_editbox ( &username_box, &username, NULL, USERNAME_ROW,
70  init_editbox ( &password_box, &password, NULL, PASSWORD_ROW,
72 
73  /* Draw initial UI */
75  erase();
76  attron ( A_BOLD );
77  mvprintw ( USERNAME_LABEL_ROW, LABEL_COL, "Username:" );
78  mvprintw ( PASSWORD_LABEL_ROW, LABEL_COL, "Password:" );
79  attroff ( A_BOLD );
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 ( 0 );
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 */
120  erase();
121  endwin();
122 
123  /* Store settings on successful completion */
124  if ( rc == 0 )
125  rc = storef_setting ( NULL, &username_setting, username );
126  if ( rc == 0 )
127  rc = storef_setting ( NULL, &password_setting, password );
128 
129  /* Free setting values */
130  free ( username );
131  free ( password );
132 
133  return rc;
134 }
int getkey(unsigned long timeout)
Get single keypress.
Definition: getkey.c:71
MuCurses header file.
#define KEY_ENTER
Definition: keys.h:127
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
Login UI.
Editable text box widget.
int erase(void)
Completely clear the screen.
Definition: clear.c:97
int fetchf_setting_copy(struct settings *settings, const struct setting *setting, struct settings **origin, struct setting *fetched, char **value)
Fetch copy of formatted value of setting.
Definition: settings.c:1276
Error codes.
static int attroff(int attrs)
Definition: curses.h:508
#define CPAIR_EDIT
Editable text.
Definition: ansicol.h:49
#define start_color()
Definition: curses.h:396
int endwin(void)
Finalise console environment.
Definition: wininit.c:31
#define mvprintw(y, x, fmt,...)
Definition: curses.h:648
#define EDITBOX_WIDTH
Definition: login_ui.c:50
#define ECANCELED
Operation canceled.
Definition: errno.h:343
#define CPAIR_NORMAL
Normal text.
Definition: ansicol.h:40
#define PASSWORD_ROW
Definition: login_ui.c:47
#define KEY_DOWN
Down arrow.
Definition: keys.h:105
#define KEY_UP
Up arrow.
Definition: keys.h:104
void init_editbox(struct edit_box *box, char **buf, WINDOW *win, unsigned int row, unsigned int col, unsigned int width, unsigned int flags)
Initialise text box widget.
Definition: editbox.c:49
WINDOW * initscr(void)
Initialise console environment.
Definition: wininit.c:17
#define CTRL_C
Definition: keys.h:20
static int edit_editbox(struct edit_box *box, int key) __nonnull
Edit text box widget.
Definition: editbox.h:57
#define TAB
Definition: keys.h:46
#define ESC
Escape character.
Definition: ansiesc.h:92
An editable text box widget.
Definition: editbox.h:16
User interaction.
#define EINPROGRESS
Operation in progress.
Definition: errno.h:418
Configuration settings.
int storef_setting(struct settings *settings, const struct setting *setting, const char *value)
Store formatted value of setting.
Definition: settings.c:1319
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
#define EDITBOX_COL
Definition: login_ui.c:49
#define USERNAME_ROW
Definition: login_ui.c:45
void draw_editbox(struct edit_box *box)
Draw text box widget.
Definition: editbox.c:68
Show stars instead of contents (for password widgets)
Definition: editbox.h:36
#define PASSWORD_LABEL_ROW
Definition: login_ui.c:46
Key definitions.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define USERNAME_LABEL_ROW
Definition: login_ui.c:44
#define color_set(cpno, opts)
Definition: curses.h:240
#define LABEL_COL
Definition: login_ui.c:48
#define A_BOLD
Definition: curses.h:138
int login_ui(void)
Definition: login_ui.c:52
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
union @382 key
Sense key.
Definition: crypto.h:284
ANSI colours.
static int attron(int attrs)
Definition: curses.h:512