new('struct termios'); function enableRawMode(): void { global $ffi; global $original_termios; register_shutdown_function('Kilo\\disableRawMode'); // Populate the original terminal settings $ffi->tcgetattr(STDIN_FILENO, FFI::addr($original_termios)); $termios = $ffi->new('struct termios'); $termios->c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); $termios->c_oflag &= ~(OPOST); $termios->c_cflag |= (CS8); $termios->c_lflag &= ~(_ECHO | ICANON | IEXTEN | ISIG); $termios->c_cc[VMIN] = 0; $termios->c_cc[VTIME] = 1; // Turn on raw mode $ffi->tcsetattr(STDIN_FILENO, TCSAFLUSH, FFI::addr($termios)); } function disableRawMode(): void { global $ffi; global $original_termios; $ffi->tcsetattr(STDIN_FILENO, TCSAFLUSH, FFI::addr($original_termios)); } function read_stdin(int $len = 128): string { $handle = fopen('php://stdin', 'rb'); $input = fread($handle, $len); $input = rtrim($input); fclose($handle); return $input; } function ctrl_key(string $char): int { return ord($char) & 0x1f; }