wlbg: save cursor positions for all outputs

This commit is contained in:
LordMZTE 2023-10-31 13:30:23 +01:00
parent 70e60a3b95
commit ad019a7331
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
3 changed files with 28 additions and 35 deletions

View file

@ -156,15 +156,14 @@ pub fn preDraw(
self: *Gfx,
dt: i64,
pointer_state: *PointerState,
outputs: []const OutputWindow,
infos: []const OutputInfo,
dth: *DrawTimerHandler,
) !void {
for (self.cursor_positions, infos, outputs, 0..) |*pos, inf, outp, i| {
for (self.cursor_positions, infos, 0..) |*pos, inf, i| {
const lerp_amt = std.math.clamp(@as(f32, @floatFromInt(std.math.clamp(dt, 0, 10))) / 150.0, 0.0, 1.0);
const target = if (pointer_state.surface == outp.surface)
.{ pointer_state.x, pointer_state.y }
const target = if (pointer_state.active_surface_idx == i)
pointer_state.surface_positions[i]
else
.{ @divTrunc(inf.width, 2), @divTrunc(inf.height, 2) };
@ -235,7 +234,7 @@ pub fn draw(
);
c.glUniform1f(
c.glGetUniformLocation(self.main_shader_program, "hasCursor"),
if (outputs[output_idx].surface == pointer_state.surface) 1.0 else 0.0,
if (pointer_state.active_surface_idx == output_idx) 1.0 else 0.0,
);
//c.glUniform1f(
// c.glGetUniformLocation(self.main_shader_program, "time"),

View file

@ -1,5 +1,4 @@
const wl = @import("wayland").client.wl;
surface: ?*wl.Surface,
x: c_int,
y: c_int,
active_surface_idx: ?usize,
surface_positions: [][2]c_int,

View file

@ -144,10 +144,11 @@ pub fn main() !void {
@memset(dth.should_redraw, true);
var pointer_state = PointerState{
.surface = null,
.x = 0,
.y = 0,
.active_surface_idx = null,
.surface_positions = try std.heap.c_allocator.alloc([2]c_int, output_info.len),
};
defer std.heap.c_allocator.free(pointer_state.surface_positions);
@memset(pointer_state.surface_positions, .{ 0, 0 });
var pointer_listener_data = PointerListenerData{
.pstate = &pointer_state,
@ -265,7 +266,6 @@ fn renderCb(
data.?.gfx.preDraw(
delta_time,
data.?.pointer_state,
data.?.outputs,
data.?.output_info,
data.?.dth,
) catch |e| {
@ -387,38 +387,33 @@ const PointerListenerData = struct {
pstate: *PointerState,
outputs: []const OutputWindow,
dth: *DrawTimerHandler,
fn damageCurrentWindow(self: *PointerListenerData) !void {
if (self.pstate.surface) |ps| {
for (self.outputs, 0..) |o, i| {
if (ps == o.surface) {
self.dth.damage(i);
}
}
}
}
};
fn pointerListener(_: *wl.Pointer, ev: wl.Pointer.Event, d: *PointerListenerData) void {
switch (ev) {
.motion => |motion| {
d.pstate.x = motion.surface_x.toInt();
d.pstate.y = motion.surface_y.toInt();
d.damageCurrentWindow() catch |e| {
std.log.err("unable to damage window: {}", .{e});
};
if (d.pstate.active_surface_idx) |i| {
d.pstate.surface_positions[i] = .{
motion.surface_x.toInt(),
motion.surface_y.toInt(),
};
d.dth.damage(i);
}
},
.enter => |enter| {
d.pstate.surface = enter.surface;
d.damageCurrentWindow() catch |e| {
std.log.err("unable to damage window: {}", .{e});
};
for (d.outputs, 0..) |out, i| {
if (out.surface == enter.surface) {
d.dth.damage(i);
d.pstate.active_surface_idx = i;
break;
}
}
},
.leave => {
d.damageCurrentWindow() catch |e| {
std.log.err("unable to damage window: {}", .{e});
};
d.pstate.surface = null;
if (d.pstate.active_surface_idx) |i| {
d.dth.damage(i);
d.pstate.active_surface_idx = null;
}
},
else => {},
}