 var onlineDisplayIDs = UnsafeMutablePointer<CGDirectDisplayID>.alloc(Int(maxDisplays))
 
 func main () -> Void {
-    var error:CGError = CGGetOnlineDisplayList(maxDisplays, onlineDisplayIDs, &displayCount32)
-    
-    if (error != MyCGError.kCGErrorSuccess.rawValue) {
-		println("Error on getting online display List.");
+    let error:CGError = CGGetOnlineDisplayList(maxDisplays, onlineDisplayIDs, &displayCount32)
+    
+    if (error != .Success) {
+		print("Error on getting online display List.");
         return
     }
 

                "   -m 0        list all mode from a certain display\n",
                "   -s 0 800    set resolution of display 0 to 800*600\n",
                ])
    var help_display_list = "List all available displays by:\n    \(binary_name) -l"
    

    let argc = Process.arguments.count
    if argc > 1 {
        if Process.arguments[1] == "-l" || Process.arguments[1] == "--list" {
            listDisplays(onlineDisplayIDs, displayCount)
            return
        }

        if Process.arguments[1] == "-m" || Process.arguments[1] == "--mode" {
            if argc < 3 {
                println("Specify a display to see its supported modes. \(help_display_list)")
                return
            }

            if let displayIndex = Process.arguments[2].toInt() {
                if displayIndex < displayCount {
                    let _info = listModesByDisplayID(onlineDisplayIDs[displayIndex]) 
                    displayModes(onlineDisplayIDs[displayIndex], displayIndex, _info)
                    
                } else {
                    println("Display index: \(displayIndex) not found. \(help_display_list)")
                }
            } else {
                println("Display index should be a number. \(help_display_list)")
            }
            return
        }

        if Process.arguments[1] == "-s" || Process.arguments[1] == "--set" {
            if argc < 3 {
                println("Specify a display to set its mode. \(help_display_list)")
                return
            }

            if argc < 4 {
                println("Specify display width")
                return
            }


            if let displayIndex = Process.arguments[2].toInt() {
                if displayIndex < displayCount {
                    if let designatedWidth = Process.arguments[3].toUInt() {
                        if let modesArray = listModesByDisplayID(onlineDisplayIDs[displayIndex]) {
                            var designatedWidthIndex:Int?
                            for i in 0..<modesArray.count {
                                var di = displayInfo(onlineDisplayIDs[displayIndex], modesArray[i])
                                if di.width == designatedWidth {
                                    designatedWidthIndex = i
                                    break
                                }
                            }
                            if designatedWidthIndex != nil {
                                println("setting display mode")
                                setDisplayMode(onlineDisplayIDs[displayIndex], modesArray[designatedWidthIndex!], designatedWidth)
                            }
                            else {
                                println("This mode is unavailable for current desktop GUI")
                            }
                        }
                    }
                } else {
                    println("Display index: \(displayIndex) not found. \(help_display_list)")
                }                
            }
            


// print a list of all displays
// used by -l
func listDisplays(var displayIDs:UnsafeMutablePointer<CGDirectDisplayID>, let count:Int) -> Void {
    for i in 0..<count {
        let di = displayInfo(displayIDs[i], mode:nil)
        print("Display \(i):  \(di.width) * \(di.height) @ \(di.frequency)Hz")
    }
}

