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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
package main
import (
"github.com/BurntSushi/xgb"
//"github.com/BurntSushi/xgb/xkb"
"github.com/BurntSushi/xgb/xproto"
"log"
)
func main() {
X, err := xgb.NewConn()
if err != nil {
log.Fatalln(err)
}
/*
// Use the extension if available, makes better use of state bits.
if err := xkb.Init(X); err == nil {
if _, err := xkb.UseExtension(X, 1, 0).Reply(); err != nil {
log.Fatalln(err)
}
}
*/
setup := xproto.Setup(X)
screen := setup.DefaultScreen(X)
visual, depth := screen.RootVisual, screen.RootDepth
// TODO: We should check that we find it, though we don't /need/ alpha here,
// it's just a minor improvement--affects the backpixel value.
for _, i := range screen.AllowedDepths {
for _, v := range i.Visuals {
// TODO: Could/should check other parameters.
if i.Depth == 32 && v.Class == xproto.VisualClassTrueColor {
visual, depth = v.VisualId, i.Depth
break
}
}
}
mid, err := xproto.NewColormapId(X)
if err != nil {
log.Fatalln(err)
}
_ = xproto.CreateColormap(
X, xproto.ColormapAllocNone, mid, screen.Root, visual)
wid, err := xproto.NewWindowId(X)
if err != nil {
log.Fatalln(err)
}
// Border pixel and colormap are required when depth differs from parent.
_ = xproto.CreateWindow(X, depth, wid, screen.Root,
0, 0, 500, 500, 0, xproto.WindowClassInputOutput,
visual, xproto.CwBackPixel|xproto.CwBorderPixel|xproto.CwEventMask|
xproto.CwColormap, []uint32{0x80808080, 0,
xproto.EventMaskStructureNotify | xproto.EventMaskKeyPress |
/* KeymapNotify */ xproto.EventMaskKeymapState, uint32(mid)})
title := []byte("Keys")
_ = xproto.ChangeProperty(X, xproto.PropModeReplace, wid, xproto.AtomWmName,
xproto.AtomString, 8, uint32(len(title)), title)
_ = xproto.MapWindow(X, wid)
mapping, err := xproto.GetKeyboardMapping(X, setup.MinKeycode,
byte(setup.MaxKeycode-setup.MinKeycode+1)).Reply()
if err != nil {
log.Fatalln(err)
}
// The order is "Shift, Lock, Control, Mod1, Mod2, Mod3, Mod4, and Mod5."
mm, err := xproto.GetModifierMapping(X).Reply()
if err != nil {
log.Fatalln(err)
}
// XXX: This seems pointless, the key will just end up switching groups
// instead of levels without full XKB handling. Though perhaps it might
// at least work as intended when there's only one XKB group.
const MODE_SWITCH = 0xff7e
var modeSwitchMask uint16
for mod := 0; mod < 8; mod++ {
perMod := int(mm.KeycodesPerModifier)
for _, kc := range mm.Keycodes[mod*perMod : (mod+1)*perMod] {
if kc == 0 {
continue
}
perKc := int(mapping.KeysymsPerKeycode)
k := int(kc - setup.MinKeycode)
for _, ks := range mapping.Keysyms[k*perKc : (k+1)*perKc] {
if ks == MODE_SWITCH {
modeSwitchMask |= 1 << uint(mod)
}
}
}
}
for {
ev, xerr := X.WaitForEvent()
if xerr != nil {
log.Printf("Error: %s\n", xerr)
return
}
if ev == nil {
return
}
log.Printf("Event: %s\n", ev)
switch e := ev.(type) {
case xproto.UnmapNotifyEvent:
return
case xproto.KeymapNotifyEvent:
// e.Keys is a 32 * 8 large bitmap indicating which keys are
// currently pressed down. This is sent "after every EnterNotify
// and FocusIn" but it also seems to fire when the keyboard layout
// changes. aixterm manual even speaks of that explicitly.
//
// But since changing the effective group involves no changes to
// the compatibility mapping, there's nothing for us to do.
case xproto.MappingNotifyEvent:
// e.FirstKeyCode .. e.Count changes have happened but rereading
// everything is the simpler thing to do.
mapping, err = xproto.GetKeyboardMapping(X, setup.MinKeycode,
byte(setup.MaxKeycode-setup.MinKeycode+1)).Reply()
if err != nil {
log.Fatalln(err)
}
// TODO: We should also repeat the search for MODE SWITCH.
case xproto.KeyPressEvent:
step := int(mapping.KeysymsPerKeycode)
from := int(e.Detail-setup.MinKeycode) * step
ks := mapping.Keysyms[from : from+step]
// Strip trailing NoSymbol entries.
for len(ks) > 0 && ks[len(ks)-1] == 0 {
ks = ks[:len(ks)-1]
}
// Expand back to at least 4.
switch {
case len(ks) == 1:
ks = append(ks, 0, ks[0], 0)
case len(ks) == 2:
ks = append(ks, ks[0], ks[1])
case len(ks) == 3:
ks = append(ks, 0)
}
// Other silly expansion rules, only applied to basic ASCII.
if ks[1] == 0 {
ks[1] = ks[0]
if ks[0] >= 'A' && ks[0] <= 'Z' ||
ks[0] >= 'a' && ks[0] <= 'z' {
ks[0] = ks[0] | 32
ks[1] = ks[0] &^ 32
}
}
if ks[3] == 0 {
ks[3] = ks[2]
if ks[2] >= 'A' && ks[2] <= 'Z' ||
ks[2] >= 'a' && ks[2] <= 'z' {
ks[2] = ks[2] | 32
ks[3] = ks[2] &^ 32
}
}
// We only have enough information to switch between two groups.
offset := 0
if e.State&modeSwitchMask != 0 {
offset += 2
}
var result xproto.Keysym
shift := e.State&xproto.ModMaskShift != 0
lock := e.State&xproto.ModMaskLock != 0
switch {
case !shift && !lock:
result = ks[offset+0]
case !shift && lock:
if ks[offset+0] >= 'a' && ks[offset+0] <= 'z' {
result = ks[offset+1]
} else {
result = ks[offset+0]
}
case shift && lock:
if ks[offset+1] >= 'a' && ks[offset+1] <= 'z' {
result = ks[offset+1] &^ 32
} else {
result = ks[offset+1]
}
case shift:
result = ks[offset+1]
}
if result <= 0xff {
log.Printf("%c (Latin-1)\n", rune(result))
} else {
log.Println(result)
}
}
}
}
|