aaio.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @file aaio.h
  3. *
  4. * AAIO Advanced I/O
  5. *
  6. * Many people moving from Windows programming to UNIX program have problems
  7. * with the missing non-blocking getch() and getche() functions provided by
  8. * conio.h. This library provides the functionality of getch(), getche() and
  9. * kbhit(). It does not require an initialization (like curses) and does
  10. * not abuse the terminal (i.e. whenever the mode of the terminal is
  11. * changed its state is stored so it can be restored). For increased
  12. * efficiency there exists funcionality to allow abuse of the terminal as well
  13. * as functions to restore or reset the terminal when the application exits.
  14. *
  15. * @author Daniel Aarno
  16. * @version 0.3.0
  17. */
  18. /*
  19. -----BEGIN PGP SIGNED MESSAGE-----
  20. Hash: SHA1
  21. * Copyright (c) 2004-2005 by Daniel Aarno - <[email protected]>
  22. * M. Sc. Electrical Engineering * http://www.nada.kth.se/~bishop
  23. *
  24. * All Rights Reserved
  25. * ATTRIBUTION ASSURANCE LICENSE (adapted from the original BSD license)
  26. * Redistribution and use in source and binary forms, with or without
  27. * modification, are permitted provided that the conditions below are met.
  28. * These conditions require a modest attribution to (the
  29. * "Author"), who hopes that its promotional value may help justify the
  30. * thousands of dollars in otherwise billable time invested in writing
  31. * this and other freely available, open-source software.
  32. *
  33. * 1. Redistributions of source code, in whole or part and with or without
  34. * modification (the "Code"), must prominently display this GPG-signed
  35. * text in verifiable form.
  36. * 2. Redistributions of the Code in binary form must be accompanied by
  37. * this GPG-signed text in any documentation and, each time the resulting
  38. * executable program or a program dependent thereon is launched, a
  39. * prominent display (e.g., splash screen or banner text) of the Author's
  40. * attribution information, which includes:
  41. * (a) Name ("Daniel Aarno"),
  42. * (b) Professional identification ("M. Sc. Electrical Engineering"), and
  43. * (c) URL ("http://www.nada.kth.se/~bishop").
  44. * 3. Neither the name nor any trademark of the Author may be used to
  45. * endorse or promote products derived from this software without specific
  46. * prior written permission.
  47. * 4. Users are entirely responsible, to the exclusion of the Author and
  48. * any other persons, for compliance with (1) regulations set by owners or
  49. * administrators of employed equipment, (2) licensing terms of any other
  50. * software, and (3) local regulations regarding use, including those
  51. * regarding import, export, and use of encryption software.
  52. *
  53. * THIS FREE SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND
  54. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  55. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  56. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  57. * EVENT SHALL THE AUTHOR OR ANY CONTRIBUTOR BE LIABLE FOR
  58. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  59. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  60. * EFFECTS OF UNAUTHORIZED OR MALICIOUS NETWORK ACCESS;
  61. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  62. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  63. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  64. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  65. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  66. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  67. -----BEGIN PGP SIGNATURE-----
  68. Version: GnuPG v1.2.4 (GNU/Linux)
  69. iD8DBQFCHuXui6ECThHTSIkRAk9qAKCVs7kMSUtv5YhljeQsAA52EcjTFgCeNflz
  70. w0lAUG3zeHQcJ+7t6tpce4s=
  71. =qlVs
  72. -----END PGP SIGNATURE-----
  73. */
  74. #ifndef _L_AAIO_
  75. #define _L_AAIO_
  76. #define AAIO_VERSION 0.3.0
  77. #ifdef __cplusplus
  78. extern "C" {
  79. #endif
  80. /**
  81. * Get a character from stdin. Block until a character is available
  82. * but does not wait for a new line. Characters are not echoed on to
  83. * screen.
  84. *
  85. * @return The first character available cast to an int or -1 on error.
  86. * @see getche
  87. */
  88. int getch(void);
  89. /**
  90. * Get a character from stdin. Block until a character is available
  91. * but does not wait for a new line. Characters are echoed on to
  92. * screen.
  93. *
  94. * @return The first character available cast to an int or -1 on error.
  95. * @see getch
  96. */
  97. int getche(void);
  98. /**
  99. * Get the number of characters available on stdin.
  100. *
  101. * @return The number of characters that can be read from stdin without
  102. * blocking, -1 on error.
  103. * @see getch
  104. * @see getche
  105. */
  106. int kbhit(void);
  107. /**
  108. * Perform a hard reset of the terminal to cause it to go back to its
  109. * original state.
  110. *
  111. * @return -1 on error, 0 on success.
  112. * @see reset(1)
  113. * @see aaio_reset
  114. */
  115. int aaio_hard_reset(void);
  116. /**
  117. * Reset the terminal to the state it was in when aaio_grant_tty_lock was
  118. * called.
  119. *
  120. * @deprecated There is no need to use the aaio_grant_tty_lock and
  121. * aaio_reset functions as of version 0.2.0 an later. The speedup thay
  122. * can achieve is reduced to an insignificant level. The functionality
  123. * may be removed in later versions of the library.
  124. *
  125. * @return -1 on error, 0 on success.
  126. * @see aaio_hard_reset
  127. * @see aaio_grant_tty_lock
  128. */
  129. int aaio_reset(void);
  130. /**
  131. * Allow the aaio functions (getch, getche and kbhit) to "abuse" the terminal
  132. * (i.e. change the terminal settings without resetting them. This gives a
  133. * boost to performance that can be usefull in some cases. Most often,
  134. * however, it is not necessary or recomended to grant the tty lock to aaio.
  135. *
  136. * If the lock has been granted aaio_reset must be called before program
  137. * exit to reset the terminal to its original state.
  138. *
  139. * @deprecated There is no need to use the aaio_grant_tty_lock and
  140. * aaio_reset functions as of version 0.2.0 an later. The speedup thay
  141. * can achieve is reduced to an insignificant level. The functionality
  142. * may be removed in later versions of the library.
  143. *
  144. * @return -1 on error, 0 on success.
  145. * @note The aaio library does not really get a lock on the tty, others may
  146. * change the tty settings, however this will corrupt the aaio library and
  147. * can cause undefined results.
  148. * @see aaio_reset
  149. */
  150. int aaio_grant_tty_lock(void);
  151. /**
  152. * Flush any remaining characters from the stdandard input.
  153. *
  154. * @return The number of charactes removed.
  155. */
  156. int aaio_flush(void);
  157. #ifdef __cplusplus
  158. }
  159. #endif
  160. #endif