input.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* $NetBSD: input.c,v 1.9 2003/08/07 09:37:47 agc Exp $ */
  2. /*-
  3. * Copyright (c) 1992, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Chris Torek and Darren F. Provine.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * @(#)input.c 8.1 (Berkeley) 5/31/93
  34. */
  35. /*
  36. * Tetris input.
  37. */
  38. #include <sys/types.h>
  39. #include <sys/time.h>
  40. #include <sys/poll.h>
  41. #include <errno.h>
  42. #include <unistd.h>
  43. #include "input.h"
  44. #include "tetris.h"
  45. /* return true iff the given timeval is positive */
  46. #define TV_POS(tv) \
  47. ((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0))
  48. /* subtract timeval `sub' from `res' */
  49. #define TV_SUB(res, sub) \
  50. (res)->tv_sec -= (sub)->tv_sec; \
  51. (res)->tv_usec -= (sub)->tv_usec; \
  52. if ((res)->tv_usec < 0) { \
  53. (res)->tv_usec += 1000000; \
  54. (res)->tv_sec--; \
  55. }
  56. /*
  57. * Do a `read wait': poll for reading from stdin, with timeout *tvp.
  58. * On return, modify *tvp to reflect the amount of time spent waiting.
  59. * It will be positive only if input appeared before the time ran out;
  60. * otherwise it will be zero or perhaps negative.
  61. *
  62. * If tvp is nil, wait forever, but return if poll is interrupted.
  63. *
  64. * Return 0 => no input, 1 => can read() from stdin
  65. */
  66. int
  67. rwait(tvp)
  68. struct timeval *tvp;
  69. {
  70. struct pollfd set[1];
  71. struct timeval starttv, endtv;
  72. int timeout;
  73. #define NILTZ ((struct timezone *)0)
  74. if (tvp) {
  75. (void) gettimeofday(&starttv, NILTZ);
  76. endtv = *tvp;
  77. timeout = tvp->tv_sec * 1000 + tvp->tv_usec / 1000;
  78. } else
  79. timeout = INFTIM;
  80. again:
  81. set[0].fd = STDIN_FILENO;
  82. set[0].events = POLLIN;
  83. switch (poll(set, 1, timeout)) {
  84. case -1:
  85. if (tvp == 0)
  86. return (-1);
  87. if (errno == EINTR)
  88. goto again;
  89. stop("poll failed, help");
  90. /* NOTREACHED */
  91. case 0: /* timed out */
  92. tvp->tv_sec = 0;
  93. tvp->tv_usec = 0;
  94. return (0);
  95. }
  96. if (tvp) {
  97. /* since there is input, we may not have timed out */
  98. (void) gettimeofday(&endtv, NILTZ);
  99. TV_SUB(&endtv, &starttv);
  100. TV_SUB(tvp, &endtv); /* adjust *tvp by elapsed time */
  101. }
  102. return (1);
  103. }
  104. /*
  105. * `sleep' for the current turn time.
  106. * Eat any input that might be available.
  107. */
  108. void
  109. tsleep()
  110. {
  111. struct timeval tv;
  112. char c;
  113. tv.tv_sec = 0;
  114. tv.tv_usec = fallrate;
  115. while (TV_POS(&tv))
  116. if (rwait(&tv) && read(0, &c, 1) != 1)
  117. break;
  118. }
  119. /*
  120. * getchar with timeout.
  121. */
  122. int
  123. tgetchar()
  124. {
  125. static struct timeval timeleft;
  126. char c;
  127. /*
  128. * Reset timeleft to fallrate whenever it is not positive.
  129. * In any case, wait to see if there is any input. If so,
  130. * take it, and update timeleft so that the next call to
  131. * tgetchar() will not wait as long. If there is no input,
  132. * make timeleft zero or negative, and return -1.
  133. *
  134. * Most of the hard work is done by rwait().
  135. */
  136. if (!TV_POS(&timeleft)) {
  137. faster(); /* go faster */
  138. timeleft.tv_sec = 0;
  139. timeleft.tv_usec = fallrate;
  140. }
  141. if (!rwait(&timeleft))
  142. return (-1);
  143. if (read(0, &c, 1) != 1)
  144. stop("end of file, help");
  145. return ((int)(unsigned char)c);
  146. }