comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:5daf3f236d87
1 import
2 x11/xlib,
3 x11/xutil,
4 x11/x
5
6 var deleteMessage*: Atom
7
8 template checkXlibResult*(call: untyped) =
9 let value = call
10 if value == 0:
11 raise newException(Exception, "Xlib error: " & astToStr(call) & " returned " & $value)
12
13 proc xlibInit*(): (PDisplay, Window) =
14 let display = XOpenDisplay(nil)
15 if display == nil:
16 quit "Failed to open display"
17
18 let
19 screen = XDefaultScreen(display)
20 rootWindow = XRootWindow(display, screen)
21 foregroundColor = XBlackPixel(display, screen)
22 backgroundColor = XWhitePixel(display, screen)
23
24 let window = XCreateSimpleWindow(display, rootWindow, -1, -1, 800, 600, 0, foregroundColor, backgroundColor)
25 checkXlibResult XSetStandardProperties(display, window, "Nim X11", "window", 0, nil, 0, nil)
26 checkXlibResult XSelectInput(display, window, ButtonPressMask or KeyPressMask or ExposureMask)
27 checkXlibResult XMapWindow(display, window)
28
29 deleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", false.XBool)
30 checkXlibResult XSetWMProtocols(display, window, addr(deleteMessage), 1)
31
32 return (display, window)
33
34 proc xlibFramebufferSize*(display: PDisplay, window: Window): (int, int) =
35 var attribs: XWindowAttributes
36 checkXlibResult XGetWindowAttributes(display, window, addr(attribs))
37 return (int(attribs.width), int(attribs.height))