Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

1.7 KB
Raw Blame
//! The window an output shows as a game: the keyboard-focused toplevel that
//! fills it, fullscreen or borderless. Tearing follows it (and automatic
//! adaptive sync will), so every caller agrees on one rule.

const wlr = @import("wlroots");
const Output = @import("../Output.zig");
const Toplevel = @import("../Toplevel.zig");

pub fn window(output: *Output) ?*Toplevel {
    const server = output.server;
    const focused = server.input.seat.keyboard_state.focused_surface orelse return null;
    const top = Toplevel.fromSurface(server, focused) orelse return null;
    if (!top.in_world or top.minimized or top.tab_hidden or top.currentOutput() != output) return null;
    if (top.isFullscreen() or coversOutput(top, output)) return top;
    return null;
}

/// Borderless windowed, the default of many games: an undecorated window the
/// client placed itself (not maximized or tiled) whose frame is the output.
fn coversOutput(top: *Toplevel, output: *Output) bool {
    if (top.hasServerDecorations() or top.isMaximized() or top.forcedGeometry() != null) return false;
    var box: wlr.Box = undefined;
    output.server.output_layout.getBox(output.wlr_output, &box);
    if (box.width <= 0 or box.height <= 0) return false;
    const world = &output.server.world;
    const a = top.frameWorld(0, 0);
    const b = top.frameWorld(@floatFromInt(top.chrome_width), @floatFromInt(top.chrome_height));
    const start = world.toLayout(a.x, a.y);
    const end = world.toLayout(b.x, b.y);
    return near(start.x, box.x) and near(start.y, box.y) and
        near(end.x, box.x + box.width) and near(end.y, box.y + box.height);
}

fn near(value: f64, edge: i32) bool {
    return @abs(value - @as(f64, @floatFromInt(edge))) < 1.0;
}