vx11 / window.v
139 lines · 120 sloc · 4.35 KB · c4bc47d81a04ec4c69336c7c85575735cb003365
Raw
1module vx11
2
3@[params]
4pub struct WindowOptions {
5pub:
6 title string = 'V / X11'
7 x int
8 y int
9 width int = 640
10 height int = 480
11 border_width int
12 background ?CULong
13 border ?CULong
14 events CLong = exposure_mask | key_press_mask | structure_notify_mask
15}
16
17// WindowHandle owns a server window. Keep its Connection open until destroy().
18@[heap; noinit]
19pub struct WindowHandle {
20pub:
21 id Window
22mut:
23 connection &Connection = unsafe { nil }
24 destroyed bool
25 delete_atom Atom
26 protocols_atom Atom
27}
28
29fn (w &WindowHandle) display() !&Display {
30 if w.destroyed { return error('window is destroyed') }
31 return w.connection.raw()
32}
33
34pub fn (c &Connection) new_window(options WindowOptions) !&WindowHandle {
35 if options.width <= 0 || options.height <= 0 || options.width > 65535 || options.height > 65535 || options.border_width < 0 || options.border_width > 65535 {
36 return error('invalid X11 window dimensions')
37 }
38 if options.title.contains('\x00') { return error('title contains NUL') }
39 d := c.raw()!
40 screen := default_screen(d)
41 id := create_simple_window(d, root_window(d, screen), i32(options.x), i32(options.y), u32(options.width), u32(options.height), u32(options.border_width), options.border or { black_pixel(d, screen) }, options.background or { white_pixel(d, screen) })
42 if id == 0 { return error('cannot allocate window') }
43 mut w := &WindowHandle{ id: id, connection: c }
44 w.set_title(options.title) or {
45 w.destroy()
46 return err
47 }
48 w.select_events(options.events) or {
49 w.destroy()
50 return err
51 }
52 w.enable_close() or {
53 w.destroy()
54 return err
55 }
56 return w
57}
58
59pub fn (mut w WindowHandle) destroy() {
60 if !w.destroyed {
61 if w.connection.display != unsafe { nil } { destroy_window(w.connection.display, w.id) }
62 w.destroyed = true
63 }
64}
65
66pub fn (w &WindowHandle) show() ! {
67 map_window(w.display()!, w.id)
68 w.connection.flush()!
69}
70
71pub fn (w &WindowHandle) hide() ! { unmap_window(w.display()!, w.id) }
72
73pub fn (w &WindowHandle) clear() ! { clear_window(w.display()!, w.id) }
74
75pub fn (w &WindowHandle) raise() ! { raise_window(w.display()!, w.id) }
76
77pub fn (w &WindowHandle) lower() ! { lower_window(w.display()!, w.id) }
78
79pub fn (w &WindowHandle) select_events(mask CLong) ! { select_input(w.display()!, w.id, mask) }
80
81pub fn (w &WindowHandle) resize(width int, height int) ! {
82 if width <= 0 || height <= 0 || width > 65535 || height > 65535 {
83 return error('invalid X11 window dimensions')
84 }
85 resize_window(w.display()!, w.id, u32(width), u32(height))
86}
87
88pub fn (w &WindowHandle) move(x int, y int) ! { move_window(w.display()!, w.id, i32(x), i32(y)) }
89
90pub fn (w &WindowHandle) set_title(title string) ! {
91 if title.contains('\x00') { return error('title contains NUL') }
92 d := w.display()!
93 store_name(d, w.id, &char(title.str))
94 // EWMH title is explicitly UTF-8; also keep the legacy WM_NAME for older WMs.
95 w.set_property_bytes(w.connection.atom('_NET_WM_NAME')!, w.connection.atom('UTF8_STRING')!, title.bytes())!
96}
97
98pub fn (w &WindowHandle) title() !string {
99 d := w.display()!
100 mut p := unsafe { &char(nil) }
101 if fetch_name(d, w.id, &p) == 0 || p == unsafe { nil } { return error('window has no title') }
102 defer { free(p) }
103 return unsafe { p.vstring().clone() }
104}
105
106pub fn (mut w WindowHandle) enable_close() ! {
107 d := w.display()!
108 w.delete_atom = w.connection.atom('WM_DELETE_WINDOW')!
109 w.protocols_atom = w.connection.atom('WM_PROTOCOLS')!
110 mut atom := w.delete_atom
111 if set_wm_protocols(d, w.id, &atom, 1) == 0 { return error('cannot install WM_DELETE_WINDOW') }
112}
113
114pub fn (w &WindowHandle) should_close(event Event) bool {
115 m := event.client_message() or { return false }
116 return m.window == w.id && m.message_type == w.protocols_atom && Atom(m.data[0]) == w.delete_atom
117}
118
119pub fn (w &WindowHandle) attributes() !XWindowAttributes {
120 mut a := XWindowAttributes{}
121 if get_window_attributes(w.display()!, w.id, &a) == 0 {
122 return error('cannot query window attributes')
123 }
124 return a
125}
126
127pub fn (w &WindowHandle) children() ![]Window {
128 mut root := Window(0)
129 mut parent := Window(0)
130 mut children := unsafe { &Window(nil) }
131 mut count := u32(0)
132 if query_tree(w.display()!, w.id, &root, &parent, &children, &count) == 0 {
133 return error('cannot query window tree')
134 }
135 defer { if children != unsafe { nil } { free(children) } }
136 mut result := []Window{cap: int(count)}
137 for i in 0 .. int(count) { result << unsafe { children[i] } }
138 return result
139}
140