slos2tty.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "_slang.h"
  10. #define INCL_BASE
  11. #define INCL_NOPM
  12. #define INCL_VIO
  13. #define INCL_KBD
  14. #define INCL_DOS
  15. #if 0
  16. # define INCL_DOSSEMAPHORES
  17. #endif
  18. #ifdef LONG
  19. #undef LONG
  20. #endif
  21. #ifdef VOID
  22. #undef VOID
  23. #endif
  24. #include <os2.h>
  25. #include <signal.h>
  26. #include <process.h>
  27. KBDINFO initialKbdInfo; /* keyboard info */
  28. /* Code to read keystrokes in a separate thread */
  29. typedef struct kbdcodes {
  30. UCHAR ascii;
  31. UCHAR scan;
  32. /* USHORT shift; */
  33. } KBDCODES;
  34. #define BUFFER_LEN 4096
  35. static KBDCODES threadKeys[BUFFER_LEN];
  36. static int atEnd = 0;
  37. static int startBuf;
  38. static int endBuf;
  39. /* Original code used semaphores to control access to threadKeys.
  40. * It is expected that the semaphore code will be deleted after 0.97.
  41. */
  42. #if 0
  43. #ifdef __os2_16__
  44. typedef USHORT APIRET;
  45. static HSEM Hmtx;
  46. #define DosRequestMutexSem(hmtx,timeout) DosSemRequest(hmtx,timeout)
  47. #define DosReleaseMutexSem(hmtx) DosSemClear(hmtx)
  48. #define DosCloseMutexSem(hmtx) DosCloseSem(hmtx)
  49. #else /* !defined(__os2_16__) */
  50. static HMTX Hmtx; /* Mutex Semaphore */
  51. #endif
  52. static APIRET CreateSem(void)
  53. {
  54. #ifdef __os2_16__
  55. char SemName[32];
  56. sprintf(SemName, "\\SEM\\jed\\%u", getpid());
  57. return ( DosCreateSem (0, &Hmtx, SemName) );
  58. #else
  59. return ( DosCreateMutexSem (NULL, &Hmtx, 0, 0) );
  60. #endif
  61. }
  62. static APIRET RequestSem(void)
  63. {
  64. return ( DosRequestMutexSem (Hmtx, -1) );
  65. }
  66. static APIRET ReleaseSem(void)
  67. {
  68. return ( DosReleaseMutexSem (Hmtx) );
  69. }
  70. static APIRET CloseSem(void)
  71. {
  72. return( DosCloseMutexSem (Hmtx) );
  73. }
  74. #else
  75. #define CreateSem()
  76. #define RequestSem()
  77. #define ReleaseSem()
  78. #define CloseSem()
  79. #endif
  80. static void set_kbd(void)
  81. {
  82. KBDINFO kbdInfo;
  83. kbdInfo = initialKbdInfo;
  84. kbdInfo.fsMask &= ~0x0001; /* not echo on */
  85. kbdInfo.fsMask |= 0x0002; /* echo off */
  86. kbdInfo.fsMask &= ~0x0008; /* cooked mode off */
  87. kbdInfo.fsMask |= 0x0004; /* raw mode */
  88. kbdInfo.fsMask &= ~0x0100; /* shift report off */
  89. KbdSetStatus(&kbdInfo, 0);
  90. }
  91. static void thread_getkey ()
  92. {
  93. KBDKEYINFO keyInfo;
  94. int n;
  95. while (!atEnd) { /* at end is a flag */
  96. set_kbd();
  97. KbdCharIn(&keyInfo, IO_NOWAIT, 0); /* get a character */
  98. if (keyInfo.fbStatus & 0x040) { /* found a char process it */
  99. if (keyInfo.chChar == SLang_Abort_Char) {
  100. if (SLang_Ignore_User_Abort == 0) SLang_Error = USER_BREAK;
  101. SLKeyBoard_Quit = 1;
  102. }
  103. n = (endBuf + 1) % BUFFER_LEN;
  104. if (n == startBuf) {
  105. DosBeep (500, 20);
  106. KbdFlushBuffer(0);
  107. continue;
  108. }
  109. RequestSem();
  110. threadKeys [n].ascii = keyInfo.chChar;
  111. threadKeys [n].scan = keyInfo.chScan;
  112. /* threadKeys [n].shift = keyInfo.fsState; */
  113. endBuf = n;
  114. ReleaseSem();
  115. } else /* no char available*/
  116. DosSleep (20);
  117. }
  118. }
  119. static void thread_code (void *Args)
  120. {
  121. (void) Args;
  122. startBuf = -1; /* initialize the buffer pointers */
  123. endBuf = -1;
  124. thread_getkey ();
  125. atEnd = 0; /* reset the flag */
  126. _endthread();
  127. }
  128. /* The code below is in the main thread */
  129. int SLang_init_tty(int abort_char, int dum2, int dum3)
  130. {
  131. VIOCURSORINFO cursorInfo, OldcursorInfo;
  132. (void) dum2; (void) dum3;
  133. if (abort_char == -1) abort_char = 3; /* ^C */
  134. SLang_Abort_Char = abort_char;
  135. /* set ^C off */
  136. signal (SIGINT, SIG_IGN);
  137. signal (SIGBREAK, SIG_IGN);
  138. /* set up the keyboard */
  139. initialKbdInfo.cb = sizeof(initialKbdInfo);
  140. KbdGetStatus(&initialKbdInfo, 0);
  141. set_kbd();
  142. /* open a semaphore */
  143. CreateSem();
  144. /* start a separate thread to read the keyboard */
  145. #if defined(__BORLANDC__)
  146. _beginthread (thread_code, 8096, NULL);
  147. #else
  148. _beginthread (thread_code, NULL, 8096, NULL);
  149. #endif
  150. VioGetCurType (&OldcursorInfo, 0);
  151. cursorInfo.yStart = 1;
  152. cursorInfo.cEnd = 15;
  153. cursorInfo.cx = 1;
  154. cursorInfo.attr = 1;
  155. if (VioSetCurType (&cursorInfo, 0))
  156. VioSetCurType (&OldcursorInfo, 0); /* reset to previous value */
  157. return 0;
  158. }
  159. void SLang_reset_tty (void)
  160. {
  161. atEnd = 1; /* set flag and wait until thread ends */
  162. while (atEnd) {DosSleep (0);}
  163. CloseSem();
  164. /* close the keyboard */
  165. KbdSetStatus(&initialKbdInfo, 0); /* restore original state */
  166. }
  167. #define keyWaiting() (endBuf != startBuf)
  168. /* sleep for *tsecs tenths of a sec waiting for input */
  169. int SLsys_input_pending(int tsecs)
  170. {
  171. int count = tsecs * 5;
  172. if (count)
  173. {
  174. while(count > 0)
  175. {
  176. DosSleep(20); /* 20 ms or 1/50 sec */
  177. if (keyWaiting ()) break;
  178. count--;
  179. }
  180. return(count);
  181. }
  182. else return(keyWaiting ());
  183. }
  184. unsigned int SLsys_getkey ()
  185. {
  186. unsigned int c;
  187. unsigned char scan;
  188. int tsecs = 300;
  189. if (!keyWaiting())
  190. while (!SLsys_input_pending(tsecs));
  191. /* read codes from buffer */
  192. RequestSem();
  193. startBuf = (startBuf + 1) % BUFFER_LEN;
  194. c = threadKeys [startBuf].ascii;
  195. scan = threadKeys [startBuf].scan;
  196. ReleaseSem();
  197. if ((c == 8) && (scan == 0x0e)) c = 127;
  198. if (c == 0xE0) c = 0;
  199. if (c == 0) SLang_ungetkey (scan);
  200. return (c);
  201. }
  202. void SLang_set_abort_signal (void (*dum)(int))
  203. {
  204. (void) dum;
  205. }