2023-10-27 16:02:54 -04:00
|
|
|
// Bun-specific ffi code
|
2023-10-31 17:38:15 -04:00
|
|
|
import { dlopen, ptr, FFIType, suffix, toArrayBuffer } from "bun:ffi";
|
|
|
|
import {STDIN_FILENO, TCSANOW} from "../common/ffi";
|
|
|
|
|
|
|
|
const termiosArray = new Uint8Array(60);
|
|
|
|
|
|
|
|
const getLib = (name: string) => {
|
|
|
|
return dlopen(
|
|
|
|
name,
|
|
|
|
{
|
|
|
|
tcgetattr: {
|
|
|
|
args: ['i32', 'pointer'],
|
|
|
|
returns: 'i32',
|
|
|
|
},
|
|
|
|
tcsetattr: {
|
|
|
|
args: ['i32', 'i32', 'pointer'],
|
|
|
|
returns: 'i32',
|
|
|
|
},
|
|
|
|
cfmakeraw: {
|
|
|
|
args: ['pointer'],
|
|
|
|
returns: 'void',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let cStdLib = {symbols: {}};
|
|
|
|
|
|
|
|
try {
|
|
|
|
cStdLib = getLib(`libc.${suffix}`);
|
|
|
|
} catch {
|
|
|
|
cStdLib = getLib(`libc.${suffix}.6`);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const enableRawMode = () => {
|
|
|
|
// Get the current termios settings
|
|
|
|
const termiosPtr = ptr(termiosArray);
|
|
|
|
cStdLib.symbols.tcgetattr(STDIN_FILENO, termiosPtr);
|
|
|
|
|
|
|
|
const oldTermios = new Uint8Array(toArrayBuffer(termiosPtr, 0, 60), 0, 60);
|
|
|
|
console.info(oldTermios);
|
|
|
|
|
|
|
|
// Update termios struct with raw settings
|
|
|
|
cStdLib.symbols.cfmakeraw(termiosPtr);
|
|
|
|
|
|
|
|
// Actually set the new termios settings
|
|
|
|
cStdLib.symbols.tcsetattr(STDIN_FILENO, TCSANOW, termiosPtr);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
const oldTermiosPtr = ptr(oldTermios);
|
|
|
|
|
|
|
|
cStdLib.symbols.tcsetattr(STDIN_FILENO, TCSANOW, oldTermiosPtr);
|
|
|
|
}
|
|
|
|
}
|