| 1 |
package input |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
"github.com/charmbracelet/x/ansi" |
| 7 |
) |
| 8 |
|
| 9 |
// MouseButton represents the button that was pressed during a mouse message. |
| 10 |
type MouseButton = ansi.MouseButton |
| 11 |
|
| 12 |
// Mouse event buttons |
| 13 |
// |
| 14 |
// This is based on X11 mouse button codes. |
| 15 |
// |
| 16 |
// 1 = left button |
| 17 |
// 2 = middle button (pressing the scroll wheel) |
| 18 |
// 3 = right button |
| 19 |
// 4 = turn scroll wheel up |
| 20 |
// 5 = turn scroll wheel down |
| 21 |
// 6 = push scroll wheel left |
| 22 |
// 7 = push scroll wheel right |
| 23 |
// 8 = 4th button (aka browser backward button) |
| 24 |
// 9 = 5th button (aka browser forward button) |
| 25 |
// 10 |
| 26 |
// 11 |
| 27 |
// |
| 28 |
// Other buttons are not supported. |
| 29 |
const ( |
| 30 |
MouseNone = ansi.MouseNone |
| 31 |
MouseLeft = ansi.MouseLeft |
| 32 |
MouseMiddle = ansi.MouseMiddle |
| 33 |
MouseRight = ansi.MouseRight |
| 34 |
MouseWheelUp = ansi.MouseWheelUp |
| 35 |
MouseWheelDown = ansi.MouseWheelDown |
| 36 |
MouseWheelLeft = ansi.MouseWheelLeft |
| 37 |
MouseWheelRight = ansi.MouseWheelRight |
| 38 |
MouseBackward = ansi.MouseBackward |
| 39 |
MouseForward = ansi.MouseForward |
| 40 |
MouseButton10 = ansi.MouseButton10 |
| 41 |
MouseButton11 = ansi.MouseButton11 |
| 42 |
) |
| 43 |
|
| 44 |
// MouseEvent represents a mouse message. This is a generic mouse message that |
| 45 |
// can represent any kind of mouse event. |
| 46 |
type MouseEvent interface { |
| 47 |
fmt.Stringer |
| 48 |
|
| 49 |
// Mouse returns the underlying mouse event. |
| 50 |
Mouse() Mouse |
| 51 |
} |
| 52 |
|
| 53 |
// Mouse represents a Mouse message. Use [MouseEvent] to represent all mouse |
| 54 |
// messages. |
| 55 |
// |
| 56 |
// The X and Y coordinates are zero-based, with (0,0) being the upper left |
| 57 |
// corner of the terminal. |
| 58 |
// |
| 59 |
// // Catch all mouse events |
| 60 |
// switch Event := Event.(type) { |
| 61 |
// case MouseEvent: |
| 62 |
// m := Event.Mouse() |
| 63 |
// fmt.Println("Mouse event:", m.X, m.Y, m) |
| 64 |
// } |
| 65 |
// |
| 66 |
// // Only catch mouse click events |
| 67 |
// switch Event := Event.(type) { |
| 68 |
// case MouseClickEvent: |
| 69 |
// fmt.Println("Mouse click event:", Event.X, Event.Y, Event) |
| 70 |
// } |
| 71 |
type Mouse struct { |
| 72 |
X, Y int |
| 73 |
Button MouseButton |
| 74 |
Mod KeyMod |
| 75 |
} |
| 76 |
|
| 77 |
// String returns a string representation of the mouse message. |
| 78 |
func (m Mouse) String() (s string) { |
| 79 |
if m.Mod.Contains(ModCtrl) { |
| 80 |
s += "ctrl+" |
| 81 |
} |
| 82 |
if m.Mod.Contains(ModAlt) { |
| 83 |
s += "alt+" |
| 84 |
} |
| 85 |
if m.Mod.Contains(ModShift) { |
| 86 |
s += "shift+" |
| 87 |
} |
| 88 |
|
| 89 |
str := m.Button.String() |
| 90 |
if str == "" { |
| 91 |
s += "unknown" |
| 92 |
} else if str != "none" { // motion events don't have a button |
| 93 |
s += str |
| 94 |
} |
| 95 |
|
| 96 |
return s |
| 97 |
} |
| 98 |
|
| 99 |
// MouseClickEvent represents a mouse button click event. |
| 100 |
type MouseClickEvent Mouse |
| 101 |
|
| 102 |
// String returns a string representation of the mouse click event. |
| 103 |
func (e MouseClickEvent) String() string { |
| 104 |
return Mouse(e).String() |
| 105 |
} |
| 106 |
|
| 107 |
// Mouse returns the underlying mouse event. This is a convenience method and |
| 108 |
// syntactic sugar to satisfy the [MouseEvent] interface, and cast the mouse |
| 109 |
// event to [Mouse]. |
| 110 |
func (e MouseClickEvent) Mouse() Mouse { |
| 111 |
return Mouse(e) |
| 112 |
} |
| 113 |
|
| 114 |
// MouseReleaseEvent represents a mouse button release event. |
| 115 |
type MouseReleaseEvent Mouse |
| 116 |
|
| 117 |
// String returns a string representation of the mouse release event. |
| 118 |
func (e MouseReleaseEvent) String() string { |
| 119 |
return Mouse(e).String() |
| 120 |
} |
| 121 |
|
| 122 |
// Mouse returns the underlying mouse event. This is a convenience method and |
| 123 |
// syntactic sugar to satisfy the [MouseEvent] interface, and cast the mouse |
| 124 |
// event to [Mouse]. |
| 125 |
func (e MouseReleaseEvent) Mouse() Mouse { |
| 126 |
return Mouse(e) |
| 127 |
} |
| 128 |
|
| 129 |
// MouseWheelEvent represents a mouse wheel message event. |
| 130 |
type MouseWheelEvent Mouse |
| 131 |
|
| 132 |
// String returns a string representation of the mouse wheel event. |
| 133 |
func (e MouseWheelEvent) String() string { |
| 134 |
return Mouse(e).String() |
| 135 |
} |
| 136 |
|
| 137 |
// Mouse returns the underlying mouse event. This is a convenience method and |
| 138 |
// syntactic sugar to satisfy the [MouseEvent] interface, and cast the mouse |
| 139 |
// event to [Mouse]. |
| 140 |
func (e MouseWheelEvent) Mouse() Mouse { |
| 141 |
return Mouse(e) |
| 142 |
} |
| 143 |
|
| 144 |
// MouseMotionEvent represents a mouse motion event. |
| 145 |
type MouseMotionEvent Mouse |
| 146 |
|
| 147 |
// String returns a string representation of the mouse motion event. |
| 148 |
func (e MouseMotionEvent) String() string { |
| 149 |
m := Mouse(e) |
| 150 |
if m.Button != 0 { |
| 151 |
return m.String() + "+motion" |
| 152 |
} |
| 153 |
return m.String() + "motion" |
| 154 |
} |
| 155 |
|
| 156 |
// Mouse returns the underlying mouse event. This is a convenience method and |
| 157 |
// syntactic sugar to satisfy the [MouseEvent] interface, and cast the mouse |
| 158 |
// event to [Mouse]. |
| 159 |
func (e MouseMotionEvent) Mouse() Mouse { |
| 160 |
return Mouse(e) |
| 161 |
} |
| 162 |
|
| 163 |
// Parse SGR-encoded mouse events; SGR extended mouse events. SGR mouse events |
| 164 |
// look like: |
| 165 |
// |
| 166 |
// ESC [ < Cb ; Cx ; Cy (M or m) |
| 167 |
// |
| 168 |
// where: |
| 169 |
// |
| 170 |
// Cb is the encoded button code |
| 171 |
// Cx is the x-coordinate of the mouse |
| 172 |
// Cy is the y-coordinate of the mouse |
| 173 |
// M is for button press, m is for button release |
| 174 |
// |
| 175 |
// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Extended-coordinates |
| 176 |
func parseSGRMouseEvent(cmd ansi.Cmd, params ansi.Params) Event { |
| 177 |
x, _, ok := params.Param(1, 1) |
| 178 |
if !ok { |
| 179 |
x = 1 |
| 180 |
} |
| 181 |
y, _, ok := params.Param(2, 1) |
| 182 |
if !ok { |
| 183 |
y = 1 |
| 184 |
} |
| 185 |
release := cmd.Final() == 'm' |
| 186 |
b, _, _ := params.Param(0, 0) |
| 187 |
mod, btn, _, isMotion := parseMouseButton(b) |
| 188 |
|
| 189 |
// (1,1) is the upper left. We subtract 1 to normalize it to (0,0). |
| 190 |
x-- |
| 191 |
y-- |
| 192 |
|
| 193 |
m := Mouse{X: x, Y: y, Button: btn, Mod: mod} |
| 194 |
|
| 195 |
// Wheel buttons don't have release events |
| 196 |
// Motion can be reported as a release event in some terminals (Windows Terminal) |
| 197 |
if isWheel(m.Button) { |
| 198 |
return MouseWheelEvent(m) |
| 199 |
} else if !isMotion && release { |
| 200 |
return MouseReleaseEvent(m) |
| 201 |
} else if isMotion { |
| 202 |
return MouseMotionEvent(m) |
| 203 |
} |
| 204 |
return MouseClickEvent(m) |
| 205 |
} |
| 206 |
|
| 207 |
const x10MouseByteOffset = 32 |
| 208 |
|
| 209 |
// Parse X10-encoded mouse events; the simplest kind. The last release of X10 |
| 210 |
// was December 1986, by the way. The original X10 mouse protocol limits the Cx |
| 211 |
// and Cy coordinates to 223 (=255-032). |
| 212 |
// |
| 213 |
// X10 mouse events look like: |
| 214 |
// |
| 215 |
// ESC [M Cb Cx Cy |
| 216 |
// |
| 217 |
// See: http://www.xfree86.org/current/ctlseqs.html#Mouse%20Tracking |
| 218 |
func parseX10MouseEvent(buf []byte) Event { |
| 219 |
v := buf[3:6] |
| 220 |
b := int(v[0]) |
| 221 |
if b >= x10MouseByteOffset { |
| 222 |
// XXX: b < 32 should be impossible, but we're being defensive. |
| 223 |
b -= x10MouseByteOffset |
| 224 |
} |
| 225 |
|
| 226 |
mod, btn, isRelease, isMotion := parseMouseButton(b) |
| 227 |
|
| 228 |
// (1,1) is the upper left. We subtract 1 to normalize it to (0,0). |
| 229 |
x := int(v[1]) - x10MouseByteOffset - 1 |
| 230 |
y := int(v[2]) - x10MouseByteOffset - 1 |
| 231 |
|
| 232 |
m := Mouse{X: x, Y: y, Button: btn, Mod: mod} |
| 233 |
if isWheel(m.Button) { |
| 234 |
return MouseWheelEvent(m) |
| 235 |
} else if isMotion { |
| 236 |
return MouseMotionEvent(m) |
| 237 |
} else if isRelease { |
| 238 |
return MouseReleaseEvent(m) |
| 239 |
} |
| 240 |
return MouseClickEvent(m) |
| 241 |
} |
| 242 |
|
| 243 |
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Extended-coordinates |
| 244 |
func parseMouseButton(b int) (mod KeyMod, btn MouseButton, isRelease bool, isMotion bool) { |
| 245 |
// mouse bit shifts |
| 246 |
const ( |
| 247 |
bitShift = 0b0000_0100 |
| 248 |
bitAlt = 0b0000_1000 |
| 249 |
bitCtrl = 0b0001_0000 |
| 250 |
bitMotion = 0b0010_0000 |
| 251 |
bitWheel = 0b0100_0000 |
| 252 |
bitAdd = 0b1000_0000 // additional buttons 8-11 |
| 253 |
|
| 254 |
bitsMask = 0b0000_0011 |
| 255 |
) |
| 256 |
|
| 257 |
// Modifiers |
| 258 |
if b&bitAlt != 0 { |
| 259 |
mod |= ModAlt |
| 260 |
} |
| 261 |
if b&bitCtrl != 0 { |
| 262 |
mod |= ModCtrl |
| 263 |
} |
| 264 |
if b&bitShift != 0 { |
| 265 |
mod |= ModShift |
| 266 |
} |
| 267 |
|
| 268 |
if b&bitAdd != 0 { |
| 269 |
btn = MouseBackward + MouseButton(b&bitsMask) |
| 270 |
} else if b&bitWheel != 0 { |
| 271 |
btn = MouseWheelUp + MouseButton(b&bitsMask) |
| 272 |
} else { |
| 273 |
btn = MouseLeft + MouseButton(b&bitsMask) |
| 274 |
// X10 reports a button release as 0b0000_0011 (3) |
| 275 |
if b&bitsMask == bitsMask { |
| 276 |
btn = MouseNone |
| 277 |
isRelease = true |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
// Motion bit doesn't get reported for wheel events. |
| 282 |
if b&bitMotion != 0 && !isWheel(btn) { |
| 283 |
isMotion = true |
| 284 |
} |
| 285 |
|
| 286 |
return mod, btn, isRelease, isMotion |
| 287 |
} |
| 288 |
|
| 289 |
// isWheel returns true if the mouse event is a wheel event. |
| 290 |
func isWheel(btn MouseButton) bool { |
| 291 |
return btn >= MouseWheelUp && btn <= MouseWheelRight |
| 292 |
} |