From a87e3460be918b491878b151991ece4e5246819e Mon Sep 17 00:00:00 2001 From: kitty piapiac Date: Thu, 30 Jul 2020 11:01:46 -0700 Subject: =?UTF-8?q?=CE=BB=20rework=20handling=20key=20events?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config.nim | 2 +- src/keys.nim | 18 ++++++++++++++++++ src/types.nim | 18 ------------------ src/windowmanager.nim | 50 +++++++++++++++----------------------------------- 4 files changed, 34 insertions(+), 54 deletions(-) create mode 100644 src/keys.nim delete mode 100644 src/types.nim (limited to 'src') diff --git a/src/config.nim b/src/config.nim index 628a4ff..cd5b802 100644 --- a/src/config.nim +++ b/src/config.nim @@ -1,4 +1,4 @@ -import types, x11/x +import /keys, x11/x # settings, or something along those lines const diff --git a/src/keys.nim b/src/keys.nim new file mode 100644 index 0000000..bfe79aa --- /dev/null +++ b/src/keys.nim @@ -0,0 +1,18 @@ +type + KeyFunc* = enum + closeWindow, + nextWindow, + spawnCustom + + Key* = object + mods*: cuint + key*: string + keyfunc*: KeyFunc + command*: string + +proc initKey* (keyfunc: KeyFunc, mods: cuint, key: string, command = ""): Key = + return Key( + mods: mods, + key: key, + command: command, + keyfunc: keyfunc) \ No newline at end of file diff --git a/src/types.nim b/src/types.nim deleted file mode 100644 index bfe79aa..0000000 --- a/src/types.nim +++ /dev/null @@ -1,18 +0,0 @@ -type - KeyFunc* = enum - closeWindow, - nextWindow, - spawnCustom - - Key* = object - mods*: cuint - key*: string - keyfunc*: KeyFunc - command*: string - -proc initKey* (keyfunc: KeyFunc, mods: cuint, key: string, command = ""): Key = - return Key( - mods: mods, - key: key, - command: command, - keyfunc: keyfunc) \ No newline at end of file diff --git a/src/windowmanager.nim b/src/windowmanager.nim index a823c33..04636a3 100644 --- a/src/windowmanager.nim +++ b/src/windowmanager.nim @@ -1,6 +1,6 @@ import x11/[x, xlib], - config, /types, + config, /keys, logging, /logger, tables, os @@ -11,21 +11,14 @@ type colormap: Colormap root: Window - keyhandlers: Table[cuint, proc (wm: WindowManager)] - clients: seq[Window] + keys: Table[cuint, Key] # Initialiazation stuff proc initKeybindings (wm: WindowManager) proc initButtons (wm: WindowManager) proc initCommands (wm: WindowManager) -# KeyFunc Handlers -proc procFromFunc (wm: WindowManager, keyfunc: KeyFunc): proc (wm: WindowManager) -proc funcCloseWindow (wm: WindowManager) -proc funcNextWindow (wm: WindowManager) -proc funcSpawnCustom (wm: WindowManager) - # Error Handlers proc onWMDetected (display: PDisplay, e: PXErrorEvent): cint{.cdecl.} proc onXError (display: PDisplay, e: PXErrorEvent): cint{.cdecl.} @@ -59,7 +52,6 @@ proc createWindowManager*: WindowManager = var screen = display.DefaultScreenOfDisplay() - keyhandlers = initTable[cuint, proc (wm: WindowManager)](1) return WindowManager( display: display, @@ -67,9 +59,8 @@ proc createWindowManager*: WindowManager = colormap: screen.DefaultColormapOfScreen(), root: display.DefaultRootWindow(), - keyhandlers: keyhandlers, - - clients: @[]) + clients: @[], + keys: initTable[cuint, Key](1)) # Run window manager proc run* (wm: WindowManager) = @@ -111,12 +102,9 @@ proc initKeybindings (wm: WindowManager) = discard wm.display.XUngrabKey(AnyKey, AnyModifier, wm.root) for key in config.keybindings: - var - keysym = XStringToKeysym key.key - keycode = wm.display.XKeysymToKeycode keysym - keyfunc = wm.procFromFunc key.keyfunc - - wm.keyhandlers[cuint keycode] = keyfunc + let keycode = wm.display.XKeysymToKeycode(XStringToKeysym key.key) + + wm.keys[cuint keycode] = key discard wm.display.XGrabKey( cint keycode, @@ -145,19 +133,9 @@ proc initCommands (wm: WindowManager) = for cmd in config.init: discard execShellCmd cmd -# KeyFunc Handlers -proc procFromFunc (wm: WindowManager, keyfunc: KeyFunc): proc (wm: WindowManager) = - let procFuncTable = { - closeWindow: funcCloseWindow, - nextWindow: funcNextWindow, - spawnCustom: funcSpawnCustom}.toTable - - return procFuncTable[keyfunc] - -# TODO: These -proc funcCloseWindow (wm: WindowManager) = return -proc funcNextWindow (wm: WindowManager) = return -proc funcSpawnCustom (wm: WindowManager) = return +proc λcloseWindow (wm: WindowManager) = return +proc λnextWindow (wm: WindowManager) = return +proc λspawnCustom (wm: WindowManager, key: Key) = return # Error Handlers proc onWMDetected (display: PDisplay, e: PXErrorEvent): cint{.cdecl.} = @@ -239,8 +217,10 @@ proc onButtonPress (wm: WindowManager, e: PXButtonEvent) = return proc onButtonRelease (wm: WindowManager, e: PXButtonEvent) = return proc onMotionNotify (wm: WindowManager, e: PXMotionEvent) = return proc onKeyPress (wm: WindowManager, e: PXKeyEvent) = - lvlDebug.log("key event " & $e.keycode) - var handler = wm.keyhandlers[e.keycode] - handler wm + let key = wm.keys[e.keycode] + case key.keyfunc: + of closeWindow: wm.λcloseWindow() + of nextWindow: wm.λnextWindow() + of spawnCustom: wm.λspawnCustom key proc onKeyRelease (wm: WindowManager, e: PXKeyEvent) = return \ No newline at end of file -- cgit v1.2.3