From 65c3abd6ee6b32235eedad6c0481d4014385b195 Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Sun, 29 Mar 2020 15:11:45 -0400 Subject: [PATCH] port over getChildren iterator from bouncywm --- src/nimwin.nim | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/nimwin.nim b/src/nimwin.nim index 45ff35b..9c1befe 100644 --- a/src/nimwin.nim +++ b/src/nimwin.nim @@ -1,6 +1,52 @@ import x11/xlib, x11/xutil, x11/x, x11/keysym import threadpool, osproc +var root : TWindow + +type Window = ref object of RootObj + x : cint + y : cint + width : cint + height : cint + win : TWindow + screen : PScreen + +iterator getChildren(display : PDisplay, rootHeight : int, rootWidth : int) : Window = + var currentWindow : PWindow + var rootReturn : TWindow + var parentReturn : TWindow + var childrenReturn : PWindow + var nChildrenReturn : cuint + + discard XQueryTree(display, + root, + rootReturn.addr, + parentReturn.addr, + childrenReturn.addr, + nChildrenReturn.addr) + + + for i in 0..(nChildrenReturn.int - 1): + var attr : TXWindowAttributes + + currentWindow = cast[PWindow]( + cast[uint](childrenReturn) + cast[uint](i * currentWindow[].sizeof) + ) + + if display.XGetWindowAttributes(currentWindow[], attr.addr) == BadWindow: + continue + + yield Window( + x: attr.x.cint, + y: attr.y.cint, + width: attr.width, + height: attr.height, + win: currentWindow[], + screen: attr.screen + ) + + discard XFree(childrenReturn) + proc getDisplay : PDisplay = result = XOpenDisplay(nil) if result == nil: @@ -56,6 +102,8 @@ when isMainModule: let display = getDisplay() + root = DefaultRootWindow(display) + display.grabKeys display.grabMouse(1) display.grabMouse(3)