Last active
          March 6, 2022 11:18 
        
      - 
      
 - 
        
Save lilyball/569d5ba0f1b0961a15c0 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import CoreGraphics | |
| func printDisplays() { | |
| var displayCount: UInt32 = 0; | |
| var result = CGGetActiveDisplayList(0, nil, &displayCount) | |
| if (result != 0) { | |
| println("error: \(result)") | |
| return | |
| } | |
| let allocated = Int(displayCount) | |
| var activeDisplays = UnsafeMutablePointer<CGDirectDisplayID>.alloc(allocated) | |
| result = CGGetActiveDisplayList(displayCount, activeDisplays, &displayCount) | |
| if (result != 0) { | |
| println("error: \(result)") | |
| return | |
| } | |
| println("\(displayCount) displays:") | |
| for i in 0..<displayCount { | |
| println("[\(i)] - \(activeDisplays[Int(i)])") | |
| } | |
| activeDisplays.dealloc(allocated) | |
| } | |
| printDisplays() | 
      
          
      
      
            nakajijapan
  
      
      
      commented 
        Feb 17, 2016 
      
    
  
adjusted for swift 5.2:
import AVFoundation
    func printDisplays() {
        var displayCount: UInt32 = 0;
        var result = CGGetActiveDisplayList(0, nil, &displayCount)
            
        if result != .success {
            print("error: \(result)")
            return
        }
        let allocated = Int(displayCount)
        let activeDisplays = UnsafeMutablePointer<CGDirectDisplayID>.allocate(capacity: allocated)
        result = CGGetActiveDisplayList(displayCount, activeDisplays, &displayCount)
        if result != .success {
            print("error: \(result)")
            return
        }
        print("\(displayCount) displays:")
        for i in 0..<displayCount {
            print("[\(i)] - \(activeDisplays[Int(i)])")
        }
        activeDisplays.deallocate()
    }
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment