diff --git a/README.md b/README.md index 0b1e51e..882750b 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ $ composer require sikofitt/getch:dev-master ```php use Sikofitt\Console\Getch; - $g = new Getch($linuxLibrary = null); // can also be a library that implements a function called _getwch; - // by default uses the bundled Resources/libgetwch.so - // on windows uses the built in _getwch function. + $g = new Getch($linuxLibrary = null); // can also be a library that implements a function called _getch; + // by default uses the bundled Resources/libgetch.so + // on windows uses the built in _getch function. $char = $g->getch(); print $char; ``` diff --git a/src/Console/Getch.php b/src/Console/Getch.php index ddb6e3b..a4b3b19 100644 --- a/src/Console/Getch.php +++ b/src/Console/Getch.php @@ -17,7 +17,7 @@ use RuntimeException; final class Getch { - private const LINUX_LIBRARY = __DIR__ . '/Resources/libgetwch.so'; + private const LINUX_LIBRARY = __DIR__ . '/Resources/libgetch.so'; private const WINDOWS_LIBRARY = 'ucrtbase.dll'; private static ?FFI $ffi = null; @@ -31,13 +31,13 @@ final class Getch if (self::$ffi === null) { $osFamily = PHP_OS_FAMILY; if ($osFamily === 'Windows') { - self::$ffi = FFI::cdef('char _getwch();', self::WINDOWS_LIBRARY); + self::$ffi = FFI::cdef('char _getch();', self::WINDOWS_LIBRARY); } elseif ($osFamily === 'Linux') { if (!file_exists($linuxLibrary)) { throw new RuntimeException(sprintf('Could not find library file %s.', $linuxLibrary)); } - self::$ffi = FFI::cdef('char _getwch();', $linuxLibrary); + self::$ffi = FFI::cdef('char _getch(); int _ungetch(int ch);', $linuxLibrary); } else { throw new RuntimeException(sprintf('Sorry, %s is not supported yet.', $osFamily)); } @@ -46,6 +46,11 @@ final class Getch public function getch(): string { - return self::$ffi->_getwch(); + return self::$ffi->_getch(); + } + + public function ungetch(string $char) + { + return self::$ffi->_ungetch($char); } } diff --git a/src/Console/Resources/Makefile b/src/Console/Resources/Makefile index 189e7d6..fa3a278 100644 --- a/src/Console/Resources/Makefile +++ b/src/Console/Resources/Makefile @@ -2,8 +2,8 @@ CC = gcc CFLAGS = -shared -Wall -fPIC all: - @${CC} ${CFLAGS} getwch.c -o libgetwch.so + @${CC} ${CFLAGS} getch.c -o libgetch.so clean: - @rm -f libgetwch.so + @rm -f libgetch.so diff --git a/src/Console/Resources/getwch.c b/src/Console/Resources/getch.c similarity index 84% rename from src/Console/Resources/getwch.c rename to src/Console/Resources/getch.c index c495867..5f2e769 100644 --- a/src/Console/Resources/getwch.c +++ b/src/Console/Resources/getch.c @@ -3,7 +3,7 @@ #include /* reads from keypress, doesn't echo */ -int _getwch(void) +int _getch(void) { struct termios oldattr, newattr; int ch; @@ -14,4 +14,9 @@ int _getwch(void) ch = getchar(); tcsetattr( STDIN_FILENO, TCSANOW, &oldattr ); return ch; +} + +int _ungetch(int ch) +{ + return ungetc(ch, stdin); } \ No newline at end of file diff --git a/src/Console/Resources/libgetwch.so b/src/Console/Resources/libgetch.so similarity index 59% rename from src/Console/Resources/libgetwch.so rename to src/Console/Resources/libgetch.so index dea1f53..a6bc39f 100755 Binary files a/src/Console/Resources/libgetwch.so and b/src/Console/Resources/libgetch.so differ