darwin_pci.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """PCI display controllers on macOS, read from the IOKit registry.
  2. Intel Macs name their GPU vendors here; Apple silicon has no PCI display controller. Read through
  3. ctypes without environment-variable input or subprocesses.
  4. """
  5. from __future__ import annotations
  6. import ctypes
  7. import ctypes.util
  8. _UTF8 = 0x08000100 # kCFStringEncodingUTF8
  9. _DISPLAY_CLASS = 0x03 # PCI base class: display controller
  10. _MAX_DEVICES = 256
  11. def _data_int(cf, ref: int | None) -> int | None:
  12. """Little-endian integer from a CFData property, or ``None`` for a missing or non-data value."""
  13. if not ref:
  14. return None
  15. try:
  16. if cf.CFGetTypeID(ref) != cf.CFDataGetTypeID():
  17. return None
  18. size = cf.CFDataGetLength(ref)
  19. return int.from_bytes(ctypes.string_at(cf.CFDataGetBytePtr(ref), size), "little") if size else None
  20. finally:
  21. cf.CFRelease(ref)
  22. def _bind(iokit, cf) -> None:
  23. cf.CFStringCreateWithCString.restype = ctypes.c_void_p
  24. cf.CFStringCreateWithCString.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_uint32]
  25. cf.CFGetTypeID.restype = cf.CFDataGetTypeID.restype = ctypes.c_ulong
  26. cf.CFGetTypeID.argtypes = [ctypes.c_void_p]
  27. cf.CFDataGetLength.restype = ctypes.c_long
  28. cf.CFDataGetLength.argtypes = [ctypes.c_void_p]
  29. cf.CFDataGetBytePtr.restype = ctypes.c_void_p
  30. cf.CFDataGetBytePtr.argtypes = [ctypes.c_void_p]
  31. cf.CFRelease.argtypes = [ctypes.c_void_p]
  32. iokit.IOServiceMatching.restype = ctypes.c_void_p
  33. iokit.IOServiceMatching.argtypes = [ctypes.c_char_p]
  34. iokit.IOServiceGetMatchingServices.argtypes = [ctypes.c_uint32, ctypes.c_void_p, ctypes.POINTER(ctypes.c_uint32)]
  35. iokit.IOIteratorNext.restype = ctypes.c_uint32
  36. iokit.IOIteratorNext.argtypes = [ctypes.c_uint32]
  37. iokit.IORegistryEntryCreateCFProperty.restype = ctypes.c_void_p
  38. iokit.IORegistryEntryCreateCFProperty.argtypes = [ctypes.c_uint32, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint32]
  39. iokit.IOObjectRelease.argtypes = [ctypes.c_uint32]
  40. def display_vendor_ids() -> list[str] | None:
  41. """Hex PCI vendor ids of every display controller, or ``None`` when IOKit cannot be read."""
  42. iokit = ctypes.CDLL(ctypes.util.find_library("IOKit"))
  43. cf = ctypes.CDLL(ctypes.util.find_library("CoreFoundation"))
  44. _bind(iokit, cf)
  45. iterator = ctypes.c_uint32()
  46. # The matching dictionary is consumed by the call; port 0 is kIOMainPortDefault.
  47. if iokit.IOServiceGetMatchingServices(0, iokit.IOServiceMatching(b"IOPCIDevice"), ctypes.byref(iterator)) != 0:
  48. return None
  49. keys = {name: cf.CFStringCreateWithCString(None, name.encode(), _UTF8) for name in ("class-code", "vendor-id")}
  50. vendors: list[str] = []
  51. try:
  52. for _ in range(_MAX_DEVICES):
  53. entry = iokit.IOIteratorNext(iterator)
  54. if not entry:
  55. break
  56. try:
  57. read = {name: _data_int(cf, iokit.IORegistryEntryCreateCFProperty(entry, key, None, 0))
  58. for name, key in keys.items()}
  59. finally:
  60. iokit.IOObjectRelease(entry)
  61. if read["class-code"] is not None and read["class-code"] >> 16 == _DISPLAY_CLASS:
  62. vendors.append("" if read["vendor-id"] is None else f"{read['vendor-id'] & 0xFFFF:04x}")
  63. finally:
  64. iokit.IOObjectRelease(iterator.value)
  65. for key in keys.values():
  66. cf.CFRelease(key)
  67. return vendors