forked from rollcat/dewm
/
client.go
251 lines (231 loc) · 6.35 KB
/
client.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
package main
import (
"time"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/xinerama"
"github.com/BurntSushi/xgb/xproto"
)
// Client is an X11 client managed by us.
type Client struct {
// X and Y are the coordinates of the topleft corner of the window.
X, Y int16
// W and H are width and height. Zero means don't change.
W, H uint16
// one of: StackModeAbove (default), StackModeBelow,
// StackModeTopIf, StackModeBottomIf, StackModeOpposite.
StackMode uint32
// Name is the window name
Name string
// xc is our private pointer to the X11 socket
xc *xgb.Conn
// window is the (private) ID of our X11 window
window xproto.Window
}
// NewClient allocates the Client struct, with the X socket and Window
// ID. Usually you won't call this directly, unless in response to a
// MapRequest - use WM.GetClient. You should call Client.Init
// afterwards.
func NewClient(xc *xgb.Conn, w xproto.Window) *Client {
return &Client{
X: 0,
Y: 0,
W: 0,
H: 0,
StackMode: xproto.StackModeAbove,
xc: xc,
window: w,
}
}
// Init initializes the client - initial configuration and event mask.
func (c *Client) Init() (err error) {
// Ensure that we can manage this window.
if err = c.Configure(); err != nil {
return
}
// Get notifications when this window is deleted.
if err = xproto.ChangeWindowAttributesChecked(
c.xc,
c.window,
xproto.CwEventMask,
[]uint32{
xproto.EventMaskStructureNotify |
xproto.EventMaskEnterWindow,
},
).Check(); err != nil {
return
}
if c.Name, err = c.GetName(); err != nil {
return
}
return
}
// Configure sends a configuration request to inflict Client's
// internal state on the real world.
func (c *Client) Configure() error {
valueMask := uint16(xproto.ConfigWindowX |
xproto.ConfigWindowY |
xproto.ConfigWindowBorderWidth |
xproto.ConfigWindowStackMode)
valueList := []uint32{}
valueList = append(valueList, uint32(c.X), uint32(c.Y))
if c.W > 0 {
valueMask |= xproto.ConfigWindowWidth
valueList = append(valueList, uint32(c.W))
}
if c.H > 0 {
valueMask |= xproto.ConfigWindowHeight
valueList = append(valueList, uint32(c.H))
}
valueList = append(valueList, 0 /* BorderWidth= */, c.StackMode)
err := xproto.ConfigureWindowChecked(
c.xc,
c.window,
valueMask,
valueList,
).Check()
if err != nil {
return err
}
return xproto.SendEventChecked(
c.xc, // conn
false, // propagate
c.window, // target
xproto.EventMaskStructureNotify, // mask
string(xproto.ConfigureNotifyEvent{
Event: c.window,
Window: c.window,
AboveSibling: 0,
X: c.X,
Y: c.Y,
Width: c.W,
Height: c.H,
OverrideRedirect: false,
}.Bytes()),
).Check()
}
// WarpPointer puts the mouse pointer inside of this client's window.
func (c *Client) WarpPointer() error {
return xproto.WarpPointerChecked(
c.xc, // conn
0, // src
c.window, // dst
0, // src x
0, // src x
0, // src w
0, // src h
10, // dst x
10, // dst y
).Check()
}
// CloseGracefully will do the ICCCM 4.2.8 dance to close the window
// nicely. If that fails or is impossible, CloseForcefully will be
// called for you next.
func (c *Client) CloseGracefully() error {
prop, err := xproto.GetProperty(
c.xc, // conn
false, // delete
c.window, // window
atomWMProtocols, // property
xproto.GetPropertyTypeAny, // atom
0, // offset
64, // length
).Reply()
if err != nil {
return err
}
if prop == nil {
// There were no properties, so the window doesn't follow ICCCM.
// Just destroy it.
return c.CloseForcefully()
}
for v := prop.Value; len(v) >= 4; v = v[4:] {
if decodeAtom(v) == atomWMDeleteWindow {
// ICCCM 4.2.8 ClientMessage
t := time.Now().Unix()
ev := xproto.ClientMessageEvent{
Format: 32,
Window: c.window,
Type: atomWMProtocols,
Data: xproto.ClientMessageDataUnionData32New([]uint32{
uint32(atomWMDeleteWindow),
uint32(t),
0,
0,
0,
}),
}
return xproto.SendEventChecked(
c.xc, // conn
false, // propagate
c.window, // destination
xproto.EventMaskNoEvent, // eventmask
string(ev.Bytes()), // event
).Check()
}
}
// No WM_DELETE_WINDOW protocol, so destroy.
return c.CloseForcefully()
}
// CloseForcefully destroys the window.
func (c *Client) CloseForcefully() error {
return xproto.DestroyWindowChecked(c.xc, c.window).Check()
}
// Hide requests the client to unmap (hide).
func (c *Client) Hide() error {
return xproto.UnmapWindowChecked(c.xc, c.window).Check()
}
// Show requests the client to show up again.
func (c *Client) Show() error {
return xproto.MapWindowChecked(c.xc, c.window).Check()
}
// GetName queries for the current WM_NAME / _NET_WM_NAME property
// (window name). _NET_WM_NAME is checked first, and if empty, WM_NAME
// is checked.
func (c *Client) GetName() (name string, err error) {
prop, err := xproto.GetProperty(
c.xc, // conn
false, // delete
c.window, // window
atomNETWMName, // property
xproto.GetPropertyTypeAny, // atom
0, // offset
(1<<32)-1, // length
).Reply()
if err != nil {
return
}
name = string(prop.Value)
if name != "" {
return
}
prop, err = xproto.GetProperty(
c.xc, // conn
false, // delete
c.window, // window
atomWMName, // property
xproto.GetPropertyTypeAny, // atom
0, // offset
(1<<32)-1, // length
).Reply()
if err != nil {
return
}
name = string(prop.Value)
return
}
// MakeFullscreen will re-arrange this client to fit the given screen.
func (c *Client) MakeFullscreen(screen *xinerama.ScreenInfo) {
c.X = screen.XOrg
c.Y = screen.YOrg
c.W = screen.Width
c.H = screen.Height
}
// Focus will shift keyboard focus to this client
func (c *Client) Focus() {
xproto.SetInputFocus(
c.xc,
xproto.InputFocusPointerRoot, // revert
c.window, // focus
xproto.TimeCurrentTime, // timestamp
)
}