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