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 1.9KB

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