comparison 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
comparison
equal deleted inserted replaced
37:6859bcfabc62 38:c3c963e7c1a6
1 import 1 import
2 x11/xlib, 2 x11/xlib,
3 x11/xutil, 3 x11/xutil,
4 x11/keysym 4 x11/keysym,
5 x11/xinput,
6 x11/xinput2
5 import x11/x 7 import x11/x
6 8
7 import ../../events 9 import ../../events
8 10
9 import ./symkey_map 11 import ./symkey_map
14 16
15 type 17 type
16 NativeWindow* = object 18 NativeWindow* = object
17 display*: PDisplay 19 display*: PDisplay
18 window*: Window 20 window*: Window
21 emptyCursor: Cursor
19 22
20 template checkXlibResult*(call: untyped) = 23 template checkXlibResult*(call: untyped) =
21 let value = call 24 let value = call
22 if value == 0: 25 if value == 0:
23 raise newException(Exception, "Xlib error: " & astToStr(call) & " returned " & $value) 26 raise newException(Exception, "Xlib error: " & astToStr(call) & " returned " & $value)
34 foregroundColor = XBlackPixel(display, screen) 37 foregroundColor = XBlackPixel(display, screen)
35 backgroundColor = XWhitePixel(display, screen) 38 backgroundColor = XWhitePixel(display, screen)
36 39
37 let window = XCreateSimpleWindow(display, rootWindow, -1, -1, 800, 600, 0, foregroundColor, backgroundColor) 40 let window = XCreateSimpleWindow(display, rootWindow, -1, -1, 800, 600, 0, foregroundColor, backgroundColor)
38 checkXlibResult XSetStandardProperties(display, window, title, "window", 0, nil, 0, nil) 41 checkXlibResult XSetStandardProperties(display, window, title, "window", 0, nil, 0, nil)
39 checkXlibResult XSelectInput(display, window, ButtonPressMask or KeyPressMask or ExposureMask) 42 checkXlibResult XSelectInput(display, window, PointerMotionMask or ButtonPressMask or ButtonReleaseMask or KeyPressMask or KeyReleaseMask or ExposureMask)
40 checkXlibResult XMapWindow(display, window) 43 checkXlibResult XMapWindow(display, window)
41 44
42 deleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", XBool(false)) 45 deleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", XBool(false))
43 checkXlibResult XSetWMProtocols(display, window, addr(deleteMessage), 1) 46 checkXlibResult XSetWMProtocols(display, window, addr(deleteMessage), 1)
44 47
45 return NativeWindow(display: display, window: window) 48 # quite a lot of work to hide the cursor...
49 var data = "\0".cstring
50 var pixmap = XCreateBitmapFromData(display, window, data, 1, 1)
51 var color: XColor
52 var empty_cursor = XCreatePixmapCursor(display, pixmap, pixmap, addr(color), addr(color), 0, 0)
53 checkXlibResult XFreePixmap(display, pixmap)
54 checkXlibResult XDefineCursor(display, window, empty_cursor)
55
56 return NativeWindow(display: display, window: window, emptyCursor: empty_cursor)
46 57
47 proc trash*(window: NativeWindow) = 58 proc trash*(window: NativeWindow) =
59 checkXlibResult window.display.XFreeCursor(window.emptyCursor)
48 checkXlibResult window.display.XDestroyWindow(window.window) 60 checkXlibResult window.display.XDestroyWindow(window.window)
49 discard window.display.XCloseDisplay() # always returns 0 61 discard window.display.XCloseDisplay() # always returns 0
50 62
51 proc size*(window: NativeWindow): (int, int) = 63 proc size*(window: NativeWindow): (int, int) =
52 var attribs: XWindowAttributes 64 var attribs: XWindowAttributes
60 case event.theType 72 case event.theType
61 of ClientMessage: 73 of ClientMessage:
62 if cast[Atom](event.xclient.data.l[0]) == deleteMessage: 74 if cast[Atom](event.xclient.data.l[0]) == deleteMessage:
63 result.add(Event(eventType: Quit)) 75 result.add(Event(eventType: Quit))
64 of KeyPress: 76 of KeyPress:
65 let xkey: KeySym = XLookupKeysym(cast[PXKeyEvent](addr(event)), 0) 77 let xkey = int(cast[PXKeyEvent](addr(event)).keycode)
66 result.add(Event(eventType: KeyDown, key: KeyTypeMap.getOrDefault(xkey, UNKNOWN))) 78 result.add Event(eventType: KeyPressed, key: KeyTypeMap.getOrDefault(xkey, Key.UNKNOWN))
67 of KeyRelease: 79 of KeyRelease:
68 let xkey: KeySym = XLookupKeysym(cast[PXKeyEvent](addr(event)), 0) 80 let xkey = int(cast[PXKeyEvent](addr(event)).keycode)
69 result.add(Event(eventType: KeyUp, key: KeyTypeMap.getOrDefault(xkey, UNKNOWN))) 81 result.add Event(eventType: KeyReleased, key: KeyTypeMap.getOrDefault(xkey, Key.UNKNOWN))
82 of ButtonPress:
83 let button = int(cast[PXButtonEvent](addr(event)).button)
84 result.add Event(eventType: MousePressed, button: MouseButtonTypeMap.getOrDefault(button, MouseButton.UNKNOWN))
85 of ButtonRelease:
86 let button = int(cast[PXButtonEvent](addr(event)).button)
87 result.add Event(eventType: MouseReleased, button: MouseButtonTypeMap.getOrDefault(button, MouseButton.UNKNOWN))
88 of MotionNotify:
89 let motion = cast[PXMotionEvent](addr(event))
90 result.add Event(eventType: MouseMoved, x: motion.x, y: motion.y)
70 of ConfigureNotify: 91 of ConfigureNotify:
71 result.add(Event(eventType: ResizedWindow)) 92 result.add Event(eventType: ResizedWindow)
72 else: 93 else:
73 discard 94 discard