Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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