mouse.go 7.4 KB

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