1const std = @import("std");
2const log = std.log;
3
4const wl = @import("wayland").client.wl;
5
6const Bar = @import("Bar.zig");
7const Tags = @import("Tags.zig");
8const Seat = @import("Seat.zig");
9pub const Monitor = @This();
10
11const state = &@import("root").state;
12
13output: *wl.Output,
14globalName: u32,
15scale: i32,
16
17bar: ?*Bar,
18tags: *Tags,
19
20pub fn create(registry: *wl.Registry, name: u32) !*Monitor {
21 const self = try state.gpa.create(Monitor);
22 self.output = try registry.bind(name, wl.Output, 4);
23 self.globalName = name;
24 self.scale = 1;
25
26 self.bar = null;
27 self.tags = try Tags.create(self);
28
29 self.output.setListener(*Monitor, listener, self);
30 return self;
31}
32
33pub fn destroy(self: *Monitor) void {
34 if (self.bar) |bar| {
35 bar.destroy();
36 }
37 self.tags.destroy();
38 state.gpa.destroy(self);
39}
40
41pub fn confBar(self: *Monitor) ?*Bar {
42 if (self.bar) |bar| {
43 if (bar.configured) {
44 return bar;
45 }
46 }
47
48 return null;
49}
50
51fn listener(_: *wl.Output, event: wl.Output.Event, monitor: *Monitor) void {
52 switch (event) {
53 .scale => |scale| {
54 monitor.scale = scale.factor;
55 },
56 .geometry => {},
57 .mode => {},
58 .done => {
59 if (monitor.bar) |_| {
60 return;
61 }
62 monitor.bar = Bar.create(monitor) catch |err| {
63 log.err("cannot create bar for monitor {}: {s}",
64 .{monitor.globalName, @errorName(err)});
65 return;
66 };
67 },
68 }
69}