slw32tty.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Copyright (c) 1992, 1995 John E. Davis
  2. * All rights reserved.
  3. *
  4. * You may distribute under the terms of either the GNU General Public
  5. * License or the Perl Artistic License.
  6. */
  7. #include "config.h"
  8. #include <stdio.h>
  9. #include <windows.h>
  10. #include <winbase.h>
  11. #include "slang.h"
  12. #include "_slang.h"
  13. #ifdef __cplusplus
  14. # define _DOTS_ ...
  15. #else
  16. # define _DOTS_ void
  17. #endif
  18. /*----------------------------------------------------------------------*\
  19. * Function: static void set_ctrl_break (int state);
  20. *
  21. * set the control-break setting
  22. \*----------------------------------------------------------------------*/
  23. static void set_ctrl_break (int state)
  24. {
  25. }
  26. /*----------------------------------------------------------------------*\
  27. * Function: int SLang_init_tty (int abort_char, int no_flow_control,
  28. * int opost);
  29. *
  30. * initialize the keyboard interface and attempt to set-up the interrupt 9
  31. * handler if ABORT_CHAR is non-zero.
  32. * NO_FLOW_CONTROL and OPOST are only for compatiblity and are ignored.
  33. \*----------------------------------------------------------------------*/
  34. HANDLE hStdout, hStdin;
  35. CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
  36. int SLang_init_tty (int abort_char, int no_flow_control, int opost)
  37. {
  38. SMALL_RECT windowRect;
  39. COORD newPosition;
  40. long flags;
  41. #ifndef SLANG_SAVES_CONSOLE
  42. /* first off, create a new console so the old one can be restored on exit */
  43. HANDLE console = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
  44. FILE_SHARE_READ |FILE_SHARE_WRITE,
  45. 0,
  46. CONSOLE_TEXTMODE_BUFFER,
  47. 0);
  48. if (SetConsoleActiveScreenBuffer(console) == FALSE) {
  49. return -1;
  50. }
  51. #endif
  52. /* start things off at the origin */
  53. newPosition.X = 0;
  54. newPosition.Y = 0;
  55. /* still read in characters from stdin, but output to the new console */
  56. /* this way, on program exit, the original screen is restored */
  57. hStdin = GetStdHandle(STD_INPUT_HANDLE);
  58. /* hStdin = console; */
  59. #ifndef SLANG_SAVES_CONSOLE
  60. hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  61. #else
  62. hStdout = console;
  63. #endif
  64. if (hStdin == INVALID_HANDLE_VALUE || hStdout == INVALID_HANDLE_VALUE) {
  65. return -1; /* failure */
  66. }
  67. if (!GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) {
  68. return -1; /* failure */
  69. } // if
  70. windowRect.Left = 0;
  71. windowRect.Top = 0;
  72. windowRect.Right = csbiInfo.dwSize.X - 1;
  73. windowRect.Bottom = csbiInfo.dwSize.Y - 1;
  74. if (!SetConsoleWindowInfo(hStdout, TRUE, &windowRect)) {
  75. return -1;
  76. }
  77. if (SetConsoleMode(hStdin, 0) == FALSE) {
  78. return -1; /* failure */
  79. }
  80. if (SetConsoleMode(hStdout, 0) == FALSE) {
  81. return -1; /* failure */
  82. }
  83. if (GetConsoleMode(hStdin, &flags)) {
  84. if (flags & ENABLE_PROCESSED_INPUT) {
  85. return -1;
  86. }
  87. }
  88. (void) SetConsoleCursorPosition(hStdout, newPosition);
  89. /* success */
  90. return 0;
  91. } /* SLang_init_tty */
  92. /*----------------------------------------------------------------------*\
  93. * Function: void SLang_reset_tty (void);
  94. *
  95. * reset the tty before exiting
  96. \*----------------------------------------------------------------------*/
  97. void SLang_reset_tty (void)
  98. {
  99. set_ctrl_break (1);
  100. }
  101. /*----------------------------------------------------------------------*\
  102. * Function: int SLsys_input_pending (int tsecs);
  103. *
  104. * sleep for *tsecs tenths of a sec waiting for input
  105. \*----------------------------------------------------------------------*/
  106. int SLsys_input_pending (int tsecs)
  107. {
  108. INPUT_RECORD record;
  109. long one = 1;
  110. long bytesRead;
  111. while (1)
  112. {
  113. if (PeekConsoleInput(hStdin, &record, 1, &bytesRead))
  114. {
  115. if (bytesRead == 1)
  116. {
  117. if ((record.EventType == KEY_EVENT)
  118. && record.Event.KeyEvent.bKeyDown)
  119. {
  120. /* ok, there is a keypress here */
  121. return 1;
  122. }
  123. else
  124. {
  125. /* something else is here, so read it and try again */
  126. (void) ReadConsoleInput(hStdin, &record, 1, &bytesRead);
  127. }
  128. }
  129. else
  130. {
  131. /* no Pending events */
  132. return 0;
  133. }
  134. }
  135. else
  136. {
  137. /* function failed */
  138. return 0;
  139. }
  140. }
  141. #if 0
  142. /* no delays yet */
  143. /* use Sleep */
  144. /*
  145. int count = tsecs * 5;
  146. if (keyWaiting()) return 1;
  147. while (count > 0)
  148. {
  149. delay (20); 20 ms or 1/50 sec
  150. if (keyWaiting()) break;
  151. count--;
  152. }
  153. return (count);
  154. */
  155. #endif
  156. }
  157. /*----------------------------------------------------------------------*\
  158. * Function: unsigned int SLsys_getkey (void);
  159. *
  160. * wait for and get the next available keystroke.
  161. * Also re-maps some useful keystrokes.
  162. *
  163. * Backspace (^H) => Del (127)
  164. * Ctrl-Space => ^@ (^@^3 - a pc NUL char)
  165. * extended keys are prefixed by a null character
  166. \*----------------------------------------------------------------------*/
  167. unsigned int SLsys_getkey (void)
  168. {
  169. unsigned int scan, ch, shift;
  170. long key, bytesRead;
  171. INPUT_RECORD record;
  172. while (1) {
  173. if (!ReadConsoleInput(hStdin, &record, 1, &bytesRead)) {
  174. return 0;
  175. }
  176. if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown) {
  177. #ifndef __MINGW32__
  178. return record.Event.KeyEvent.uChar.AsciiChar;
  179. #else
  180. return record.Event.KeyEvent.AsciiChar;
  181. #endif
  182. }
  183. }
  184. /* ReadFile(hStdin, &key, 1, &bytesRead, NULL); */
  185. /* return key; */
  186. }
  187. /*----------------------------------------------------------------------*\
  188. * Function: void SLang_set_abort_signal (void (*handler)(int));
  189. \*----------------------------------------------------------------------*/
  190. void SLang_set_abort_signal (void (*handler)(int))
  191. {
  192. }