123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- #define BUILDING_OPENDOORS
- #include <stddef.h>
- #include <string.h>
- #include "OpenDoor.h"
- #include "ODInQue.h"
- #include "ODCore.h"
- #include "ODKrnl.h"
- typedef struct
- {
- char *pszSequence;
- char chExtendedKey;
- BOOL bIsControlKey;
- } tODKeySequence;
- tODKeySequence aKeySequences[] =
- {
-
- {"\033A", OD_KEY_UP, FALSE},
- {"\033B", OD_KEY_DOWN, FALSE},
- {"\033C", OD_KEY_RIGHT, FALSE},
- {"\033D", OD_KEY_LEFT, FALSE},
- {"\033H", OD_KEY_HOME, FALSE},
- {"\033K", OD_KEY_END, FALSE},
- {"\033P", OD_KEY_F1, FALSE},
- {"\033Q", OD_KEY_F2, FALSE},
- {"\033?w", OD_KEY_F3, FALSE},
- {"\033?x", OD_KEY_F4, FALSE},
- {"\033?t", OD_KEY_F5, FALSE},
- {"\033?u", OD_KEY_F6, FALSE},
- {"\033?q", OD_KEY_F7, FALSE},
- {"\033?r", OD_KEY_F8, FALSE},
- {"\033?p", OD_KEY_F9, FALSE},
-
- {"\033[A", OD_KEY_UP, FALSE},
- {"\033[B", OD_KEY_DOWN, FALSE},
- {"\033[C", OD_KEY_RIGHT, FALSE},
- {"\033[D", OD_KEY_LEFT, FALSE},
- {"\033[M", OD_KEY_PGUP, FALSE},
- {"\033[H\x1b[2J", OD_KEY_PGDN, FALSE},
- {"\033[H", OD_KEY_HOME, FALSE},
- {"\033[K", OD_KEY_END, FALSE},
- {"\033OP", OD_KEY_F1, FALSE},
- {"\033OQ", OD_KEY_F2, FALSE},
- {"\033OR", OD_KEY_F3, FALSE},
- {"\033OS", OD_KEY_F4, FALSE},
-
- {"\033[1~", OD_KEY_HOME, FALSE},
- {"\033[2~", OD_KEY_INSERT, FALSE},
- {"\033[3~", OD_KEY_DELETE, FALSE},
- {"\033[4~", OD_KEY_END, FALSE},
- {"\033[5~", OD_KEY_PGUP, FALSE},
- {"\033[6~", OD_KEY_PGDN, FALSE},
- {"\033[17~", OD_KEY_F6, FALSE},
- {"\033[18~", OD_KEY_F7, FALSE},
- {"\033[19~", OD_KEY_F8, FALSE},
- {"\033[20~", OD_KEY_F9, FALSE},
- {"\033[21~", OD_KEY_F10, FALSE},
- {"\033[23~", OD_KEY_F11, FALSE},
- {"\033[24~", OD_KEY_F12, FALSE},
-
- {"\033[L", OD_KEY_HOME, FALSE},
- {"\033Ow", OD_KEY_F3, FALSE},
- {"\033Ox", OD_KEY_F4, FALSE},
- {"\033Ot", OD_KEY_F5, FALSE},
- {"\033Ou", OD_KEY_F6, FALSE},
- {"\033Oq", OD_KEY_F7, FALSE},
- {"\033Or", OD_KEY_F8, FALSE},
- {"\033Op", OD_KEY_F9, FALSE},
-
- {"\033OA", OD_KEY_UP, FALSE},
- {"\033OB", OD_KEY_DOWN, FALSE},
- {"\033OC", OD_KEY_RIGHT, FALSE},
- {"\033OD", OD_KEY_LEFT, FALSE},
- {"\033OH", OD_KEY_HOME, FALSE},
- {"\033OK", OD_KEY_END, FALSE},
-
- {"\x16\t", OD_KEY_INSERT, TRUE},
-
- {"\x7f", OD_KEY_DELETE, FALSE},
- {"\x5", OD_KEY_UP, TRUE},
- {"\x18", OD_KEY_DOWN, TRUE},
- {"\x13", OD_KEY_LEFT, TRUE},
- {"\x4", OD_KEY_RIGHT, TRUE},
- {"\x7", OD_KEY_DELETE, TRUE},
- {"\x16", OD_KEY_INSERT, TRUE},
- };
- #define NO_MATCH DIM(aKeySequences)
- #define MAX_CHARACTER_LATENCY 250
- #define SEQUENCE_BUFFER_SIZE 10
- static char szCurrentSequence[SEQUENCE_BUFFER_SIZE] = "";
- #if 0
- static tODTimer SequenceFailTimer;
- static BOOL bSequenceFromRemote;
- static int nMatchedSequence = NO_MATCH;
- static BOOL bTimerActive = FALSE;
- #endif
- static BOOL bDoorwaySequence = FALSE;
- static BOOL bDoorwaySequencePending = FALSE;
- static int ODGetCodeIfLongest(WORD wFlags);
- static int ODHaveStartOfSequence(WORD wFlags);
- static int ODLongestFullCode(WORD wFlags);
- static void ODShiftSeq(int chars);
- ODAPIDEF BOOL ODCALL od_get_input(tODInputEvent *pInputEvent,
- tODMilliSec TimeToWait, WORD wFlags)
- {
- tODInputEvent LastInputEvent;
- int nSequence;
- int nSequenceLen;
-
- TRACE(TRACE_API, "od_get_input()");
-
- if(!bODInitialized) od_init();
- OD_API_ENTRY();
-
- if(pInputEvent == NULL)
- {
- od_control.od_error = ERR_PARAMETER;
- OD_API_EXIT();
- return(FALSE);
- }
-
- CALL_KERNEL_IF_NEEDED();
-
-
- LastInputEvent.bFromRemote = TRUE;
- if((!bDoorwaySequence) && bDoorwaySequencePending && (!szCurrentSequence[0])) {
- bDoorwaySequencePending=FALSE;
- bDoorwaySequence=TRUE;
- }
-
- if((!szCurrentSequence[0]) && (!bDoorwaySequence)) {
- if(ODInQueueGetNextEvent(hODInputQueue, &LastInputEvent, TimeToWait)
- != kODRCSuccess) {
- OD_API_EXIT();
- return(FALSE);
- }
-
- if((!LastInputEvent.bFromRemote) && (LastInputEvent.chKeyPress != 0)
- #if 0
- && (LastInputEvent.EventType == EVENT_EXTENDED_KEY)) {
- #else
- ) {
- #endif
- memcpy(pInputEvent, &LastInputEvent, sizeof(tODInputEvent));
- OD_API_EXIT();
- return(TRUE);
- }
-
- if(!LastInputEvent.chKeyPress)
- bDoorwaySequence = TRUE;
- else {
- szCurrentSequence[0]=LastInputEvent.chKeyPress;
- szCurrentSequence[1]=0;
- }
- }
- nSequenceLen=strlen(szCurrentSequence);
- CALL_KERNEL_IF_NEEDED();
-
- if((!bDoorwaySequence) && (!ODHaveStartOfSequence(wFlags))) {
- pInputEvent->chKeyPress = szCurrentSequence[0];
- pInputEvent->EventType = EVENT_CHARACTER;
- pInputEvent->bFromRemote = LastInputEvent.bFromRemote;
-
- ODShiftSeq(1);
- OD_API_EXIT();
- return(TRUE);
- }
-
- if((nSequence = ODGetCodeIfLongest(wFlags)) != NO_MATCH) {
- pInputEvent->chKeyPress = aKeySequences[nSequence].chExtendedKey;
- pInputEvent->EventType = EVENT_EXTENDED_KEY;
- pInputEvent->bFromRemote = LastInputEvent.bFromRemote;
-
- ODShiftSeq(strlen(aKeySequences[nSequence].pszSequence));
- OD_API_EXIT();
- return(TRUE);
- }
-
- CALL_KERNEL_IF_NEEDED();
- while((!bDoorwaySequencePending)
- && (ODInQueueGetNextEvent(hODInputQueue, &LastInputEvent, MAX_CHARACTER_LATENCY)
- == kODRCSuccess)) {
-
-
- if(bDoorwaySequence) {
- memcpy(pInputEvent, &LastInputEvent, sizeof(tODInputEvent));
- pInputEvent->EventType = EVENT_EXTENDED_KEY;
- bDoorwaySequence=FALSE;
- OD_API_EXIT();
- return(TRUE);
- }
-
- if(LastInputEvent.chKeyPress == 0) {
- bDoorwaySequencePending=TRUE;
- break;
- }
-
- if((!LastInputEvent.bFromRemote) && (LastInputEvent.chKeyPress != 0)
- #if 0
- && (LastInputEvent.EventType == EVENT_EXTENDED_KEY)) {
- #else
- ) {
- #endif
- memcpy(pInputEvent, &LastInputEvent, sizeof(tODInputEvent));
- OD_API_EXIT();
- return(TRUE);
- }
-
- szCurrentSequence[nSequenceLen++]=LastInputEvent.chKeyPress;
- szCurrentSequence[nSequenceLen]=0;
-
- if((nSequence = ODGetCodeIfLongest(wFlags)) != NO_MATCH) {
- pInputEvent->chKeyPress = aKeySequences[nSequence].chExtendedKey;
- pInputEvent->EventType = EVENT_EXTENDED_KEY;
- pInputEvent->bFromRemote = LastInputEvent.bFromRemote;
-
- ODShiftSeq(strlen(aKeySequences[nSequence].pszSequence));
- OD_API_EXIT();
- return(TRUE);
- }
- CALL_KERNEL_IF_NEEDED();
- }
-
- if(bDoorwaySequence) {
- pInputEvent->chKeyPress = 0;
- pInputEvent->EventType = EVENT_CHARACTER;
- pInputEvent->bFromRemote = LastInputEvent.bFromRemote;
- bDoorwaySequence=FALSE;
- OD_API_EXIT();
- return(TRUE);
- }
-
- if((nSequence = ODLongestFullCode(wFlags)) != NO_MATCH) {
- pInputEvent->chKeyPress = aKeySequences[nSequence].chExtendedKey;
- pInputEvent->EventType = EVENT_EXTENDED_KEY;
- pInputEvent->bFromRemote = LastInputEvent.bFromRemote;
-
- ODShiftSeq(strlen(aKeySequences[nSequence].pszSequence));
- OD_API_EXIT();
- return(TRUE);
- }
-
- pInputEvent->chKeyPress = szCurrentSequence[0];
- if(szCurrentSequence[0]) {
- pInputEvent->EventType = EVENT_CHARACTER;
- pInputEvent->bFromRemote = LastInputEvent.bFromRemote;
-
- ODShiftSeq(1);
- }
- OD_API_EXIT();
-
- return(pInputEvent->chKeyPress);
- }
- static int ODLongestFullCode(WORD wFlags)
- {
- int CurrLen=0;
- int seqlen;
- int i;
- int retval=NO_MATCH;;
- if(wFlags & GETIN_RAW)
- return(NO_MATCH);
- for(i = 0; i < DIM(aKeySequences); ++i) {
- if((wFlags & GETIN_RAWCTRL) && aKeySequences[i].bIsControlKey) {
- continue;
- }
- seqlen=strlen(aKeySequences[i].pszSequence);
- if(seqlen>CurrLen) {
- if(strncmp(aKeySequences[i].pszSequence, szCurrentSequence, seqlen)==0) {
- retval=i;
- CurrLen=seqlen;
- }
- }
- }
- return(retval);
- }
- static int ODHaveStartOfSequence(WORD wFlags)
- {
- int seqlen1;
- int seqlen2;
- int i;
- if(wFlags & GETIN_RAW)
- return(FALSE);
- seqlen1=strlen(szCurrentSequence);
- for(i = 0; i < DIM(aKeySequences); ++i) {
- if((wFlags & GETIN_RAWCTRL) && aKeySequences[i].bIsControlKey) {
- continue;
- }
- seqlen2=strlen(aKeySequences[i].pszSequence);
- if(seqlen1<seqlen2)
- seqlen2=seqlen1;
- if(strncmp(aKeySequences[i].pszSequence, szCurrentSequence, seqlen2)==0) {
- return(TRUE);
- }
- }
- return(FALSE);
- }
- static int ODGetCodeIfLongest(WORD wFlags)
- {
- int CurrLen=0;
- int seqlen1;
- int seqlen2;
- int i;
- int retval=NO_MATCH;;
- if(wFlags & GETIN_RAW)
- return(NO_MATCH);
- seqlen1=strlen(szCurrentSequence);
- for(i = 0; i < DIM(aKeySequences); ++i) {
- if((wFlags & GETIN_RAWCTRL) && aKeySequences[i].bIsControlKey) {
- continue;
- }
- seqlen2=strlen(aKeySequences[i].pszSequence);
- if(seqlen2>CurrLen) {
- if(seqlen2<=seqlen1) {
- if(strncmp(aKeySequences[i].pszSequence, szCurrentSequence, seqlen2)==0) {
- retval=i;
- CurrLen=seqlen2;
- }
- }
- else {
- if(strncmp(aKeySequences[i].pszSequence, szCurrentSequence, seqlen1)==0) {
- return(NO_MATCH);
- }
- }
- }
- }
- return(retval);
- }
- static void ODShiftSeq(int chars)
- {
- char *in;
- char *out;
- if(!chars)
- return;
- out=szCurrentSequence;
- in=out+chars;
- if(in>strchr(szCurrentSequence,0))
- return;
- while(*in) {
- *(out++)=*(in++);
- }
- *out=*in;
- }
|