usbhid: add support for mouse buttons (#2900)

* usbhid: add support for mouse buttons
Этот коммит содержится в:
sago35 2022-06-11 23:11:04 +09:00 коммит произвёл GitHub
родитель ada11090a2
коммит 76bba13963
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23

Просмотреть файл

@ -6,8 +6,17 @@ import (
var Mouse *mouse
type Button byte
const (
Left Button = 1 << iota
Right
Middle
)
type mouse struct {
buf *hid.RingBuffer
button Button
}
func init() {
@ -57,11 +66,33 @@ func (m *mouse) Move(vx, vy int) {
}
m.buf.Put([]byte{
0x01, 0x00, byte(vx), byte(vy), 0x00,
0x01, byte(m.button), byte(vx), byte(vy), 0x00,
})
}
// WHEEL controls the mouse wheel.
// Cilck clicks the mouse button.
func (m *mouse) Click(btn Button) {
m.Press(btn)
m.Release(btn)
}
// Press presses the given mouse buttons.
func (m *mouse) Press(btn Button) {
m.button |= btn
m.buf.Put([]byte{
0x01, byte(m.button), 0x00, 0x00, 0x00,
})
}
// Release releases the given mouse buttons.
func (m *mouse) Release(btn Button) {
m.button &= ^btn
m.buf.Put([]byte{
0x01, byte(m.button), 0x00, 0x00, 0x00,
})
}
// Wheel controls the mouse wheel.
func (m *mouse) Wheel(v int) {
if v == 0 {
return
@ -75,7 +106,7 @@ func (m *mouse) Wheel(v int) {
}
m.buf.Put([]byte{
0x01, 0x00, 0x00, 0x00, byte(v),
0x01, byte(m.button), 0x00, 0x00, byte(v),
})
}