Skip to content

Commit

Permalink
windows: add console input handling api
Browse files Browse the repository at this point in the history
Add Console API input handling syscalls. This is an effort to upstream
the amazing coninput library by @erikgeiser
https://github.com/erikgeiser/coninput
  • Loading branch information
aymanbagabas committed May 14, 2024
1 parent 6943ab6 commit 44194b0
Show file tree
Hide file tree
Showing 3 changed files with 498 additions and 0 deletions.
45 changes: 45 additions & 0 deletions windows/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ func NewCallbackCDecl(fn interface{}) uintptr {
//sys WriteConsole(console Handle, buf *uint16, towrite uint32, written *uint32, reserved *byte) (err error) = kernel32.WriteConsoleW
//sys ReadConsole(console Handle, buf *uint16, toread uint32, read *uint32, inputControl *byte) (err error) = kernel32.ReadConsoleW
//sys resizePseudoConsole(pconsole Handle, size uint32) (hr error) = kernel32.ResizePseudoConsole
//sys readConsoleInputW(console Handle, buf *InputRecord, toread uint32, read *uint32) (err error) = kernel32.ReadConsoleInputW
//sys peekConsoleInputW(console Handle, buf *InputRecord, toread uint32, read *uint32) (err error) = kernel32.PeekConsoleInputW
//sys getNumberOfConsoleInputEvents(console Handle, numevents *uint32) (err error) = kernel32.GetNumberOfConsoleInputEvents
//sys FlushConsoleInputBuffer(console Handle) (err error) = kernel32.FlushConsoleInputBuffer
//sys CreateToolhelp32Snapshot(flags uint32, processId uint32) (handle Handle, err error) [failretval==InvalidHandle] = kernel32.CreateToolhelp32Snapshot
//sys Module32First(snapshot Handle, moduleEntry *ModuleEntry32) (err error) = kernel32.Module32FirstW
//sys Module32Next(snapshot Handle, moduleEntry *ModuleEntry32) (err error) = kernel32.Module32NextW
Expand Down Expand Up @@ -1916,3 +1920,44 @@ const (
EV_ERR = 0x0080
EV_RING = 0x0100
)

// ReadConsoleInputW provides an ideomatic interface to the Windows console API
// function ReadConsoleInputW (see
// https://docs.microsoft.com/en-us/windows/console/readconsoleinput). The size
// of inputRecords must be greater than zero.
func ReadConsoleInputW(console Handle, inputRecords []InputRecord) (uint32, error) {
if len(inputRecords) == 0 {
return 0, fmt.Errorf("size of input record buffer cannot be zero")
}

var read uint32
err := readConsoleInputW(console, &inputRecords[0], uint32(len(inputRecords)), &read)

return read, err
}

// PeekConsoleInputW provides an ideomatic interface to the Windows console API
// function PeekConsoleInputW (see
// https://docs.microsoft.com/en-us/windows/console/peekconsoleinput). The size
// of inputRecords must be greater than zero.
func PeekConsoleInputW(console Handle, inputRecords []InputRecord) (uint32, error) {
if len(inputRecords) == 0 {
return 0, fmt.Errorf("size of input record buffer cannot be zero")
}

var read uint32

err := peekConsoleInputW(console, &inputRecords[0], uint32(len(inputRecords)), &read)

return read, err
}

// GetNumberOfConsoleInputEvents provides an ideomatic interface to the Windows
// console API function GetNumberOfConsoleInputEvents (see
// https://docs.microsoft.com/en-us/windows/console/getnumberofconsoleinputevents).
func GetNumberOfConsoleInputEvents(console Handle) (uint32, error) {
var nEvents uint32
err := getNumberOfConsoleInputEvents(console, &nEvents)

return nEvents, err
}
Loading

0 comments on commit 44194b0

Please sign in to comment.