-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinput_events.go
276 lines (261 loc) · 6.76 KB
/
input_events.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"github.com/ziutek/mdtwm/xgb_patched"
)
func keyPress(e xgb.KeyPressEvent) {
d.Printf("%T: %+v", e, e)
if e.State == cfg.ModMask || e.State == cfg.ModMask|xgb.ModMask2 {
cmd, ok := cfg.Keys[keyCodeToSym[e.Detail]]
if !ok {
l.Print("Unhandled key: ", e.Detail)
return
}
if err := cmd.Run(); err != nil {
l.Printf("cmd(%s): %s", cmd.Param, err)
}
}
}
// TODO: Following code isn't good (it need to be reimplemented!)
const (
resizeLeft = 1 << iota
resizeRight
resizeTop
resizeBottom
)
// Distinguishes following actions (they can be used in specified handler):
// 1. One click and move, in: MotionNotify, ButtonRelease
// 2. One long click without move, in: ButtonRelease
// 3. Two clicks and move, in: MotionNotify, ButtonRelease
// 4. Two clicks without move when second click is long, in: ButtonRelease
// 4. Three clicks and move, in: MotionNotify, ButtonRelease
// 5. Three clicks without move, in: ButtonRelease
type Multiclick struct {
Box Box
X, Y, RootX, RootY int16
Num int // number of clicks
Moved bool // indicates that cursor has moved during click
Resize byte
Child Window
last xgb.Timestamp // time of last click
counter int // Multiclick counter
}
func (c *Multiclick) Inc(t xgb.Timestamp) {
c.counter++
if c.counter == 1 || t-c.last > cfg.MultiClickTime*2 {
// First click or to long interval betwen clicks
c.last = t
c.Num = 0
c.Moved = false
} else if c.counter == 3 {
// Maximum number of clicks
c.Num = c.counter
c.counter = 0
}
}
func (c *Multiclick) First() bool {
return c.counter == 1
}
func (c *Multiclick) Update(t xgb.Timestamp, moved bool) {
if !c.Moved {
c.Moved = moved
}
if c.counter == 0 {
return
}
if t-c.last > cfg.MultiClickTime || moved {
// We obtained all clicks from this multiclick
c.Num = c.counter
c.counter = 0
}
}
var click Multiclick
func buttonPress(e xgb.ButtonPressEvent) {
d.Printf("%T: %+v", e, e)
click.Inc(e.Time)
if click.First() {
// Save first clicked box and coordinates of first click
click.Box = currentBox
click.RootX, click.RootY = e.RootX, e.RootY
click.Resize = 0
if click.Box.Float() {
click.Box.Raise()
}
// Check for click in resize border
w := click.Box.Window()
var ok bool
click.X, click.Y, click.Child, _, ok = w.TranslateCoordinates(
root.Window(), e.RootX, e.RootY,
)
if !ok {
return
}
g := click.Box.Geometry()
rbw := cfg.ResizeBorderWidth - cfg.BorderWidth
if click.X < rbw {
click.Resize |= resizeLeft
} else if click.X >= g.W-rbw {
click.Resize |= resizeRight
}
if click.Y < rbw {
click.Resize |= resizeTop
} else if click.Y >= g.H-rbw {
click.Resize |= resizeBottom
}
return
}
click.Update(e.Time, false)
}
func buttonRelease(e xgb.ButtonReleaseEvent) {
d.Printf("%T: %+v", e, e)
click.Update(e.Time, false)
// Actions
switch click.Num {
case 1: // One click
if _, ok := click.Box.(ParentBox); ok {
return // For now, we don't move panels
}
w := click.Box.Window()
if !click.Moved {
e.Event = w.Id()
e.EventX = click.X
e.EventY = click.Y
e.Child = click.Child.Id()
e.Time = xgb.TimeCurrentTime
e.State = 0
w.Send(false, xgb.EventMaskNoEvent, xgb.ButtonPressEvent(e))
e.State = xgb.EventMaskButton3Motion
w.Send(false, xgb.EventMaskNoEvent, e)
return
}
if click.Box.Float() || currentBox == nil || currentBox.Window() == w {
// There isn't any action for floating box or if box have been
// moved outside of desk or havent moved
return
}
// Move a box
click.Box.Parent().Remove(click.Box)
x, y, _, _, ok := currentBox.Window().TranslateCoordinates(
root.Window(), e.RootX, e.RootY,
)
if !ok {
currentPanel().Append(click.Box)
return
}
currentPanel().InsertNextTo(click.Box, currentBox, x, y)
case 2: // Two clicks
case 3: // Three clicks
if click.Moved {
return
}
switch b := click.Box.(type) {
case *BoxedWindow:
if b.Protocols().Contains(AtomWmDeleteWindow) {
b.SendMessage(AtomWmDeleteWindow, b.Window())
} else {
b.Window().Destroy()
}
}
}
}
func motionNotify(e xgb.MotionNotifyEvent) {
//d.Printf("%T: %+v", e, e)
dx := e.RootX - click.RootX
dy := e.RootY - click.RootY
click.Update(e.Time, dx*dx+dy*dy > cfg.MovedClickRadius)
// Actions
switch click.Num {
case 2: // Two clicks and move: tile/untile box
click.Num = 1
click.Resize = 0
click.Box.SetFloat(!click.Box.Float())
fallthrough
case 1: // One click and move: move/resize box
if _, ok := click.Box.(ParentBox); ok {
return // For now, we don't move panels
}
conn.ChangeActivePointerGrab(cfg.MoveCursor, xgb.TimeCurrentTime,
rightButtonEventMask)
if click.Resize != 0 {
if click.Box.Float() {
x, y, w, h := click.Box.PosSize()
bb := cfg.ResizeBorderWidth * 2
if click.Resize&resizeLeft != 0 && w-dx > bb {
x += dx
w -= dx
} else if click.Resize&resizeRight != 0 && w+dx > bb {
w += dx
}
if click.Resize&resizeTop != 0 && h-dy > bb {
y += dy
h -= dy
} else if click.Resize&resizeBottom != 0 && h+dy > bb {
h += dy
}
click.Box.SetPosSize(x, y, w, h)
click.RootX += dx
click.RootY += dy
} else {
// TODO: implement resize of tiled window
}
return
}
// Use left and right borders for change desktop
_, _, rootWidth, _ := root.PosSize()
switch e.RootX {
case 0: // Left border
// WarpPointer must be first, if not we obtain to many Motion events
e.RootX = rootWidth - 2
conn.WarpPointer(xgb.WindowNone, root.Window().Id(), 0, 0, 0, 0,
e.RootX, e.RootY)
setPrevDesk()
skipBorderEvents()
case rootWidth - 1: // Right border
// WarpPointer must be first, if not we obtain to many Motion events
e.RootX = 1
conn.WarpPointer(xgb.WindowNone, root.Window().Id(), 0, 0, 0, 0,
e.RootX, e.RootY)
setNextDesk()
skipBorderEvents()
}
if click.Box.Float() {
// Move floating box
x, y, w, h := click.Box.PosSize()
dx, dy := e.RootX-click.RootX, e.RootY-click.RootY
click.Box.SetPosSize(x+dx, y+dy, w, h)
click.RootX += dx
click.RootY += dy
if click.Box.Parent().Window() != currentDesk.Window() {
// Floating window moved to new desk
click.Box.Parent().Remove(click.Box)
currentDesk.Append(click.Box)
}
}
case 3: // Three clicks
}
}
/*func isRootOrDesk(b Box) bool {
return b.Parent() == nil || b.Parent().Window() == root.Window()
}*/
func skipBorderEvents() {
_, _, maxX, maxY := root.PosSize()
maxX--
maxY--
var (
event xgb.Event
err error
)
for {
event, err = conn.WaitForEvent()
if err != nil {
break
}
e, ok := event.(xgb.MotionNotifyEvent)
if !ok {
break
}
if e.RootX != 0 && e.RootX != maxX && e.RootY != 0 && e.RootY != maxY {
break
}
}
handleEvent(event, err)
}