annotate src/xlib_helpers.nim @ 0:5daf3f236d87

add: initial version
author Sam <sam@basx.dev>
date Wed, 14 Dec 2022 00:49:35 +0700
parents
children 213fdf8d31dd
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,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
4 x11/x
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
5
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
6 var deleteMessage*: Atom
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
7
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
8 template checkXlibResult*(call: untyped) =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
9 let value = call
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
10 if value == 0:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
11 raise newException(Exception, "Xlib error: " & astToStr(call) & " returned " & $value)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
12
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
13 proc xlibInit*(): (PDisplay, Window) =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
14 let display = XOpenDisplay(nil)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
15 if display == nil:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
16 quit "Failed to open display"
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
17
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
18 let
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
19 screen = XDefaultScreen(display)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
20 rootWindow = XRootWindow(display, screen)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
21 foregroundColor = XBlackPixel(display, screen)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
22 backgroundColor = XWhitePixel(display, screen)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
23
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
24 let window = XCreateSimpleWindow(display, rootWindow, -1, -1, 800, 600, 0, foregroundColor, backgroundColor)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
25 checkXlibResult XSetStandardProperties(display, window, "Nim X11", "window", 0, nil, 0, nil)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
26 checkXlibResult XSelectInput(display, window, ButtonPressMask or KeyPressMask or ExposureMask)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
27 checkXlibResult XMapWindow(display, window)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
28
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
29 deleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", false.XBool)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
30 checkXlibResult XSetWMProtocols(display, window, addr(deleteMessage), 1)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
31
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
32 return (display, window)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
33
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
34 proc xlibFramebufferSize*(display: PDisplay, window: Window): (int, int) =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
35 var attribs: XWindowAttributes
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
36 checkXlibResult XGetWindowAttributes(display, window, addr(attribs))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
37 return (int(attribs.width), int(attribs.height))