-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathxkb.go
79 lines (63 loc) · 1.53 KB
/
xkb.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
package xkb
// #include <stdlib.h>
// #include <xkbcommon/xkbcommon.h>
// #cgo pkg-config: xkbcommon
import "C"
import "unsafe"
type (
KeyCode uint32
KeySymFlags uint32
)
const (
KeySymFlagNoFlags KeySymFlags = C.XKB_KEYSYM_NO_FLAGS
KeySymFlagCaseInsensitive KeySymFlags = C.XKB_KEYSYM_CASE_INSENSITIVE
)
type Context struct {
p *C.struct_xkb_context
}
type Keymap struct {
p *C.struct_xkb_keymap
}
type State struct {
p *C.struct_xkb_state
}
func WrapState(p unsafe.Pointer) State {
return State{p: (*C.struct_xkb_state)(p)}
}
func NewContext(ksf KeySymFlags) Context {
p := C.xkb_context_new(uint32(ksf))
return Context{p: p}
}
func SymFromName(name string, flags KeySymFlags) KeySym {
s := C.CString(name)
sym := C.xkb_keysym_from_name(s, C.enum_xkb_keysym_flags(flags))
C.free(unsafe.Pointer(s))
return KeySym(sym)
}
func (c Context) Destroy() {
C.xkb_context_unref(c.p)
}
func (c Context) KeyMap() Keymap {
var rules C.struct_xkb_rule_names
p := C.xkb_keymap_new_from_names(c.p, &rules, C.XKB_KEYMAP_COMPILE_NO_FLAGS)
return Keymap{p: p}
}
func (m Keymap) Ptr() unsafe.Pointer {
return unsafe.Pointer(m.p)
}
func (m Keymap) Destroy() {
C.xkb_keymap_unref(m.p)
}
func (s State) Syms(keyCode KeyCode) []KeySym {
var syms *C.xkb_keysym_t
n := int(C.xkb_state_key_get_syms(s.p, C.uint32_t(keyCode), &syms))
if n == 0 || syms == nil {
return nil
}
slice := (*[1 << 30]C.xkb_keysym_t)(unsafe.Pointer(syms))[:n:n]
res := make([]KeySym, n)
for i := 0; i < n; i++ {
res[i] = KeySym(slice[i])
}
return res
}