2019-10-10 12:28:46 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Kilo;
|
|
|
|
|
|
|
|
use FFI;
|
|
|
|
|
2019-10-10 15:49:01 -04:00
|
|
|
$ffi = FFI::load(__DIR__ . '/ffi.h');
|
|
|
|
$original_termios = $ffi->new('struct termios');
|
2019-10-10 12:28:46 -04:00
|
|
|
|
2019-10-24 10:58:38 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// ! Raw mode / Terminal size
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function enable_raw_mode(): void
|
2019-10-10 12:28:46 -04:00
|
|
|
{
|
|
|
|
global $ffi;
|
|
|
|
global $original_termios;
|
|
|
|
|
2019-10-24 10:58:38 -04:00
|
|
|
register_shutdown_function('Kilo\disable_raw_mode');
|
2019-10-11 16:32:47 -04:00
|
|
|
|
2019-10-10 12:28:46 -04:00
|
|
|
// Populate the original terminal settings
|
2019-10-14 16:21:41 -04:00
|
|
|
$res = $ffi->tcgetattr(STDIN_FILENO, FFI::addr($original_termios));
|
|
|
|
if ($res === -1)
|
|
|
|
{
|
|
|
|
die('tcgetattr');
|
|
|
|
}
|
2019-10-10 12:28:46 -04:00
|
|
|
|
2019-10-21 15:37:20 -04:00
|
|
|
// So, the only thing that seems to really matter here is that c_oflag is 0...
|
|
|
|
$termios = clone $original_termios;
|
2019-10-22 17:50:35 -04:00
|
|
|
$termios->c_iflag = 0; //$termios->c_iflag & ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
|
2019-10-21 15:37:20 -04:00
|
|
|
$termios->c_oflag = 0; // $termios->c_oflag && ~(OPOST);
|
2019-10-10 15:49:01 -04:00
|
|
|
$termios->c_cflag |= (CS8);
|
2019-10-21 15:37:20 -04:00
|
|
|
$termios->c_lflag = $termios->c_lflag & ~(_ECHO | ICANON | IEXTEN | ISIG);
|
2019-10-10 15:49:01 -04:00
|
|
|
$termios->c_cc[VMIN] = 0;
|
|
|
|
$termios->c_cc[VTIME] = 1;
|
2019-10-10 12:28:46 -04:00
|
|
|
|
|
|
|
// Turn on raw mode
|
2019-10-14 16:21:41 -04:00
|
|
|
$res = $ffi->tcsetattr(STDIN_FILENO, TCSAFLUSH, FFI::addr($termios));
|
|
|
|
if ($res === -1)
|
|
|
|
{
|
|
|
|
die('tcsetattr');
|
|
|
|
}
|
2019-10-10 12:28:46 -04:00
|
|
|
}
|
|
|
|
|
2019-10-24 10:58:38 -04:00
|
|
|
function disable_raw_mode(): void
|
2019-10-10 12:28:46 -04:00
|
|
|
{
|
|
|
|
global $ffi;
|
|
|
|
global $original_termios;
|
|
|
|
|
2019-10-14 16:21:41 -04:00
|
|
|
$res = $ffi->tcsetattr(STDIN_FILENO, TCSAFLUSH, FFI::addr($original_termios));
|
|
|
|
|
2019-10-18 16:20:34 -04:00
|
|
|
echo "\n";
|
|
|
|
|
2019-10-14 16:21:41 -04:00
|
|
|
if ($res === -1)
|
|
|
|
{
|
|
|
|
die('tcsetattr');
|
|
|
|
}
|
2019-10-10 12:28:46 -04:00
|
|
|
}
|
|
|
|
|
2019-10-24 10:58:38 -04:00
|
|
|
/**
|
|
|
|
* @TODO fix
|
|
|
|
*/
|
|
|
|
function get_cursor_position()
|
2019-10-10 15:49:01 -04:00
|
|
|
{
|
2019-10-24 10:58:38 -04:00
|
|
|
write_stdout("\x1b[999C\x1b[999B");
|
|
|
|
write_stdout("\x1b[6n");
|
2019-10-10 12:28:46 -04:00
|
|
|
|
2019-10-24 10:58:38 -04:00
|
|
|
$rows = 0;
|
|
|
|
$cols = 0;
|
2019-10-11 16:32:47 -04:00
|
|
|
|
2019-10-24 10:58:38 -04:00
|
|
|
$buffer = read_stdout();
|
2019-10-14 16:21:41 -04:00
|
|
|
|
2019-10-24 10:58:38 -04:00
|
|
|
$res = sscanf($buffer, '\x1b[%d;%dR', $rows, $cols);
|
2019-10-14 16:21:41 -04:00
|
|
|
|
2019-10-24 10:58:38 -04:00
|
|
|
if ($res === -1 || $buffer[0] !== '\x1b' || $buffer[1] !== '[')
|
|
|
|
{
|
|
|
|
die('Failed to get screen size');
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$rows, $cols];
|
2019-10-14 16:21:41 -04:00
|
|
|
}
|
|
|
|
|
2019-10-24 10:58:38 -04:00
|
|
|
function get_window_size()
|
2019-10-14 16:21:41 -04:00
|
|
|
{
|
2019-10-24 10:58:38 -04:00
|
|
|
global $ffi;
|
2019-10-14 16:21:41 -04:00
|
|
|
|
2019-10-24 10:58:38 -04:00
|
|
|
$ws = $ffi->new('struct winsize');
|
|
|
|
$res = $ffi->ioctl(STDOUT_FILENO, TIOCGWINSZ, FFI::addr($ws));
|
2019-10-14 16:21:41 -04:00
|
|
|
|
2019-10-24 10:58:38 -04:00
|
|
|
if ($res === -1 || $ws->ws_col === 0)
|
|
|
|
{
|
|
|
|
return get_cursor_position();
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$ws->ws_row, $ws->ws_col];
|
2019-10-14 16:21:41 -04:00
|
|
|
}
|
|
|
|
|
2019-10-24 10:58:38 -04:00
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// ! C function/macro equivalents
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2019-10-22 16:16:28 -04:00
|
|
|
/**
|
|
|
|
* Do bit twiddling to convert a letter into
|
2019-10-24 10:58:38 -04:00
|
|
|
* its Ctrl-letter equivalent ordinal ascii value
|
2019-10-22 16:16:28 -04:00
|
|
|
*
|
|
|
|
* @param string $char
|
|
|
|
* @return int
|
|
|
|
*/
|
2019-10-11 16:32:47 -04:00
|
|
|
function ctrl_key(string $char): int
|
|
|
|
{
|
2019-10-24 10:58:38 -04:00
|
|
|
if ( ! is_ascii($char))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-10-22 16:16:28 -04:00
|
|
|
// b1,100,001 (a) & b0,011,111 = b0,000,001
|
|
|
|
// b1,100,010 (b) & b0,011,111 = b0,000,010
|
2019-10-11 16:32:47 -04:00
|
|
|
return ord($char) & 0x1f;
|
2019-10-22 17:50:35 -04:00
|
|
|
}
|
2019-10-23 13:34:40 -04:00
|
|
|
|
2019-10-24 10:58:38 -04:00
|
|
|
function is_ascii(string $single_char): bool
|
2019-10-23 13:34:40 -04:00
|
|
|
{
|
|
|
|
if (strlen($single_char) > 1)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ord($single_char) < 0x80;
|
|
|
|
}
|
|
|
|
|
2019-10-24 10:58:38 -04:00
|
|
|
function is_cntrl(string $char): bool
|
2019-10-23 13:34:40 -04:00
|
|
|
{
|
|
|
|
$c = ord($char);
|
2019-10-24 10:58:38 -04:00
|
|
|
return is_ascii($char) && ( $c < 0x20 || $c === 0x7f );
|
2019-10-23 13:34:40 -04:00
|
|
|
}
|
|
|
|
|
2019-10-24 10:58:38 -04:00
|
|
|
function is_digit(string $char): bool
|
2019-10-23 13:34:40 -04:00
|
|
|
{
|
|
|
|
$c = ord($char);
|
2019-10-24 10:58:38 -04:00
|
|
|
return is_ascii($char) && ( $c > 0x2f && $c < 0x3a );
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// ! Helper functions
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function read_stdin(int $len = 128): string
|
|
|
|
{
|
|
|
|
$handle = fopen('php://stdin', 'rb');
|
|
|
|
$input = fread($handle, $len);
|
|
|
|
fclose($handle);
|
|
|
|
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
|
|
|
function write_stdout(string $str, int $len = NULL): int
|
|
|
|
{
|
|
|
|
$handle = fopen('php://stdout', 'ab');
|
|
|
|
$res = (is_int($len))
|
|
|
|
? fwrite($handle, $str, $len)
|
|
|
|
: fwrite($handle, $str);
|
|
|
|
|
|
|
|
fclose($handle);
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function read_stdout(int $len = 128): string
|
|
|
|
{
|
|
|
|
$handle = fopen('php://stdout', 'rb');
|
|
|
|
$input = fread($handle, $len);
|
|
|
|
|
|
|
|
$input = rtrim($input);
|
|
|
|
fclose($handle);
|
|
|
|
|
|
|
|
return $input;
|
2019-10-23 13:34:40 -04:00
|
|
|
}
|