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

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