...
1
2 package ge
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, 23, "Generic Event Extension").Reply()
15 switch {
16 case err != nil:
17 return err
18 case !reply.Present:
19 return xgb.Errorf("No extension named Generic Event Extension could be found on on the server.")
20 }
21
22 c.ExtLock.Lock()
23 c.Extensions["Generic Event Extension"] = reply.MajorOpcode
24 c.ExtLock.Unlock()
25 for evNum, fun := range xgb.NewExtEventFuncs["Generic Event Extension"] {
26 xgb.NewEventFuncs[int(reply.FirstEvent)+evNum] = fun
27 }
28 for errNum, fun := range xgb.NewExtErrorFuncs["Generic Event Extension"] {
29 xgb.NewErrorFuncs[int(reply.FirstError)+errNum] = fun
30 }
31 return nil
32 }
33
34 func init() {
35 xgb.NewExtEventFuncs["Generic Event Extension"] = make(map[int]xgb.NewEventFun)
36 xgb.NewExtErrorFuncs["Generic Event Extension"] = 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 QueryVersionCookie struct {
65 *xgb.Cookie
66 }
67
68
69
70 func QueryVersion(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) QueryVersionCookie {
71 c.ExtLock.RLock()
72 defer c.ExtLock.RUnlock()
73 if _, ok := c.Extensions["Generic Event Extension"]; !ok {
74 panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'Generic Event Extension'. ge.Init(connObj) must be called first.")
75 }
76 cookie := c.NewCookie(true, true)
77 c.NewRequest(queryVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
78 return QueryVersionCookie{cookie}
79 }
80
81
82
83 func QueryVersionUnchecked(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) QueryVersionCookie {
84 c.ExtLock.RLock()
85 defer c.ExtLock.RUnlock()
86 if _, ok := c.Extensions["Generic Event Extension"]; !ok {
87 panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'Generic Event Extension'. ge.Init(connObj) must be called first.")
88 }
89 cookie := c.NewCookie(false, true)
90 c.NewRequest(queryVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
91 return QueryVersionCookie{cookie}
92 }
93
94
95 type QueryVersionReply struct {
96 Sequence uint16
97 Length uint32
98
99 MajorVersion uint16
100 MinorVersion uint16
101
102 }
103
104
105 func (cook QueryVersionCookie) Reply() (*QueryVersionReply, error) {
106 buf, err := cook.Cookie.Reply()
107 if err != nil {
108 return nil, err
109 }
110 if buf == nil {
111 return nil, nil
112 }
113 return queryVersionReply(buf), nil
114 }
115
116
117 func queryVersionReply(buf []byte) *QueryVersionReply {
118 v := new(QueryVersionReply)
119 b := 1
120
121 b += 1
122
123 v.Sequence = xgb.Get16(buf[b:])
124 b += 2
125
126 v.Length = xgb.Get32(buf[b:])
127 b += 4
128
129 v.MajorVersion = xgb.Get16(buf[b:])
130 b += 2
131
132 v.MinorVersion = xgb.Get16(buf[b:])
133 b += 2
134
135 b += 20
136
137 return v
138 }
139
140
141
142 func queryVersionRequest(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) []byte {
143 size := 8
144 b := 0
145 buf := make([]byte, size)
146
147 c.ExtLock.RLock()
148 buf[b] = c.Extensions["Generic Event Extension"]
149 c.ExtLock.RUnlock()
150 b += 1
151
152 buf[b] = 0
153 b += 1
154
155 xgb.Put16(buf[b:], uint16(size/4))
156 b += 2
157
158 xgb.Put16(buf[b:], ClientMajorVersion)
159 b += 2
160
161 xgb.Put16(buf[b:], ClientMinorVersion)
162 b += 2
163
164 return buf
165 }
166
View as plain text