Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

getkey.c 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (C) 2006 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. #include <ctype.h>
  21. #include <ipxe/console.h>
  22. #include <ipxe/process.h>
  23. #include <ipxe/keys.h>
  24. #include <ipxe/timer.h>
  25. #include <ipxe/nap.h>
  26. /** @file
  27. *
  28. * Special key interpretation
  29. *
  30. */
  31. #define GETKEY_TIMEOUT ( TICKS_PER_SEC / 4 )
  32. /**
  33. * Read character from console if available within timeout period
  34. *
  35. * @v timeout Timeout period, in ticks (0=indefinite)
  36. * @ret character Character read from console
  37. */
  38. static int getchar_timeout ( unsigned long timeout ) {
  39. unsigned long start = currticks();
  40. while ( ( timeout == 0 ) || ( ( currticks() - start ) < timeout ) ) {
  41. step();
  42. if ( iskey() )
  43. return getchar();
  44. cpu_nap();
  45. }
  46. return -1;
  47. }
  48. /**
  49. * Get single keypress
  50. *
  51. * @v timeout Timeout period, in ticks (0=indefinite)
  52. * @ret key Key pressed
  53. *
  54. * The returned key will be an ASCII value or a KEY_XXX special
  55. * constant. This function differs from getchar() in that getchar()
  56. * will return "special" keys (e.g. cursor keys) as a series of
  57. * characters forming an ANSI escape sequence.
  58. */
  59. int getkey ( unsigned long timeout ) {
  60. int character;
  61. unsigned int n = 0;
  62. character = getchar_timeout ( timeout );
  63. if ( character != ESC )
  64. return character;
  65. while ( ( character = getchar_timeout ( GETKEY_TIMEOUT ) ) >= 0 ) {
  66. if ( character == '[' )
  67. continue;
  68. if ( isdigit ( character ) ) {
  69. n = ( ( n * 10 ) + ( character - '0' ) );
  70. continue;
  71. }
  72. if ( character >= 0x40 )
  73. return KEY_ANSI ( n, character );
  74. }
  75. return ESC;
  76. }