| 1 | module main |
| 2 | |
| 3 | import vx11 |
| 4 | |
| 5 | fn expected_error(_ &vx11.Display, event &vx11.XErrorEvent) i32 { |
| 6 | assert event.error_code == vx11.bad_window |
| 7 | return 0 |
| 8 | } |
| 9 | |
| 10 | fn client_message_only(_ &vx11.Display, event &vx11.XEvent, _ vx11.XPointer) i32 { |
| 11 | return if unsafe { event.@type } == vx11.client_message { 1 } else { 0 } |
| 12 | } |
| 13 | |
| 14 | fn main() { |
| 15 | mut c := vx11.connect('')! |
| 16 | defer { c.close() } |
| 17 | previous_sync := vx11.synchronize(c.raw()!, 1) |
| 18 | vx11.set_after_function(c.raw()!, previous_sync) |
| 19 | mut w := c.new_window(title: 'V X11 integration', width: 200, height: 120)! |
| 20 | defer { w.destroy() } |
| 21 | assert w.title()! == 'V X11 integration' |
| 22 | a := w.attributes()! |
| 23 | assert a.width == 200 && a.height == 120 |
| 24 | assert c.atom_name(c.atom('VX11_TEST')!)! == 'VX11_TEST' |
| 25 | assert c.extension_names()!.len > 0 |
| 26 | mut opcode := i32(0) |
| 27 | mut event_base := i32(0) |
| 28 | mut error_base := i32(0) |
| 29 | mut major := i32(1) |
| 30 | mut minor := i32(0) |
| 31 | assert c.xkb_query_extension(&opcode, &event_base, &error_base, &major, &minor)! != 0 |
| 32 | mut keyboard := vx11.XkbStateRec{} |
| 33 | assert c.xkb_get_state(vx11.xkb_use_core_kbd, &keyboard)! == 0 |
| 34 | previous := vx11.set_error_handler(expected_error) |
| 35 | vx11.destroy_window(c.raw()!, 0) |
| 36 | c.sync()! |
| 37 | vx11.set_error_handler(previous) |
| 38 | // Check the variadic adapter with real XIM attribute input and output. |
| 39 | vx11.set_locale_modifiers(c'@im=none') |
| 40 | im := vx11.open_im(c.raw()!, unsafe { nil }, unsafe { nil }, unsafe { nil }) |
| 41 | assert im != unsafe { nil } |
| 42 | style := vx11.XIMStyle(vx11.xim_preedit_nothing | vx11.xim_status_nothing) |
| 43 | ic := vx11.create_ic(im, [voidptr(vx11.xn_input_style.str), voidptr(style), |
| 44 | voidptr(vx11.xn_client_window.str), voidptr(w.id)])! |
| 45 | assert ic != unsafe { nil } |
| 46 | mut returned_style := vx11.XIMStyle(0) |
| 47 | assert vx11.get_ic_values(ic, [voidptr(vx11.xn_input_style.str), voidptr(&returned_style)])! == unsafe { nil } |
| 48 | assert returned_style == style |
| 49 | vx11.destroy_ic(ic) |
| 50 | vx11.close_im(im) |
| 51 | prop := c.atom('VX11_TEST_DATA')! |
| 52 | w.set_property_bytes(prop, vx11.Atom(vx11.xa_string), 'hello\x00world'.bytes())! |
| 53 | assert w.property(prop)!.bytes == 'hello\x00world'.bytes() |
| 54 | w.set_property_words(prop, vx11.Atom(vx11.xa_cardinal), [u32(0), 0x12345678, 0xffffffff])! |
| 55 | assert w.property(prop)!.words == [u32(0), 0x12345678, 0xffffffff] |
| 56 | w.delete_property(prop)! |
| 57 | mut missing := false |
| 58 | w.property(prop) or { missing = true } |
| 59 | assert missing |
| 60 | mut pen := w.graphics()! |
| 61 | defer { pen.close() } |
| 62 | pen.foreground(c.black_pixel()!)! |
| 63 | w.show()! |
| 64 | c.sync()! |
| 65 | events := c.poll_events()! |
| 66 | assert events.any(it.kind == vx11.map_notify) |
| 67 | pen.rectangle(0, 0, 32, 32, true)! |
| 68 | pen.text(40, 40, 'V / Xlib')! |
| 69 | c.sync()! |
| 70 | image := vx11.get_image(c.raw()!, vx11.Drawable(w.id), 0, 0, 1, 1, ~vx11.CULong(0), vx11.z_pixmap) |
| 71 | assert image != unsafe { nil } |
| 72 | assert vx11.get_pixel(image, 0, 0) == c.black_pixel()! |
| 73 | vx11.destroy_image(image) |
| 74 | w.resize(240, 140)! |
| 75 | c.sync()! |
| 76 | assert w.attributes()!.width == 240 |
| 77 | assert w.children()!.len == 0 |
| 78 | // Exercise a format-32 WM close message through the server/event queue. |
| 79 | mut event := vx11.XEvent{} |
| 80 | unsafe { |
| 81 | event.xclient.@type = vx11.client_message |
| 82 | event.xclient.display = c.raw()! |
| 83 | event.xclient.window = w.id |
| 84 | event.xclient.message_type = c.atom('WM_PROTOCOLS')! |
| 85 | event.xclient.format = 32 |
| 86 | event.xclient.data.l[0] = vx11.CLong(c.atom('WM_DELETE_WINDOW')!) |
| 87 | } |
| 88 | assert vx11.send_event(c.raw()!, w.id, 0, 0, &event) != 0 |
| 89 | c.sync()! |
| 90 | mut received := vx11.XEvent{} |
| 91 | assert vx11.check_if_event(c.raw()!, &received, client_message_only, vx11.XPointer(unsafe { nil })) != 0 |
| 92 | vx11.put_back_event(c.raw()!, &received) |
| 93 | batch := c.poll_events()! |
| 94 | assert batch.any(w.should_close(it)) |
| 95 | pen.close() |
| 96 | pen.close() |
| 97 | w.destroy() |
| 98 | w.destroy() |
| 99 | mut rejected := false |
| 100 | w.show() or { rejected = true } |
| 101 | assert rejected |
| 102 | c.close() |
| 103 | c.close() |
| 104 | mut closed := false |
| 105 | c.pending() or { closed = true } |
| 106 | assert closed |
| 107 | println('PASS: window, events, drawing, strings, properties, and resource lifecycle') |
| 108 | } |
| 109 | |