Scoop API

scoop:scoop.terminal:0.1.0

src/internal/input/mouse.scoop

Selected production source · src/internal/input/mouse.scoop

1package scoop.terminal.internal.input
3import scoop.terminal.protocol.*
4import scoop.terminal.event.*
5import scoop.text.split
7internal 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 -= 32
15 }
16 return mouseItem(button, coordinate(fields[1L]), coordinate(fields[2L]), last == 'm')
19internal fun decodeLegacyMouse(bytes: List<Byte>): InputItem {
20 val button = (bytes[0L].toInt() and 255) - 32
21 val column = (bytes[1L].toInt() and 255) - 33
22 val row = (bytes[2L].toInt() and 255) - 33
23 if (button < 0 || column < 0 || row < 0) invalidInput("invalid traditional mouse report")
24 return mouseItem(button, column.toLong(), row.toLong(), false)
27private 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) != 0
30 val kind = if (number <= 2) {
31 val button = when (number) {
32 case 0 -> MouseButton.Left; case 1 -> MouseButton.Middle; case _ -> MouseButton.Right
33 }
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.Moved
39 else if (!drag) when (number) {
40 case 4 -> MouseEventKind.ScrollUp; case 5 -> MouseEventKind.ScrollDown
41 case 6 -> MouseEventKind.ScrollLeft; case 7 -> MouseEventKind.ScrollRight
42 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)))