/
cursor.go
129 lines (104 loc) · 3.38 KB
/
cursor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package wlr
/*
#include <stdlib.h>
#include <linux/input-event-codes.h>
#include <wayland-server-core.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_pointer.h>
*/
import "C"
import (
"time"
"unsafe"
)
type Cursor struct {
p *C.struct_wlr_cursor
}
func CreateCursor() Cursor {
p := C.wlr_cursor_create()
return Cursor{p: p}
}
func (c Cursor) Destroy() {
C.wlr_cursor_destroy(c.p)
}
func (c Cursor) X() float64 {
return float64(c.p.x)
}
func (c Cursor) Y() float64 {
return float64(c.p.y)
}
func (c Cursor) AttachOutputLayout(layout OutputLayout) {
C.wlr_cursor_attach_output_layout(c.p, layout.p)
}
func (c Cursor) AttachInputDevice(dev InputDevice) {
C.wlr_cursor_attach_input_device(c.p, dev.p)
}
func (c Cursor) Move(dev InputDevice, dx float64, dy float64) {
C.wlr_cursor_move(c.p, dev.p, C.double(dx), C.double(dy))
}
func (c Cursor) WarpAbsolute(dev InputDevice, x float64, y float64) {
C.wlr_cursor_warp_absolute(c.p, dev.p, C.double(x), C.double(y))
}
func (c Cursor) SetSurface(surface Surface, hotspotX int32, hotspotY int32) {
C.wlr_cursor_set_surface(c.p, surface.p, C.int32_t(hotspotX), C.int32_t(hotspotY))
}
func (c Cursor) SetXCursor(m XCursorManager, name string) {
s := C.CString(name)
defer C.free(unsafe.Pointer(s))
C.wlr_cursor_set_xcursor(c.p, m.p, s)
}
func (c Cursor) OnMotion(cb func(p Pointer, time time.Time, dx, dy float64)) Listener {
return newListener(&c.p.events.motion, func(lis Listener, data unsafe.Pointer) {
event := (*C.struct_wlr_pointer_motion_event)(data)
dev := Pointer{p: event.pointer}
cb(dev, time.UnixMilli(int64(event.time_msec)), float64(event.delta_x), float64(event.delta_y))
})
}
func (c Cursor) OnMotionAbsolute(cb func(p Pointer, time time.Time, x, y float64)) Listener {
return newListener(&c.p.events.motion_absolute, func(lis Listener, data unsafe.Pointer) {
event := (*C.struct_wlr_pointer_motion_absolute_event)(data)
dev := Pointer{p: event.pointer}
cb(dev, time.UnixMilli(int64(event.time_msec)), float64(event.x), float64(event.y))
})
}
func (c Cursor) OnButton(cb func(p Pointer, time time.Time, button CursorButton, state ButtonState)) Listener {
return newListener(&c.p.events.button, func(lis Listener, data unsafe.Pointer) {
event := (*C.struct_wlr_pointer_button_event)(data)
dev := Pointer{p: event.pointer}
cb(dev, time.UnixMilli(int64(event.time_msec)), CursorButton(event.button), ButtonState(event.state))
})
}
func (c Cursor) OnAxis(cb func(p Pointer, time time.Time, source AxisSource, orientation AxisOrientation, delta float64, deltaDiscrete int32)) Listener {
return newListener(&c.p.events.axis, func(lis Listener, data unsafe.Pointer) {
event := (*C.struct_wlr_pointer_axis_event)(data)
dev := Pointer{p: event.pointer}
cb(
dev,
time.UnixMilli(int64(event.time_msec)),
AxisSource(event.source),
AxisOrientation(event.orientation),
float64(event.delta),
int32(event.delta_discrete),
)
})
}
func (c Cursor) OnFrame(cb func()) Listener {
return newListener(&c.p.events.frame, func(lis Listener, data unsafe.Pointer) {
cb()
})
}
type CursorButton uint32
const (
BtnLeft CursorButton = C.BTN_LEFT
BtnRight CursorButton = C.BTN_RIGHT
BtnMiddle CursorButton = C.BTN_MIDDLE
)
type Pointer struct {
p *C.struct_wlr_pointer
}
func (p Pointer) Base() InputDevice {
return InputDevice{p: &p.p.base}
}
func (p Pointer) OutputName() string {
return C.GoString(p.p.output_name)
}