| 1 | const std = @import("std"); |
| 2 | const xlib = @import("x11/xlib.zig"); |
| 3 | |
| 4 | const Client = @import("client.zig").Client; |
| 5 | const WindowManager = @import("wm/wm.zig").WindowManager; |
| 6 | |
| 7 | pub const Layout = struct { |
| 8 | symbol: []const u8, |
| 9 | arrange_fn: ?*const fn (*Monitor) void, |
| 10 | }; |
| 11 | |
| 12 | pub const Pertag = struct { |
| 13 | curtag: u32 = 1, |
| 14 | prevtag: u32 = 1, |
| 15 | nmasters: [10]i32 = [_]i32{1} ** 10, |
| 16 | mfacts: [10]f32 = [_]f32{0.55} ** 10, |
| 17 | sellts: [10]u32 = [_]u32{0} ** 10, |
| 18 | ltidxs: [10][5]?*const Layout = [_][5]?*const Layout{.{ null, null, null, null, null }} ** 10, |
| 19 | showbars: [10]bool = [_]bool{true} ** 10, |
| 20 | }; |
| 21 | |
| 22 | pub const Monitor = struct { |
| 23 | lt_symbol: [16]u8 = std.mem.zeroes([16]u8), |
| 24 | mfact: f32 = 0.55, |
| 25 | nmaster: i32 = 1, |
| 26 | num: i32 = 0, |
| 27 | bar_y: i32 = 0, |
| 28 | mon_x: i32 = 0, |
| 29 | mon_y: i32 = 0, |
| 30 | mon_w: i32 = 0, |
| 31 | mon_h: i32 = 0, |
| 32 | win_x: i32 = 0, |
| 33 | win_y: i32 = 0, |
| 34 | win_w: i32 = 0, |
| 35 | win_h: i32 = 0, |
| 36 | gap_inner_h: i32 = 0, |
| 37 | gap_inner_v: i32 = 0, |
| 38 | gap_outer_h: i32 = 0, |
| 39 | gap_outer_v: i32 = 0, |
| 40 | smartgaps_enabled: bool = false, |
| 41 | scroll_offset: i32 = 0, |
| 42 | sel_tags: u32 = 0, |
| 43 | sel_lt: u32 = 0, |
| 44 | tagset: [2]u32 = .{ 1, 1 }, |
| 45 | show_bar: bool = true, |
| 46 | top_bar: bool = true, |
| 47 | clients: ?*Client = null, |
| 48 | sel: ?*Client = null, |
| 49 | stack: ?*Client = null, |
| 50 | next: ?*Monitor = null, |
| 51 | bar_win: xlib.Window = 0, |
| 52 | lt: [5]?*const Layout = .{null} ** 5, |
| 53 | pertag: Pertag = .{}, |
| 54 | }; |
| 55 | |
| 56 | /// Allocates and zero-initialises a new `Monitor`. |
| 57 | pub fn create(allocator: std.mem.Allocator) ?*Monitor { |
| 58 | const mon = allocator.create(Monitor) catch return null; |
| 59 | mon.* = Monitor{}; |
| 60 | return mon; |
| 61 | } |
| 62 | |
| 63 | /// Frees a monitor previously returned by `create`. |
| 64 | pub fn destroy(allocator: std.mem.Allocator, mon: *Monitor) void { |
| 65 | allocator.destroy(mon); |
| 66 | } |
| 67 | |
| 68 | /// Returns the monitor whose bar window or client matches `win`, falling |
| 69 | /// back to a pointer-position query when `win` is the root window. |
| 70 | pub fn windowToMonitor(wm: *WindowManager, win: xlib.Window) ?*Monitor { |
| 71 | const monitors = wm.monitors; |
| 72 | const selected_monitor = wm.selected_monitor; |
| 73 | |
| 74 | if (win == wm.display.root) { |
| 75 | var root_x: c_int = undefined; |
| 76 | var root_y: c_int = undefined; |
| 77 | var dummy_win: xlib.Window = undefined; |
| 78 | var dummy_int: c_int = undefined; |
| 79 | var dummy_uint: c_uint = undefined; |
| 80 | if (xlib.XQueryPointer(wm.display.handle, wm.display.root, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint) != 0) { |
| 81 | return rectToMonitor(wm, root_x, root_y, 1, 1); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | var current = monitors; |
| 86 | while (current) |monitor| { |
| 87 | if (monitor.bar_win == win) return monitor; |
| 88 | current = monitor.next; |
| 89 | } |
| 90 | |
| 91 | const client = @import("client.zig").windowToClient(monitors, win); |
| 92 | if (client) |found_client| { |
| 93 | return found_client.monitor; |
| 94 | } |
| 95 | |
| 96 | return selected_monitor; |
| 97 | } |
| 98 | |
| 99 | /// Returns the monitor with the greatest intersection area with the given |
| 100 | /// rectangle, or `selected_monitor` if no intersection is found. |
| 101 | pub fn rectToMonitor(wm: *WindowManager, x: i32, y: i32, width: i32, height: i32) ?*Monitor { |
| 102 | const monitors = wm.monitors; |
| 103 | const selected_monitor = wm.selected_monitor; |
| 104 | var result = selected_monitor; |
| 105 | var max_area: i32 = 0; |
| 106 | |
| 107 | var current = monitors; |
| 108 | while (current) |monitor| { |
| 109 | const intersect_x = @max(0, @min(x + width, monitor.win_x + monitor.win_w) - @max(x, monitor.win_x)); |
| 110 | const intersect_y = @max(0, @min(y + height, monitor.win_y + monitor.win_h) - @max(y, monitor.win_y)); |
| 111 | const area = intersect_x * intersect_y; |
| 112 | if (area > max_area) { |
| 113 | max_area = area; |
| 114 | result = monitor; |
| 115 | } |
| 116 | current = monitor.next; |
| 117 | } |
| 118 | return result; |
| 119 | } |
| 120 | |
| 121 | /// Returns the next or previous monitor relative to `wm.selected_monitor`. |
| 122 | /// |
| 123 | /// Positive `direction` moves forward through the linked list (wrapping to |
| 124 | /// the head); negative moves backward (wrapping to the tail). |
| 125 | /// |
| 126 | // TODO: |
| 127 | // - Change direction to an enum/enum_literal |
| 128 | // - Rename function |
| 129 | pub fn dirToMonitor(wm: *WindowManager, direction: i32) ?*Monitor { |
| 130 | const monitors = wm.monitors; |
| 131 | const selected_monitor = wm.selected_monitor; |
| 132 | var target: ?*Monitor = null; |
| 133 | |
| 134 | if (direction > 0) { |
| 135 | target = if (selected_monitor) |current| current.next else null; |
| 136 | if (target == null) { |
| 137 | target = monitors; |
| 138 | } |
| 139 | } else if (selected_monitor == monitors) { |
| 140 | // Already at head, walk to tail. |
| 141 | var last = monitors; |
| 142 | while (last) |iter| { |
| 143 | if (iter.next == null) { |
| 144 | target = iter; |
| 145 | break; |
| 146 | } |
| 147 | last = iter.next; |
| 148 | } |
| 149 | } else { |
| 150 | // Walk until we find the node just before selected_monitor. |
| 151 | var previous = monitors; |
| 152 | while (previous) |iter| { |
| 153 | if (iter.next == selected_monitor) { |
| 154 | target = iter; |
| 155 | break; |
| 156 | } |
| 157 | previous = iter.next; |
| 158 | } |
| 159 | } |
| 160 | return target; |
| 161 | } |