vinix-ports / tests / x11 / client.v
83 lines · 77 sloc · 2.99 KB
Raw
1// With --display, also round-trip a pixel through the running X server.
2module main
3
4import os
5
6#include <X11/Xlib.h>
7#include <X11/Xutil.h>
8#include <X11/fonts/fontenc.h>
9#include <pixman.h>
10#pkgconfig pixman-1
11#flag -lX11
12#flag -lfontenc
13
14fn C.XParseGeometry(&char, &int, &int, &u32, &u32) int
15fn C.FontEncMapFind(&char, int, int, int, &char) voidptr
16fn C.FontEncRecode(u32, voidptr) u32
17fn C.pixman_image_create_bits(u32, int, int, &u32, int) voidptr
18fn C.pixman_image_composite32(int, voidptr, voidptr, voidptr, int, int, int, int, int, int, int, int)
19fn C.pixman_image_unref(voidptr) int
20fn C.XOpenDisplay(&char) voidptr
21fn C.XDefaultScreen(voidptr) int
22fn C.XRootWindow(voidptr, int) u64
23fn C.XDefaultDepth(voidptr, int) int
24fn C.XWhitePixel(voidptr, int) u64
25fn C.XCreatePixmap(voidptr, u64, u32, u32, u32) u64
26fn C.XCreateGC(voidptr, u64, u64, voidptr) voidptr
27fn C.XSetForeground(voidptr, voidptr, u64) int
28fn C.XFillRectangle(voidptr, u64, voidptr, int, int, u32, u32) int
29@[typedef]
30struct C.XImage {}
31
32fn C.XGetImage(voidptr, u64, int, int, u32, u32, u64, int) &C.XImage
33fn C.XGetPixel(&C.XImage, int, int) u64
34fn C.XDestroyImage(&C.XImage) int
35fn C.XFreeGC(voidptr, voidptr) int
36fn C.XFreePixmap(voidptr, u64) int
37fn C.XCloseDisplay(voidptr) int
38
39fn display_pixel() {
40 display := C.XOpenDisplay(unsafe { nil })
41 assert display != unsafe { nil }
42 defer { C.XCloseDisplay(display) }
43 screen := C.XDefaultScreen(display)
44 pixmap := C.XCreatePixmap(display, C.XRootWindow(display, screen), 1, 1,
45 u32(C.XDefaultDepth(display, screen)))
46 defer { C.XFreePixmap(display, pixmap) }
47 gc := C.XCreateGC(display, pixmap, 0, unsafe { nil })
48 defer { C.XFreeGC(display, gc) }
49 white := C.XWhitePixel(display, screen)
50 C.XSetForeground(display, gc, white)
51 C.XFillRectangle(display, pixmap, gc, 0, 0, 1, 1)
52 image := C.XGetImage(display, pixmap, 0, 0, 1, 1, ~u64(0), C.ZPixmap)
53 assert image != unsafe { nil }
54 defer { C.XDestroyImage(image) }
55 assert C.XGetPixel(image, 0, 0) == white
56 println('X server pixmap round-trip passed.')
57}
58
59fn main() {
60 zero := unsafe { C.malloc(0) }
61 assert zero != unsafe { nil }
62 unsafe { C.free(zero) }
63 mut x, mut y := 0, 0
64 mut w, mut h := u32(0), u32(0)
65 assert C.XParseGeometry(c'=17x9-2+3', &x, &y, &w, &h) & C.XNegative != 0
66 assert x == -2 && y == 3 && w == 17 && h == 9
67 mapping := C.FontEncMapFind(c'iso8859-15', C.FONT_ENCODING_UNICODE, 0, 0, unsafe { nil })
68 assert mapping != unsafe { nil }
69 assert C.FontEncRecode(0xa4, mapping) == 0x20ac
70 mut input := u32(0xff3388cc)
71 mut output := u32(0)
72 src := C.pixman_image_create_bits(C.PIXMAN_a8r8g8b8, 1, 1, &input, 4)
73 dst := C.pixman_image_create_bits(C.PIXMAN_a8r8g8b8, 1, 1, &output, 4)
74 assert src != unsafe { nil } && dst != unsafe { nil }
75 defer { C.pixman_image_unref(src) }
76 defer { C.pixman_image_unref(dst) }
77 C.pixman_image_composite32(C.PIXMAN_OP_SRC, src, unsafe { nil }, dst, 0, 0, 0, 0, 0, 0, 1, 1)
78 assert output == input
79 println('Xlib geometry, Latin-9 font mapping and Pixman pixel copy passed.')
80 if os.args[1..] == ['--display'] {
81 display_pixel()
82 }
83}
84