vx11 / xinerama / xinerama.v
67 lines · 60 sloc · 2.14 KB · c4bc47d81a04ec4c69336c7c85575735cb003365
Raw
1module xinerama
2
3import vx11
4
5// Screen is a V-owned snapshot of one Xinerama head, in root-window coordinates.
6// The number is not necessarily an Xlib screen number or a stable monitor ID.
7pub struct Screen {
8pub:
9 number int
10 x int
11 y int
12 width int
13 height int
14}
15
16// Extension borrows its connection. It has no resources to close independently.
17@[heap; noinit]
18pub struct Extension {
19 connection &vx11.Connection
20pub:
21 event_base int
22 error_base int
23 major int
24 minor int
25}
26
27pub fn available(connection &vx11.Connection) !bool {
28 mut event_base := i32(0)
29 mut error_base := i32(0)
30 return query_extension(connection.raw()!, &event_base, &error_base) != 0
31}
32
33// bind fails when the server does not advertise Xinerama. Presence and activity
34// are separate: an installed but inactive extension returns an empty screen list.
35pub fn bind(connection &vx11.Connection) !&Extension {
36 display := connection.raw()!
37 mut event_base := i32(0)
38 mut error_base := i32(0)
39 if query_extension(display, &event_base, &error_base) == 0 {
40 return error('Xinerama extension is unavailable')
41 }
42 mut major := i32(0)
43 mut minor := i32(0)
44 if query_version(display, &major, &minor) == 0 { return error('cannot query Xinerama version') }
45 return &Extension{ connection: connection, event_base: int(event_base), error_base: int(error_base), major: int(major), minor: int(minor) }
46}
47
48pub fn (e &Extension) active() !bool { return is_active(e.connection.raw()!) != 0 }
49
50// screens copies the server's list and releases its Xlib allocation. An inactive
51// extension returns []; do not cache this list across monitor layout changes.
52pub fn (e &Extension) screens() ![]Screen {
53 mut count := i32(0)
54 p := query_screens(e.connection.raw()!, &count)
55 if p == unsafe { nil } {
56 if count != 0 { return error('invalid Xinerama screen list') }
57 return []Screen{}
58 }
59 defer { vx11.free(p) }
60 if count < 0 { return error('invalid Xinerama screen count') }
61 mut screens := []Screen{cap: int(count)}
62 for i in 0 .. int(count) {
63 s := unsafe { p[i] }
64 screens << Screen{ number: int(s.screen_number), x: int(s.x_org), y: int(s.y_org), width: int(s.width), height: int(s.height) }
65 }
66 return screens
67}
68