Mercurial > games > semicongine
annotate semiconginev2/rendering/platform/linux.nim @ 1254:b0f4c8ccd49a
did: stuff to test gltf importer
author | sam <sam@basx.dev> |
---|---|
date | Sat, 27 Jul 2024 20:47:54 +0700 |
parents | a3fa15c25026 |
children | bfb75c934f4e |
rev | line source |
---|---|
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
1 import ../../thirdparty/x11/xlib |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
2 import ../../thirdparty/x11/xutil |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
3 import ../../thirdparty/x11/keysym |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
4 import ../../thirdparty/x11/x as x11 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
5 import ../../thirdparty/x11/xkblib |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
6 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
7 const REQUIRED_PLATFORM_EXTENSIONS = @["VK_KHR_xlib_surface"] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
8 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
9 # got values (keycodes) from xev |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
10 const KeyTypeMap = { |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
11 9: Escape, 67: F1, 68: F2, 69: F3, 70: F4, 71: F5, 72: F6, 73: F7, 74: F8, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
12 75: F9, 76: F10, 95: F11, 96: F12, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
13 49: NumberRowExtra1, 10: `1`, 11: `2`, 12: `3`, 13: `4`, 14: `5`, 15: `6`, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
14 16: `7`, 17: `8`, 18: `9`, 19: `0`, 20: NumberRowExtra2, 21: NumberRowExtra3, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
15 24: Q, 25: W, 26: E, 27: R, 28: T, 29: Y, 30: U, 31: I, 32: O, 33: P, 38: A, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
16 39: S, 40: D, 41: Key.F, 42: G, 43: H, 44: J, 45: K, 46: L, 52: Z, 53: X, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
17 54: C, 55: V, 56: B, 57: N, 58: M, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
18 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
19 23: Tab, 66: CapsLock, 50: ShiftL, 62: ShiftR, 37: CtrlL, 105: CtrlR, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
20 133: SuperL, 134: SuperR, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
21 64: AltL, 108: AltR, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
22 65: Space, 36: Enter, 22: Backspace, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
23 34: LetterRow1Extra1, 35: LetterRow1Extra2, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
24 47: LetterRow2Extra1, 48: LetterRow2Extra2, 51: LetterRow2Extra3, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
25 59: LetterRow3Extra1, 60: LetterRow3Extra2, 61: LetterRow3Extra3, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
26 111: Up, 116: Down, 113: Left, 114: Right, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
27 112: PageUp, 117: PageDown, 110: Home, 115: End, 118: Insert, 119: Delete, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
28 107: PrintScreen, 78: ScrollLock, 127: Pause, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
29 }.toTable |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
30 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
31 const MouseButtonTypeMap = { |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
32 x11.Button1: MouseButton.Mouse1, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
33 x11.Button2: MouseButton.Mouse2, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
34 x11.Button3: MouseButton.Mouse3, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
35 }.toTable |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
36 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
37 var deleteMessage*: Atom |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
38 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
39 type |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
40 NativeWindow* = object |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
41 display*: ptr xlib.Display |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
42 window*: x11.Window |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
43 emptyCursor: Cursor |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
44 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
45 template checkXlibResult(call: untyped) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
46 let value = call |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
47 if value == 0: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
48 raise newException(Exception, "Xlib error: " & astToStr(call) & |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
49 " returned " & $value) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
50 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
51 proc XErrorLogger(display: PDisplay, event: PXErrorEvent): cint {.cdecl.} = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
52 logging.error &"Xlib: {event[]}" |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
53 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
54 proc CreateWindow*(title: string): NativeWindow = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
55 checkXlibResult XInitThreads() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
56 let display = XOpenDisplay(nil) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
57 if display == nil: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
58 quit "Failed to open display" |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
59 discard XSetErrorHandler(XErrorLogger) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
60 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
61 let rootWindow = display.XDefaultRootWindow() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
62 discard display.XkbSetDetectableAutoRepeat(true, nil) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
63 var |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
64 attribs: XWindowAttributes |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
65 width = cuint(800) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
66 height = cuint(600) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
67 checkXlibResult display.XGetWindowAttributes(rootWindow, addr(attribs)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
68 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
69 var attrs = XSetWindowAttributes() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
70 let window = XCreateWindow( |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
71 display, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
72 rootWindow, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
73 (attribs.width - cint(width)) div 2, (attribs.height - cint(height)) div 2, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
74 width, height, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
75 0, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
76 CopyFromParent, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
77 InputOutput, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
78 cast[PVisual](CopyFromParent), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
79 0, # CWOverrideRedirect, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
80 addr attrs, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
81 # foregroundColor, backgroundColor |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
82 ) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
83 checkXlibResult XSetStandardProperties(display, window, title, "window", 0, nil, 0, nil) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
84 checkXlibResult XSelectInput(display, window, PointerMotionMask or ButtonPressMask or ButtonReleaseMask or KeyPressMask or KeyReleaseMask or ExposureMask) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
85 checkXlibResult XMapWindow(display, window) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
86 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
87 deleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", XBool(false)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
88 checkXlibResult XSetWMProtocols(display, window, addr(deleteMessage), 1) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
89 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
90 var data = "\0".cstring |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
91 var pixmap = display.XCreateBitmapFromData(window, data, 1, 1) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
92 var color: XColor |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
93 var empty_cursor = display.XCreatePixmapCursor(pixmap, pixmap, addr(color), addr(color), 0, 0) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
94 checkXlibResult display.XFreePixmap(pixmap) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
95 return NativeWindow(display: display, window: window, emptyCursor: empty_cursor) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
96 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
97 proc SetTitle*(window: NativeWindow, title: string) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
98 checkXlibResult XSetStandardProperties(window.display, window.window, title, "window", 0, nil, 0, nil) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
99 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
100 proc Fullscreen*(window: var NativeWindow, enable: bool) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
101 var |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
102 wm_state = window.display.XInternAtom("_NET_WM_STATE", 0) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
103 wm_fullscreen = window.display.XInternAtom("_NET_WM_STATE_FULLSCREEN", 0) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
104 var |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
105 xev: XEvent |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
106 xev.xclient = XClientMessageEvent( |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
107 message_type: wm_state, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
108 format: 32, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
109 window: window.window, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
110 data: XClientMessageData( |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
111 l: [ |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
112 int(not enable) xor 1, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
113 clong(wm_fullscreen), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
114 0, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
115 0, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
116 0 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
117 ] |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
118 ) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
119 ) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
120 xev.theType = ClientMessage |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
121 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
122 checkXlibResult window.display.XSendEvent( |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
123 window.display.DefaultRootWindow(), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
124 0, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
125 SubstructureRedirectMask or SubstructureNotifyMask, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
126 addr xev |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
127 ) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
128 checkXlibResult window.display.XFlush() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
129 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
130 proc HideSystemCursor*(window: NativeWindow) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
131 checkXlibResult XDefineCursor(window.display, window.window, window.emptyCursor) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
132 checkXlibResult window.display.XFlush() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
133 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
134 proc ShowSystemCursor*(window: NativeWindow) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
135 checkXlibResult XUndefineCursor(window.display, window.window) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
136 checkXlibResult window.display.XFlush() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
137 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
138 proc Destroy*(window: NativeWindow) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
139 checkXlibResult window.display.XFreeCursor(window.emptyCursor) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
140 checkXlibResult window.display.XDestroyWindow(window.window) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
141 discard window.display.XCloseDisplay() # always returns 0 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
142 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
143 proc Size*(window: NativeWindow): (int, int) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
144 var attribs: XWindowAttributes |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
145 checkXlibResult XGetWindowAttributes(window.display, window.window, addr(attribs)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
146 return (int(attribs.width), int(attribs.height)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
147 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
148 proc PendingEvents*(window: NativeWindow): seq[Event] = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
149 var event: XEvent |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
150 while window.display.XPending() > 0: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
151 discard window.display.XNextEvent(addr(event)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
152 case event.theType |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
153 of ClientMessage: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
154 if cast[Atom](event.xclient.data.l[0]) == deleteMessage: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
155 result.add(Event(eventType: Quit)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
156 of KeyPress: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
157 let keyevent = cast[PXKeyEvent](addr(event)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
158 let xkey = int(keyevent.keycode) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
159 result.add Event(eventType: KeyPressed, key: KeyTypeMap.getOrDefault(xkey, Key.UNKNOWN)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
160 of KeyRelease: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
161 let keyevent = cast[PXKeyEvent](addr(event)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
162 let xkey = int(keyevent.keycode) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
163 result.add Event(eventType: KeyReleased, key: KeyTypeMap.getOrDefault(xkey, Key.UNKNOWN)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
164 of ButtonPress: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
165 let button = int(cast[PXButtonEvent](addr(event)).button) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
166 if button == Button4: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
167 result.add Event(eventType: MouseWheel, amount: 1'f32) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
168 elif button == Button5: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
169 result.add Event(eventType: MouseWheel, amount: -1'f32) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
170 else: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
171 result.add Event(eventType: MousePressed, button: MouseButtonTypeMap.getOrDefault(button, MouseButton.UNKNOWN)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
172 of ButtonRelease: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
173 let button = int(cast[PXButtonEvent](addr(event)).button) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
174 result.add Event(eventType: MouseReleased, button: MouseButtonTypeMap.getOrDefault(button, MouseButton.UNKNOWN)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
175 of MotionNotify: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
176 let motion = cast[PXMotionEvent](addr(event)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
177 result.add Event(eventType: MouseMoved, x: motion.x, y: motion.y) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
178 of ConfigureNotify, Expose: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
179 result.add Event(eventType: ResizedWindow) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
180 else: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
181 discard |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
182 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
183 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
184 proc GetMousePosition*(window: NativeWindow): Option[Vec2f] = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
185 var |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
186 root: x11.Window |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
187 win: x11.Window |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
188 rootX: cint |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
189 rootY: cint |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
190 winX: cint |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
191 winY: cint |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
192 mask: cuint |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
193 onscreen = XQueryPointer( |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
194 window.display, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
195 window.window, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
196 addr(root), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
197 addr(win), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
198 addr(rootX), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
199 addr(rootY), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
200 addr(winX), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
201 addr(winY), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
202 addr(mask), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
203 ) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
204 if onscreen != 0: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
205 result = some(Vec2f([float32(winX), float32(winY)])) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
206 |
1254 | 207 proc SetMousePosition*(window: NativeWindow, x, y: int) = |
208 checkXlibResult XWarpPointer( | |
209 window.display, | |
210 default(x11.Window), | |
211 window.window, | |
212 0, 0, 0, 0, | |
213 x.cint, | |
214 y.cint, | |
215 ) | |
216 checkXlibResult XSync(window.display, false.XBool) | |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
217 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
218 proc CreateNativeSurface(instance: VkInstance, window: NativeWindow): VkSurfaceKHR = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
219 var surfaceCreateInfo = VkXlibSurfaceCreateInfoKHR( |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
220 sType: VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
221 dpy: cast[ptr Display](window.display), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
222 window: cast[Window](window.window), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
223 ) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
224 checkVkResult vkCreateXlibSurfaceKHR(instance, addr(surfaceCreateInfo), nil, addr(result)) |