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