diff --git a/README.md b/README.md index 956242a..fbaf1b2 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,8 @@ SPDX-License-Identifier: EUPL-1.2 --> # beansprout wm + + +## TODOs +[ ] Support multiple outputs +[ ] Support multiple seats diff --git a/src/Output.zig b/src/Output.zig index d5e3b04..7a07c21 100644 --- a/src/Output.zig +++ b/src/Output.zig @@ -13,10 +13,17 @@ height: i32 = 0, x: i32 = 0, y: i32 = 0, +/// Tags are 32-bit bitfield. A window can be active on one(?) or more tags. tags: u32 = 0x0001, +pending_manage: PendingManage = .{}, + link: wl.list.Link, +pub const PendingManage = struct { + tags: ?u32 = null, +}; + pub fn create(context: *Context, river_output_v1: *river.OutputV1) !*Output { var output = try utils.allocator.create(Output); errdefer output.destroy(); @@ -49,7 +56,8 @@ fn outputListener(river_output_v1: *river.OutputV1, event: river.OutputV1.Event, output.height = ev.height; }, .position => |ev| { - // TODO - CONFIG: Allow setting output position (do I even need to do this?) + // TODO - CONFIG: Allow setting output position (do I even need to + // do this, or do I just get told what the position is?) output.x = ev.x; output.y = ev.y; }, @@ -57,7 +65,11 @@ fn outputListener(river_output_v1: *river.OutputV1, event: river.OutputV1.Event, } pub fn manage(output: *Output) void { - _ = output; + defer output.pending_manage = .{}; + + if (output.pending_manage.tags) |tags| { + output.tags = tags; + } } pub fn render(output: *Output) void { diff --git a/src/Window.zig b/src/Window.zig index f3b2137..8b6138a 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -28,6 +28,10 @@ pending_manage: PendingManage = .{}, /// State consumed in render() phase, reset at end of render(). pending_render: PendingRender = .{}, +/// Used to put Windows into a list in +/// WindowManager.calculatePriamryStackLayout() +active_list_node: DoublyLinkedList.Node = .{}, + link: wl.list.Link, pub const PendingManage = struct { @@ -36,6 +40,8 @@ pub const PendingManage = struct { fullscreen: ?bool = null, maximized: ?bool = null, + + tags: ?u32 = null, }; pub const PendingRender = struct { @@ -45,7 +51,7 @@ pub const PendingRender = struct { focused: ?bool = null, }; -pub fn create(context: *Context, river_window_v1: *river.WindowV1, output: ?*Output) !*Window { +pub fn create(context: *Context, river_window_v1: *river.WindowV1, output: *Output) !*Window { var window = try utils.allocator.create(Window); errdefer window.destroy(); @@ -53,15 +59,10 @@ pub fn create(context: *Context, river_window_v1: *river.WindowV1, output: ?*Out .context = context, .river_window_v1 = river_window_v1, .river_node_v1 = river_window_v1.getNode() catch @panic("Failed to get node"), - .output = output, + .tags = if (output.tags != 0) output.tags else 0x0001, .link = undefined, // Handled by the wl.list }; - if (output) |out| { - window.tags = if (out.tags != 0) out.tags else 0x0001; - } else { - window.tags = 0x0001; - } window.river_window_v1.setListener(*Window, windowListener, window); @@ -119,6 +120,7 @@ fn windowListener(river_window_v1: *river.WindowV1, event: river.WindowV1.Event, pub fn manage(window: *Window) void { if (!window.initialized) { + // Only happens once per Window @branchHint(.unlikely); window.initialized = true; @@ -130,7 +132,7 @@ pub fn manage(window: *Window) void { window.river_window_v1.setTiled(.{ .top = true, .bottom = true, .left = true, .right = true }); } - // Any new state since the last manage event + // Updating state since the last manage event defer window.pending_manage = .{}; const pending_manage = window.pending_manage; // Layout (pre-computed by WindowManager.calculatePrimaryStackLayout()) @@ -141,7 +143,6 @@ pub fn manage(window: *Window) void { window.river_window_v1.proposeDimensions(new_width, new_height); } } - // Fullscreen and maximize operations if (pending_manage.fullscreen) |fullscreen| { window.fullscreen = fullscreen; @@ -162,6 +163,10 @@ pub fn manage(window: *Window) void { window.river_window_v1.informUnmaximized(); } } + // New tags + if (pending_manage.tags) |tags| { + window.tags = tags; + } } pub fn render(window: *Window) void { @@ -202,6 +207,7 @@ fn applyBorders(window: *Window, color: utils.RiverColor) void { const std = @import("std"); const assert = std.debug.assert; +const DoublyLinkedList = std.DoublyLinkedList; const wayland = @import("wayland"); const wl = wayland.client.wl; diff --git a/src/WindowManager.zig b/src/WindowManager.zig index 78d86af..3d178d8 100644 --- a/src/WindowManager.zig +++ b/src/WindowManager.zig @@ -10,7 +10,7 @@ const MIN_RIVER_SEAT_V1_VERSION: u2 = 3; context: *Context, -window_manager_v1: *river.WindowManagerV1, +river_window_manager_v1: *river.WindowManagerV1, seats: wl.list.Head(Seat, .link), outputs: wl.list.Head(Output, .link), @@ -24,7 +24,7 @@ pub fn create(context: *Context, window_manager_v1: *river.WindowManagerV1) !*Wi wm.* = .{ .context = context, - .window_manager_v1 = window_manager_v1, + .river_window_manager_v1 = window_manager_v1, .seats = undefined, // we will initialize these shortly .outputs = undefined, .windows = undefined, @@ -34,28 +34,32 @@ pub fn create(context: *Context, window_manager_v1: *river.WindowManagerV1) !*Wi wm.outputs.init(); wm.windows.init(); - wm.window_manager_v1.setListener(*WindowManager, windowManagerV1Listener, wm); + wm.river_window_manager_v1.setListener(*WindowManager, windowManagerV1Listener, wm); return wm; } pub fn destroy(wm: *WindowManager) void { - var window_it = wm.windows.iterator(.forward); - while (window_it.next()) |window| { - window.link.remove(); - window.destroy(); + { + var it = wm.windows.safeIterator(.forward); + while (it.next()) |window| { + window.link.remove(); + window.destroy(); + } } - - var output_it = wm.outputs.iterator(.forward); - while (output_it.next()) |output| { - output.link.remove(); - output.destroy(); + { + var it = wm.outputs.safeIterator(.forward); + while (it.next()) |output| { + output.link.remove(); + output.destroy(); + } } - - var seat_it = wm.seats.iterator(.forward); - while (seat_it.next()) |seat| { - seat.link.remove(); - seat.destroy(); + { + var it = wm.seats.safeIterator(.forward); + while (it.next()) |seat| { + seat.link.remove(); + seat.destroy(); + } } utils.allocator.destroy(wm); @@ -89,20 +93,34 @@ pub fn getPrevWindow(wm: *WindowManager, current: *Window) ?*Window { /// - Single window: maximized /// - Multiple windows: stack (45% left, vertically tiled), primary (55% right) fn calculatePrimaryStackLayout(wm: *WindowManager) void { - const count = wm.window_count; - if (count == 0) return; - + // TODO: Support multiple outputs const output = wm.outputs.first() orelse return; + + // Get a list of active windows + var active_list: DoublyLinkedList = .{}; + var active_count: u31 = 0; + var it = wm.windows.iterator(.forward); + while (it.next()) |window| { + if (output.tags & window.tags != 0x0000) { + active_list.append(&window.active_list_node); + active_count += 1; + } + } + + if (active_count == 0) return; + // Output dimensions come as i32 from the protocol, convert to u31 for window dimensions + // since they can't be negative. const output_width: u31 = @intCast(output.width); const output_height: u31 = @intCast(output.height); const output_x = output.x; const output_y = output.y; - var index: u8 = 0; - var it = wm.windows.iterator(.forward); - while (it.next()) |window| { - if (count == 1) { + // Iterate through the active windows and apply the tags + var i: u31 = 0; + while (active_list.popFirst()) |node| : (i += 1) { + const window: *Window = @fieldParentPtr("active_list_node", node); + if (active_count == 1) { // Single window: maximize window.pending_render.x = output_x; window.pending_render.y = output_y; @@ -114,11 +132,11 @@ fn calculatePrimaryStackLayout(wm: *WindowManager) void { // TODO: Support multiple windows in primary stack const primary_width: u31 = @intFromFloat(@as(f32, @floatFromInt(output_width)) * PRIMARY_RATIO); const stack_width: u31 = output_width - primary_width; - const stack_count = count - 1; + const stack_count = active_count - 1; const stack_height: u31 = @divFloor(output_height, stack_count); window.pending_manage.maximized = false; - if (index == 0) { + if (i == 0) { // Primary window (first window) - right side window.pending_render.x = output_x + @as(i32, stack_width); window.pending_render.y = output_y; @@ -126,12 +144,12 @@ fn calculatePrimaryStackLayout(wm: *WindowManager) void { window.pending_manage.height = output_height; } else { // Stack window(s) - left side - const stack_index = index - 1; + const stack_index = i - 1; window.pending_render.x = output_x; window.pending_render.y = output_y + @as(i32, stack_index) * @as(i32, stack_height); window.pending_manage.width = stack_width; // Last stack window gets remaining height to avoid gaps from integer division - if (index == count - 1) { + if (i == active_count - 1) { window.pending_manage.height = output_height - stack_index * stack_height; } else { window.pending_manage.height = stack_height; @@ -142,83 +160,92 @@ fn calculatePrimaryStackLayout(wm: *WindowManager) void { // Borders are automatically disabled when a window is fullscreened so we don't // have to worry about that. const border_width = wm.context.config.border_width; + // We use .? because we know we set the windows height, width, x, and y above window.pending_manage.height.? -= 2 * border_width; window.pending_manage.width.? -= 2 * border_width; window.pending_render.x.? += border_width; window.pending_render.y.? += border_width; - - index += 1; } + + // Make sure we went through the whole list + assert(active_list.first == null); +} + +fn manage_start(wm: *WindowManager) void { + const river_window_manager_v1 = wm.river_window_manager_v1; + const context = wm.context; + if (!context.initialized) { + // This code (should) only be run once while initializing the WM, so let's + // mark it as cold. + @branchHint(.cold); + context.initialized = true; + + const seat = wm.seats.first() orelse @panic("Failed to get seat"); + const river_seat_v1 = seat.river_seat_v1; + context.xkb_bindings.addBinding(river_seat_v1, xkbcommon.Keysym.t, .{ .mod4 = true }, .{ .spawn = &.{"foot"} }); + context.xkb_bindings.addBinding(river_seat_v1, xkbcommon.Keysym.j, .{ .mod4 = true }, .focus_next); + context.xkb_bindings.addBinding(river_seat_v1, xkbcommon.Keysym.k, .{ .mod4 = true }, .focus_prev); + context.xkb_bindings.addBinding(river_seat_v1, xkbcommon.Keysym.f, .{ .mod4 = true }, .fullscreen); + context.xkb_bindings.addBinding(river_seat_v1, xkbcommon.Keysym.q, .{ .mod4 = true, .shift = true }, .close_window); + context.xkb_bindings.addBinding(river_seat_v1, xkbcommon.Keysym.e, .{ .mod4 = true, .shift = true }, .exit); + } + + { + var it = wm.outputs.iterator(.forward); + while (it.next()) |output| { + output.manage(); + } + } + { + // Calculate layout before managing windows + wm.calculatePrimaryStackLayout(); + var it = wm.windows.iterator(.forward); + while (it.next()) |window| { + window.manage(); + } + } + { + var it = wm.seats.iterator(.forward); + while (it.next()) |seat| { + seat.manage(); + } + } + river_window_manager_v1.manageFinish(); +} + +fn render_start(wm: *WindowManager) void { + const river_window_manager_v1 = wm.river_window_manager_v1; + { + var it = wm.seats.iterator(.forward); + while (it.next()) |seat| { + seat.render(); + } + } + { + var it = wm.outputs.iterator(.forward); + while (it.next()) |output| { + output.render(); + } + } + { + var it = wm.windows.iterator(.forward); + while (it.next()) |window| { + window.render(); + } + } + river_window_manager_v1.renderFinish(); } fn windowManagerV1Listener(window_manager_v1: *river.WindowManagerV1, event: river.WindowManagerV1.Event, wm: *WindowManager) void { - assert(wm.window_manager_v1 == window_manager_v1); + assert(wm.river_window_manager_v1 == window_manager_v1); const context = wm.context; switch (event) { .unavailable => { log.err("Window manager unavailable (some other wm instance is running). Exiting", .{}); std.posix.exit(1); }, - .manage_start => { - if (!context.initialized) { - // This code (should) only be run once while initializing the WM, so let's - // mark it as cold. - @branchHint(.cold); - context.initialized = true; - - const seat = wm.seats.first() orelse @panic("Failed to get seat"); - const river_seat_v1 = seat.river_seat_v1; - context.xkb_bindings.addBinding(river_seat_v1, xkbcommon.Keysym.t, .{ .mod4 = true }, .{ .spawn = &.{"foot"} }); - context.xkb_bindings.addBinding(river_seat_v1, xkbcommon.Keysym.j, .{ .mod4 = true }, .focus_next); - context.xkb_bindings.addBinding(river_seat_v1, xkbcommon.Keysym.k, .{ .mod4 = true }, .focus_prev); - context.xkb_bindings.addBinding(river_seat_v1, xkbcommon.Keysym.f, .{ .mod4 = true }, .fullscreen); - context.xkb_bindings.addBinding(river_seat_v1, xkbcommon.Keysym.q, .{ .mod4 = true, .shift = true }, .close_window); - context.xkb_bindings.addBinding(river_seat_v1, xkbcommon.Keysym.e, .{ .mod4 = true, .shift = true }, .exit); - } - - { - var it = wm.outputs.iterator(.forward); - while (it.next()) |output| { - output.manage(); - } - } - // Calculate layout before managing windows - wm.calculatePrimaryStackLayout(); - { - var it = wm.windows.iterator(.forward); - while (it.next()) |window| { - window.manage(); - } - } - { - var it = wm.seats.iterator(.forward); - while (it.next()) |seat| { - seat.manage(); - } - } - window_manager_v1.manageFinish(); - }, - .render_start => { - { - var it = wm.seats.iterator(.forward); - while (it.next()) |seat| { - seat.render(); - } - } - { - var it = wm.outputs.iterator(.forward); - while (it.next()) |output| { - output.render(); - } - } - { - var it = wm.windows.iterator(.forward); - while (it.next()) |window| { - window.render(); - } - } - window_manager_v1.renderFinish(); - }, + .manage_start => wm.manage_start(), + .render_start => wm.render_start(), .output => |ev| { log.debug("initializing new wl_output: {d}", .{ev.id.getId()}); // TODO: Support multi-output @@ -238,6 +265,7 @@ fn windowManagerV1Listener(window_manager_v1: *river.WindowManagerV1, event: riv wm.seats.append(seat); }, .window => |ev| { + // TODO: Support multiple seats const seat = wm.seats.first() orelse @panic("Failed to get seat"); // TODO: Support multiple outputs const output = wm.outputs.first() orelse @panic("Failed to get output"); @@ -258,6 +286,7 @@ fn windowManagerV1Listener(window_manager_v1: *river.WindowManagerV1, event: riv const std = @import("std"); const assert = std.debug.assert; +const DoublyLinkedList = std.DoublyLinkedList; const wayland = @import("wayland"); const wl = wayland.client.wl; diff --git a/src/XkbBindings.zig b/src/XkbBindings.zig index 59f38fc..88fd4fa 100644 --- a/src/XkbBindings.zig +++ b/src/XkbBindings.zig @@ -40,8 +40,8 @@ const XkbBinding = struct { utils.allocator.destroy(xkb_binding); } - fn xkbBindingListener(xkb_binding_v1: *river.XkbBindingV1, event: river.XkbBindingV1.Event, xkb_binding: *XkbBinding) void { - assert(xkb_binding.xkb_binding_v1 == xkb_binding_v1); + fn xkbBindingListener(river_xkb_binding_v1: *river.XkbBindingV1, event: river.XkbBindingV1.Event, xkb_binding: *XkbBinding) void { + assert(xkb_binding.xkb_binding_v1 == river_xkb_binding_v1); switch (event) { .pressed => { xkb_binding.executeCommand(); @@ -136,7 +136,7 @@ pub fn create(context: *Context, xkb_bindings_v1: *river.XkbBindingsV1) !*XkbBin } pub fn destroy(xkb_bindings: *XkbBindings) void { - var it = xkb_bindings.bindings.iterator(.forward); + var it = xkb_bindings.bindings.safeIterator(.forward); while (it.next()) |binding| { binding.link.remove(); binding.xkb_binding_v1.destroy(); diff --git a/src/utils.zig b/src/utils.zig index cba5ff2..049d81d 100644 --- a/src/utils.zig +++ b/src/utils.zig @@ -2,6 +2,8 @@ // // SPDX-License-Identifier: GPL-3.0-or-later +// Allocator used by the program. We use the c_allocator since we interact with C code +// via zig-wayland (and, in the future as of 2026-01-26, other libraries, too). pub const allocator = std.heap.c_allocator; pub const RiverColor = struct { @@ -71,7 +73,7 @@ pub fn interfaceNotAdvertised(comptime WaylandGlobal: type) noreturn { std.posix.exit(1); } -/// Report that the given WaylandGlobal was advertised but the support version was too low exit the program +/// Report that the given WaylandGlobal was advertised but the support version was too low and exit the program pub fn versionNotSupported(comptime WaylandGlobal: type, have_version: u32, need_version: u32) noreturn { log.err("The compositor only advertised {s} version {d} but version {d} is required. Exiting", .{ WaylandGlobal.interface.name, have_version, need_version }); std.posix.exit(1);