summaryrefslogtreecommitdiff
path: root/xgob/xproto.go
blob: 87ca85b709c872613cdd7ef9482dcdbd60cf3cbd (plain)
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
package xproto

import (
	"xgob"
)

type Atom uint32
type Window uint32

type QueryTreeReply struct {
	Error *xgob.Error
	Root, Parent Window
	Children []Window
}

type InternAtomReply struct {
	Error *xgob.Error
	Atom Atom
}

type GetPropertyReply struct {
	Error *xgob.Error
	Format uint8
	Type Atom
	BytesAfter uint32
	Value []byte
}

func appendUint16(buf []byte, val uint16) []byte {
	buf = append(buf, uint8(val >> 8))
	buf = append(buf, uint8(val & 0xFF))
	return buf
}

func appendUint32(buf []byte, val uint32) []byte {
	buf = append(buf, uint8(val >> 24))
	buf = append(buf, uint8(val >> 16))
	buf = append(buf, uint8(val >> 8))
	buf = append(buf, uint8(val & 0xFF))
	return buf
}

func getUint16(buf []byte) uint16 {
	var rv uint16
	rv = uint16(buf[0]) << 8
	rv |= uint16(buf[1])
	return rv
}

func getUint32(buf []byte) uint32 {
	var rv uint32
	rv = uint32(buf[0]) << 24
	rv |= uint32(buf[1]) << 16
	rv |= uint32(buf[2]) << 8
	rv |= uint32(buf[3])
	return rv
}

func queryTreeReply(in chan interface{}, out chan QueryTreeReply) {
	var rv QueryTreeReply
	data := <-in

	switch d := data.(type) {
	case xgob.Error:
		rv.Error = &d
	case xgob.Reply:
		rep := d.Remainder
		rv.Root = Window(getUint32(rep))
		rep = rep[4:]
		rv.Parent = Window(getUint32(rep))
		rep = rep[4:]
		children_len := getUint16(rep)
		rep = rep[2:]
		rv.Children = make([]Window, 0, children_len)
		rep = rep[14:]	// Pad
		
		for i := uint16(0); i < children_len; i++ {
			rv.Children = append(rv.Children, Window(getUint32(rep)))
			rep = rep[4:]
		}
	}
	out <- rv
}

func QueryTree(c *xgob.Connection, window Window) chan QueryTreeReply {
	req := make([]byte, 0, 8)

	req = append(req, 15)		// Opcode
	req = append(req, 0)		// Pad
	req = appendUint16(req, 2)	// Length
	req = appendUint32(req, uint32(window))

	rv := make(chan QueryTreeReply, 1)
	reply := c.WriteReplyRequest(req)

	go queryTreeReply(reply, rv)
	return rv
}

func internAtomReply(in chan interface{}, out chan InternAtomReply) {
	var rv InternAtomReply
	data := <-in

	switch d := data.(type) {
	case xgob.Error:
		rv.Error = &d
	case xgob.Reply:
		rv.Atom = Atom(getUint32(d.Remainder))
	}
	out <- rv
}

func InternAtom(c *xgob.Connection, only_if_exists bool, name string) chan InternAtomReply {
	length := len(name) + 8
	length += -length & 3

	req := make([]byte, 0, length)

	req = append(req, 16)		// Opcode
	if only_if_exists {
		req = append(req, 1)
	} else {
		req = append(req, 0)
	}
	req = appendUint16(req, uint16(length/4))
	req = appendUint16(req, uint16(len(name)))
	req = appendUint16(req, 0)	// Pad

	req = req[0:length]	// Expand for name
	copy(req[8:], name)

	rv := make(chan InternAtomReply, 1)
	reply := c.WriteReplyRequest(req)

	go internAtomReply(reply, rv)
	return rv
}

func getPropertyReply(in chan interface{}, out chan GetPropertyReply) {
	var rv GetPropertyReply
	data := <-in

	switch d := data.(type) {
	case xgob.Error:
		rv.Error = &d
	case xgob.Reply:
		rv.Format = d.Aux
		rep := d.Remainder
		rv.Type = Atom(getUint32(rep))
		rep = rep[4:]
		rv.BytesAfter = getUint32(rep)
		rep = rep[4:]
		value_len := getUint32(rep)
		rep = rep[4:]
		rep = rep[12:]	// Pad
		rv.Value = rep[:value_len]
		rep = rep[value_len:]
	}
	out <- rv
}

func GetProperty(c *xgob.Connection, delete bool, window Window, property Atom, _type Atom, long_offset uint32, long_length uint32) chan GetPropertyReply {
	req := make([]byte, 0, 6 * 4)

	req = append(req, 20)		// Opcode
	if delete {
		req = append(req, 1)
	} else {
		req = append(req, 0)
	}
	req = appendUint16(req, 6)
	req = appendUint32(req, uint32(window))
	req = appendUint32(req, uint32(property))
	req = appendUint32(req, uint32(_type))
	req = appendUint32(req, long_offset)
	req = appendUint32(req, long_length)

	rv := make(chan GetPropertyReply, 1)
	reply := c.WriteReplyRequest(req)

	go getPropertyReply(reply, rv)
	return rv
}