mouse.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package input
  2. import (
  3. "regexp"
  4. "github.com/charmbracelet/x/ansi"
  5. )
  6. // MouseButton represents the button that was pressed during a mouse event.
  7. type MouseButton byte
  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 MouseButton = iota
  27. MouseLeft
  28. MouseMiddle
  29. MouseRight
  30. MouseWheelUp
  31. MouseWheelDown
  32. MouseWheelLeft
  33. MouseWheelRight
  34. MouseBackward
  35. MouseForward
  36. MouseExtra1
  37. MouseExtra2
  38. )
  39. var mouseButtons = map[MouseButton]string{
  40. MouseNone: "none",
  41. MouseLeft: "left",
  42. MouseMiddle: "middle",
  43. MouseRight: "right",
  44. MouseWheelUp: "wheelup",
  45. MouseWheelDown: "wheeldown",
  46. MouseWheelLeft: "wheelleft",
  47. MouseWheelRight: "wheelright",
  48. MouseBackward: "backward",
  49. MouseForward: "forward",
  50. MouseExtra1: "button10",
  51. MouseExtra2: "button11",
  52. }
  53. // Mouse represents a Mouse event.
  54. type Mouse struct {
  55. X, Y int
  56. Button MouseButton
  57. Mod KeyMod
  58. }
  59. // String implements fmt.Stringer.
  60. func (m Mouse) String() (s string) {
  61. if m.Mod.HasCtrl() {
  62. s += "ctrl+"
  63. }
  64. if m.Mod.HasAlt() {
  65. s += "alt+"
  66. }
  67. if m.Mod.HasShift() {
  68. s += "shift+"
  69. }
  70. str, ok := mouseButtons[m.Button]
  71. if !ok {
  72. s += "unknown"
  73. } else if str != "none" { // motion events don't have a button
  74. s += str
  75. }
  76. return s
  77. }
  78. // MouseClickEvent represents a mouse button click event.
  79. type MouseClickEvent Mouse
  80. // String implements fmt.Stringer.
  81. func (e MouseClickEvent) String() string {
  82. return Mouse(e).String()
  83. }
  84. // MouseReleaseEvent represents a mouse button release event.
  85. type MouseReleaseEvent Mouse
  86. // String implements fmt.Stringer.
  87. func (e MouseReleaseEvent) String() string {
  88. return Mouse(e).String()
  89. }
  90. // MouseWheelEvent represents a mouse wheel event.
  91. type MouseWheelEvent Mouse
  92. // String implements fmt.Stringer.
  93. func (e MouseWheelEvent) String() string {
  94. return Mouse(e).String()
  95. }
  96. // MouseMotionEvent represents a mouse motion event.
  97. type MouseMotionEvent Mouse
  98. // String implements fmt.Stringer.
  99. func (e MouseMotionEvent) String() string {
  100. m := Mouse(e)
  101. if m.Button != 0 {
  102. return m.String() + "+motion"
  103. }
  104. return m.String() + "motion"
  105. }
  106. var mouseSGRRegex = regexp.MustCompile(`(\d+);(\d+);(\d+)([Mm])`)
  107. // Parse SGR-encoded mouse events; SGR extended mouse events. SGR mouse events
  108. // look like:
  109. //
  110. // ESC [ < Cb ; Cx ; Cy (M or m)
  111. //
  112. // where:
  113. //
  114. // Cb is the encoded button code
  115. // Cx is the x-coordinate of the mouse
  116. // Cy is the y-coordinate of the mouse
  117. // M is for button press, m is for button release
  118. //
  119. // https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Extended-coordinates
  120. func parseSGRMouseEvent(csi *ansi.CsiSequence) Event {
  121. x := csi.Param(1)
  122. y := csi.Param(2)
  123. release := csi.Command() == 'm'
  124. mod, btn, _, isMotion := parseMouseButton(csi.Param(0))
  125. // (1,1) is the upper left. We subtract 1 to normalize it to (0,0).
  126. x--
  127. y--
  128. m := Mouse{X: x, Y: y, Button: btn, Mod: mod}
  129. // Wheel buttons don't have release events
  130. // Motion can be reported as a release event in some terminals (Windows Terminal)
  131. if isWheel(m.Button) {
  132. return MouseWheelEvent(m)
  133. } else if !isMotion && release {
  134. return MouseReleaseEvent(m)
  135. } else if isMotion {
  136. return MouseMotionEvent(m)
  137. }
  138. return MouseClickEvent(m)
  139. }
  140. const x10MouseByteOffset = 32
  141. // Parse X10-encoded mouse events; the simplest kind. The last release of X10
  142. // was December 1986, by the way. The original X10 mouse protocol limits the Cx
  143. // and Cy coordinates to 223 (=255-032).
  144. //
  145. // X10 mouse events look like:
  146. //
  147. // ESC [M Cb Cx Cy
  148. //
  149. // See: http://www.xfree86.org/current/ctlseqs.html#Mouse%20Tracking
  150. func parseX10MouseEvent(buf []byte) Event {
  151. v := buf[3:6]
  152. b := int(v[0])
  153. if b >= x10MouseByteOffset {
  154. // XXX: b < 32 should be impossible, but we're being defensive.
  155. b -= x10MouseByteOffset
  156. }
  157. mod, btn, isRelease, isMotion := parseMouseButton(b)
  158. // (1,1) is the upper left. We subtract 1 to normalize it to (0,0).
  159. x := int(v[1]) - x10MouseByteOffset - 1
  160. y := int(v[2]) - x10MouseByteOffset - 1
  161. m := Mouse{X: x, Y: y, Button: btn, Mod: mod}
  162. if isWheel(m.Button) {
  163. return MouseWheelEvent(m)
  164. } else if isMotion {
  165. return MouseMotionEvent(m)
  166. } else if isRelease {
  167. return MouseReleaseEvent(m)
  168. }
  169. return MouseClickEvent(m)
  170. }
  171. // See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Extended-coordinates
  172. func parseMouseButton(b int) (mod KeyMod, btn MouseButton, isRelease bool, isMotion bool) {
  173. // mouse bit shifts
  174. const (
  175. bitShift = 0b0000_0100
  176. bitAlt = 0b0000_1000
  177. bitCtrl = 0b0001_0000
  178. bitMotion = 0b0010_0000
  179. bitWheel = 0b0100_0000
  180. bitAdd = 0b1000_0000 // additional buttons 8-11
  181. bitsMask = 0b0000_0011
  182. )
  183. // Modifiers
  184. if b&bitAlt != 0 {
  185. mod |= ModAlt
  186. }
  187. if b&bitCtrl != 0 {
  188. mod |= ModCtrl
  189. }
  190. if b&bitShift != 0 {
  191. mod |= ModShift
  192. }
  193. if b&bitAdd != 0 {
  194. btn = MouseBackward + MouseButton(b&bitsMask)
  195. } else if b&bitWheel != 0 {
  196. btn = MouseWheelUp + MouseButton(b&bitsMask)
  197. } else {
  198. btn = MouseLeft + MouseButton(b&bitsMask)
  199. // X10 reports a button release as 0b0000_0011 (3)
  200. if b&bitsMask == bitsMask {
  201. btn = MouseNone
  202. isRelease = true
  203. }
  204. }
  205. // Motion bit doesn't get reported for wheel events.
  206. if b&bitMotion != 0 && !isWheel(btn) {
  207. isMotion = true
  208. }
  209. return
  210. }
  211. // isWheel returns true if the mouse event is a wheel event.
  212. func isWheel(btn MouseButton) bool {
  213. return btn >= MouseWheelUp && btn <= MouseWheelRight
  214. }