xcffibhelpers.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2020 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. """
  21. This is a set of helper classes, designed to facilitate importing an X extension
  22. that's not supported by default by xcffib.
  23. """
  24. import io
  25. import struct
  26. import xcffib
  27. class XkbUseExtensionReply(xcffib.Reply):
  28. """Helper class to parse XkbUseExtensionReply
  29. Contains hardcoded values based on X11/XKBproto.h"""
  30. # pylint: disable=too-few-public-methods
  31. def __init__(self, unpacker):
  32. if isinstance(unpacker, xcffib.Protobj):
  33. unpacker = xcffib.MemoryUnpacker(unpacker.pack())
  34. xcffib.Reply.__init__(self, unpacker)
  35. base = unpacker.offset
  36. self.major_version, self.minor_version = unpacker.unpack(
  37. "xx2x4xHH4x4x4x4x")
  38. self.bufsize = unpacker.offset - base
  39. class XkbUseExtensionCookie(xcffib.Cookie):
  40. """Helper class for use in loading Xkb extension"""
  41. reply_type = XkbUseExtensionReply
  42. class XkbGetStateReply(xcffib.Reply):
  43. """Helper class to parse XkbGetState; copy&paste from X11/XKBproto.h"""
  44. # pylint: disable=too-few-public-methods
  45. _typedef = """
  46. BYTE type;
  47. BYTE deviceID;
  48. CARD16 sequenceNumber B16;
  49. CARD32 length B32;
  50. CARD8 mods;
  51. CARD8 baseMods;
  52. CARD8 latchedMods;
  53. CARD8 lockedMods;
  54. CARD8 group;
  55. CARD8 lockedGroup;
  56. INT16 baseGroup B16;
  57. INT16 latchedGroup B16;
  58. CARD8 compatState;
  59. CARD8 grabMods;
  60. CARD8 compatGrabMods;
  61. CARD8 lookupMods;
  62. CARD8 compatLookupMods;
  63. CARD8 pad1;
  64. CARD16 ptrBtnState B16;
  65. CARD16 pad2 B16;
  66. CARD32 pad3 B32;"""
  67. _type_mapping = {
  68. "BYTE": "B",
  69. "CARD16": "H",
  70. "CARD8": "B",
  71. "CARD32": "I",
  72. "INT16": "h",
  73. }
  74. def __init__(self, unpacker):
  75. if isinstance(unpacker, xcffib.Protobj):
  76. unpacker = xcffib.MemoryUnpacker(unpacker.pack())
  77. xcffib.Reply.__init__(self, unpacker)
  78. base = unpacker.offset
  79. # dynamic parse of copy&pasted struct content, for easy re-usability
  80. for line in self._typedef.splitlines():
  81. line = line.strip()
  82. line = line.rstrip(';')
  83. if not line:
  84. continue
  85. typename, name = line.split()[:2] # ignore optional third part
  86. setattr(self, name, unpacker.unpack(self._type_mapping[typename]))
  87. self.bufsize = unpacker.offset - base
  88. class XkbGetStateCookie(xcffib.Cookie):
  89. """Helper class for use in parsing Xkb GetState"""
  90. reply_type = XkbGetStateReply
  91. class XkbExtension(xcffib.Extension):
  92. """Helper class to load and use Xkb xcffib extension; needed
  93. because there is not XKB support in xcffib."""
  94. # pylint: disable=invalid-name,missing-function-docstring
  95. def UseExtension(self, is_checked=True):
  96. buf = io.BytesIO()
  97. buf.write(struct.pack("=xx2xHH", 1, 0))
  98. return self.send_request(0, buf, XkbGetStateCookie,
  99. is_checked=is_checked)
  100. def GetState(self, deviceSpec=0x100, is_checked=True):
  101. buf = io.BytesIO()
  102. buf.write(struct.pack("=xx2xHxx", deviceSpec))
  103. return self.send_request(4, buf, XkbGetStateCookie,
  104. is_checked=is_checked)
  105. key = xcffib.ExtensionKey("XKEYBOARD")
  106. # this is a lie: there are events and errors types
  107. _events = {}
  108. _errors = {}
  109. # pylint: disable=protected-access
  110. xcffib._add_ext(key, XkbExtension, _events, _errors)