您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

login_ui.c 3.5KB

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