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.

getkey.c 2.2KB

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