annotate src/xlib_helpers.nim @ 2:213fdf8d31dd

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