Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Fullscreen detection using Core Graphic

Hi, I am trying to detect if all the screen are in fullscreen mode.

The current approach is to get all windows' information from CGWindowListCopyWindowInfo and then compare the frame and coordinate with the frame of NSScreen.screens.

However, there is a problem, the y position of the window seems to be relative to the screen. As it is not absolute position, I cannot compare it with the coordinate of the screen.

Does anyone know if there are other information that I can use? Or is there a way to retrieve the absolute position or screen ID from the GCWIndow object?

Answered by DTS Engineer in 834006022

You can use the Quartz Display Services' CGDisplayBounds(_:) function to get the bounds of a display in global coordinates.

For example:

import Foundation
import CoreGraphics

var mainDisplay: CGDirectDisplayID = CGMainDisplayID()
var displayCount: UInt32 = 0
let maxDisplays: UInt32 = 100
let displaysList: UnsafeMutablePointer<CGDirectDisplayID> = UnsafeMutablePointer<CGDirectDisplayID>.allocate(capacity: Int(maxDisplays))
var status: CGError = CGGetOnlineDisplayList(maxDisplays, displaysList, &displayCount)
guard status == CGError.success else {
    fatalError("Couldn't get online displays list")
}
for displayIndex: Int in 0..<Int(displayCount) {
    let nthDisplayID: CGDirectDisplayID = displaysList[displayIndex]
    print("Display #\(displayIndex):")
    print(" bounds: \(CGDisplayBounds(nthDisplayID))")
}
Accepted Answer

You can use the Quartz Display Services' CGDisplayBounds(_:) function to get the bounds of a display in global coordinates.

For example:

import Foundation
import CoreGraphics

var mainDisplay: CGDirectDisplayID = CGMainDisplayID()
var displayCount: UInt32 = 0
let maxDisplays: UInt32 = 100
let displaysList: UnsafeMutablePointer<CGDirectDisplayID> = UnsafeMutablePointer<CGDirectDisplayID>.allocate(capacity: Int(maxDisplays))
var status: CGError = CGGetOnlineDisplayList(maxDisplays, displaysList, &displayCount)
guard status == CGError.success else {
    fatalError("Couldn't get online displays list")
}
for displayIndex: Int in 0..<Int(displayCount) {
    let nthDisplayID: CGDirectDisplayID = displaysList[displayIndex]
    print("Display #\(displayIndex):")
    print(" bounds: \(CGDisplayBounds(nthDisplayID))")
}
Fullscreen detection using Core Graphic
 
 
Q