// 列出 macOS 目前「有接上」與「啟用中」的顯示器
// 用法：swift displays.swift
//
// 注意：被 CGSConfigureDisplayEnabled 停用的螢幕不會出現在 online 清單，
// 要判斷螢幕是「沒接上」還是「被停用」，改查硬體層：
//   ioreg -lw0 | grep -o '"ProductName" = "[^"]*"'
import AppKit

var online = [CGDirectDisplayID](repeating: 0, count: 16); var nOnline: UInt32 = 0
CGGetOnlineDisplayList(16, &online, &nOnline)
var active = [CGDirectDisplayID](repeating: 0, count: 16); var nActive: UInt32 = 0
CGGetActiveDisplayList(16, &active, &nActive)
let activeSet = Set(active.prefix(Int(nActive)))

print("online（有接上）: \(nOnline) 台，active（啟用中）: \(nActive) 台")
for id in online.prefix(Int(nOnline)) {
    let uuid = CGDisplayCreateUUIDFromDisplayID(id)?.takeRetainedValue()
    let u = uuid.map { CFUUIDCreateString(nil, $0) as String } ?? "?"
    let b = CGDisplayBounds(id)
    print(String(format: "  id=%-3u builtin=%@ active=%@ vendor=%u model=%u %dx%d uuid=%@",
        id, CGDisplayIsBuiltin(id) != 0 ? "Y" : "N", activeSet.contains(id) ? "Y" : "N",
        CGDisplayVendorNumber(id), CGDisplayModelNumber(id), Int(b.width), Int(b.height), u))
}
