     }
 }
 
+// Cleanup
 smc.close()
-battery.close()+battery.close()

import Dispatch
import Foundation


/**
App version.

TODO: -v command line arg
*/
let VERSION = "0.0.1"
let MAX_WIDTH = 20.0

var HAS_BATTERY = false
var HAS_SMC     = false


// TODO: This should probably be a serial queue
// TODO: Custom serial attr prop creation causes runtime crash - Swift bug?
var source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
                                    0, 0,
                                    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0))

dispatch_source_set_timer(source, dispatch_time(DISPATCH_TIME_NOW, 0), 1 * NSEC_PER_SEC, 0)

//
//dispatch_source_set_event_handler(source, {
//    // getyx is complex macro
//    var x = getcurx(stdscr)
//    var y = getcury(stdscr)
//    
//    //getyx(stdscr,row,col)
//    move(20, 20)
//    addstr("HELLO - YEAH GCD")
//    move(y, x)
//    refresh()
//})
//
//// We have to resume the the dispatch source as it is paused by default
//dispatch_resume(source)



public struct Window {
    var size  : (length : Int32, width : Int32) = (0, 0)
    var pos   : (x : Int32, y : Int32) = (0, 0)
}


protocol Widget {

}


//------------------------------------------------------------------------------
// MARK: GLOBAL PROPERTIES
//------------------------------------------------------------------------------


/**
dshb version
*/
let DSHB_VERSION = "0.0.1"


/**
Does the machine have a battery?
*/
var HAS_BATTERY = false


/**
Does the machine have a SMC (System Management Controller)?
*/
var HAS_SMC = false


/**
Statistic update frequency in seconds
*/
var FREQ: UInt64 = 1


/**
Gap between widgets
*/
var gap: Int32 = 1


/**
Statistic widgets that are on (displayed)
*/
var widgets = [Widget]()


// TODO: This should probably be a serial queue
// TODO: Custom serial attr prop creation causes runtime crash - Swift bug?
var source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,
                                    dispatch_get_global_queue(
                                               DISPATCH_QUEUE_PRIORITY_HIGH, 0))



//------------------------------------------------------------------------------
// MARK: NCURSES SETTINGS
//------------------------------------------------------------------------------


setlocale(LC_ALL, "")
initscr()                   // Init window. Must be first
cbreak()
//nodelay(stdscr, true)     // Make input reads non-blocking - WARN - HIGH CPU!!
noecho()                    // Don't echo user input
nonl()                      // Disable newline mode
intrflush(stdscr, true)     // Prevent flush
keypad(stdscr, true)        // Enable function and arrow keys
//timeout(0)
curs_set(0)                 // Set cursor to invisible
//idlok(stdscr, true)
//scrollok(stdscr, true)


// TODO: If no colour support, is it still safe to make colour related ncurses
//       calls?
if (!has_colors()) {
    println("WARN: Terminal doesn't have support for colours")
}

// Init terminal colours
start_color()
