This patch adds the get-key-map word to the keyboard node. This patch is an improvement on the last get-key-map patch. It adds a keyboard node to /aliases and fixes a bug with the getKeyMap() function. If you wish to test out this patch, the program below will show you what key you pushed. The output will display hex values and text. The hex values in the middle tells you which key is being pushed.
: myword { ; keyboardDevice } " keyboard" open-dev -> keyboardDevice begin " get-key-map" keyboardDevice $call-method 32 dump 10000 ms \ update speed again ;
signed-off-by: John Arbuckle programmingkidx@gmail.com
--- drivers/adb_kbd.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 82 insertions(+), 4 deletions(-)
diff --git a/drivers/adb_kbd.c b/drivers/adb_kbd.c index e38798a..e799704 100644 --- a/drivers/adb_kbd.c +++ b/drivers/adb_kbd.c @@ -42,11 +42,13 @@ keyboard_close(int *idx) }
static void keyboard_read(void); +void getKeyMap(void);
NODE_METHODS( keyboard ) = { - { "open", keyboard_open }, - { "close", keyboard_close }, - { "read", keyboard_read }, + { "open", keyboard_open }, + { "close", keyboard_close }, + { "read", keyboard_read }, + { "get-key-map", getKeyMap }, };
/* VT100 escape sequences */ @@ -545,7 +547,8 @@ void *adb_kbd_new (char *path, void *private) set_property(ph, "reg", (char *)&props, sizeof(props));
aliases = find_dev("/aliases"); - set_property(aliases, "adb-keyboard", buf, strlen(buf) + 1); + set_property(aliases, "adb-keyboard", buf, strlen(buf) + 1); + set_property(aliases, "keyboard", buf, strlen(buf) + 1);
return kbd; } @@ -566,3 +569,78 @@ static void keyboard_read(void) } PUSH(i); } + + +// Returns the base to the exponent power +static int pow(int base, int exponent) +{ + int index, answer; + answer = base; + for(index = 1; index < exponent; index++) + { + answer = answer * base; + } + return answer; +} + +// The implementation of the get-key-map word. +// Returns an array of 32 bytes. Certain bits +// are used to determine which keys are being +// held down. + +void getKeyMap(void) +{ + int keyPushed, *keyMapArray, offset; + const int sizeOfArray = 8; + const int modifierKeyIndex = 7; + const int commandKeyValue = pow(2,28); + const int shiftKeyValue = pow(2,30); + const int theAKeyValue = pow(2,27); + + keyMapArray = (int *) malloc(sizeOfArray); + if(!keyMapArray) // if failed to allocate memory + { + printk("Failed to allocate memory for keyMapArray!\n"); + PUSH(0); + return; + } + + // Set all the elements to zero + int index; + for(index = 0; index < sizeOfArray; index++) + { + keyMapArray[index] = 0; + } + + feval("key?"); + if(POP() != -1) // if no key was pushed + { + PUSH((int)keyMapArray); // returns the address of keyMapArray + return; + } + + feval("key"); + keyPushed = POP(); + + if(keyPushed > 0 && keyPushed < 27) // control key combination + { + offset = 1; + keyMapArray[modifierKeyIndex] = commandKeyValue; + } + + else if(keyPushed > 64 && keyPushed < 91) // shift key combination + { + offset = 65; + keyMapArray[modifierKeyIndex] = shiftKeyValue; + } + + else // just a letter is being held down + { + offset = 97; + } + + // Determines the value and location of a bit in the array. + *keyMapArray = theAKeyValue >> (keyPushed - offset); + + PUSH((int) keyMapArray); +} \ No newline at end of file