Skip to content

Commit

Permalink
Fix crash on windows raw mode due to unexpected event
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Jul 6, 2024
1 parent 75bc2a4 commit 24d4ff2
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased
### Fixed
- Fix markdown rendering not supporting math blocks [(#182)](https://github.com/ajalt/mordant/issues/182)
- Fix exception thrown when using `readEvent` in raw mode when some windows terminals lose focus

## 2.7.0
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ internal object SyscallHandlerJnaWindows : SyscallHandlerWindows() {
kernel.SetConsoleMode(stdinHandle, dwMode.toInt())
}

override fun readRawEvent(dwMilliseconds: Int): EventRecord {
override fun readRawEvent(dwMilliseconds: Int): EventRecord? {
val waitResult = kernel.WaitForSingleObject(stdinHandle.pointer, dwMilliseconds)
if (waitResult != 0) {
throw RuntimeException("Timeout reading from console input")
Expand Down Expand Up @@ -309,9 +309,7 @@ internal object SyscallHandlerJnaWindows : SyscallHandlerWindows() {
)
}

else -> throw RuntimeException(
"Error reading from console input: unexpected event type ${inputEvent.EventType}"
)
else -> null // Ignore other event types like FOCUS_EVENT that we can't opt out of
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ internal class SyscallHandlerNativeImageWindows : SyscallHandlerWindows() {
// }
}

override fun readRawEvent(dwMilliseconds: Int): EventRecord {
override fun readRawEvent(dwMilliseconds: Int): EventRecord? {
throw NotImplementedError(
"Raw mode is not currently supported for native-image. If you are familiar with " +
"GraalVM native-image and would like to contribute, see the commented out " +
Expand Down Expand Up @@ -249,9 +249,7 @@ internal class SyscallHandlerNativeImageWindows : SyscallHandlerWindows() {
// )
// }
//
// else -> throw RuntimeException(
// "Error reading from console input: unexpected event type ${inputEvents.EventType}"
// )
// else -> null // Ignore other event types like FOCUS_EVENT that we can't opt out of
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal object SyscallHandlerNativeWindows : SyscallHandlerWindows() {
csbi.srWindow.run { Size(width = Right - Left + 1, height = Bottom - Top + 1) }
}

override fun readRawEvent(dwMilliseconds: Int): EventRecord = memScoped {
override fun readRawEvent(dwMilliseconds: Int): EventRecord? = memScoped {
val stdinHandle = GetStdHandle(STD_INPUT_HANDLE)
val waitResult = WaitForSingleObject(stdinHandle, dwMilliseconds.toUInt())
if (waitResult != 0u) {
Expand Down Expand Up @@ -67,9 +67,7 @@ internal object SyscallHandlerNativeWindows : SyscallHandlerWindows() {
)
}

else -> throw RuntimeException(
"Error reading from console input: unexpected event type ${inputEvent.EventType}"
)
else -> null // Ignore other event types like FOCUS_EVENT that we can't opt out of
}
}

Expand All @@ -80,7 +78,7 @@ internal object SyscallHandlerNativeWindows : SyscallHandlerWindows() {

override fun setStdinConsoleMode(dwMode: UInt) {
val stdinHandle = GetStdHandle(STD_INPUT_HANDLE)
if(SetConsoleMode(stdinHandle, 0u) == 0) {
if (SetConsoleMode(stdinHandle, 0u) == 0) {
throw RuntimeException("Error setting console mode")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal abstract class SyscallHandlerWindows : SyscallHandler {
) : EventRecord()
}

protected abstract fun readRawEvent(dwMilliseconds: Int): EventRecord
protected abstract fun readRawEvent(dwMilliseconds: Int): EventRecord?
protected abstract fun getStdinConsoleMode(): UInt
protected abstract fun setStdinConsoleMode(dwMode: UInt)

Expand All @@ -75,6 +75,7 @@ internal abstract class SyscallHandlerWindows : SyscallHandler {
val dwMilliseconds = (timeout - t0.elapsedNow()).inWholeMilliseconds
.coerceIn(0, Int.MAX_VALUE.toLong()).toInt()
return when (val event = readRawEvent(dwMilliseconds)) {
null -> SysInputEvent.Retry
is EventRecord.Key -> processKeyEvent(event)
is EventRecord.Mouse -> processMouseEvent(event, mouseTracking)
}
Expand Down

0 comments on commit 24d4ff2

Please sign in to comment.