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.4KB

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