1import xcffib
2import struct
3import six
4MAJOR_VERSION = 1
5MINOR_VERSION = 2
6key = xcffib.ExtensionKey("MIT-SHM")
7_events = {}
8_errors = {}
9from . import xproto
10class CompletionEvent(xcffib.Event):
11    def __init__(self, unpacker):
12        if isinstance(unpacker, xcffib.Protobj):
13            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
14        xcffib.Event.__init__(self, unpacker)
15        base = unpacker.offset
16        self.drawable, self.minor_event, self.major_event, self.shmseg, self.offset = unpacker.unpack("xx2xIHBxII")
17        self.bufsize = unpacker.offset - base
18    def pack(self):
19        buf = six.BytesIO()
20        buf.write(struct.pack("=B", 0))
21        buf.write(struct.pack("=x2xIHBxII", self.drawable, self.minor_event, self.major_event, self.shmseg, self.offset))
22        buf_len = len(buf.getvalue())
23        if buf_len < 32:
24            buf.write(struct.pack("x" * (32 - buf_len)))
25        return buf.getvalue()
26    @classmethod
27    def synthetic(cls, drawable, minor_event, major_event, shmseg, offset):
28        self = cls.__new__(cls)
29        self.drawable = drawable
30        self.minor_event = minor_event
31        self.major_event = major_event
32        self.shmseg = shmseg
33        self.offset = offset
34        return self
35_events[0] = CompletionEvent
36class BadSegError(xcffib.Error):
37    def __init__(self, unpacker):
38        if isinstance(unpacker, xcffib.Protobj):
39            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
40        xcffib.Error.__init__(self, unpacker)
41        base = unpacker.offset
42        self.bad_value, self.minor_opcode, self.major_opcode = unpacker.unpack("xx2xIHBx")
43        self.bufsize = unpacker.offset - base
44    def pack(self):
45        buf = six.BytesIO()
46        buf.write(struct.pack("=B", 0))
47        buf.write(struct.pack("=x2xIHBx", self.bad_value, self.minor_opcode, self.major_opcode))
48        return buf.getvalue()
49BadBadSeg = BadSegError
50_errors[0] = BadSegError
51class QueryVersionReply(xcffib.Reply):
52    def __init__(self, unpacker):
53        if isinstance(unpacker, xcffib.Protobj):
54            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
55        xcffib.Reply.__init__(self, unpacker)
56        base = unpacker.offset
57        self.shared_pixmaps, self.major_version, self.minor_version, self.uid, self.gid, self.pixmap_format = unpacker.unpack("xB2x4xHHHHB15x")
58        self.bufsize = unpacker.offset - base
59class QueryVersionCookie(xcffib.Cookie):
60    reply_type = QueryVersionReply
61class GetImageReply(xcffib.Reply):
62    def __init__(self, unpacker):
63        if isinstance(unpacker, xcffib.Protobj):
64            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
65        xcffib.Reply.__init__(self, unpacker)
66        base = unpacker.offset
67        self.depth, self.visual, self.size = unpacker.unpack("xB2x4xII")
68        self.bufsize = unpacker.offset - base
69class GetImageCookie(xcffib.Cookie):
70    reply_type = GetImageReply
71class CreateSegmentReply(xcffib.Reply):
72    def __init__(self, unpacker):
73        if isinstance(unpacker, xcffib.Protobj):
74            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
75        xcffib.Reply.__init__(self, unpacker)
76        base = unpacker.offset
77        self.nfd, = unpacker.unpack("xB2x4x24x")
78        self.bufsize = unpacker.offset - base
79class CreateSegmentCookie(xcffib.Cookie):
80    reply_type = CreateSegmentReply
81class shmExtension(xcffib.Extension):
82    def QueryVersion(self, is_checked=True):
83        buf = six.BytesIO()
84        buf.write(struct.pack("=xx2x"))
85        return self.send_request(0, buf, QueryVersionCookie, is_checked=is_checked)
86    def Attach(self, shmseg, shmid, read_only, is_checked=False):
87        buf = six.BytesIO()
88        buf.write(struct.pack("=xx2xIIB3x", shmseg, shmid, read_only))
89        return self.send_request(1, buf, is_checked=is_checked)
90    def Detach(self, shmseg, is_checked=False):
91        buf = six.BytesIO()
92        buf.write(struct.pack("=xx2xI", shmseg))
93        return self.send_request(2, buf, is_checked=is_checked)
94    def PutImage(self, drawable, gc, total_width, total_height, src_x, src_y, src_width, src_height, dst_x, dst_y, depth, format, send_event, shmseg, offset, is_checked=False):
95        buf = six.BytesIO()
96        buf.write(struct.pack("=xx2xIIHHHHHHhhBBBxII", drawable, gc, total_width, total_height, src_x, src_y, src_width, src_height, dst_x, dst_y, depth, format, send_event, shmseg, offset))
97        return self.send_request(3, buf, is_checked=is_checked)
98    def GetImage(self, drawable, x, y, width, height, plane_mask, format, shmseg, offset, is_checked=True):
99        buf = six.BytesIO()
100        buf.write(struct.pack("=xx2xIhhHHIB3xII", drawable, x, y, width, height, plane_mask, format, shmseg, offset))
101        return self.send_request(4, buf, GetImageCookie, is_checked=is_checked)
102    def CreatePixmap(self, pid, drawable, width, height, depth, shmseg, offset, is_checked=False):
103        buf = six.BytesIO()
104        buf.write(struct.pack("=xx2xIIHHB3xII", pid, drawable, width, height, depth, shmseg, offset))
105        return self.send_request(5, buf, is_checked=is_checked)
106    def AttachFd(self, shmseg, read_only, is_checked=False):
107        buf = six.BytesIO()
108        buf.write(struct.pack("=xx2xIB3x", shmseg, read_only))
109        return self.send_request(6, buf, is_checked=is_checked)
110    def CreateSegment(self, shmseg, size, read_only, is_checked=True):
111        buf = six.BytesIO()
112        buf.write(struct.pack("=xx2xIIB3x", shmseg, size, read_only))
113        return self.send_request(7, buf, CreateSegmentCookie, is_checked=is_checked)
114xcffib._add_ext(key, shmExtension, _events, _errors)
115