annotate src/platform/linux/xlib.nim @ 474:d4750f2e0760

add: vector/matrix modules, tests, nim config script
author Sam <sam@basx.dev>
date Wed, 28 Dec 2022 11:42:21 +0700
parents 1dd9e2393a9e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
466
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
1 import
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
2 x11/xlib,
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
3 x11/xutil,
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
4 x11/keysym
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
5 import x11/x
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
6
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
7 import ../../events
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
8
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
9 import ./symkey_map
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
10
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
11 export keysym
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
12
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
13 var deleteMessage*: Atom
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
14
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
15 type
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
16 NativeWindow* = object
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
17 display*: PDisplay
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
18 window*: Window
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
19
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
20 template checkXlibResult*(call: untyped) =
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
21 let value = call
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
22 if value == 0:
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
23 raise newException(Exception, "Xlib error: " & astToStr(call) & " returned " & $value)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
24
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
25 proc createWindow*(title: string): NativeWindow =
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
26 checkXlibResult XInitThreads()
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
27 let display = XOpenDisplay(nil)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
28 if display == nil:
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
29 quit "Failed to open display"
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
30
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
31 let
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
32 screen = XDefaultScreen(display)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
33 rootWindow = XRootWindow(display, screen)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
34 foregroundColor = XBlackPixel(display, screen)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
35 backgroundColor = XWhitePixel(display, screen)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
36
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
37 let window = XCreateSimpleWindow(display, rootWindow, -1, -1, 800, 600, 0, foregroundColor, backgroundColor)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
38 checkXlibResult XSetStandardProperties(display, window, title, "window", 0, nil, 0, nil)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
39 checkXlibResult XSelectInput(display, window, ButtonPressMask or KeyPressMask or ExposureMask)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
40 checkXlibResult XMapWindow(display, window)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
41
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
42 deleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", XBool(false))
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
43 checkXlibResult XSetWMProtocols(display, window, addr(deleteMessage), 1)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
44
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
45 return NativeWindow(display: display, window: window)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
46
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
47 proc trash*(window: NativeWindow) =
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
48 checkXlibResult window.display.XDestroyWindow(window.window)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
49 discard window.display.XCloseDisplay() # always returns 0
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
50
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
51 proc size*(window: NativeWindow): (int, int) =
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
52 var attribs: XWindowAttributes
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
53 checkXlibResult XGetWindowAttributes(window.display, window.window, addr(attribs))
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
54 return (int(attribs.width), int(attribs.height))
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
55
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
56 proc pendingEvents*(window: NativeWindow): seq[Event] =
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
57 var event: XEvent
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
58 while window.display.XPending() > 0:
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
59 discard window.display.XNextEvent(addr(event))
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
60 case event.theType
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
61 of ClientMessage:
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
62 if cast[Atom](event.xclient.data.l[0]) == deleteMessage:
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
63 result.add(Event(eventType: Quit))
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
64 of KeyPress:
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
65 let xkey: KeySym = XLookupKeysym(cast[PXKeyEvent](addr(event)), 0)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
66 result.add(Event(eventType: KeyDown, key: KeyTypeMap.getOrDefault(xkey, UNKNOWN)))
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
67 of KeyRelease:
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
68 let xkey: KeySym = XLookupKeysym(cast[PXKeyEvent](addr(event)), 0)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
69 result.add(Event(eventType: KeyUp, key: KeyTypeMap.getOrDefault(xkey, UNKNOWN)))
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
70 of ConfigureNotify:
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
71 result.add(Event(eventType: ResizedWindow))
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
72 else:
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
73 discard