Updated constants to match C declarations

This commit is contained in:
R. Eric Wheeler 2021-03-01 10:46:07 -08:00
parent ba09e1d356
commit aa72be226d
3 changed files with 48 additions and 53 deletions

View File

@ -20,29 +20,34 @@ use RuntimeException;
final class Getch final class Getch
{ {
// Special key codes // Special key codes
public const GETCH_SPECIAL = 0; // Extended scan code used to indicate special keyboard functions
public const GETCH_F1 = 59; public const KEY_RESERVED = 0;
public const GETCH_F2 = 60; public const KEY_E0 = 0;
public const GETCH_F3 = 61; public const KEY_E1 = 224;
public const GETCH_F4 = 62;
public const GETCH_F5 = 63; // Supported scan scodes.
public const GETCH_F6 = 64; public const KEY_F1 = 59;
public const GETCH_F7 = 65; public const KEY_F2 = 60;
public const GETCH_F8 = 66; public const KEY_F3 = 61;
public const GETCH_F9 = 67; public const KEY_F4 = 62;
public const GETCH_F10 = 68; public const KEY_F5 = 63;
public const GETCH_F11 = 87; public const KEY_F6 = 64;
public const GETCH_F12 = 88; public const KEY_F7 = 65;
public const GETCH_UP_ARROW = 72; public const KEY_F8 = 66;
public const GETCH_LEFT_ARROW = 75; public const KEY_F9 = 67;
public const GETCH_RIGHT_ARROW = 77; public const KEY_F10 = 68;
public const GETCH_DOWN_ARROW = 80; public const KEY_F11 = 87;
public const GETCH_DELETE = 83; public const KEY_F12 = 88;
public const GETCH_HOME = 102; public const KEY_UP = 72;
public const GETCH_PGUP = 104; public const KEY_LEFT = 75;
public const GETCH_END = 107; public const KEY_RIGHT = 77;
public const GETCH_PGDN = 109; public const KEY_DOWN = 80;
public const GETCH_INSERT = 110; public const KEY_DELETE = 83;
public const KEY_HOME = 71;
public const KEY_PAGEUP = 73;
public const KEY_END = 79;
public const KEY_PAGEDOWN = 81;
public const KEY_INSERT = 82;
private const LINUX_LIBRARY = __DIR__.'/Resources/libgetch.so'; private const LINUX_LIBRARY = __DIR__.'/Resources/libgetch.so';
private const WINDOWS_LIBRARY = 'ucrtbase.dll'; private const WINDOWS_LIBRARY = 'ucrtbase.dll';

View File

@ -13,10 +13,16 @@
#include <linux/input.h> #include <linux/input.h>
#include <glob.h> #include <glob.h>
#define EVENT_DEVICE_GLOB "/dev/input/by-path/*-event-kbd"
#define FKEY(k) ((k) >= KEY_F1 && (k) <= KEY_F10) || (k) == KEY_F12 || (k) == KEY_F11 #define FKEY(k) ((k) >= KEY_F1 && (k) <= KEY_F10) || (k) == KEY_F12 || (k) == KEY_F11
#define NOTNUMPAD(k) ((k) >= KEY_HOME && (k) <= KEY_DELETE) #define NOTNUMPAD(k) ((k) >= KEY_HOME && (k) <= KEY_DELETE)
#define XOR_SWAP(a,b) do\
#define EVENT_DEVICE_GLOB "/dev/input/by-path/*-event-kbd" {\
(a) ^= (b);\
(b) ^= (a);\
(a) ^= (b);\
} while (0)
static struct termios oldTermAttributes; static struct termios oldTermAttributes;
@ -26,24 +32,12 @@ inline static void reverseString(char * str)
{ {
char * end = str + strlen(str) - 1; char * end = str + strlen(str) - 1;
// swap the values in the two given variables
// XXX: fails when a and b refer to same memory location
# define XOR_SWAP(a,b) do\
{\
(a) ^= (b);\
(b) ^= (a);\
(a) ^= (b);\
} while (0)
// walk inwards from both ends of the string,
// swapping until we get to the middle
while (str < end) while (str < end)
{ {
XOR_SWAP(*str, *end); XOR_SWAP(*str, *end);
str++; str++;
end--; end--;
} }
# undef XOR_SWAP
} }
} }
@ -103,7 +97,7 @@ static int getEventDevice(char ** device)
static unsigned short getScanCode() static unsigned short getScanCode()
{ {
struct input_event inputEvent[40]; struct input_event inputEvent[5];
int eventDevice; int eventDevice;
char *device; char *device;
@ -129,12 +123,7 @@ static unsigned short getScanCode()
close(eventDevice); close(eventDevice);
// for(int i = 0;i<39;i++) { for(int i = 0;i<5;i++) {
// printf("Type %d, Code %d, Value %d\r\n", inputEvent[i].type, inputEvent[i].code, inputEvent[i].value);
// }
for(int i = 0;i<40;i++) {
//printf("Type %d, Code %d\n", inputEvent[i].type, inputEvent[i].code);
if(inputEvent[i].type == EV_KEY && inputEvent[i].code != KEY_ENTER) { if(inputEvent[i].type == EV_KEY && inputEvent[i].code != KEY_ENTER) {
return inputEvent[i].code; return inputEvent[i].code;
} }
@ -212,17 +201,12 @@ static int readKey(void) {
case KEY_KP6: // RIGHT case KEY_KP6: // RIGHT
case KEY_KP7: // HOME case KEY_KP7: // HOME
case KEY_KP8: // UP case KEY_KP8: // UP
//getchar();
//getchar();
discardBytes = 2; discardBytes = 2;
break; break;
case KEY_KP0: // INSERT case KEY_KP0: // INSERT
case KEY_KPDOT: // DELETE case KEY_KPDOT: // DELETE
case KEY_KP9: // PAGEUP case KEY_KP9: // PAGEUP
case KEY_KP3: // PAGEDOWN case KEY_KP3: // PAGEDOWN
/*getchar();
getchar();
getchar(); */
discardBytes = 3; discardBytes = 3;
break; break;
case KEY_F5: case KEY_F5:
@ -233,10 +217,6 @@ static int readKey(void) {
case KEY_F10: case KEY_F10:
case KEY_F11: case KEY_F11:
case KEY_F12: case KEY_F12:
/* getchar();
getchar();
getchar();
getchar(); */
discardBytes = 4; discardBytes = 4;
break; break;
@ -269,11 +249,21 @@ int _getch(void) {
return key; return key;
} }
int getch(void)
{
return _getch();
}
int _ungetch(int ch) int _ungetch(int ch)
{ {
return ungetc(ch, stdin); return ungetc(ch, stdin);
} }
int ungetch(int ch)
{
return _ungetch(ch);
}
int *cinPeekCount(ushort count) int *cinPeekCount(ushort count)
{ {
char buffer[count]; char buffer[count];

Binary file not shown.