ODGetIn.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* OpenDoors Online Software Programming Toolkit
  2. * (C) Copyright 1991 - 1999 by Brian Pirie.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. *
  19. * File: ODGetIn.c
  20. *
  21. * Description: Implements the od_get_input() function, which obtains the
  22. * next input event of any type, optionally performing
  23. * translation.
  24. *
  25. * Revisions: Date Ver Who Change
  26. * ---------------------------------------------------------------
  27. * Jan 04, 1996 6.00 BP Created.
  28. * Jan 30, 1996 6.00 BP Tweaked TREAT_ESC_AS_ANSI_TIMEOUT.
  29. * Jan 30, 1996 6.00 BP Replaced od_yield() with od_sleep().
  30. * Jan 30, 1996 6.00 BP Add ODInQueueGetNextEvent() timeout.
  31. * Jan 31, 1996 6.00 BP Added timeout for od_get_input().
  32. * Feb 13, 1996 6.00 BP Added od_get_input() flags parameter.
  33. * Feb 19, 1996 6.00 BP Changed version number to 6.00.
  34. * Feb 19, 1996 6.00 BP Switched to table implementation.
  35. * Feb 25, 1996 6.00 BP Added new control sequences to table.
  36. * Feb 27, 1996 6.00 BP Added od_max_key_latency.
  37. * Mar 03, 1996 6.10 BP Begin version 6.10.
  38. * Aug 10, 2003 6.23 SH *nix support
  39. * Apr 11, 2005 6.23 SH Fix hang forver on ESC press and latency timeout.
  40. */
  41. #define BUILDING_OPENDOORS
  42. #include <stddef.h>
  43. #include <string.h>
  44. #include "OpenDoor.h"
  45. #include "ODInQue.h"
  46. #include "ODCore.h"
  47. #include "ODKrnl.h"
  48. /* Control sequence table definitions. */
  49. typedef struct
  50. {
  51. char *pszSequence;
  52. char chExtendedKey;
  53. BOOL bIsControlKey;
  54. } tODKeySequence;
  55. tODKeySequence aKeySequences[] =
  56. {
  57. /* VT-52 control sequences. */
  58. {"\033A", OD_KEY_UP, FALSE},
  59. {"\033B", OD_KEY_DOWN, FALSE},
  60. {"\033C", OD_KEY_RIGHT, FALSE},
  61. {"\033D", OD_KEY_LEFT, FALSE},
  62. {"\033H", OD_KEY_HOME, FALSE},
  63. {"\033K", OD_KEY_END, FALSE},
  64. {"\033P", OD_KEY_F1, FALSE},
  65. {"\033Q", OD_KEY_F2, FALSE},
  66. {"\033?w", OD_KEY_F3, FALSE},
  67. {"\033?x", OD_KEY_F4, FALSE},
  68. {"\033?t", OD_KEY_F5, FALSE},
  69. {"\033?u", OD_KEY_F6, FALSE},
  70. {"\033?q", OD_KEY_F7, FALSE},
  71. {"\033?r", OD_KEY_F8, FALSE},
  72. {"\033?p", OD_KEY_F9, FALSE},
  73. /* Control sequences common to VT-100/VT-102/VT-220/VT-320/ANSI. */
  74. {"\033[A", OD_KEY_UP, FALSE},
  75. {"\033[B", OD_KEY_DOWN, FALSE},
  76. {"\033[C", OD_KEY_RIGHT, FALSE},
  77. {"\033[D", OD_KEY_LEFT, FALSE},
  78. {"\033[V", OD_KEY_PGUP, FALSE},
  79. {"\033[U", OD_KEY_PGDN, FALSE},
  80. {"\033[H", OD_KEY_HOME, FALSE},
  81. {"\033[K", OD_KEY_END, FALSE},
  82. {"\033OP", OD_KEY_F1, FALSE},
  83. {"\033OQ", OD_KEY_F2, FALSE},
  84. {"\033OR", OD_KEY_F3, FALSE},
  85. {"\033OS", OD_KEY_F4, FALSE},
  86. /* VT-220/VT-320 specific control sequences. */
  87. {"\033[1~", OD_KEY_HOME, FALSE}, /* Windows XP telnet.exe. Is actually FIND */
  88. {"\033[2~", OD_KEY_INSERT, FALSE},
  89. {"\033[3~", OD_KEY_DELETE, FALSE},
  90. {"\033[4~", OD_KEY_END, FALSE}, /* Windows XP telnet.exe. Is actually SELECT */
  91. {"\033[5~", OD_KEY_PGUP, FALSE},
  92. {"\033[6~", OD_KEY_PGDN, FALSE},
  93. {"\033[17~", OD_KEY_F6, FALSE},
  94. {"\033[18~", OD_KEY_F7, FALSE},
  95. {"\033[19~", OD_KEY_F8, FALSE},
  96. {"\033[20~", OD_KEY_F9, FALSE},
  97. {"\033[21~", OD_KEY_F10, FALSE},
  98. {"\033[23~", OD_KEY_F11, FALSE},
  99. {"\033[24~", OD_KEY_F12, FALSE},
  100. /* ANSI-specific control sequences. */
  101. {"\033[L", OD_KEY_HOME, FALSE},
  102. {"\033Ow", OD_KEY_F3, FALSE},
  103. {"\033Ox", OD_KEY_F4, FALSE},
  104. {"\033Ot", OD_KEY_F5, FALSE},
  105. {"\033Ou", OD_KEY_F6, FALSE},
  106. {"\033Oq", OD_KEY_F7, FALSE},
  107. {"\033Or", OD_KEY_F8, FALSE},
  108. {"\033Op", OD_KEY_F9, FALSE},
  109. /* PROCOMM-specific control sequences (non-keypad alternatives). */
  110. {"\033OA", OD_KEY_UP, FALSE},
  111. {"\033OB", OD_KEY_DOWN, FALSE},
  112. {"\033OC", OD_KEY_RIGHT, FALSE},
  113. {"\033OD", OD_KEY_LEFT, FALSE},
  114. {"\033OH", OD_KEY_HOME, FALSE},
  115. {"\033OK", OD_KEY_END, FALSE},
  116. /* Other standard control sequences. */
  117. {"\x16\t", OD_KEY_INSERT, TRUE},
  118. /* OpenDoors-specific alternatives. */
  119. {"\x7f", OD_KEY_DELETE, FALSE},
  120. {"\x5", OD_KEY_UP, TRUE},
  121. {"\x18", OD_KEY_DOWN, TRUE},
  122. {"\x13", OD_KEY_LEFT, TRUE},
  123. {"\x4", OD_KEY_RIGHT, TRUE},
  124. {"\x7", OD_KEY_DELETE, TRUE},
  125. {"\x16", OD_KEY_INSERT, TRUE},
  126. };
  127. /* Constant that indicates no match has been found. */
  128. #define NO_MATCH DIM(aKeySequences)
  129. /* Configurable constants. */
  130. /* The time, in milliseconds, to wait for a second character in a control */
  131. /* sequence, before deciding that none is going to come. */
  132. #define MAX_CHARACTER_LATENCY 250
  133. /* Size of inbound sequence buffer. */
  134. #define SEQUENCE_BUFFER_SIZE 10
  135. /* Current control sequence received state. */
  136. static char szCurrentSequence[SEQUENCE_BUFFER_SIZE] = "";
  137. #if 0 // Unused...
  138. static tODTimer SequenceFailTimer;
  139. static BOOL bSequenceFromRemote;
  140. static int nMatchedSequence = NO_MATCH;
  141. static BOOL bTimerActive = FALSE;
  142. #endif
  143. static BOOL bDoorwaySequence = FALSE;
  144. static BOOL bDoorwaySequencePending = FALSE;
  145. /* Local private function prototypes. */
  146. static int ODGetCodeIfLongest(WORD wFlags);
  147. static int ODHaveStartOfSequence(WORD wFlags);
  148. static int ODLongestFullCode(WORD wFlags);
  149. static void ODShiftSeq(int chars);
  150. /* ----------------------------------------------------------------------------
  151. * od_get_input()
  152. *
  153. * Obtains the next input event of any type, optionally performing
  154. * translation on input events.
  155. *
  156. * Parameters: pInputEvent - Pointer to a tODInputEvent structure, which
  157. * will be filled by information on the next input
  158. * event, if any is obtained.
  159. *
  160. * TimeToWait - Number of milliseconds to wait for input to be
  161. * available. A value of 0 causes od_get_input()
  162. * to return immediately if no input is waiting,
  163. * while a value of OD_NO_TIMEOUT causes the
  164. * function to never return unless input has been
  165. * obtained.
  166. *
  167. * wFlags - Flags which customize od_get_input()'s behaviour.
  168. *
  169. * Return: TRUE if an input event was obtained, FALSE if not.
  170. */
  171. ODAPIDEF BOOL ODCALL od_get_input(tODInputEvent *pInputEvent,
  172. tODMilliSec TimeToWait, WORD wFlags)
  173. {
  174. tODInputEvent LastInputEvent;
  175. int nSequence;
  176. int nSequenceLen;
  177. /* Log function entry if running in trace mode. */
  178. TRACE(TRACE_API, "od_get_input()");
  179. /* Initialize OpenDoors if it hasn't already been done. */
  180. if(!bODInitialized) od_init();
  181. OD_API_ENTRY();
  182. /* Check for parameter validity. */
  183. if(pInputEvent == NULL)
  184. {
  185. od_control.od_error = ERR_PARAMETER;
  186. OD_API_EXIT();
  187. return(FALSE);
  188. }
  189. /* Call the OpenDoors kernel, if applicable */
  190. CALL_KERNEL_IF_NEEDED();
  191. /* If you don't know better, it's a remote char */
  192. /* Local chars are always correctly received (no ANSI sequences) */
  193. LastInputEvent.bFromRemote = TRUE;
  194. if((!bDoorwaySequence) && bDoorwaySequencePending && (!szCurrentSequence[0])) {
  195. bDoorwaySequencePending=FALSE;
  196. bDoorwaySequence=TRUE;
  197. }
  198. /* If no pending input string, wait for the first keystroke */
  199. if((!szCurrentSequence[0]) && (!bDoorwaySequence)) {
  200. if(ODInQueueGetNextEvent(hODInputQueue, &LastInputEvent, TimeToWait)
  201. != kODRCSuccess) {
  202. OD_API_EXIT();
  203. return(FALSE);
  204. }
  205. /* If you have a *local* char, send it immediately */
  206. if((!LastInputEvent.bFromRemote) && (LastInputEvent.chKeyPress != 0)
  207. #if 0
  208. && (LastInputEvent.EventType == EVENT_EXTENDED_KEY)) {
  209. #else
  210. ) {
  211. #endif
  212. memcpy(pInputEvent, &LastInputEvent, sizeof(tODInputEvent));
  213. OD_API_EXIT();
  214. return(TRUE);
  215. }
  216. /* IF the received char is a NULL, this is the start of a doorway char */
  217. if(!LastInputEvent.chKeyPress)
  218. bDoorwaySequence = TRUE;
  219. else {
  220. szCurrentSequence[0]=LastInputEvent.chKeyPress;
  221. szCurrentSequence[1]=0;
  222. }
  223. }
  224. nSequenceLen=strlen(szCurrentSequence);
  225. CALL_KERNEL_IF_NEEDED();
  226. /* If you don't have the start of a sequence, and it's not doorway mode, you have
  227. * a char. It's that simple.
  228. */
  229. if((!bDoorwaySequence) && (!ODHaveStartOfSequence(wFlags))) {
  230. pInputEvent->chKeyPress = szCurrentSequence[0];
  231. pInputEvent->EventType = EVENT_CHARACTER;
  232. pInputEvent->bFromRemote = LastInputEvent.bFromRemote;
  233. /* Shift the sequence buffer over one */
  234. ODShiftSeq(1);
  235. OD_API_EXIT();
  236. return(TRUE);
  237. }
  238. /* Now... if the current sequence IS the longest valid one, return it
  239. * immediately. If it's sequence leftovers, it HAS to be a remote key
  240. * since local chars are #1 always doorway mode and #2 have no delay
  241. * betwixt them.
  242. */
  243. if((nSequence = ODGetCodeIfLongest(wFlags)) != NO_MATCH) {
  244. pInputEvent->chKeyPress = aKeySequences[nSequence].chExtendedKey;
  245. pInputEvent->EventType = EVENT_EXTENDED_KEY;
  246. pInputEvent->bFromRemote = LastInputEvent.bFromRemote;
  247. /* Shift the sequence buffer... being sure to copy the terminator */
  248. ODShiftSeq(strlen(aKeySequences[nSequence].pszSequence));
  249. OD_API_EXIT();
  250. return(TRUE);
  251. }
  252. /* Now, continue adding chars, waiting at MOST MAX_CHARACTER_LATENCY between them */
  253. CALL_KERNEL_IF_NEEDED();
  254. while((!bDoorwaySequencePending)
  255. && (ODInQueueGetNextEvent(hODInputQueue, &LastInputEvent, MAX_CHARACTER_LATENCY)
  256. == kODRCSuccess)) {
  257. /* If you are looking for a doorway sequence, any char completes it (honest!) */
  258. /* Further, thanks to some lack of planning, it's EXACTLY THE SAME as the char,
  259. * only it's extended.
  260. */
  261. if(bDoorwaySequence) {
  262. memcpy(pInputEvent, &LastInputEvent, sizeof(tODInputEvent));
  263. pInputEvent->EventType = EVENT_EXTENDED_KEY;
  264. bDoorwaySequence=FALSE;
  265. OD_API_EXIT();
  266. return(TRUE);
  267. }
  268. /* If we get a 0, we *WILL BE* looking for a doorway sequence. But NOT
  269. * until the current sequence buffer is drained!
  270. */
  271. if(LastInputEvent.chKeyPress == 0) {
  272. bDoorwaySequencePending=TRUE;
  273. break;
  274. }
  275. /* If you have a *local* char, send it immediately */
  276. if((!LastInputEvent.bFromRemote) && (LastInputEvent.chKeyPress != 0)
  277. #if 0
  278. && (LastInputEvent.EventType == EVENT_EXTENDED_KEY)) {
  279. #else
  280. ) {
  281. #endif
  282. memcpy(pInputEvent, &LastInputEvent, sizeof(tODInputEvent));
  283. OD_API_EXIT();
  284. return(TRUE);
  285. }
  286. /* Put this char into the sequence buffer */
  287. szCurrentSequence[nSequenceLen++]=LastInputEvent.chKeyPress;
  288. szCurrentSequence[nSequenceLen]=0;
  289. /* When you have the longest possible sequence, you ARE done */
  290. if((nSequence = ODGetCodeIfLongest(wFlags)) != NO_MATCH) {
  291. pInputEvent->chKeyPress = aKeySequences[nSequence].chExtendedKey;
  292. pInputEvent->EventType = EVENT_EXTENDED_KEY;
  293. pInputEvent->bFromRemote = LastInputEvent.bFromRemote;
  294. /* Shift the sequence buffer... being sure to copy the terminator */
  295. ODShiftSeq(strlen(aKeySequences[nSequence].pszSequence));
  296. OD_API_EXIT();
  297. return(TRUE);
  298. }
  299. CALL_KERNEL_IF_NEEDED();
  300. }
  301. /* If we were looking for a doorway sequence, tough, we didn't get it. */
  302. if(bDoorwaySequence) {
  303. pInputEvent->chKeyPress = 0;
  304. pInputEvent->EventType = EVENT_CHARACTER;
  305. pInputEvent->bFromRemote = LastInputEvent.bFromRemote;
  306. bDoorwaySequence=FALSE;
  307. OD_API_EXIT();
  308. return(TRUE);
  309. }
  310. /* Now, if we have any kind of sequence, we'll settle for it. */
  311. if((nSequence = ODLongestFullCode(wFlags)) != NO_MATCH) {
  312. pInputEvent->chKeyPress = aKeySequences[nSequence].chExtendedKey;
  313. pInputEvent->EventType = EVENT_EXTENDED_KEY;
  314. pInputEvent->bFromRemote = LastInputEvent.bFromRemote;
  315. /* Shift the sequence buffer... being sure to copy the terminator */
  316. ODShiftSeq(strlen(aKeySequences[nSequence].pszSequence));
  317. OD_API_EXIT();
  318. return(TRUE);
  319. }
  320. /* If we don't have a complete sequence, send a single char */
  321. pInputEvent->chKeyPress = szCurrentSequence[0];
  322. if(szCurrentSequence[0]) {
  323. pInputEvent->EventType = EVENT_CHARACTER;
  324. pInputEvent->bFromRemote = LastInputEvent.bFromRemote;
  325. /* Shift the sequence buffer over one */
  326. ODShiftSeq(1);
  327. }
  328. OD_API_EXIT();
  329. /* Return false if something broke */
  330. return(pInputEvent->chKeyPress);
  331. }
  332. /* ----------------------------------------------------------------------------
  333. * ODLongestFullCode() *** PRIVATE FUNCTION ***
  334. *
  335. * Return the index of the longest full code that matches the start of the
  336. * sequence buffer
  337. *
  338. * Parameters: wFlags from od_get_input()
  339. *
  340. * Return: void
  341. */
  342. static int ODLongestFullCode(WORD wFlags)
  343. {
  344. int CurrLen=0;
  345. int seqlen;
  346. int i;
  347. int retval=NO_MATCH;;
  348. if(wFlags & GETIN_RAW)
  349. return(NO_MATCH);
  350. for(i = 0; i < DIM(aKeySequences); ++i) {
  351. if((wFlags & GETIN_RAWCTRL) && aKeySequences[i].bIsControlKey) {
  352. continue;
  353. }
  354. seqlen=strlen(aKeySequences[i].pszSequence);
  355. if(seqlen>CurrLen) {
  356. if(strncmp(aKeySequences[i].pszSequence, szCurrentSequence, seqlen)==0) {
  357. retval=i;
  358. CurrLen=seqlen;
  359. }
  360. }
  361. }
  362. return(retval);
  363. }
  364. /* ----------------------------------------------------------------------------
  365. * ODHaveStartOfSequence() *** PRIVATE FUNCTION ***
  366. *
  367. * If the current sequence buffer is the start of a valid sequence, return TRUE
  368. *
  369. * Parameters: wFlags from od_get_input()
  370. *
  371. * Return: void
  372. */
  373. static int ODHaveStartOfSequence(WORD wFlags)
  374. {
  375. int seqlen1;
  376. int seqlen2;
  377. int i;
  378. if(wFlags & GETIN_RAW)
  379. return(FALSE);
  380. seqlen1=strlen(szCurrentSequence);
  381. for(i = 0; i < DIM(aKeySequences); ++i) {
  382. if((wFlags & GETIN_RAWCTRL) && aKeySequences[i].bIsControlKey) {
  383. continue;
  384. }
  385. seqlen2=strlen(aKeySequences[i].pszSequence);
  386. if(seqlen1<seqlen2)
  387. seqlen2=seqlen1;
  388. if(strncmp(aKeySequences[i].pszSequence, szCurrentSequence, seqlen2)==0) {
  389. return(TRUE);
  390. }
  391. }
  392. return(FALSE);
  393. }
  394. /* ----------------------------------------------------------------------------
  395. * ODGetCodeIfLongest() *** PRIVATE FUNCTION ***
  396. *
  397. * Returns the index of the entry in the sequence buffer only if there
  398. * are no longer sequences that could start the same way.
  399. *
  400. * Parameters: wFlags from od_get_input()
  401. *
  402. * Return: void
  403. */
  404. static int ODGetCodeIfLongest(WORD wFlags)
  405. {
  406. int CurrLen=0;
  407. int seqlen1;
  408. int seqlen2;
  409. int i;
  410. int retval=NO_MATCH;;
  411. if(wFlags & GETIN_RAW)
  412. return(NO_MATCH);
  413. seqlen1=strlen(szCurrentSequence);
  414. for(i = 0; i < DIM(aKeySequences); ++i) {
  415. if((wFlags & GETIN_RAWCTRL) && aKeySequences[i].bIsControlKey) {
  416. continue;
  417. }
  418. seqlen2=strlen(aKeySequences[i].pszSequence);
  419. if(seqlen2>CurrLen) {
  420. if(seqlen2<=seqlen1) { /* The sequence would be completed in buffer */
  421. if(strncmp(aKeySequences[i].pszSequence, szCurrentSequence, seqlen2)==0) {
  422. retval=i;
  423. CurrLen=seqlen2;
  424. }
  425. }
  426. else { /* Possible partial sequence */
  427. if(strncmp(aKeySequences[i].pszSequence, szCurrentSequence, seqlen1)==0) {
  428. return(NO_MATCH);
  429. }
  430. }
  431. }
  432. }
  433. return(retval);
  434. }
  435. /* ----------------------------------------------------------------------------
  436. * ODShiftSeq() *** PRIVATE FUNCTION ***
  437. *
  438. * Shifts szCurrentSequence left the specified number of chars
  439. *
  440. * Parameters: int chars to shift
  441. *
  442. * Return: void
  443. */
  444. static void ODShiftSeq(int chars)
  445. {
  446. char *in;
  447. char *out;
  448. if(!chars)
  449. return;
  450. out=szCurrentSequence;
  451. in=out+chars;
  452. if(in>strchr(szCurrentSequence,0))
  453. return;
  454. while(*in) {
  455. *(out++)=*(in++);
  456. }
  457. *out=*in;
  458. }