You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

login_ui.c 3.4KB

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