diff src/zamikongine/platform/linux/xlib.nim @ 38:c3c963e7c1a6

did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
author Sam <sam@basx.dev>
date Wed, 18 Jan 2023 09:52:03 +0700
parents b55d6ecde79d
children
line wrap: on
line diff
--- a/src/zamikongine/platform/linux/xlib.nim	Mon Jan 16 00:51:03 2023 +0700
+++ b/src/zamikongine/platform/linux/xlib.nim	Wed Jan 18 09:52:03 2023 +0700
@@ -1,7 +1,9 @@
 import
   x11/xlib,
   x11/xutil,
-  x11/keysym
+  x11/keysym,
+  x11/xinput,
+  x11/xinput2
 import x11/x
 
 import ../../events
@@ -16,6 +18,7 @@
   NativeWindow* = object
     display*: PDisplay
     window*: Window
+    emptyCursor: Cursor
 
 template checkXlibResult*(call: untyped) =
   let value = call
@@ -36,15 +39,24 @@
 
   let window = XCreateSimpleWindow(display, rootWindow, -1, -1, 800, 600, 0, foregroundColor, backgroundColor)
   checkXlibResult XSetStandardProperties(display, window, title, "window", 0, nil, 0, nil)
-  checkXlibResult XSelectInput(display, window, ButtonPressMask or KeyPressMask or ExposureMask)
+  checkXlibResult XSelectInput(display, window, PointerMotionMask or ButtonPressMask or ButtonReleaseMask or KeyPressMask or KeyReleaseMask or ExposureMask)
   checkXlibResult XMapWindow(display, window)
 
   deleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", XBool(false))
   checkXlibResult XSetWMProtocols(display, window, addr(deleteMessage), 1)
 
-  return NativeWindow(display: display, window: window)
+  # quite a lot of work to hide the cursor...
+  var data = "\0".cstring
+  var pixmap = XCreateBitmapFromData(display, window, data, 1, 1)
+  var color: XColor
+  var empty_cursor = XCreatePixmapCursor(display, pixmap, pixmap, addr(color), addr(color), 0, 0)
+  checkXlibResult XFreePixmap(display, pixmap)
+  checkXlibResult XDefineCursor(display, window, empty_cursor)
+
+  return NativeWindow(display: display, window: window, emptyCursor: empty_cursor)
 
 proc trash*(window: NativeWindow) =
+  checkXlibResult window.display.XFreeCursor(window.emptyCursor)
   checkXlibResult window.display.XDestroyWindow(window.window)
   discard window.display.XCloseDisplay() # always returns 0
 
@@ -62,12 +74,21 @@
       if cast[Atom](event.xclient.data.l[0]) == deleteMessage:
         result.add(Event(eventType: Quit))
     of KeyPress:
-      let xkey: KeySym = XLookupKeysym(cast[PXKeyEvent](addr(event)), 0)
-      result.add(Event(eventType: KeyDown, key: KeyTypeMap.getOrDefault(xkey, UNKNOWN)))
+      let xkey = int(cast[PXKeyEvent](addr(event)).keycode)
+      result.add Event(eventType: KeyPressed, key: KeyTypeMap.getOrDefault(xkey, Key.UNKNOWN))
     of KeyRelease:
-      let xkey: KeySym = XLookupKeysym(cast[PXKeyEvent](addr(event)), 0)
-      result.add(Event(eventType: KeyUp, key: KeyTypeMap.getOrDefault(xkey, UNKNOWN)))
+      let xkey = int(cast[PXKeyEvent](addr(event)).keycode)
+      result.add Event(eventType: KeyReleased, key: KeyTypeMap.getOrDefault(xkey, Key.UNKNOWN))
+    of ButtonPress:
+      let button = int(cast[PXButtonEvent](addr(event)).button)
+      result.add Event(eventType: MousePressed, button: MouseButtonTypeMap.getOrDefault(button, MouseButton.UNKNOWN))
+    of ButtonRelease:
+      let button = int(cast[PXButtonEvent](addr(event)).button)
+      result.add Event(eventType: MouseReleased, button: MouseButtonTypeMap.getOrDefault(button, MouseButton.UNKNOWN))
+    of MotionNotify:
+      let motion = cast[PXMotionEvent](addr(event))
+      result.add Event(eventType: MouseMoved, x: motion.x, y: motion.y)
     of ConfigureNotify:
-      result.add(Event(eventType: ResizedWindow))
+      result.add Event(eventType: ResizedWindow)
     else:
       discard