Skip to content

Commit 1265f1f

Browse files
alebastrvasily-v-ryabov
authored andcommitted
Add examples/xres.py to demonstrate and test X-Resource extension
1 parent 0f97016 commit 1265f1f

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

examples/xres.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/python
2+
#
3+
# examples/xres.py -- demonstrate the X-Resource extension
4+
#
5+
# Copyright (C) 2021 Aleksei Bavshin <alebastr89@gmail.com>
6+
#
7+
# This library is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU Lesser General Public License
9+
# as published by the Free Software Foundation; either version 2.1
10+
# of the License, or (at your option) any later version.
11+
#
12+
# This library is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15+
# See the GNU Lesser General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser General Public
18+
# License along with this library; if not, write to the
19+
# Free Software Foundation, Inc.,
20+
# 51 Franklin Street,
21+
# Fifth Floor,
22+
# Boston, MA 02110-1301 USA
23+
24+
import os
25+
import sys
26+
27+
# Change path so we find Xlib
28+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
29+
30+
from Xlib.display import Display
31+
from Xlib.ext import res as XRes
32+
33+
34+
def check_ext(disp, extname, version):
35+
if disp.query_extension(extname) is None:
36+
raise AssertionError("Server has {} extension".format(extname))
37+
38+
r = disp.res_query_version()
39+
if (r.server_major, r.server_minor) < version:
40+
raise AssertionError(
41+
"Server has requested version {} of {} extension".format(version, extname)
42+
)
43+
44+
45+
def query_client_id(display, wid):
46+
specs = [{"client": wid, "mask": XRes.LocalClientPIDMask}]
47+
r = display.res_query_client_ids(specs)
48+
for id in r.ids:
49+
if id.spec.client > 0 and id.spec.mask == XRes.LocalClientPIDMask:
50+
for value in id.value:
51+
return value
52+
return None
53+
54+
55+
def print_client_info(disp, client):
56+
print("client: {}".format(client))
57+
58+
resources = disp.res_query_client_resources(client)
59+
rc = [r.count for r in resources.types]
60+
print("\tresouces: {} resources of {} types".format(sum(rc), len(rc)))
61+
62+
pb = disp.res_query_client_pixmap_bytes(client)
63+
print("\tpixmaps: {} bytes {} overflow".format(pb.bytes, pb.bytes_overflow))
64+
65+
pid = query_client_id(disp, client)
66+
print("\tpid: {}".format(pid))
67+
68+
rb = disp.res_query_resource_bytes(client, [{"resource": 0, "type": 0}])
69+
sizes = [s.size.bytes for s in rb.sizes]
70+
print("\t{} resources consume {} bytes".format(len(sizes), sum(sizes)))
71+
72+
73+
def main():
74+
display = Display()
75+
check_ext(display, XRes.extname, (1, 2))
76+
77+
clients = display.res_query_clients().clients
78+
print("{} clients connected to the server".format(len(clients)))
79+
80+
for client in clients:
81+
print_client_info(display, client.resource_base)
82+
83+
84+
if __name__ == "__main__":
85+
main()

0 commit comments

Comments
 (0)