1
2 package xcmisc
3
4
5
6 import (
7 "github.com/jezek/xgb"
8
9 "github.com/jezek/xgb/xproto"
10 )
11
12
13 func Init(c *xgb.Conn) error {
14 reply, err := xproto.QueryExtension(c, 7, "XC-MISC").Reply()
15 switch {
16 case err != nil:
17 return err
18 case !reply.Present:
19 return xgb.Errorf("No extension named XC-MISC could be found on on the server.")
20 }
21
22 c.ExtLock.Lock()
23 c.Extensions["XC-MISC"] = reply.MajorOpcode
24 c.ExtLock.Unlock()
25 for evNum, fun := range xgb.NewExtEventFuncs["XC-MISC"] {
26 xgb.NewEventFuncs[int(reply.FirstEvent)+evNum] = fun
27 }
28 for errNum, fun := range xgb.NewExtErrorFuncs["XC-MISC"] {
29 xgb.NewErrorFuncs[int(reply.FirstError)+errNum] = fun
30 }
31 return nil
32 }
33
34 func init() {
35 xgb.NewExtEventFuncs["XC-MISC"] = make(map[int]xgb.NewEventFun)
36 xgb.NewExtErrorFuncs["XC-MISC"] = make(map[int]xgb.NewErrorFun)
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 type GetVersionCookie struct {
65 *xgb.Cookie
66 }
67
68
69
70 func GetVersion(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) GetVersionCookie {
71 c.ExtLock.RLock()
72 defer c.ExtLock.RUnlock()
73 if _, ok := c.Extensions["XC-MISC"]; !ok {
74 panic("Cannot issue request 'GetVersion' using the uninitialized extension 'XC-MISC'. xcmisc.Init(connObj) must be called first.")
75 }
76 cookie := c.NewCookie(true, true)
77 c.NewRequest(getVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
78 return GetVersionCookie{cookie}
79 }
80
81
82
83 func GetVersionUnchecked(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) GetVersionCookie {
84 c.ExtLock.RLock()
85 defer c.ExtLock.RUnlock()
86 if _, ok := c.Extensions["XC-MISC"]; !ok {
87 panic("Cannot issue request 'GetVersion' using the uninitialized extension 'XC-MISC'. xcmisc.Init(connObj) must be called first.")
88 }
89 cookie := c.NewCookie(false, true)
90 c.NewRequest(getVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
91 return GetVersionCookie{cookie}
92 }
93
94
95 type GetVersionReply struct {
96 Sequence uint16
97 Length uint32
98
99 ServerMajorVersion uint16
100 ServerMinorVersion uint16
101 }
102
103
104 func (cook GetVersionCookie) Reply() (*GetVersionReply, error) {
105 buf, err := cook.Cookie.Reply()
106 if err != nil {
107 return nil, err
108 }
109 if buf == nil {
110 return nil, nil
111 }
112 return getVersionReply(buf), nil
113 }
114
115
116 func getVersionReply(buf []byte) *GetVersionReply {
117 v := new(GetVersionReply)
118 b := 1
119
120 b += 1
121
122 v.Sequence = xgb.Get16(buf[b:])
123 b += 2
124
125 v.Length = xgb.Get32(buf[b:])
126 b += 4
127
128 v.ServerMajorVersion = xgb.Get16(buf[b:])
129 b += 2
130
131 v.ServerMinorVersion = xgb.Get16(buf[b:])
132 b += 2
133
134 return v
135 }
136
137
138
139 func getVersionRequest(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) []byte {
140 size := 8
141 b := 0
142 buf := make([]byte, size)
143
144 c.ExtLock.RLock()
145 buf[b] = c.Extensions["XC-MISC"]
146 c.ExtLock.RUnlock()
147 b += 1
148
149 buf[b] = 0
150 b += 1
151
152 xgb.Put16(buf[b:], uint16(size/4))
153 b += 2
154
155 xgb.Put16(buf[b:], ClientMajorVersion)
156 b += 2
157
158 xgb.Put16(buf[b:], ClientMinorVersion)
159 b += 2
160
161 return buf
162 }
163
164
165 type GetXIDListCookie struct {
166 *xgb.Cookie
167 }
168
169
170
171 func GetXIDList(c *xgb.Conn, Count uint32) GetXIDListCookie {
172 c.ExtLock.RLock()
173 defer c.ExtLock.RUnlock()
174 if _, ok := c.Extensions["XC-MISC"]; !ok {
175 panic("Cannot issue request 'GetXIDList' using the uninitialized extension 'XC-MISC'. xcmisc.Init(connObj) must be called first.")
176 }
177 cookie := c.NewCookie(true, true)
178 c.NewRequest(getXIDListRequest(c, Count), cookie)
179 return GetXIDListCookie{cookie}
180 }
181
182
183
184 func GetXIDListUnchecked(c *xgb.Conn, Count uint32) GetXIDListCookie {
185 c.ExtLock.RLock()
186 defer c.ExtLock.RUnlock()
187 if _, ok := c.Extensions["XC-MISC"]; !ok {
188 panic("Cannot issue request 'GetXIDList' using the uninitialized extension 'XC-MISC'. xcmisc.Init(connObj) must be called first.")
189 }
190 cookie := c.NewCookie(false, true)
191 c.NewRequest(getXIDListRequest(c, Count), cookie)
192 return GetXIDListCookie{cookie}
193 }
194
195
196 type GetXIDListReply struct {
197 Sequence uint16
198 Length uint32
199
200 IdsLen uint32
201
202 Ids []uint32
203 }
204
205
206 func (cook GetXIDListCookie) Reply() (*GetXIDListReply, error) {
207 buf, err := cook.Cookie.Reply()
208 if err != nil {
209 return nil, err
210 }
211 if buf == nil {
212 return nil, nil
213 }
214 return getXIDListReply(buf), nil
215 }
216
217
218 func getXIDListReply(buf []byte) *GetXIDListReply {
219 v := new(GetXIDListReply)
220 b := 1
221
222 b += 1
223
224 v.Sequence = xgb.Get16(buf[b:])
225 b += 2
226
227 v.Length = xgb.Get32(buf[b:])
228 b += 4
229
230 v.IdsLen = xgb.Get32(buf[b:])
231 b += 4
232
233 b += 20
234
235 v.Ids = make([]uint32, v.IdsLen)
236 for i := 0; i < int(v.IdsLen); i++ {
237 v.Ids[i] = xgb.Get32(buf[b:])
238 b += 4
239 }
240
241 return v
242 }
243
244
245
246 func getXIDListRequest(c *xgb.Conn, Count uint32) []byte {
247 size := 8
248 b := 0
249 buf := make([]byte, size)
250
251 c.ExtLock.RLock()
252 buf[b] = c.Extensions["XC-MISC"]
253 c.ExtLock.RUnlock()
254 b += 1
255
256 buf[b] = 2
257 b += 1
258
259 xgb.Put16(buf[b:], uint16(size/4))
260 b += 2
261
262 xgb.Put32(buf[b:], Count)
263 b += 4
264
265 return buf
266 }
267
268
269 type GetXIDRangeCookie struct {
270 *xgb.Cookie
271 }
272
273
274
275 func GetXIDRange(c *xgb.Conn) GetXIDRangeCookie {
276 c.ExtLock.RLock()
277 defer c.ExtLock.RUnlock()
278 if _, ok := c.Extensions["XC-MISC"]; !ok {
279 panic("Cannot issue request 'GetXIDRange' using the uninitialized extension 'XC-MISC'. xcmisc.Init(connObj) must be called first.")
280 }
281 cookie := c.NewCookie(true, true)
282 c.NewRequest(getXIDRangeRequest(c), cookie)
283 return GetXIDRangeCookie{cookie}
284 }
285
286
287
288 func GetXIDRangeUnchecked(c *xgb.Conn) GetXIDRangeCookie {
289 c.ExtLock.RLock()
290 defer c.ExtLock.RUnlock()
291 if _, ok := c.Extensions["XC-MISC"]; !ok {
292 panic("Cannot issue request 'GetXIDRange' using the uninitialized extension 'XC-MISC'. xcmisc.Init(connObj) must be called first.")
293 }
294 cookie := c.NewCookie(false, true)
295 c.NewRequest(getXIDRangeRequest(c), cookie)
296 return GetXIDRangeCookie{cookie}
297 }
298
299
300 type GetXIDRangeReply struct {
301 Sequence uint16
302 Length uint32
303
304 StartId uint32
305 Count uint32
306 }
307
308
309 func (cook GetXIDRangeCookie) Reply() (*GetXIDRangeReply, error) {
310 buf, err := cook.Cookie.Reply()
311 if err != nil {
312 return nil, err
313 }
314 if buf == nil {
315 return nil, nil
316 }
317 return getXIDRangeReply(buf), nil
318 }
319
320
321 func getXIDRangeReply(buf []byte) *GetXIDRangeReply {
322 v := new(GetXIDRangeReply)
323 b := 1
324
325 b += 1
326
327 v.Sequence = xgb.Get16(buf[b:])
328 b += 2
329
330 v.Length = xgb.Get32(buf[b:])
331 b += 4
332
333 v.StartId = xgb.Get32(buf[b:])
334 b += 4
335
336 v.Count = xgb.Get32(buf[b:])
337 b += 4
338
339 return v
340 }
341
342
343
344 func getXIDRangeRequest(c *xgb.Conn) []byte {
345 size := 4
346 b := 0
347 buf := make([]byte, size)
348
349 c.ExtLock.RLock()
350 buf[b] = c.Extensions["XC-MISC"]
351 c.ExtLock.RUnlock()
352 b += 1
353
354 buf[b] = 1
355 b += 1
356
357 xgb.Put16(buf[b:], uint16(size/4))
358 b += 2
359
360 return buf
361 }
362
View as plain text