core: remove redundant writes to input_state

This commit is contained in:
Joshua Holmes 2024-11-16 16:28:18 -08:00 committed by Emi Gutekanst
parent a1dfaa2032
commit f220494649
2 changed files with 8 additions and 23 deletions

View file

@ -4,6 +4,7 @@ const gpu = mach.gpu;
const Linux = @import("../Linux.zig"); const Linux = @import("../Linux.zig");
const Core = @import("../../Core.zig"); const Core = @import("../../Core.zig");
const InitOptions = Core.InitOptions; const InitOptions = Core.InitOptions;
const KeyEvent = Core.KeyEvent;
const log = std.log.scoped(.mach); const log = std.log.scoped(.mach);
pub const Wayland = @This(); pub const Wayland = @This();
@ -58,7 +59,6 @@ libwaylandclient: LibWaylandClient,
// input stuff // input stuff
keyboard: ?*c.wl_keyboard = null, keyboard: ?*c.wl_keyboard = null,
pointer: ?*c.wl_pointer = null, pointer: ?*c.wl_pointer = null,
input_state: Core.InputState,
// keyboard stuff // keyboard stuff
xkb_context: ?*c.xkb_context = null, xkb_context: ?*c.xkb_context = null,
@ -91,7 +91,6 @@ pub fn init(
.shift = false, .shift = false,
.super = false, .super = false,
}, },
.input_state = .{},
.modifier_indices = .{ // TODO: make sure these are always getting initialized, we don't want undefined behavior .modifier_indices = .{ // TODO: make sure these are always getting initialized, we don't want undefined behavior
.control_index = undefined, .control_index = undefined,
.alt_index = undefined, .alt_index = undefined,
@ -481,16 +480,12 @@ const keyboard_listener = struct {
_ = time; _ = time;
var wl = &linux.backend.wayland; var wl = &linux.backend.wayland;
const key = toMachKey(scancode);
const pressed = state == 1; const pressed = state == 1;
wl.input_state.keys.setValue(@intFromEnum(key), pressed); const key_event = KeyEvent{ .key = toMachKey(scancode), .mods = wl.modifiers };
if (pressed) { if (pressed) {
wl.state.pushEvent(Core.Event{ .key_press = .{ wl.state.pushEvent(.{ .key_press = key_event });
.key = key,
.mods = wl.modifiers,
} });
var keysyms: ?[*]c.xkb_keysym_t = undefined; var keysyms: ?[*]c.xkb_keysym_t = undefined;
//Get the keysym from the keycode (scancode + 8) //Get the keysym from the keycode (scancode + 8)
@ -505,10 +500,7 @@ const keyboard_listener = struct {
} }
} }
} else { } else {
wl.state.pushEvent(Core.Event{ .key_release = .{ wl.state.pushEvent(.{ .key_release = key_event });
.key = key,
.mods = wl.modifiers,
} });
} }
} }
@ -638,7 +630,6 @@ const pointer_listener = struct {
const y = c.wl_fixed_to_double(fixed_y); const y = c.wl_fixed_to_double(fixed_y);
wl.state.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } }); wl.state.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } });
wl.input_state.mouse_position = .{ .x = x, .y = y };
} }
fn handlePointerButton(linux: *Linux, pointer: ?*c.struct_wl_pointer, serial: u32, time: u32, button: u32, state: u32) callconv(.C) void { fn handlePointerButton(linux: *Linux, pointer: ?*c.struct_wl_pointer, serial: u32, time: u32, button: u32, state: u32) callconv(.C) void {
@ -649,20 +640,20 @@ const pointer_listener = struct {
const mouse_button: Core.MouseButton = @enumFromInt(button - c.BTN_LEFT); const mouse_button: Core.MouseButton = @enumFromInt(button - c.BTN_LEFT);
const pressed = state == c.WL_POINTER_BUTTON_STATE_PRESSED; const pressed = state == c.WL_POINTER_BUTTON_STATE_PRESSED;
const x = wl.state.input_state.mouse_position.x;
wl.input_state.mouse_buttons.setValue(@intFromEnum(mouse_button), pressed); const y = wl.state.input_state.mouse_position.y;
if (pressed) { if (pressed) {
wl.state.pushEvent(Core.Event{ .mouse_press = .{ wl.state.pushEvent(Core.Event{ .mouse_press = .{
.button = mouse_button, .button = mouse_button,
.mods = wl.modifiers, .mods = wl.modifiers,
.pos = wl.input_state.mouse_position, .pos = .{ .x = x, .y = y },
} }); } });
} else { } else {
wl.state.pushEvent(Core.Event{ .mouse_release = .{ wl.state.pushEvent(Core.Event{ .mouse_release = .{
.button = mouse_button, .button = mouse_button,
.mods = wl.modifiers, .mods = wl.modifiers,
.pos = wl.input_state.mouse_position, .pos = .{ .x = x, .y = y },
} }); } });
} }
} }

View file

@ -501,7 +501,6 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
switch (event.type) { switch (event.type) {
c.KeyPress => { c.KeyPress => {
x11.state.input_state.keys.set(@intFromEnum(key_event.key));
x11.state.pushEvent(.{ .key_press = key_event }); x11.state.pushEvent(.{ .key_press = key_event });
const codepoint = x11.libxkbcommon.xkb_keysym_to_utf32(@truncate(keysym)); const codepoint = x11.libxkbcommon.xkb_keysym_to_utf32(@truncate(keysym));
@ -510,7 +509,6 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
} }
}, },
c.KeyRelease => { c.KeyRelease => {
x11.state.input_state.keys.unset(@intFromEnum(key_event.key));
x11.state.pushEvent(.{ .key_release = key_event }); x11.state.pushEvent(.{ .key_release = key_event });
}, },
else => unreachable, else => unreachable,
@ -536,7 +534,6 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
.mods = toMachMods(event.xbutton.state), .mods = toMachMods(event.xbutton.state),
}; };
x11.state.input_state.mouse_buttons.set(@intFromEnum(mouse_button.button));
x11.state.pushEvent(.{ .mouse_press = mouse_button }); x11.state.pushEvent(.{ .mouse_press = mouse_button });
}, },
c.ButtonRelease => { c.ButtonRelease => {
@ -548,7 +545,6 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
.mods = toMachMods(event.xbutton.state), .mods = toMachMods(event.xbutton.state),
}; };
x11.state.input_state.mouse_buttons.unset(@intFromEnum(mouse_button.button));
x11.state.pushEvent(.{ .mouse_release = mouse_button }); x11.state.pushEvent(.{ .mouse_release = mouse_button });
}, },
c.ClientMessage => { c.ClientMessage => {
@ -578,13 +574,11 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
c.EnterNotify => { c.EnterNotify => {
const x: f32 = @floatFromInt(event.xcrossing.x); const x: f32 = @floatFromInt(event.xcrossing.x);
const y: f32 = @floatFromInt(event.xcrossing.y); const y: f32 = @floatFromInt(event.xcrossing.y);
x11.state.input_state.mouse_position = .{ .x = x, .y = y };
x11.state.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } }); x11.state.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } });
}, },
c.MotionNotify => { c.MotionNotify => {
const x: f32 = @floatFromInt(event.xmotion.x); const x: f32 = @floatFromInt(event.xmotion.x);
const y: f32 = @floatFromInt(event.xmotion.y); const y: f32 = @floatFromInt(event.xmotion.y);
x11.state.input_state.mouse_position = .{ .x = x, .y = y };
x11.state.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } }); x11.state.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } });
}, },
c.ConfigureNotify => { c.ConfigureNotify => {