| 1 | // With --display, also round-trip a pixel through the running X server. |
| 2 | module main |
| 3 | |
| 4 | import 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 | |
| 14 | fn C.XParseGeometry(&char, &int, &int, &u32, &u32) int |
| 15 | fn C.FontEncMapFind(&char, int, int, int, &char) voidptr |
| 16 | fn C.FontEncRecode(u32, voidptr) u32 |
| 17 | fn C.pixman_image_create_bits(u32, int, int, &u32, int) voidptr |
| 18 | fn C.pixman_image_composite32(int, voidptr, voidptr, voidptr, int, int, int, int, int, int, int, int) |
| 19 | fn C.pixman_image_unref(voidptr) int |
| 20 | fn C.XOpenDisplay(&char) voidptr |
| 21 | fn C.XDefaultScreen(voidptr) int |
| 22 | fn C.XRootWindow(voidptr, int) u64 |
| 23 | fn C.XDefaultDepth(voidptr, int) int |
| 24 | fn C.XWhitePixel(voidptr, int) u64 |
| 25 | fn C.XCreatePixmap(voidptr, u64, u32, u32, u32) u64 |
| 26 | fn C.XCreateGC(voidptr, u64, u64, voidptr) voidptr |
| 27 | fn C.XSetForeground(voidptr, voidptr, u64) int |
| 28 | fn C.XFillRectangle(voidptr, u64, voidptr, int, int, u32, u32) int |
| 29 | @[typedef] |
| 30 | struct C.XImage {} |
| 31 | |
| 32 | fn C.XGetImage(voidptr, u64, int, int, u32, u32, u64, int) &C.XImage |
| 33 | fn C.XGetPixel(&C.XImage, int, int) u64 |
| 34 | fn C.XDestroyImage(&C.XImage) int |
| 35 | fn C.XFreeGC(voidptr, voidptr) int |
| 36 | fn C.XFreePixmap(voidptr, u64) int |
| 37 | fn C.XCloseDisplay(voidptr) int |
| 38 | |
| 39 | fn 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 | |
| 59 | fn 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 | |