Guest User

Untitled

a guest
Nov 18th, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #[
  2. Whim - Minimal floating window manager in Nim.
  3. Based off of https://github.com/mackstann/tinywm.
  4. ]#
  5.  
  6. import
  7. std/strformat,
  8. std/tables,
  9.  
  10. x11/xlib,
  11. x11/xutil,
  12. x11/x
  13.  
  14. type
  15. KeyMapping* = ref object
  16. code: char
  17. modifiers: cuint
  18.  
  19. Whim = object
  20. dpy: PDisplay
  21. keys: Table[KeyMapping, string]
  22.  
  23. var
  24. wm = Whim()
  25.  
  26. proc grabKeys*(self: Whim) =
  27. let root = DefaultRootWindow(self.dpy)
  28.  
  29. for key, command in self.keys:
  30. discard XGrabKey(self.dpy,
  31. cast[cint](key.code),
  32. key.modifiers,
  33. root,
  34. 1,
  35. GrabModeAsync,
  36. GrabModeAsync)
  37.  
  38. proc makeKeyMapping(xk: XKeyEvent): KeyMapping =
  39. KeyMapping(code: cast[char](xk.keycode), modifiers: xk.state)
  40.  
  41. proc initX() =
  42. # open a connection to the display
  43. wm.dpy = XOpenDisplay(nil)
  44. wm.keys = initTable[KeyMapping, string]()
  45.  
  46. proc mainLoop() =
  47. var event: XEvent
  48.  
  49. while true:
  50. # Get the next event from X
  51. discard XNextEvent(wm.dpy, event.addr)
  52.  
  53. if event.theType == KeyPress and event.xkey.subwindow != None:
  54. #let mapping = makeKeyMapping(event.xkey)
  55. if makeKeyMapping(event.xkey) in wm.keys: # error here
  56. echo "key was pressed"
  57.  
  58. discard XRaiseWindow(wm.dpy, event.xkey.subwindow)
  59.  
  60. when isMainModule:
  61. initX()
  62. mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment