annotate src/xlib_helpers.nim @ 463:91544fc1afe5

did: hello world triangle, a bit of code organization
author Sam <sam@basx.dev>
date Mon, 19 Dec 2022 10:41:20 +0700
parents 59d861a6a5c4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
1 import
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
2 x11/xlib,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
3 x11/xutil,
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 461
diff changeset
4 x11/x,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 461
diff changeset
5 x11/keysym
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 461
diff changeset
6
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 461
diff changeset
7 export keysym
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
8
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
9 var deleteMessage*: Atom
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
10
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
11 template checkXlibResult*(call: untyped) =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
12 let value = call
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
13 if value == 0:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
14 raise newException(Exception, "Xlib error: " & astToStr(call) & " returned " & $value)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
15
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
16 proc xlibInit*(): (PDisplay, Window) =
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 461
diff changeset
17 checkXlibResult XInitThreads()
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
18 let display = XOpenDisplay(nil)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
19 if display == nil:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
20 quit "Failed to open display"
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
21
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
22 let
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
23 screen = XDefaultScreen(display)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
24 rootWindow = XRootWindow(display, screen)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
25 foregroundColor = XBlackPixel(display, screen)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
26 backgroundColor = XWhitePixel(display, screen)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
27
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
28 let window = XCreateSimpleWindow(display, rootWindow, -1, -1, 800, 600, 0, foregroundColor, backgroundColor)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
29 checkXlibResult XSetStandardProperties(display, window, "Nim X11", "window", 0, nil, 0, nil)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
30 checkXlibResult XSelectInput(display, window, ButtonPressMask or KeyPressMask or ExposureMask)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
31 checkXlibResult XMapWindow(display, window)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
32
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 461
diff changeset
33 deleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", XBool(false))
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
34 checkXlibResult XSetWMProtocols(display, window, addr(deleteMessage), 1)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
35
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
36 return (display, window)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
37
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
38 proc xlibFramebufferSize*(display: PDisplay, window: Window): (int, int) =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
39 var attribs: XWindowAttributes
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
40 checkXlibResult XGetWindowAttributes(display, window, addr(attribs))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
41 return (int(attribs.width), int(attribs.height))