How to resolve CGDirectDisplayID changing issues on newer multi-GPU Apple laptops in Core Foundation/IO Kit?

后端 未结 3 1397
再見小時候
再見小時候 2021-02-04 10:14

In Mac OS X, every display gets a unique CGDirectDisplayID number assigned to it. You can use CGGetActiveDisplayList() or [NSScreen screens]

3条回答
  •  青春惊慌失措
    2021-02-04 11:00

    Use CFUUIDRef which can be obtained using:

    CGDisplayCreateUUIDFromDisplayID(CGDirectDisplayID displayID) and you can get the display ID back using:

    CGDisplayGetDisplayIDFromUUID(CFUUIDRef uuid)

    This is what I'm using to uniquely identify displays even when their CGDirectDisplayID changes, for example was plugged into a different port. These functions aren't properly documented by Apple unfortunately, but my testing on multiple Macs with multiple displays shown that the CFUUIDRef obtained is unique and consistent -even after a reboot-, regardless of whether CGDirectDisplayID changed for whatever reason.

    To check if a display is new/unique, take its CGDirectDisplayID and convert it to CFUUIDRef, and then compare the UUID, it is a many-to-one relationship, many CGDirectDisplayIDs will map to a single CFUUIDRef.

    These API calls are available in ApplicationServices in 10.7 - 10.12, and ColorSync since 10.13.

提交回复
热议问题