1import xcffib
2import struct
3import six
4MAJOR_VERSION = 2
5MINOR_VERSION = 2
6key = xcffib.ExtensionKey("XFree86-VidModeExtension")
7_events = {}
8_errors = {}
9class ModeFlag:
10    Positive_HSync = 1 << 0
11    Negative_HSync = 1 << 1
12    Positive_VSync = 1 << 2
13    Negative_VSync = 1 << 3
14    Interlace = 1 << 4
15    Composite_Sync = 1 << 5
16    Positive_CSync = 1 << 6
17    Negative_CSync = 1 << 7
18    HSkew = 1 << 8
19    Broadcast = 1 << 9
20    Pixmux = 1 << 10
21    Double_Clock = 1 << 11
22    Half_Clock = 1 << 12
23class ClockFlag:
24    Programable = 1 << 0
25class Permission:
26    Read = 1 << 0
27    Write = 1 << 1
28class ModeInfo(xcffib.Struct):
29    def __init__(self, unpacker):
30        if isinstance(unpacker, xcffib.Protobj):
31            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
32        xcffib.Struct.__init__(self, unpacker)
33        base = unpacker.offset
34        self.dotclock, self.hdisplay, self.hsyncstart, self.hsyncend, self.htotal, self.hskew, self.vdisplay, self.vsyncstart, self.vsyncend, self.vtotal, self.flags, self.privsize = unpacker.unpack("IHHHHIHHHH4xI12xI")
35        self.bufsize = unpacker.offset - base
36    def pack(self):
37        buf = six.BytesIO()
38        buf.write(struct.pack("=IHHHHIHHHH4xI12xI", self.dotclock, self.hdisplay, self.hsyncstart, self.hsyncend, self.htotal, self.hskew, self.vdisplay, self.vsyncstart, self.vsyncend, self.vtotal, self.flags, self.privsize))
39        return buf.getvalue()
40    fixed_size = 48
41    @classmethod
42    def synthetic(cls, dotclock, hdisplay, hsyncstart, hsyncend, htotal, hskew, vdisplay, vsyncstart, vsyncend, vtotal, flags, privsize):
43        self = cls.__new__(cls)
44        self.dotclock = dotclock
45        self.hdisplay = hdisplay
46        self.hsyncstart = hsyncstart
47        self.hsyncend = hsyncend
48        self.htotal = htotal
49        self.hskew = hskew
50        self.vdisplay = vdisplay
51        self.vsyncstart = vsyncstart
52        self.vsyncend = vsyncend
53        self.vtotal = vtotal
54        self.flags = flags
55        self.privsize = privsize
56        return self
57class QueryVersionReply(xcffib.Reply):
58    def __init__(self, unpacker):
59        if isinstance(unpacker, xcffib.Protobj):
60            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
61        xcffib.Reply.__init__(self, unpacker)
62        base = unpacker.offset
63        self.major_version, self.minor_version = unpacker.unpack("xx2x4xHH")
64        self.bufsize = unpacker.offset - base
65class QueryVersionCookie(xcffib.Cookie):
66    reply_type = QueryVersionReply
67class GetModeLineReply(xcffib.Reply):
68    def __init__(self, unpacker):
69        if isinstance(unpacker, xcffib.Protobj):
70            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
71        xcffib.Reply.__init__(self, unpacker)
72        base = unpacker.offset
73        self.dotclock, self.hdisplay, self.hsyncstart, self.hsyncend, self.htotal, self.hskew, self.vdisplay, self.vsyncstart, self.vsyncend, self.vtotal, self.flags, self.privsize = unpacker.unpack("xx2x4xIHHHHHHHHH2xI12xI")
74        self.private = xcffib.List(unpacker, "B", self.privsize)
75        self.bufsize = unpacker.offset - base
76class GetModeLineCookie(xcffib.Cookie):
77    reply_type = GetModeLineReply
78class GetMonitorReply(xcffib.Reply):
79    def __init__(self, unpacker):
80        if isinstance(unpacker, xcffib.Protobj):
81            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
82        xcffib.Reply.__init__(self, unpacker)
83        base = unpacker.offset
84        self.vendor_length, self.model_length, self.num_hsync, self.num_vsync = unpacker.unpack("xx2x4xBBBB20x")
85        self.hsync = xcffib.List(unpacker, "I", self.num_hsync)
86        unpacker.pad("I")
87        self.vsync = xcffib.List(unpacker, "I", self.num_vsync)
88        unpacker.pad("c")
89        self.vendor = xcffib.List(unpacker, "c", self.vendor_length)
90        unpacker.pad("c")
91        self.alignment_pad = xcffib.List(unpacker, "c", ((self.vendor_length + 3) & (~ 3)) - self.vendor_length)
92        unpacker.pad("c")
93        self.model = xcffib.List(unpacker, "c", self.model_length)
94        self.bufsize = unpacker.offset - base
95class GetMonitorCookie(xcffib.Cookie):
96    reply_type = GetMonitorReply
97class GetAllModeLinesReply(xcffib.Reply):
98    def __init__(self, unpacker):
99        if isinstance(unpacker, xcffib.Protobj):
100            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
101        xcffib.Reply.__init__(self, unpacker)
102        base = unpacker.offset
103        self.modecount, = unpacker.unpack("xx2x4xI20x")
104        self.modeinfo = xcffib.List(unpacker, ModeInfo, self.modecount)
105        self.bufsize = unpacker.offset - base
106class GetAllModeLinesCookie(xcffib.Cookie):
107    reply_type = GetAllModeLinesReply
108class ValidateModeLineReply(xcffib.Reply):
109    def __init__(self, unpacker):
110        if isinstance(unpacker, xcffib.Protobj):
111            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
112        xcffib.Reply.__init__(self, unpacker)
113        base = unpacker.offset
114        self.status, = unpacker.unpack("xx2x4xI20x")
115        self.bufsize = unpacker.offset - base
116class ValidateModeLineCookie(xcffib.Cookie):
117    reply_type = ValidateModeLineReply
118class GetViewPortReply(xcffib.Reply):
119    def __init__(self, unpacker):
120        if isinstance(unpacker, xcffib.Protobj):
121            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
122        xcffib.Reply.__init__(self, unpacker)
123        base = unpacker.offset
124        self.x, self.y = unpacker.unpack("xx2x4xII16x")
125        self.bufsize = unpacker.offset - base
126class GetViewPortCookie(xcffib.Cookie):
127    reply_type = GetViewPortReply
128class GetDotClocksReply(xcffib.Reply):
129    def __init__(self, unpacker):
130        if isinstance(unpacker, xcffib.Protobj):
131            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
132        xcffib.Reply.__init__(self, unpacker)
133        base = unpacker.offset
134        self.flags, self.clocks, self.maxclocks = unpacker.unpack("xx2x4xIII12x")
135        self.clock = xcffib.List(unpacker, "I", (1 - (self.flags & 1)) * self.clocks)
136        self.bufsize = unpacker.offset - base
137class GetDotClocksCookie(xcffib.Cookie):
138    reply_type = GetDotClocksReply
139class GetGammaReply(xcffib.Reply):
140    def __init__(self, unpacker):
141        if isinstance(unpacker, xcffib.Protobj):
142            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
143        xcffib.Reply.__init__(self, unpacker)
144        base = unpacker.offset
145        self.red, self.green, self.blue = unpacker.unpack("xx2x4xIII12x")
146        self.bufsize = unpacker.offset - base
147class GetGammaCookie(xcffib.Cookie):
148    reply_type = GetGammaReply
149class GetGammaRampReply(xcffib.Reply):
150    def __init__(self, unpacker):
151        if isinstance(unpacker, xcffib.Protobj):
152            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
153        xcffib.Reply.__init__(self, unpacker)
154        base = unpacker.offset
155        self.size, = unpacker.unpack("xx2x4xH22x")
156        self.red = xcffib.List(unpacker, "H", (self.size + 1) & (~ 1))
157        unpacker.pad("H")
158        self.green = xcffib.List(unpacker, "H", (self.size + 1) & (~ 1))
159        unpacker.pad("H")
160        self.blue = xcffib.List(unpacker, "H", (self.size + 1) & (~ 1))
161        self.bufsize = unpacker.offset - base
162class GetGammaRampCookie(xcffib.Cookie):
163    reply_type = GetGammaRampReply
164class GetGammaRampSizeReply(xcffib.Reply):
165    def __init__(self, unpacker):
166        if isinstance(unpacker, xcffib.Protobj):
167            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
168        xcffib.Reply.__init__(self, unpacker)
169        base = unpacker.offset
170        self.size, = unpacker.unpack("xx2x4xH22x")
171        self.bufsize = unpacker.offset - base
172class GetGammaRampSizeCookie(xcffib.Cookie):
173    reply_type = GetGammaRampSizeReply
174class GetPermissionsReply(xcffib.Reply):
175    def __init__(self, unpacker):
176        if isinstance(unpacker, xcffib.Protobj):
177            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
178        xcffib.Reply.__init__(self, unpacker)
179        base = unpacker.offset
180        self.permissions, = unpacker.unpack("xx2x4xI20x")
181        self.bufsize = unpacker.offset - base
182class GetPermissionsCookie(xcffib.Cookie):
183    reply_type = GetPermissionsReply
184class BadClockError(xcffib.Error):
185    def __init__(self, unpacker):
186        if isinstance(unpacker, xcffib.Protobj):
187            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
188        xcffib.Error.__init__(self, unpacker)
189        base = unpacker.offset
190        unpacker.unpack("xx2x")
191        self.bufsize = unpacker.offset - base
192    def pack(self):
193        buf = six.BytesIO()
194        buf.write(struct.pack("=B", 0))
195        buf.write(struct.pack("=x2x"))
196        return buf.getvalue()
197BadBadClock = BadClockError
198_errors[0] = BadClockError
199class BadHTimingsError(xcffib.Error):
200    def __init__(self, unpacker):
201        if isinstance(unpacker, xcffib.Protobj):
202            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
203        xcffib.Error.__init__(self, unpacker)
204        base = unpacker.offset
205        unpacker.unpack("xx2x")
206        self.bufsize = unpacker.offset - base
207    def pack(self):
208        buf = six.BytesIO()
209        buf.write(struct.pack("=B", 1))
210        buf.write(struct.pack("=x2x"))
211        return buf.getvalue()
212BadBadHTimings = BadHTimingsError
213_errors[1] = BadHTimingsError
214class BadVTimingsError(xcffib.Error):
215    def __init__(self, unpacker):
216        if isinstance(unpacker, xcffib.Protobj):
217            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
218        xcffib.Error.__init__(self, unpacker)
219        base = unpacker.offset
220        unpacker.unpack("xx2x")
221        self.bufsize = unpacker.offset - base
222    def pack(self):
223        buf = six.BytesIO()
224        buf.write(struct.pack("=B", 2))
225        buf.write(struct.pack("=x2x"))
226        return buf.getvalue()
227BadBadVTimings = BadVTimingsError
228_errors[2] = BadVTimingsError
229class ModeUnsuitableError(xcffib.Error):
230    def __init__(self, unpacker):
231        if isinstance(unpacker, xcffib.Protobj):
232            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
233        xcffib.Error.__init__(self, unpacker)
234        base = unpacker.offset
235        unpacker.unpack("xx2x")
236        self.bufsize = unpacker.offset - base
237    def pack(self):
238        buf = six.BytesIO()
239        buf.write(struct.pack("=B", 3))
240        buf.write(struct.pack("=x2x"))
241        return buf.getvalue()
242BadModeUnsuitable = ModeUnsuitableError
243_errors[3] = ModeUnsuitableError
244class ExtensionDisabledError(xcffib.Error):
245    def __init__(self, unpacker):
246        if isinstance(unpacker, xcffib.Protobj):
247            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
248        xcffib.Error.__init__(self, unpacker)
249        base = unpacker.offset
250        unpacker.unpack("xx2x")
251        self.bufsize = unpacker.offset - base
252    def pack(self):
253        buf = six.BytesIO()
254        buf.write(struct.pack("=B", 4))
255        buf.write(struct.pack("=x2x"))
256        return buf.getvalue()
257BadExtensionDisabled = ExtensionDisabledError
258_errors[4] = ExtensionDisabledError
259class ClientNotLocalError(xcffib.Error):
260    def __init__(self, unpacker):
261        if isinstance(unpacker, xcffib.Protobj):
262            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
263        xcffib.Error.__init__(self, unpacker)
264        base = unpacker.offset
265        unpacker.unpack("xx2x")
266        self.bufsize = unpacker.offset - base
267    def pack(self):
268        buf = six.BytesIO()
269        buf.write(struct.pack("=B", 5))
270        buf.write(struct.pack("=x2x"))
271        return buf.getvalue()
272BadClientNotLocal = ClientNotLocalError
273_errors[5] = ClientNotLocalError
274class ZoomLockedError(xcffib.Error):
275    def __init__(self, unpacker):
276        if isinstance(unpacker, xcffib.Protobj):
277            unpacker = xcffib.MemoryUnpacker(unpacker.pack())
278        xcffib.Error.__init__(self, unpacker)
279        base = unpacker.offset
280        unpacker.unpack("xx2x")
281        self.bufsize = unpacker.offset - base
282    def pack(self):
283        buf = six.BytesIO()
284        buf.write(struct.pack("=B", 6))
285        buf.write(struct.pack("=x2x"))
286        return buf.getvalue()
287BadZoomLocked = ZoomLockedError
288_errors[6] = ZoomLockedError
289class xf86vidmodeExtension(xcffib.Extension):
290    def QueryVersion(self, is_checked=True):
291        buf = six.BytesIO()
292        buf.write(struct.pack("=xx2x"))
293        return self.send_request(0, buf, QueryVersionCookie, is_checked=is_checked)
294    def GetModeLine(self, screen, is_checked=True):
295        buf = six.BytesIO()
296        buf.write(struct.pack("=xx2xH2x", screen))
297        return self.send_request(1, buf, GetModeLineCookie, is_checked=is_checked)
298    def ModModeLine(self, screen, hdisplay, hsyncstart, hsyncend, htotal, hskew, vdisplay, vsyncstart, vsyncend, vtotal, flags, privsize, private, is_checked=False):
299        buf = six.BytesIO()
300        buf.write(struct.pack("=xx2xIHHHHHHHHH2xI12xI", screen, hdisplay, hsyncstart, hsyncend, htotal, hskew, vdisplay, vsyncstart, vsyncend, vtotal, flags, privsize))
301        buf.write(xcffib.pack_list(private, "B"))
302        return self.send_request(2, buf, is_checked=is_checked)
303    def SwitchMode(self, screen, zoom, is_checked=False):
304        buf = six.BytesIO()
305        buf.write(struct.pack("=xx2xHH", screen, zoom))
306        return self.send_request(3, buf, is_checked=is_checked)
307    def GetMonitor(self, screen, is_checked=True):
308        buf = six.BytesIO()
309        buf.write(struct.pack("=xx2xH2x", screen))
310        return self.send_request(4, buf, GetMonitorCookie, is_checked=is_checked)
311    def LockModeSwitch(self, screen, lock, is_checked=False):
312        buf = six.BytesIO()
313        buf.write(struct.pack("=xx2xHH", screen, lock))
314        return self.send_request(5, buf, is_checked=is_checked)
315    def GetAllModeLines(self, screen, is_checked=True):
316        buf = six.BytesIO()
317        buf.write(struct.pack("=xx2xH2x", screen))
318        return self.send_request(6, buf, GetAllModeLinesCookie, is_checked=is_checked)
319    def AddModeLine(self, screen, dotclock, hdisplay, hsyncstart, hsyncend, htotal, hskew, vdisplay, vsyncstart, vsyncend, vtotal, flags, privsize, after_dotclock, after_hdisplay, after_hsyncstart, after_hsyncend, after_htotal, after_hskew, after_vdisplay, after_vsyncstart, after_vsyncend, after_vtotal, after_flags, private, is_checked=False):
320        buf = six.BytesIO()
321        buf.write(struct.pack("=xx2xIIHHHHHHHHH2xI12xIIHHHHHHHHH2xI12x", screen, dotclock, hdisplay, hsyncstart, hsyncend, htotal, hskew, vdisplay, vsyncstart, vsyncend, vtotal, flags, privsize, after_dotclock, after_hdisplay, after_hsyncstart, after_hsyncend, after_htotal, after_hskew, after_vdisplay, after_vsyncstart, after_vsyncend, after_vtotal, after_flags))
322        buf.write(xcffib.pack_list(private, "B"))
323        return self.send_request(7, buf, is_checked=is_checked)
324    def DeleteModeLine(self, screen, dotclock, hdisplay, hsyncstart, hsyncend, htotal, hskew, vdisplay, vsyncstart, vsyncend, vtotal, flags, privsize, private, is_checked=False):
325        buf = six.BytesIO()
326        buf.write(struct.pack("=xx2xIIHHHHHHHHH2xI12xI", screen, dotclock, hdisplay, hsyncstart, hsyncend, htotal, hskew, vdisplay, vsyncstart, vsyncend, vtotal, flags, privsize))
327        buf.write(xcffib.pack_list(private, "B"))
328        return self.send_request(8, buf, is_checked=is_checked)
329    def ValidateModeLine(self, screen, dotclock, hdisplay, hsyncstart, hsyncend, htotal, hskew, vdisplay, vsyncstart, vsyncend, vtotal, flags, privsize, private, is_checked=True):
330        buf = six.BytesIO()
331        buf.write(struct.pack("=xx2xIIHHHHHHHHH2xI12xI", screen, dotclock, hdisplay, hsyncstart, hsyncend, htotal, hskew, vdisplay, vsyncstart, vsyncend, vtotal, flags, privsize))
332        buf.write(xcffib.pack_list(private, "B"))
333        return self.send_request(9, buf, ValidateModeLineCookie, is_checked=is_checked)
334    def SwitchToMode(self, screen, dotclock, hdisplay, hsyncstart, hsyncend, htotal, hskew, vdisplay, vsyncstart, vsyncend, vtotal, flags, privsize, private, is_checked=False):
335        buf = six.BytesIO()
336        buf.write(struct.pack("=xx2xIIHHHHHHHHH2xI12xI", screen, dotclock, hdisplay, hsyncstart, hsyncend, htotal, hskew, vdisplay, vsyncstart, vsyncend, vtotal, flags, privsize))
337        buf.write(xcffib.pack_list(private, "B"))
338        return self.send_request(10, buf, is_checked=is_checked)
339    def GetViewPort(self, screen, is_checked=True):
340        buf = six.BytesIO()
341        buf.write(struct.pack("=xx2xH2x", screen))
342        return self.send_request(11, buf, GetViewPortCookie, is_checked=is_checked)
343    def SetViewPort(self, screen, x, y, is_checked=False):
344        buf = six.BytesIO()
345        buf.write(struct.pack("=xx2xH2xII", screen, x, y))
346        return self.send_request(12, buf, is_checked=is_checked)
347    def GetDotClocks(self, screen, is_checked=True):
348        buf = six.BytesIO()
349        buf.write(struct.pack("=xx2xH2x", screen))
350        return self.send_request(13, buf, GetDotClocksCookie, is_checked=is_checked)
351    def SetClientVersion(self, major, minor, is_checked=False):
352        buf = six.BytesIO()
353        buf.write(struct.pack("=xx2xHH", major, minor))
354        return self.send_request(14, buf, is_checked=is_checked)
355    def SetGamma(self, screen, red, green, blue, is_checked=False):
356        buf = six.BytesIO()
357        buf.write(struct.pack("=xx2xH2xIII12x", screen, red, green, blue))
358        return self.send_request(15, buf, is_checked=is_checked)
359    def GetGamma(self, screen, is_checked=True):
360        buf = six.BytesIO()
361        buf.write(struct.pack("=xx2xH26x", screen))
362        return self.send_request(16, buf, GetGammaCookie, is_checked=is_checked)
363    def GetGammaRamp(self, screen, size, is_checked=True):
364        buf = six.BytesIO()
365        buf.write(struct.pack("=xx2xHH", screen, size))
366        return self.send_request(17, buf, GetGammaRampCookie, is_checked=is_checked)
367    def SetGammaRamp(self, screen, size, red, green, blue, is_checked=False):
368        buf = six.BytesIO()
369        buf.write(struct.pack("=xx2xHH", screen, size))
370        buf.write(xcffib.pack_list(red, "H"))
371        buf.write(xcffib.pack_list(green, "H"))
372        buf.write(xcffib.pack_list(blue, "H"))
373        return self.send_request(18, buf, is_checked=is_checked)
374    def GetGammaRampSize(self, screen, is_checked=True):
375        buf = six.BytesIO()
376        buf.write(struct.pack("=xx2xH2x", screen))
377        return self.send_request(19, buf, GetGammaRampSizeCookie, is_checked=is_checked)
378    def GetPermissions(self, screen, is_checked=True):
379        buf = six.BytesIO()
380        buf.write(struct.pack("=xx2xH2x", screen))
381        return self.send_request(20, buf, GetPermissionsCookie, is_checked=is_checked)
382xcffib._add_ext(key, xf86vidmodeExtension, _events, _errors)
383