From aa72be226d31406ed969fa87411659133da3e3c0 Mon Sep 17 00:00:00 2001 From: "R. Eric Wheeler" Date: Mon, 1 Mar 2021 10:46:07 -0800 Subject: [PATCH] Updated constants to match C declarations --- src/Console/Getch.php | 51 ++++++++++++++++-------------- src/Console/Resources/getch.c | 50 ++++++++++++----------------- src/Console/Resources/libgetch.so | Bin 17032 -> 17536 bytes 3 files changed, 48 insertions(+), 53 deletions(-) diff --git a/src/Console/Getch.php b/src/Console/Getch.php index dbcc7d8..240f9e1 100644 --- a/src/Console/Getch.php +++ b/src/Console/Getch.php @@ -20,29 +20,34 @@ use RuntimeException; final class Getch { // Special key codes - public const GETCH_SPECIAL = 0; - public const GETCH_F1 = 59; - public const GETCH_F2 = 60; - public const GETCH_F3 = 61; - public const GETCH_F4 = 62; - public const GETCH_F5 = 63; - public const GETCH_F6 = 64; - public const GETCH_F7 = 65; - public const GETCH_F8 = 66; - public const GETCH_F9 = 67; - public const GETCH_F10 = 68; - public const GETCH_F11 = 87; - public const GETCH_F12 = 88; - public const GETCH_UP_ARROW = 72; - public const GETCH_LEFT_ARROW = 75; - public const GETCH_RIGHT_ARROW = 77; - public const GETCH_DOWN_ARROW = 80; - public const GETCH_DELETE = 83; - public const GETCH_HOME = 102; - public const GETCH_PGUP = 104; - public const GETCH_END = 107; - public const GETCH_PGDN = 109; - public const GETCH_INSERT = 110; + // Extended scan code used to indicate special keyboard functions + public const KEY_RESERVED = 0; + public const KEY_E0 = 0; + public const KEY_E1 = 224; + + // Supported scan scodes. + public const KEY_F1 = 59; + public const KEY_F2 = 60; + public const KEY_F3 = 61; + public const KEY_F4 = 62; + public const KEY_F5 = 63; + public const KEY_F6 = 64; + public const KEY_F7 = 65; + public const KEY_F8 = 66; + public const KEY_F9 = 67; + public const KEY_F10 = 68; + public const KEY_F11 = 87; + public const KEY_F12 = 88; + public const KEY_UP = 72; + public const KEY_LEFT = 75; + public const KEY_RIGHT = 77; + public const KEY_DOWN = 80; + 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 WINDOWS_LIBRARY = 'ucrtbase.dll'; diff --git a/src/Console/Resources/getch.c b/src/Console/Resources/getch.c index b39122a..398b063 100644 --- a/src/Console/Resources/getch.c +++ b/src/Console/Resources/getch.c @@ -13,10 +13,16 @@ #include #include +#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 NOTNUMPAD(k) ((k) >= KEY_HOME && (k) <= KEY_DELETE) - -#define EVENT_DEVICE_GLOB "/dev/input/by-path/*-event-kbd" +#define XOR_SWAP(a,b) do\ + {\ + (a) ^= (b);\ + (b) ^= (a);\ + (a) ^= (b);\ + } while (0) static struct termios oldTermAttributes; @@ -26,24 +32,12 @@ inline static void reverseString(char * str) { 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) { XOR_SWAP(*str, *end); str++; end--; } -# undef XOR_SWAP } } @@ -103,7 +97,7 @@ static int getEventDevice(char ** device) static unsigned short getScanCode() { - struct input_event inputEvent[40]; + struct input_event inputEvent[5]; int eventDevice; char *device; @@ -129,12 +123,7 @@ static unsigned short getScanCode() close(eventDevice); - // for(int i = 0;i<39;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); + for(int i = 0;i<5;i++) { if(inputEvent[i].type == EV_KEY && inputEvent[i].code != KEY_ENTER) { return inputEvent[i].code; } @@ -212,17 +201,12 @@ static int readKey(void) { case KEY_KP6: // RIGHT case KEY_KP7: // HOME case KEY_KP8: // UP - //getchar(); - //getchar(); discardBytes = 2; break; case KEY_KP0: // INSERT case KEY_KPDOT: // DELETE case KEY_KP9: // PAGEUP case KEY_KP3: // PAGEDOWN - /*getchar(); - getchar(); - getchar(); */ discardBytes = 3; break; case KEY_F5: @@ -233,10 +217,6 @@ static int readKey(void) { case KEY_F10: case KEY_F11: case KEY_F12: - /* getchar(); - getchar(); - getchar(); - getchar(); */ discardBytes = 4; break; @@ -269,11 +249,21 @@ int _getch(void) { return key; } +int getch(void) +{ + return _getch(); +} + int _ungetch(int ch) { return ungetc(ch, stdin); } +int ungetch(int ch) +{ + return _ungetch(ch); +} + int *cinPeekCount(ushort count) { char buffer[count]; diff --git a/src/Console/Resources/libgetch.so b/src/Console/Resources/libgetch.so index 0556d8526aff1e03b0bea98d1a5876d1955af808..ef879489ce5da81b569d395a90ff0799c57fdb14 100755 GIT binary patch delta 4155 zcmZu!4Nz3q6~1?2VHf#ZV0p`*upkO(z{LottSr_gFP%{bQ>SEtS&^T_M1I7jqcyr1 zf^la<3!Z7JEw+-_cFe>Mm6>W=v8YLmQ`2BOwxmY8rcJYqn1a!SD)M^nefJUEcxUc8 z_kQQQ=brof?%Ur<+r6~g64!4cJULWFhU`6gMG=MwwQ7jyc_BX{H&U(TM)tgsZ}f+# zHGj`X1#}pUwAt7z8u!9p+B4r^usEEK6vR>m-j(+K{(t|c;oY^B_r?yy6fGYwX^$CL ztUIVnx}Bs_fU0^LRUDZ^I^jmB^yFS)T`2sF3hPyYO9KBy;BO0jj=%!~KRIC1_s|eZ zDEuPqDO-1(YL8v1YJ;FV1U~F|7#|+U4PQcO8f-L3P;H2e+9U|!$eROQh`xqs%Pces z1`$1tX!|TQ7H%OrhG_RJGy~F%Qk3W$JrZ&ROhyT+jFC|ZysnCVC`WWZqKUK6A=qY= z==%d+V@%kI*q5nic>y8Mn~^cvJ*_B8b|I^?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