|
| 1 | +import xlib, x |
| 2 | +converter toCint(x: TKeyCode): cint = x.cint |
| 3 | +converter int32toCUint(x: int32): cuint = x.cuint |
| 4 | +converter toTBool(x: bool): TBool = x.TBool |
| 5 | +converter toBool(x: TBool): bool = x.bool |
| 6 | + |
| 7 | +type XWindowInfo = object |
| 8 | + display*: PDisplay |
| 9 | + attr*: TXWindowAttributes |
| 10 | + start*: TXButtonEvent |
| 11 | + ev*: TXEvent |
| 12 | + |
| 13 | +proc initXWIndowInfo(winInfo: var XWindowInfo): XWIndowInfo = |
| 14 | + winInfo.display = XOpenDisplay(nil) |
| 15 | + if winInfo.display == nil: |
| 16 | + quit "Failed to open display" |
| 17 | + |
| 18 | + discard XGrabKey(winInfo.display, XKeysymToKeycode(winInfo.display, XStringToKeysym("F1")), Mod1Mask, |
| 19 | + DefaultRootWindow(winInfo.display), true, GrabModeAsync, GrabModeAsync) |
| 20 | + discard XGrabButton(winInfo.display, 1, Mod1Mask, DefaultRootWindow(winInfo.display), true, |
| 21 | + ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None) |
| 22 | + discard XGrabButton(winInfo.display, 3, Mod1Mask, DefaultRootWindow(winInfo.display), true, |
| 23 | + ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None) |
| 24 | + |
| 25 | + winInfo.start.subwindow = None |
| 26 | + |
| 27 | + return winInfo |
| 28 | + |
| 29 | +when isMainModule: |
| 30 | + |
| 31 | + var winInfo: XWindowInfo |
| 32 | + |
| 33 | + winInfo = initXWIndowInfo(winInfo) |
| 34 | + |
| 35 | + while true: |
| 36 | + discard XNextEvent(winInfo.display, winInfo.ev.addr) |
| 37 | + |
| 38 | + if winInfo.ev.theType == KeyPress: |
| 39 | + var kev = cast[PXKeyEvent](winInfo.ev.addr)[] |
| 40 | + if not kev.subwindow.addr.isNil: |
| 41 | + discard XLowerWindow(winInfo.display, kev.subwindow) |
| 42 | + |
| 43 | + elif winInfo.ev.theType == ButtonPress: |
| 44 | + var bev = cast[PXButtonEvent](winInfo.ev.addr)[] |
| 45 | + if not bev.subwindow.addr.isNil: |
| 46 | + discard XGrabPointer(winInfo.display, bev.subwindow, true, |
| 47 | + PointerMotionMask or ButtonReleaseMask, GrabModeAsync, |
| 48 | + GrabModeAsync, None, None, CurrentTime) |
| 49 | + discard XGetWindowAttributes(winInfo.display, bev.subwindow, winInfo.attr.addr); |
| 50 | + winInfo.start = bev; |
| 51 | + elif winInfo.ev.theType == MotionNotify: |
| 52 | + var mnev = cast[PXMotionEvent](winInfo.ev.addr)[] |
| 53 | + var bev = cast[PXButtonEvent](winInfo.ev.addr)[] |
| 54 | + while XCheckTypedEvent(winInfo.display, MotionNotify, winInfo.ev.addr): |
| 55 | + continue |
| 56 | + var |
| 57 | + xdiff = bev.x_root - winInfo.start.x_root |
| 58 | + ydiff = bev.y_root - winInfo.start.y_root |
| 59 | + discard XMoveResizeWindow(winInfo.display, mnev.window, |
| 60 | + winInfo.attr.x + (if winInfo.start.button==1: xdiff else: 0), |
| 61 | + winInfo.attr.y + (if winInfo.start.button==1: ydiff else: 0), |
| 62 | + max(1, winInfo.attr.width + (if winInfo.start.button==3: xdiff else: 0)), |
| 63 | + max(1, winInfo.attr.height + (if winInfo.start.button==3: ydiff else: 0))) |
| 64 | + |
| 65 | + elif winInfo.ev.theType == ButtonRelease: |
| 66 | + discard XUngrabPointer(winInfo.display, CurrentTime) |
| 67 | + else: |
| 68 | + continue |
0 commit comments