var xcb = require ('./build/default/xcb')
  , wins = []
  , width = 800
  , height = 600
  , curGroup = 0
  , groups = [[]]
  , root = xcb.getScreen()
console.log('root: ', root)

function mod(x, by) {
  return (x > 0) ? x % by
         : (by - (Math.abs(x) % by)) % by
}

function nextGroup(x) {
  var t = curGroup + x
  t = mod(t, groups.length)
  changeGroup(t)
}

function sizeWindows() {
  var group = groups[curGroup]
    , len = group.length
    , winWidth = Math.floor(width / len)
  group.forEach(function(window, i) {
    console.log("\t*resizing window : ", window)
    xcb.ConfigureWindow({ window: window, value_mask: 15, value_list: [i * winWidth, 0, winWidth, 600] })
  })
  xcb.flush();
}

xcb.QueryTree({ window: root.root }, function(resp) {
  console.log("Query Tree Response")
  console.log(resp)
})

function changeGroup(num) {
  if (num === curGroup) return
  console.log('switching groups to', num)
  groups[curGroup].forEach(function(window) {
    xcb.UnmapWindow({ window: window })
  })
  curGroup = num
  groups[curGroup].forEach(function(window) {
    xcb.MapWindow({ window: window })
  })
  sizeWindows()
}

xcb.manageWindows();
xcb.onCreate = function(ev) {
  console.log("Window being created, indexing", ev.window)
  console.log('\tParent -> ', ev.parent)
  wins.push(ev.window);
  xcb.GetWindowAttributes({ window: ev.window }, function(reply) {
    console.log('Response from window ', ev.window, ' configuration!', reply)
  })
  if (groups[curGroup].length == 2) groups.push([])
    , changeGroup(groups.length - 1)
  groups[curGroup].push(ev.window)
  sizeWindows()
}

xcb.onDestroy = function(ev) {
  var win = ev.window
    , index = wins.indexOf(win)
  if (index != -1) {
    console.log('window destroyed, removing from index', win)
    wins.splice(index, 1)
    groups.forEach(function(group, i) {
      var index = group.indexOf(win)
      if (index != -1) {
        group.splice(index, 1)
        if (i === curGroup) sizeWindows()
      }
    })
  }
  else console.log('we werent watching the window', win)
}

xcb.onMapRequest = function(ev) {
  console.log('\tmapping win', ev.window)
  xcb.MapWindow({ window: ev.window })
  xcb.flush();
}

xcb.onUnmap = function(ev) {
  console.log('\t*unmapping win', ev.window)
}

xcb.onKeyPress = function(event) {
  console.log('Keypress')
  console.log(event)
  if (event.state & 8) {
    switch (event.detail) {
      case 113: return nextGroup(-1)
      case 114: return nextGroup(1)
      case 10:  return changeGroup(0)
      case 11:  return changeGroup(1)
      case 12:  return changeGroup(2)
    }
  }
}

@@ -745,20 +745,21 @@
 unction(
-resp
+qtree
 ) %7B%0A  co
@@ -809,12 +809,325 @@
 log(
-resp
+qtree)%0A  qtree.children.forEach(function(child) %7B%0A    xcb.GetWindowAttributes(%7B window: child %7D, function(attrs) %7B%0A      console.log('wid', child, attrs)%0A      if (!(attrs.override_redirect %7C%7C attrs.map_state == xcb.MapState.Unmapped)) %7B%0A        console.log('managing', child)%0A        manage(child)%0A      %7D%0A    %7D)%0A  %7D
 )%0A%7D)
@@ -1600,32 +1600,8 @@
 nt)%0A
-  wins.push(ev.window);%0A
   xc
@@ -1746,16 +1746,80 @@
 y)%0A  %7D)%0A
+  manage(ev.window)%0A%7D%0A%0Afunction manage(wid) %7B%0A  wins.push(wid);%0A
   if (gr
@@ -1923,25 +1923,19 @@
 p%5D.push(
-ev.window
+wid
 )%0A  size

add a basic scan and grab existing windows.