[Xcb-commit] XcbPythonBinding.mdwn
XCB site
xcb at freedesktop.org
Sun Jun 15 11:07:50 PDT 2008
XcbPythonBinding.mdwn | 68 ++++++++++++++++++++++++++++++++------------------
1 file changed, 44 insertions(+), 24 deletions(-)
New commits:
commit 14360dc2c22ce1c4971158fc6ab8f47c60071b3c
Author: XCB site <xcb at freedesktop.org>
Date: Sun Jun 15 11:07:49 2008 -0700
web commit by AstralStorm: Correct XAuthority parsing
diff --git a/XcbPythonBinding.mdwn b/XcbPythonBinding.mdwn
index 61f69de..d9e992a 100644
--- a/XcbPythonBinding.mdwn
+++ b/XcbPythonBinding.mdwn
@@ -166,28 +166,48 @@ The following complete program creates a window, sets a property, and does some
run()
-# XAuth example
-
-The above example doesn't include XAuthority file parsing. To parse that file, you can use code similar to:
-
- import os
- import struct
-
- xauthdata = open(os.environ["XAUTHORITY"]).read()
- # It's Python, not C, so split the two-level \0-terminated array with lengths stored as first byte
- # and the index of the last item in the array is stored too as the first item
- xauthdata = [[y[1:] for y in x.split("\0")] for x in xauthdata.split("\0\0")][1:]
-
- xauth={}
- for xline in xauthdata:
- # byteswap to network order and lose the \0-terminator
- xline[3] = struct.pack("!%ds" % len(xline[3]), xline[3])[:-1]
- xauth[":".join(xline[:2])] = ":".join(xline[2:])
-
- # No host means localhost, add these entries
- if xline[0] == "localhost":
- xauth[":%s" % xline[1]] = ":".join(xline[2:])
-
-Then to connect you can use the mapping xauth like this:
-
+# Example with autorization
+
+The above example doesn't include XAuthority file parsing. To parse that file and connect to the X server,
+you can use code like this:
+
+import struct
+import os
+
+# Some libXau constants
+FamilyWild = 0
+FamilyLocal = 256
+# Some hacked libXau constants
+FamilyIPv6 = 6
+# FamilyWildIPv6 = ?
+
+
+def read_counted_string(f):
+ slen = struct.unpack("!H", f.read(2))[0]
+ return f.read(slen)
+
+# Grab XAuth data
+xauth={}
+xauthfile = open(os.environ["XAUTHORITY"])
+try:
+ while 1:
+ family = struct.unpack("!H", xauthfile.read(2))[0]
+ addr = read_counted_string(xauthfile)
+ if family != FamilyWild:
+ if family == FamilyLocal or family == FamilyIPv6:
+ addr = ""
+ else:
+ raise Exception("Unknown address family, fix me quickly")
+ number = read_counted_string(xauthfile)
+ name = read_counted_string(xauthfile)
+ data = read_counted_string(xauthfile)
+ xauth[addr + ":" + number] = name + ":" + data
+except struct.error:
+ pass
+finally:
+ xauthfile.close()
+
+try:
conn = xcb.connect(display=os.environ["DISPLAY"], auth=xauth[os.environ["DISPLAY"]])
+except KeyError:
+ conn = xcb.connect(display=os.environ["DISPLAY"])
More information about the xcb-commit
mailing list