"""The panel through the s1d13700-tca DRM driver: one dumb buffer, with each change reported as a dirty rectangle. libdrm through ctypes, limited to calls that libdrm 2.4.97 (Debian buster) already has; the dumb buffer is made with plain ioctls. """ import ctypes import ctypes.util import fcntl import mmap import os import struct DRIVER_NAME = "s1d13700-tca" FORMAT_XRGB8888 = 0x34325258 # "XR24" class DrmError(Exception): pass class _Version(ctypes.Structure): _fields_ = [("version_major", ctypes.c_int), ("version_minor", ctypes.c_int), ("version_patchlevel", ctypes.c_int), ("name_len", ctypes.c_int), ("name", ctypes.c_char_p), ("date_len", ctypes.c_int), ("date", ctypes.c_char_p), ("desc_len", ctypes.c_int), ("desc", ctypes.c_char_p)] class _Res(ctypes.Structure): _fields_ = [("count_fbs", ctypes.c_int), ("fbs", ctypes.POINTER(ctypes.c_uint32)), ("count_crtcs", ctypes.c_int), ("crtcs", ctypes.POINTER(ctypes.c_uint32)), ("count_connectors", ctypes.c_int), ("connectors", ctypes.POINTER(ctypes.c_uint32)), ("count_encoders", ctypes.c_int), ("encoders", ctypes.POINTER(ctypes.c_uint32)), ("min_width", ctypes.c_uint32), ("max_width", ctypes.c_uint32), ("min_height", ctypes.c_uint32), ("max_height", ctypes.c_uint32)] class ModeInfo(ctypes.Structure): _fields_ = [("clock", ctypes.c_uint32)] + [ (n, ctypes.c_uint16) for n in ( "hdisplay", "hsync_start", "hsync_end", "htotal", "hskew", "vdisplay", "vsync_start", "vsync_end", "vtotal", "vscan")] + [ ("vrefresh", ctypes.c_uint32), ("flags", ctypes.c_uint32), ("type", ctypes.c_uint32), ("name", ctypes.c_char * 32)] class _Connector(ctypes.Structure): _fields_ = [("connector_id", ctypes.c_uint32), ("encoder_id", ctypes.c_uint32), ("connector_type", ctypes.c_uint32), ("connector_type_id", ctypes.c_uint32), ("connection", ctypes.c_int), ("mmWidth", ctypes.c_uint32), ("mmHeight", ctypes.c_uint32), ("subpixel", ctypes.c_int), ("count_modes", ctypes.c_int), ("modes", ctypes.POINTER(ModeInfo))] # the rest (props, encoders) is not used class _Clip(ctypes.Structure): _fields_ = [("x1", ctypes.c_uint16), ("y1", ctypes.c_uint16), ("x2", ctypes.c_uint16), ("y2", ctypes.c_uint16)] def _iowr(nr, size): return (3 << 30) | (size << 16) | (ord("d") << 8) | nr _CREATE_DUMB = struct.Struct("=IIIIIIQ") # height width bpp flags handle pitch size _MAP_DUMB = struct.Struct("=IIQ") # handle pad offset DRM_IOCTL_MODE_CREATE_DUMB = _iowr(0xB2, _CREATE_DUMB.size) DRM_IOCTL_MODE_MAP_DUMB = _iowr(0xB3, _MAP_DUMB.size) def _load_libdrm(): name = ctypes.util.find_library("drm") or "libdrm.so.2" lib = ctypes.CDLL(name, use_errno=True) vp, ci, u32 = ctypes.c_void_p, ctypes.c_int, ctypes.c_uint32 lib.drmGetVersion.restype = ctypes.POINTER(_Version) lib.drmGetVersion.argtypes = [ci] lib.drmFreeVersion.argtypes = [vp] lib.drmModeGetResources.restype = ctypes.POINTER(_Res) lib.drmModeGetResources.argtypes = [ci] lib.drmModeFreeResources.argtypes = [vp] lib.drmModeGetConnector.restype = ctypes.POINTER(_Connector) lib.drmModeGetConnector.argtypes = [ci, u32] lib.drmModeFreeConnector.argtypes = [vp] lib.drmModeAddFB2.restype = ci lib.drmModeAddFB2.argtypes = [ci, u32, u32, u32, ctypes.POINTER(u32 * 4), ctypes.POINTER(u32 * 4), ctypes.POINTER(u32 * 4), ctypes.POINTER(u32), u32] lib.drmModeSetCrtc.restype = ci lib.drmModeSetCrtc.argtypes = [ci, u32, u32, u32, u32, ctypes.POINTER(u32), ci, ctypes.POINTER(ModeInfo)] lib.drmModeDirtyFB.restype = ci lib.drmModeDirtyFB.argtypes = [ci, u32, ctypes.POINTER(_Clip), u32] return lib def _errno_error(what): e = ctypes.get_errno() return DrmError("%s: %s" % (what, os.strerror(e) if e else "failed")) class DrmPanel(object): def __init__(self): self.lib = _load_libdrm() self.fd = self._open_panel() try: self._setup() except Exception: os.close(self.fd) raise def _open_panel(self): """Only ever our own device: never take over the machine's real display""" for i in range(16): try: fd = os.open("/dev/dri/card%d" % i, os.O_RDWR | os.O_CLOEXEC) except OSError: continue v = self.lib.drmGetVersion(fd) match = bool(v) and v.contents.name == DRIVER_NAME.encode() if v: self.lib.drmFreeVersion(v) if match: self.card = i return fd os.close(fd) raise DrmError("no %s DRM device found" % DRIVER_NAME) def _setup(self): lib, fd = self.lib, self.fd res = lib.drmModeGetResources(fd) if not res: raise _errno_error("no mode resources") try: r = res.contents if r.count_connectors < 1 or r.count_crtcs < 1: raise DrmError("no connector or CRTC") crtc = r.crtcs[0] conn = lib.drmModeGetConnector(fd, r.connectors[0]) finally: lib.drmModeFreeResources(res) if not conn: raise _errno_error("no connector") try: c = conn.contents if c.count_modes < 1: raise DrmError("the connector has no mode") self.mode = ModeInfo.from_buffer_copy(c.modes[0]) conn_id = ctypes.c_uint32(c.connector_id) finally: lib.drmModeFreeConnector(conn) self.width, self.height = self.mode.hdisplay, self.mode.vdisplay req = bytearray(_CREATE_DUMB.pack(self.height, self.width, 32, 0, 0, 0, 0)) fcntl.ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, req, True) _, _, _, _, handle, self.pitch, size = _CREATE_DUMB.unpack(bytes(req)) u32x4 = ctypes.c_uint32 * 4 fb_id = ctypes.c_uint32() if lib.drmModeAddFB2(fd, self.width, self.height, FORMAT_XRGB8888, ctypes.byref(u32x4(handle)), ctypes.byref(u32x4(self.pitch)), ctypes.byref(u32x4(0)), ctypes.byref(fb_id), 0): raise _errno_error("ADDFB2") self.fb_id = fb_id.value req = bytearray(_MAP_DUMB.pack(handle, 0, 0)) fcntl.ioctl(fd, DRM_IOCTL_MODE_MAP_DUMB, req, True) offset = _MAP_DUMB.unpack(bytes(req))[2] self.map = mmap.mmap(fd, size, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE, offset=offset) self.map[:] = b"\xff" * size if lib.drmModeSetCrtc(fd, crtc, self.fb_id, 0, 0, ctypes.byref(conn_id), 1, ctypes.byref(self.mode)): raise _errno_error("SETCRTC") def i2c_bus(self): """Number of the I2C bus the panel is on, from sysfs, or None""" try: dev = os.path.realpath("/sys/class/drm/card%d/device" % self.card) return int(os.path.basename(dev).split("-", 1)[0]) except (OSError, ValueError): return None def bpp(self): """The driver's bits per pixel (its module parameter); 2 if unreadable""" try: with open("/sys/module/%s/parameters/bpp" % DRIVER_NAME.replace("-", "_")) as f: return int(f.read()) except (OSError, ValueError): return 2 def present(self, data, stride, x, y, w, h): for row in range(y, y + h): src = row * stride + x * 4 dst = row * self.pitch + x * 4 self.map[dst:dst + w * 4] = data[src:src + w * 4] clip = _Clip(x, y, x + w, y + h) if self.lib.drmModeDirtyFB(self.fd, self.fb_id, ctypes.byref(clip), 1): raise _errno_error("DIRTYFB")