File: manual.go

package info (click to toggle)
golang-github-linuxdeepin-go-x11-client 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,784 kB
  • sloc: python: 944; sh: 38; makefile: 17
file content (421 lines) | stat: -rw-r--r-- 10,063 bytes parent folder | download
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

package ewmh

import (
	"errors"
	"fmt"
	"math"
	"strconv"

	"github.com/linuxdeepin/go-x11-client"
)

const LENGTH_MAX = math.MaxUint32

type ClientSource uint32

const (
	// No source at all (for clients supporting an older version of EWMH specification)
	ClientSourceNone ClientSource = iota
	// normal application
	ClientSourceNormal
	// pagers and other clients that represent direct user actions
	ClientSourceOther
)

func getAtom(c *x.Conn, name string) x.Atom {
	atom, _ := c.GetAtom(name)
	return atom
}

func getBooleanFromReply(r *x.GetPropertyReply) (bool, error) {
	num, err := getCardinalFromReply(r)
	return num != 0, err
}

func getUTF8StrFromReply(reply *x.GetPropertyReply) (string, error) {
	if reply.Format != 8 {
		return "", errors.New("bad reply")
	}

	return string(reply.Value), nil
}

func getUTF8StrsFromReply(reply *x.GetPropertyReply) ([]string, error) {
	if reply.Format != 8 {
		return nil, errors.New("bad reply")
	}

	data := reply.Value
	var strs []string
	sstart := 0
	for i, c := range data {
		if c == 0 {
			strs = append(strs, string(data[sstart:i]))
			sstart = i + 1
		}
	}
	if sstart < len(data) {
		strs = append(strs, string(data[sstart:]))
	}
	return strs, nil
}

func getWMIconFromReply(reply *x.GetPropertyReply) ([]WMIcon, error) {
	if reply.Format != 32 {
		return nil, errors.New("bad reply")
	}
	return getIcons(reply.Value)
}

type WMIcon struct {
	Width, Height uint32
	Data          []byte
}

func (icon *WMIcon) String() string {
	if icon == nil {
		return "nil"
	}

	return fmt.Sprintf("ewmh.WMIcon{ size: %dx%d, dataLength: %d }", icon.Width, icon.Height, len(icon.Data))
}

func getIcons(p []byte) ([]WMIcon, error) {
	var icons []WMIcon
	for len(p) >= 2*4 {
		width := x.Get32(p)
		height := x.Get32(p[4:])
		area := width * height

		if len(p) >= int(2+area)*4 {
			if area > 0 {
				icon := WMIcon{
					Width:  width,
					Height: height,
					Data:   p[2*4 : (2+area)*4],
				}
				icons = append(icons, icon)
			}

			if len(p) > int(2+area)*4 {
				// next icon
				p = p[(2+area)*4:]
				continue
			} else {
				// end
				break
			}
		} else {
			break
		}
	}
	return icons, nil
}

func sendClientMessage(c *x.Conn, win, dest x.Window, msgType x.Atom, pArray *[5]uint32) x.VoidCookie {
	var data x.ClientMessageData
	data.SetData32(pArray)
	event := x.ClientMessageEvent{
		Format: 32,
		Window: win,
		Type:   msgType,
		Data:   data,
	}
	w := x.NewWriter()
	x.WriteClientMessageEvent(w, &event)
	const evMask = x.EventMaskSubstructureNotify | x.EventMaskSubstructureRedirect
	return x.SendEventChecked(c, false, dest, evMask, w.Bytes())
}

/**
 *    _NET_DESKTOP_GEOMETRY
 */

func RequestChangeDesktopGeometry(c *x.Conn, geo DesktopGeometry) x.VoidCookie {
	array := [5]uint32{
		geo.Width,
		geo.Height,
	}
	root := c.GetDefaultScreen().Root
	atomNetDesktopGeometry, _ := c.GetAtom("_NET_DESKTOP_GEOMETRY")
	return sendClientMessage(c, root, root, atomNetDesktopGeometry, &array)
}

/**
 *    _NET_DESKTOP_VIEWPORT
 */

func RequestChangeDesktopViewport(c *x.Conn, corner ViewportLeftTopCorner) x.VoidCookie {
	array := [5]uint32{
		corner.X,
		corner.Y,
	}
	root := c.GetDefaultScreen().Root
	atomNetDesktopViewport, _ := c.GetAtom("_NET_DESKTOP_VIEWPORT")
	return sendClientMessage(c, root, root, atomNetDesktopViewport, &array)
}

/**
 *    _NET_CURRENT_DESKTOP
 */

func RequestChangeCurrentDesktop(c *x.Conn, desktop uint32, timestamp x.Timestamp) x.VoidCookie {
	array := [5]uint32{
		desktop,
		uint32(timestamp),
	}
	root := c.GetDefaultScreen().Root
	atomNetCurrentDesktop, _ := c.GetAtom("_NET_CURRENT_DESKTOP")
	return sendClientMessage(c, root, root, atomNetCurrentDesktop, &array)
}

/**
 *    _NET_ACTIVE_WINDOW
 */

func RequestChangeActiveWindow(c *x.Conn, windowToActivate x.Window, source ClientSource, timestamp x.Timestamp,
	currentActiveWindow x.Window) x.VoidCookie {
	array := [5]uint32{
		uint32(source),
		uint32(timestamp),
		uint32(currentActiveWindow),
	}
	root := c.GetDefaultScreen().Root
	atomNetActivewindow, _ := c.GetAtom("_NET_ACTIVE_WINDOW")
	return sendClientMessage(c, windowToActivate, root, atomNetActivewindow, &array)
}

/**
 *    _NET_SHOWING_DESKTOP
 */

func SetShowingDesktopChecked(c *x.Conn, show bool) x.VoidCookie {
	val := uint32(0)
	if show {
		val = 1
	}
	w := x.NewWriter()
	w.Write4b(uint32(val))
	root := c.GetDefaultScreen().Root
	atomNetShowingDesktop, _ := c.GetAtom("_NET_SHOWING_DESKTOP")
	return x.ChangePropertyChecked(c, x.PropModeReplace, root, atomNetShowingDesktop,
		x.AtomCardinal, 32, w.Bytes())
}

func SetShowingDesktop(c *x.Conn, show bool) {
	val := uint32(0)
	if show {
		val = 1
	}
	w := x.NewWriter()
	w.Write4b(uint32(val))
	root := c.GetDefaultScreen().Root
	atomNetShowingDesktop, _ := c.GetAtom("_NET_SHOWING_DESKTOP")
	x.ChangeProperty(c, x.PropModeReplace, root, atomNetShowingDesktop, x.AtomCardinal,
		32, w.Bytes())
}

func RequestChangeShowingDesktop(c *x.Conn, show bool) x.VoidCookie {
	val := uint32(0)
	if show {
		val = 1
	}
	array := [5]uint32{
		val,
	}
	root := c.GetDefaultScreen().Root
	atomNetShowingDesktop, _ := c.GetAtom("_NET_SHOWING_DESKTOP")
	return sendClientMessage(c, root, root, atomNetShowingDesktop, &array)
}

/**
 *    _NET_CLOSE_WINDOW
 */

func RequestCloseWindow(c *x.Conn, window x.Window, timestamp x.Timestamp, source ClientSource) x.VoidCookie {
	array := [5]uint32{
		uint32(timestamp),
		uint32(source),
	}
	root := c.GetDefaultScreen().Root
	atomNetCloseWindow, _ := c.GetAtom("_NET_CLOSE_WINDOW")
	return sendClientMessage(c, window, root, atomNetCloseWindow, &array)
}

/**
 *    _NET_MOVERESIZE_WINDOW
 */

type MoveResizeWindowFlags uint32

const (
	MoveResizeWindowX MoveResizeWindowFlags = 1 << (8 + iota)
	MoveResizeWindowY
	MoveResizeWindowWidth
	MoveResizeWindowHeight
)

func RequestMoveResizeWindow(c *x.Conn, window x.Window, gravity uint32, source ClientSource, flags MoveResizeWindowFlags,
	x, y, width, height uint32) x.VoidCookie {
	array := [5]uint32{
		gravity | uint32(flags) | (uint32(source) << 12),
		x, y, width, height,
	}
	root := c.GetDefaultScreen().Root
	atomNetMoveResizeWindow, _ := c.GetAtom("_NET_MOVERESIZE_WINDOW")
	return sendClientMessage(c, window, root, atomNetMoveResizeWindow, &array)
}

/**
 *    _NET_WM_MOVERESIZE
 */

type MoveResizeDirection uint32

const (
	MoveResizeSizeTopLeft MoveResizeDirection = iota
	MoveResizeSizeTop
	MoveResizeSizeTopRight
	MoveResizeSizeRight
	MoveResizeSizeBottomRight
	MoveResizeSizeBottom // 5
	MoveResizeSizeBottomLeft
	MoveResizeSizeLeft
	MoveResizeMove
	MoveResizeSizeKeyboard
	MoveResizeMoveKeyboard // 10
	MoveResizeCancel
)

func RequestWMMoveResize(c *x.Conn, window x.Window, xRoot, yRoot uint32, direction MoveResizeDirection,
	button uint32, source ClientSource) x.VoidCookie {
	array := [5]uint32{
		xRoot, yRoot,
		uint32(direction),
		button,
		uint32(source),
	}
	root := c.GetDefaultScreen().Root
	atomNetWMMoveResize, _ := c.GetAtom("_NET_WM_MOVERESIZE")
	return sendClientMessage(c, window, root, atomNetWMMoveResize, &array)
}

/**
 *    _NET_RESTACK_WINDOW
 */

func RequestRestackWindow(c *x.Conn, window, siblingWindow x.Window, stackMode uint32) x.VoidCookie {
	array := [5]uint32{
		2,
		uint32(siblingWindow),
		stackMode,
	}
	root := c.GetDefaultScreen().Root
	atomNetRestackWindow, _ := c.GetAtom("_NET_RESTACK_WINDOW")
	return sendClientMessage(c, window, root, atomNetRestackWindow, &array)
}

/**
 *    _NET_WM_DESKTOP
 */
func RequestChangeWMDesktop(c *x.Conn, window x.Window, desktop uint32, source ClientSource) x.VoidCookie {
	array := [5]uint32{
		desktop,
		uint32(source),
	}
	root := c.GetDefaultScreen().Root
	atomNetWMDesktop, _ := c.GetAtom("_NET_WM_DESKTOP")
	return sendClientMessage(c, window, root, atomNetWMDesktop, &array)
}

/**
 *    _NET_WM_STATE
 */

type WMStateAction uint32

const (
	WMStateRemove WMStateAction = iota
	WMStateAdd
	WMStateToggle
)

func RequestChangeWMState(c *x.Conn, window x.Window, action WMStateAction, firstProperty, secondProperty x.Atom,
	source ClientSource) x.VoidCookie {
	array := [5]uint32{
		uint32(action),
		uint32(firstProperty),
		uint32(secondProperty),
		uint32(source),
	}
	root := c.GetDefaultScreen().Root
	atomNetWMState, _ := c.GetAtom("_NET_WM_STATE")
	return sendClientMessage(c, window, root, atomNetWMState, &array)
}

/*
 *    _NET_WM_PING
 */

func SendWMPing(c *x.Conn, window x.Window, timestamp x.Timestamp) x.VoidCookie {
	atomNetWMPing, _ := c.GetAtom("_NET_WM_PING")
	array := [5]uint32{
		uint32(atomNetWMPing),
		uint32(timestamp),
		uint32(window),
	}
	atomWMProtocols, _ := c.GetAtom("WM_PROTOCOLS")
	return sendClientMessage(c, window, window, atomWMProtocols, &array)
}

/*
 *    _NET_WM_SYNC_REQUEST
 */

func (counter WMSyncRequestCounter) ToUint64() uint64 {
	return uint64(counter.Low) | (uint64(counter.High) << 32)
}

func (counter WMSyncRequestCounter) String() string {
	return strconv.FormatUint(counter.ToUint64(), 10)
}

func NewWMSyncRequestCounter(val uint64) WMSyncRequestCounter {
	return WMSyncRequestCounter{
		Low:  uint32(val),
		High: uint32(val >> 32),
	}
}

func SendWMSyncRequest(c *x.Conn, window x.Window, timestamp x.Timestamp, counter WMSyncRequestCounter) x.VoidCookie {
	atomNetWMSyncRequest, _ := c.GetAtom("_NET_WM_SYNC_REQUEST")
	array := [5]uint32{
		uint32(atomNetWMSyncRequest),
		uint32(timestamp),
		counter.Low,
		counter.High,
	}
	atomWMProtocols, _ := c.GetAtom("WM_PROTOCOLS")
	return sendClientMessage(c, window, window, atomWMProtocols, &array)
}

/*
 *    _NET_WM_FULLSCREEN_MONITORS
 */
func RequestChangeWMFullscreenMonitors(c *x.Conn, window x.Window, edges WMFullscreenMonitors,
	source ClientSource) x.VoidCookie {
	array := [5]uint32{
		edges.Top,
		edges.Bottom,
		edges.Left,
		edges.Right,
		uint32(source),
	}
	root := c.GetDefaultScreen().Root
	atomNetWMFullscreenMonitors, _ := c.GetAtom("_NET_WM_FULLSCREEN_MONITORS")
	return sendClientMessage(c, window, root, atomNetWMFullscreenMonitors, &array)
}