File size: 8,956 Bytes
4cb60dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Xlib.ext.res -- X-Resource extension module
#
#    Copyright (C) 2021 Aleksei Bavshin <alebastr89@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
#    Free Software Foundation, Inc.,
#    51 Franklin Street,
#    Fifth Floor,
#    Boston, MA 02110-1301 USA

"""X-Resource extension allows a client to query the X server about its usage
of various resources.

For detailed description see any of the following documents.
Protocol specification:
    https://www.x.org/releases/current/doc/resourceproto/resproto.txt
XCB Protocol specification:
    https://cgit.freedesktop.org/xcb/proto/tree/src/res.xml
"""
from Xlib.protocol import rq

RES_MAJOR_VERSION = 1
RES_MINOR_VERSION = 2

extname = "X-Resource"

# v1.0
ResQueryVersion = 0
ResQueryClients = 1
ResQueryClientResources = 2
ResQueryClientPixmapBytes = 3
# v1.2
ResQueryClientIds = 4
ResQueryResourceBytes = 5


class QueryVersion(rq.ReplyRequest):
    _request = rq.Struct(
            rq.Card8("opcode"),
            rq.Opcode(ResQueryVersion),
            rq.RequestLength(),
            rq.Card8("client_major"),
            rq.Card8("client_minor"),
            rq.Pad(2))
    _reply = rq.Struct(
            rq.ReplyCode(),
            rq.Pad(1),
            rq.Card16("sequence_number"),
            rq.ReplyLength(),
            rq.Card16("server_major"),
            rq.Card16("server_minor"),
            rq.Pad(20))


def query_version(self, client_major=RES_MAJOR_VERSION,
                  client_minor=RES_MINOR_VERSION):
    """ Query the protocol version supported by the X server.

    The client sends the highest supported version to the server and the
    server sends the highest version it supports, but no higher than the
    requested version."""
    return QueryVersion(
            display=self.display,
            opcode=self.display.get_extension_major(extname),
            client_major=client_major,
            client_minor=client_minor)


Client = rq.Struct(
        rq.Card32("resource_base"),
        rq.Card32("resource_mask"))


class QueryClients(rq.ReplyRequest):
    _request = rq.Struct(
            rq.Card8("opcode"),
            rq.Opcode(ResQueryClients),
            rq.RequestLength())
    _reply = rq.Struct(
            rq.ReplyCode(),
            rq.Pad(1),
            rq.Card16("sequence_number"),
            rq.ReplyLength(),
            rq.LengthOf("clients", 4),
            rq.Pad(20),
            rq.List("clients", Client))


def query_clients(self):
    """Request the list of all currently connected clients."""
    return QueryClients(
            display=self.display,
            opcode=self.display.get_extension_major(extname))


Type = rq.Struct(
        rq.Card32("resource_type"),
        rq.Card32("count"))


class QueryClientResources(rq.ReplyRequest):
    _request = rq.Struct(
            rq.Card8("opcode"),
            rq.Opcode(ResQueryClientResources),
            rq.RequestLength(),
            rq.Card32("client"))
    _reply = rq.Struct(
            rq.ReplyCode(),
            rq.Pad(1),
            rq.Card16("sequence_number"),
            rq.ReplyLength(),
            rq.LengthOf("types", 4),
            rq.Pad(20),
            rq.List("types", Type))


def query_client_resources(self, client):
    """Request the number of resources owned by a client.

    The server will return the counts of each type of resource.
    """
    return QueryClientResources(
            display=self.display,
            opcode=self.display.get_extension_major(extname),
            client=client)


class QueryClientPixmapBytes(rq.ReplyRequest):
    _request = rq.Struct(
            rq.Card8("opcode"),
            rq.Opcode(ResQueryClientPixmapBytes),
            rq.RequestLength(),
            rq.Card32("client"))
    _reply = rq.Struct(
            rq.ReplyCode(),
            rq.Pad(1),
            rq.Card16("sequence_number"),
            rq.ReplyLength(),
            rq.Card32("bytes"),
            rq.Card32("bytes_overflow"),
            rq.Pad(16))


def query_client_pixmap_bytes(self, client):
    """Query the pixmap usage of some client.

    The returned number is a sum of memory usage of each pixmap that can be
    attributed to the given client.
    """
    return QueryClientPixmapBytes(
            display=self.display,
            opcode=self.display.get_extension_major(extname),
            client=client)


class SizeOf(rq.LengthOf):
    """A SizeOf stores the size in bytes of some other Field whose size
    may vary, e.g. List
    """
    def __init__(self, name, size, item_size):
        rq.LengthOf.__init__(self, name, size)
        self.item_size = item_size

    def parse_value(self, length, display):
        return length // self.item_size


ClientXIDMask = 1 << 0
LocalClientPIDMask = 1 << 1


ClientIdSpec = rq.Struct(
        rq.Card32("client"),
        rq.Card32("mask"))


ClientIdValue = rq.Struct(
        rq.Object("spec", ClientIdSpec),
        SizeOf("value", 4, 4),
        rq.List("value", rq.Card32Obj))


class QueryClientIds(rq.ReplyRequest):
    _request = rq.Struct(
            rq.Card8("opcode"),
            rq.Opcode(ResQueryClientIds),
            rq.RequestLength(),
            rq.LengthOf("specs", 4),
            rq.List("specs", ClientIdSpec))
    _reply = rq.Struct(
            rq.ReplyCode(),
            rq.Pad(1),
            rq.Card16("sequence_number"),
            rq.ReplyLength(),
            rq.LengthOf("ids", 4),
            rq.Pad(20),
            rq.List("ids", ClientIdValue))


def query_client_ids(self, specs):
    """Request to identify a given set of clients with some identification method.

    The request sends a list of specifiers that select clients and
    identification methods to server. The server then tries to identify the
    chosen clients using the identification methods specified for each client.
    The server returns IDs for those clients that were successfully identified.
    """
    return QueryClientIds(
            display=self.display,
            opcode=self.display.get_extension_major(extname),
            specs=specs)


ResourceIdSpec = rq.Struct(
        rq.Card32("resource"),
        rq.Card32("type"))


ResourceSizeSpec = rq.Struct(
        # inline struct ResourceIdSpec to work around
        # a parser bug with nested objects
        rq.Card32("resource"),
        rq.Card32("type"),
        rq.Card32("bytes"),
        rq.Card32("ref_count"),
        rq.Card32("use_count"))


ResourceSizeValue = rq.Struct(
        rq.Object("size", ResourceSizeSpec),
        rq.LengthOf("cross_references", 4),
        rq.List("cross_references", ResourceSizeSpec))


class QueryResourceBytes(rq.ReplyRequest):
    _request = rq.Struct(
            rq.Card8("opcode"),
            rq.Opcode(ResQueryResourceBytes),
            rq.RequestLength(),
            rq.Card32("client"),
            rq.LengthOf("specs", 4),
            rq.List("specs", ResourceIdSpec))
    _reply = rq.Struct(
            rq.ReplyCode(),
            rq.Pad(1),
            rq.Card16("sequence_number"),
            rq.ReplyLength(),
            rq.LengthOf("sizes", 4),
            rq.Pad(20),
            rq.List("sizes", ResourceSizeValue))


def query_resource_bytes(self, client, specs):
    """Query the sizes of resources from X server.

    The request sends a list of specifiers that selects resources for size
    calculation. The server tries to calculate the sizes of chosen resources
    and returns an estimate for a resource only if the size could be determined
    """
    return QueryResourceBytes(
            display=self.display,
            opcode=self.display.get_extension_major(extname),
            client=client,
            specs=specs)


def init(disp, info):
    disp.extension_add_method("display", "res_query_version", query_version)
    disp.extension_add_method("display", "res_query_clients", query_clients)
    disp.extension_add_method("display", "res_query_client_resources",
                              query_client_resources)
    disp.extension_add_method("display", "res_query_client_pixmap_bytes",
                              query_client_pixmap_bytes)
    disp.extension_add_method("display", "res_query_client_ids",
                              query_client_ids)
    disp.extension_add_method("display", "res_query_resource_bytes",
                              query_resource_bytes)