Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

getkey.c 2.0KB

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