forked from BurntSushi/wingo
/
command_mouse.go
213 lines (187 loc) · 5.4 KB
/
command_mouse.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
package main
/*
I haven't thought of a way to reconcile Mouse commands with the rest
of the commands. It may be easier to leave them separate.
Basically, the problem is that a mouse command operates on a client whereas
key (or general) commands always operate on the current state of Wingo.
The former requires parameterization over a client whereas the latter
can simply rely on the current global state.
This makes them fundamentally different.
*/
import (
"github.com/BurntSushi/xgb/xproto"
"github.com/BurntSushi/xgbutil"
"github.com/BurntSushi/xgbutil/mousebind"
"github.com/BurntSushi/xgbutil/xevent"
"github.com/BurntSushi/wingo/cursors"
"github.com/BurntSushi/wingo/focus"
"github.com/BurntSushi/wingo/logger"
"github.com/BurntSushi/wingo/stack"
)
type mouseCommand struct {
cmd string
down bool // 'up' when false
buttonStr string
direction uint32 // only used by Resize command
}
func (mcmd mouseCommand) setup(c *client, wid xproto.Window) {
// Check if this command is a drag... If it is, it needs special attention.
if mcmd.cmd == "Move" {
c.setupMoveDrag(wid, mcmd.buttonStr, true)
return
}
if mcmd.cmd == "Resize" {
c.setupResizeDrag(wid, mcmd.buttonStr, true, mcmd.direction)
return
}
// If we're putting this on the client or frame window, we need to propagate
// the events (i.e., grab synchronously).
// Otherwise, we don't need to grab at all!
run := mcmd.commandFun()
if wid == c.Id() || (c.Frame() != nil && wid == c.Frame().Parent().Id) {
if mcmd.down {
mcmd.attach(wid, func() { run(c) }, true, true)
} else { // we have to handle release grabs specially!
mcmd.attachClick(wid, func() { run(c) })
}
} else {
mcmd.attach(wid, func() { run(c) }, false, false)
}
}
// setupMoveDrag does the boiler plate for registering this client's
// "move" drag.
func (c *client) setupMoveDrag(dragWin xproto.Window,
buttonStr string, grab bool) {
dStart := xgbutil.MouseDragBeginFun(
func(X *xgbutil.XUtil, rx, ry, ex, ey int) (bool, xproto.Cursor) {
c.dragMoveBegin(rx, ry, ex, ey)
return true, cursors.Fleur
})
dStep := xgbutil.MouseDragFun(
func(X *xgbutil.XUtil, rx, ry, ex, ey int) {
c.dragMoveStep(rx, ry, ex, ey)
})
dEnd := xgbutil.MouseDragFun(
func(X *xgbutil.XUtil, rx, ry, ex, ey int) {
c.dragMoveEnd(rx, ry, ex, ey)
})
mousebind.Drag(X, X.Dummy(), dragWin, buttonStr, grab, dStart, dStep, dEnd)
}
// setupResizeDrag does the boiler plate for registering this client's
// "resize" drag.
func (c *client) setupResizeDrag(dragWin xproto.Window,
buttonStr string, grab bool, direction uint32) {
dStart := xgbutil.MouseDragBeginFun(
func(X *xgbutil.XUtil, rx, ry, ex, ey int) (bool, xproto.Cursor) {
return c.dragResizeBegin(direction, rx, ry, ex, ey)
})
dStep := xgbutil.MouseDragFun(
func(X *xgbutil.XUtil, rx, ry, ex, ey int) {
c.dragResizeStep(rx, ry, ex, ey)
})
dEnd := xgbutil.MouseDragFun(
func(X *xgbutil.XUtil, rx, ry, ex, ey int) {
c.dragResizeEnd(rx, ry, ex, ey)
})
mousebind.Drag(X, X.Dummy(), dragWin, buttonStr, grab, dStart, dStep, dEnd)
}
func (mcmd mouseCommand) attachClick(wid xproto.Window, run func()) {
mousebind.ButtonPressFun(
func(X *xgbutil.XUtil, ev xevent.ButtonPressEvent) {
// empty
}).Connect(X, wid, mcmd.buttonStr, false, true)
mousebind.ButtonReleaseFun(
func(X *xgbutil.XUtil, ev xevent.ButtonReleaseEvent) {
run()
}).Connect(X, wid, mcmd.buttonStr, false, false)
}
func (mcmd mouseCommand) attach(wid xproto.Window, run func(),
propagate, grab bool) {
if mcmd.down {
mousebind.ButtonPressFun(
func(X *xgbutil.XUtil, ev xevent.ButtonPressEvent) {
run()
}).Connect(X, wid, mcmd.buttonStr, propagate, grab)
} else {
mousebind.ButtonReleaseFun(
func(X *xgbutil.XUtil, ev xevent.ButtonReleaseEvent) {
run()
}).Connect(X, wid, mcmd.buttonStr, propagate, grab)
}
}
func rootMouseConfig() {
for _, mcmd := range wingo.conf.mouse["root"] {
run := getRootMouseCommand(mcmd.cmd)
if run == nil {
logger.Warning.Printf(
"Undefined root mouse command: '%s'", mcmd.cmd)
continue
}
mcmd.attach(wingo.root.Id, run, false, false)
}
}
func (c *client) clientMouseConfig() {
for _, mcmd := range wingo.conf.mouse["client"] {
mcmd.setup(c, c.Id())
}
}
func (c *client) frameMouseConfig() {
for _, mcmd := range wingo.conf.mouse["frame"] {
mcmd.setup(c, c.Frame().Parent().Id)
}
}
func (c *client) FramePieceMouseConfig(piece string, pieceid xproto.Window) {
for _, mcmd := range wingo.conf.mouse[piece] {
mcmd.setup(c, pieceid)
}
}
func (mcmd mouseCommand) commandFun() func(c *client) {
tryShellFun := commandShellFun(mcmd.cmd)
if tryShellFun != nil {
return func(c *client) {
tryShellFun()
xevent.ReplayPointer(X)
}
}
switch mcmd.cmd {
case "FocusRaise":
return func(c *client) {
focus.Focus(c)
stack.Raise(c)
xevent.ReplayPointer(X)
}
case "Focus":
return func(c *client) {
focus.Focus(c)
xevent.ReplayPointer(X)
}
case "Raise":
return func(c *client) {
stack.Raise(c)
xevent.ReplayPointer(X)
}
case "Close":
return func(c *client) {
c.Close()
}
case "MaximizeToggle":
return func(c *client) {
// c.MaximizeToggle()
}
case "Minimize":
return func(c *client) {
c.workspace.IconifyToggle(c)
}
}
logger.Warning.Printf("Undefined mouse command: '%s'", mcmd.cmd)
return nil
}
func getRootMouseCommand(cmd string) func() {
switch cmd {
case "Focus":
return func() {
focus.Root()
}
}
return nil
}