scoop:scoop.terminal:0.1.0
src/internal/input/mouse.scoop
Selected production source · src/internal/input/mouse.scoop
1
package scoop.terminal.internal.input3
import scoop.terminal.protocol.*4
import scoop.terminal.event.*5
import scoop.text.split7
internal fun decodeMouse(body: String, last: Char, rxvt: Boolean): InputItem {8
if (last != 'M' && last != 'm') invalidInput("invalid mouse report terminator")9
val fields = body.split(';')10
if (fields.size != 3L && !(fields.size == 4L && fields[3L] == "")) invalidInput("invalid mouse report fields")11
var button = decimal(fields[0L], 255L).toInt()12
if (rxvt) {13
if (button < 32) invalidInput("invalid rxvt button number")14
button -= 3215
}16
return mouseItem(button, coordinate(fields[1L]), coordinate(fields[2L]), last == 'm')17
}19
internal fun decodeLegacyMouse(bytes: List<Byte>): InputItem {20
val button = (bytes[0L].toInt() and 255) - 3221
val column = (bytes[1L].toInt() and 255) - 3322
val row = (bytes[2L].toInt() and 255) - 3323
if (button < 0 || column < 0 || row < 0) invalidInput("invalid traditional mouse report")24
return mouseItem(button, column.toLong(), row.toLong(), false)25
}27
private fun mouseItem(bits: Int, column: Long, row: Long, release: Boolean): InputItem {28
val number = (bits and 3) or ((bits and 192) shr 4L)29
val drag = (bits and 32) != 030
val kind = if (number <= 2) {31
val button = when (number) {32
case 0 -> MouseButton.Left; case 1 -> MouseButton.Middle; case _ -> MouseButton.Right33
}34
if (drag) MouseEventKind.Drag(button)35
else if (release) MouseEventKind.Up(button) else MouseEventKind.Down(button)36
} else if (number == 3) {37
if (drag) MouseEventKind.Moved else MouseEventKind.Up(MouseButton.Left)38
} else if (drag && (number == 4 || number == 5)) MouseEventKind.Moved39
else if (!drag) when (number) {40
case 4 -> MouseEventKind.ScrollUp; case 5 -> MouseEventKind.ScrollDown41
case 6 -> MouseEventKind.ScrollLeft; case 7 -> MouseEventKind.ScrollRight42
case _ -> invalidInput("unsupported mouse button")43
} else invalidInput("unsupported mouse motion button")44
var modifiers = KeyModifiers()45
if ((bits and 4) != 0) modifiers = modifiers.with(KeyModifier.Shift)46
if ((bits and 8) != 0) modifiers = modifiers.with(KeyModifier.Alt)47
if ((bits and 16) != 0) modifiers = modifiers.with(KeyModifier.Control)48
return InputItem.User(Event.Mouse(MouseEvent(kind, column, row, modifiers)))49
}