Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

getkey.c 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 <console.h>
  21. #include <gpxe/process.h>
  22. #include <gpxe/keys.h>
  23. #include <gpxe/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
  34. * @ret character Character read from console
  35. */
  36. static int getchar_timeout ( unsigned long timeout ) {
  37. unsigned long expiry = ( currticks() + timeout );
  38. while ( currticks() < expiry ) {
  39. step();
  40. if ( iskey() )
  41. return getchar();
  42. }
  43. return -1;
  44. }
  45. /**
  46. * Get single keypress
  47. *
  48. * @ret key Key pressed
  49. *
  50. * The returned key will be an ASCII value or a KEY_XXX special
  51. * constant. This function differs from getchar() in that getchar()
  52. * will return "special" keys (e.g. cursor keys) as a series of
  53. * characters forming an ANSI escape sequence.
  54. */
  55. int getkey ( void ) {
  56. int character;
  57. unsigned int n = 0;
  58. character = getchar();
  59. if ( character != ESC )
  60. return character;
  61. while ( ( character = getchar_timeout ( GETKEY_TIMEOUT ) ) >= 0 ) {
  62. if ( character == '[' )
  63. continue;
  64. if ( isdigit ( character ) ) {
  65. n = ( ( n * 10 ) + ( character - '0' ) );
  66. continue;
  67. }
  68. if ( character >= 0x40 )
  69. return KEY_ANSI ( n, character );
  70. }
  71. return ESC;
  72. }