kbmsdos.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 1993 Colin Plumb. All rights reserved.
  3. * For licensing and other legal details, see the file legal.c.
  4. *
  5. * MS-DOS non-echoing keyboard routines.
  6. */
  7. #include <conio.h> /* For getch() and kbhit() */
  8. #include <signal.h> /* For raise() */
  9. #ifdef _MSC_VER
  10. #include <time.h> /* For clock() */
  11. #else
  12. #include <dos.h> /* For sleep() */
  13. #endif
  14. #include "kb.h"
  15. #include "random.h" /* For randEvent() */
  16. /* These are pretty boring */
  17. void kbCbreak(void) { }
  18. void kbNorm(void) { }
  19. int kbGet(void)
  20. {
  21. int c;
  22. c = getch();
  23. if (c == 0)
  24. c = 0x100 + getch();
  25. /*
  26. * Borland C's getch() uses int 0x21 function 0x7,
  27. * which does not detect break. So we do it explicitly.
  28. */
  29. if (c == 3)
  30. raise(SIGINT);
  31. randEvent(c);
  32. return c;
  33. }
  34. #ifdef _MSC_VER
  35. /*
  36. * Microsoft Visual C 1.5 (at least) does not have sleep() in the
  37. * library. So we use this crude approximation. ("crude" because,
  38. * assuming CLOCKS_PER_SEC is 18.2, it rounds to 18 to avoid floating
  39. * point math.)
  40. */
  41. #ifndef CLOCKS_PER_SEC
  42. #define CLOCKS_PER_SEC CLK_TCK
  43. #endif
  44. static unsigned
  45. sleep(unsigned t)
  46. {
  47. clock_t target;
  48. target = clock() + t * (unsigned)CLOCKS_PER_SEC;
  49. while (clock() < target)
  50. ;
  51. return 0;
  52. }
  53. #endif
  54. void kbFlush(int thorough)
  55. {
  56. do {
  57. while(kbhit())
  58. (void)getch();
  59. if (!thorough)
  60. break;
  61. /* Extra thorough: wait for one second of quiet */
  62. sleep(1);
  63. } while (kbhit());
  64. }