vx11 / shape / shape.v
135 lines · 120 sloc · 3.9 KB · c4bc47d81a04ec4c69336c7c85575735cb003365
Raw
1module shape
2
3import vx11
4
5pub enum Kind {
6 bounding
7 clip
8 input
9}
10
11pub enum Operation {
12 set
13 union_
14 intersect
15 subtract
16 invert
17}
18
19// Rectangle uses native integers; conversion checks the protocol's 16-bit limits.
20pub struct Rectangle {
21pub:
22 x int
23 y int
24 width int
25 height int
26}
27
28pub struct Extents {
29pub:
30 bounding_shaped bool
31 bounding Rectangle
32 clip_shaped bool
33 clip Rectangle
34}
35
36@[heap; noinit]
37pub struct Extension {
38 connection &vx11.Connection
39pub:
40 event_base int
41 error_base int
42 major int
43 minor int
44}
45
46pub fn available(connection &vx11.Connection) !bool {
47 mut events := i32(0)
48 mut errors := i32(0)
49 return query_extension(connection.raw()!, &events, &errors) != 0
50}
51
52pub fn bind(connection &vx11.Connection) !&Extension {
53 d := connection.raw()!
54 mut events := i32(0)
55 mut errors := i32(0)
56 if query_extension(d, &events, &errors) == 0 { return error('SHAPE extension is unavailable') }
57 mut major := i32(0)
58 mut minor := i32(0)
59 if query_version(d, &major, &minor) == 0 { return error('cannot query SHAPE version') }
60 return &Extension{ connection: connection, event_base: int(events), error_base: int(errors), major: int(major), minor: int(minor) }
61}
62
63fn (e &Extension) check_kind(kind Kind) ! {
64 if kind == .input && (e.major < 1 || (e.major == 1 && e.minor < 1)) {
65 return error('input shapes require SHAPE 1.1')
66 }
67}
68
69// combine uses unsorted rectangles, so callers need not pre-sort a V slice.
70// An empty list is an empty region; use reset to restore the default shape.
71pub fn (e &Extension) combine(window vx11.Window, kind Kind, rectangles []Rectangle, op Operation) ! {
72 e.check_kind(kind)!
73 if rectangles.len > 0x7fffffff { return error('too many shape rectangles') }
74 mut raw := []vx11.XRectangle{cap: rectangles.len}
75 for r in rectangles {
76 if r.x < -32768 || r.x > 32767 || r.y < -32768 || r.y > 32767 || r.width < 0 || r.width > 65535 || r.height < 0 || r.height > 65535 {
77 return error('rectangle exceeds X11 shape coordinate limits')
78 }
79 raw << vx11.XRectangle{ x: i16(r.x), y: i16(r.y), width: u16(r.width), height: u16(r.height) }
80 }
81 combine_rectangles(e.connection.raw()!, window, i32(kind), 0, 0, raw.data, i32(raw.len), i32(op), vx11.unsorted)
82}
83
84pub fn (e &Extension) reset(window vx11.Window, kind Kind) ! {
85 e.check_kind(kind)!
86 combine_mask(e.connection.raw()!, window, i32(kind), 0, 0, 0, shape_set)
87}
88
89pub fn (e &Extension) rectangles(window vx11.Window, kind Kind) ![]Rectangle {
90 e.check_kind(kind)!
91 mut count := i32(0)
92 mut ordering := i32(0)
93 p := get_rectangles(e.connection.raw()!, window, i32(kind), &count, &ordering)
94 defer { if p != unsafe { nil } { vx11.free(p) } }
95 if count < 0 || (p == unsafe { nil } && count != 0) {
96 return error('invalid shape rectangle list')
97 }
98 mut result := []Rectangle{cap: int(count)}
99 for i in 0 .. int(count) {
100 r := unsafe { p[i] }
101 result << Rectangle{ x: int(r.x), y: int(r.y), width: int(r.width), height: int(r.height) }
102 }
103 return result
104}
105
106pub fn (e &Extension) extents(window vx11.Window) !Extents {
107 mut bs := i32(0)
108 mut bx := i32(0)
109 mut by := i32(0)
110 mut bw := u32(0)
111 mut bh := u32(0)
112 mut cs := i32(0)
113 mut cx := i32(0)
114 mut cy := i32(0)
115 mut cw := u32(0)
116 mut ch := u32(0)
117 if query_extents(e.connection.raw()!, window, &bs, &bx, &by, &bw, &bh, &cs, &cx, &cy, &cw, &ch) == 0 {
118 return error('cannot query shape extents')
119 }
120 return Extents{ bounding_shaped: bs != 0, bounding: Rectangle{int(bx), int(by), int(bw), int(bh)}, clip_shaped: cs != 0, clip: Rectangle{int(cx), int(cy), int(cw), int(ch)} }
121}
122
123pub fn (e &Extension) watch(window vx11.Window, enabled bool) ! {
124 select_input(e.connection.raw()!, window, if enabled {
125 vx11.CULong(notify_mask)
126 } else {
127 vx11.CULong(0)
128 })
129}
130
131// event copies the extension event after checking its server-assigned type.
132pub fn (e &Extension) event(event vx11.Event) ?XShapeEvent {
133 if event.kind != e.event_base + notify { return none }
134 return unsafe { *(&XShapeEvent(&event.raw)) }
135}
136