From b1745b4d2e449e07aa2b08a9def6894ec6d0fe53 Mon Sep 17 00:00:00 2001 From: kitty piapiac Date: Thu, 30 Jul 2020 09:29:57 -0700 Subject: =?UTF-8?q?(=E3=80=82=E3=83=98=C2=B0)=20stop=20using=20XQueryTree?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sequence of windows on the window manager --- src/ptrmath.nim | 19 ------------------- src/windowmanager.nim | 46 ++++++++++++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 37 deletions(-) delete mode 100644 src/ptrmath.nim diff --git a/src/ptrmath.nim b/src/ptrmath.nim deleted file mode 100644 index 7de2fa6..0000000 --- a/src/ptrmath.nim +++ /dev/null @@ -1,19 +0,0 @@ -# from http://forum.nim-lang.org/t/1188#7366 - -template `+`*[T](p: ptr T, off: int): ptr T = - cast[ptr type(p[])](cast[ByteAddress](p) +% off * sizeof(p[])) - -template `+=`*[T](p: ptr T, off: int) = - p = p + off - -template `-`*[T](p: ptr T, off: int): ptr T = - cast[ptr type(p[])](cast[ByteAddress](p) -% off * sizeof(p[])) - -template `-=`*[T](p: ptr T, off: int) = - p = p - off - -template `[]`*[T](p: ptr T, off: int): T = - (p + off)[] - -template `[]=`*[T](p: ptr T, off: int, val: T) = - (p + off)[] = val \ No newline at end of file diff --git a/src/windowmanager.nim b/src/windowmanager.nim index 9aa43b8..a823c33 100644 --- a/src/windowmanager.nim +++ b/src/windowmanager.nim @@ -2,7 +2,7 @@ import x11/[x, xlib], config, /types, logging, /logger, - tables, os, ptrmath + tables, os type WindowManager* = ref object @@ -13,6 +13,8 @@ type keyhandlers: Table[cuint, proc (wm: WindowManager)] + clients: seq[Window] + # Initialiazation stuff proc initKeybindings (wm: WindowManager) proc initButtons (wm: WindowManager) @@ -28,7 +30,8 @@ proc funcSpawnCustom (wm: WindowManager) proc onWMDetected (display: PDisplay, e: PXErrorEvent): cint{.cdecl.} proc onXError (display: PDisplay, e: PXErrorEvent): cint{.cdecl.} -# Main Loop +# Function +proc addWindow (wm: WindowManager, w: Window) proc tileWindows (wm: WindowManager) # Events @@ -64,7 +67,9 @@ proc createWindowManager*: WindowManager = colormap: screen.DefaultColormapOfScreen(), root: display.DefaultRootWindow(), - keyhandlers: keyhandlers) + keyhandlers: keyhandlers, + + clients: @[]) # Run window manager proc run* (wm: WindowManager) = @@ -178,39 +183,44 @@ proc onXError (display: PDisplay, e: PXErrorEvent): cint{.cdecl.} = return 0 -# Main Loop + +proc addWindow (wm: WindowManager, w: Window) = + wm.clients.add w + proc tileWindows (wm: WindowManager) = - var - parent: Window - children: PWindow - n: cuint + var + n = cuint wm.clients.len w = cuint wm.display.XDisplayWidth 0 h = cuint wm.display.XDisplayHeight 0 - discard wm.display.XQueryTree(wm.root, addr wm.root, addr parent, addr children, addr n) - + if n == 0: + return if n == 1: - # only 1 window, keep it fullscreen - discard wm.display.XMoveResizeWindow(children[0], 0, 0, w, h) + discard wm.display.XMoveResizeWindow(wm.clients[0], 0, 0, w, h) else: # resize master window to take up half the screen - discard wm.display.XMoveResizeWindow(children[0], 0, 0, cuint (w div 2), h) + discard wm.display.XMoveResizeWindow(wm.clients[0], 0, 0, cuint (w div 2), h) # maths, sort of explained here: https://i.imgur.com/fGxdfDh.png - for i in 1..n-1: - discard wm.display.XMoveResizeWindow(children[int i], cint w div 2, cint (i-1) * (h div (n-1)), w div 2, h div (n-1)) + for i in 1..wm.clients.len-1: + discard wm.display.XMoveResizeWindow(wm.clients[i], cint w div 2, cint (i-1) * cint (h div (n-1)), w div 2, h div (n-1)) # Events proc onCreateNotify (wm: WindowManager, e: PXCreateWindowEvent) = return proc onDestroyNotify (wm: WindowManager, e: PXDestroyWindowEvent) = return proc onReparentNotify (wm: WindowManager, e: PXReparentEvent) = return proc onMapNotify (wm: WindowManager, e: PXMapEvent) = return -proc onUnmapNotify (wm: WindowManager, e: PXUnmapEvent) = return +proc onUnmapNotify (wm: WindowManager, e: PXUnmapEvent) = + wm.clients.delete wm.clients.find(e.window) + # seq.delete preserves order while seq.del may scramble it a little + wm.tileWindows() + proc onConfigureNotify (wm: WindowManager, e: PXConfigureEvent) = return proc onMapRequest (wm: WindowManager, e: PXMapRequestEvent) = - discard wm.display.XMapWindow(e.window) - tileWindows wm + discard wm.display.XMapWindow e.window + wm.addWindow e.window + wm.tileWindows() proc onConfigureRequest (wm: WindowManager, e: PXConfigureRequestEvent) = var changes: XWindowChanges -- cgit v1.2.3