Skip to content

Commit

Permalink
core: remove redundant writes to input_state
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-holmes authored and emidoots committed Nov 17, 2024
1 parent a1dfaa2 commit f220494
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
25 changes: 8 additions & 17 deletions src/core/linux/Wayland.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const gpu = mach.gpu;
const Linux = @import("../Linux.zig");
const Core = @import("../../Core.zig");
const InitOptions = Core.InitOptions;
const KeyEvent = Core.KeyEvent;
const log = std.log.scoped(.mach);

pub const Wayland = @This();
Expand Down Expand Up @@ -58,7 +59,6 @@ libwaylandclient: LibWaylandClient,
// input stuff
keyboard: ?*c.wl_keyboard = null,
pointer: ?*c.wl_pointer = null,
input_state: Core.InputState,

// keyboard stuff
xkb_context: ?*c.xkb_context = null,
Expand Down Expand Up @@ -91,7 +91,6 @@ pub fn init(
.shift = false,
.super = false,
},
.input_state = .{},
.modifier_indices = .{ // TODO: make sure these are always getting initialized, we don't want undefined behavior
.control_index = undefined,
.alt_index = undefined,
Expand Down Expand Up @@ -481,16 +480,12 @@ const keyboard_listener = struct {
_ = time;
var wl = &linux.backend.wayland;

const key = toMachKey(scancode);
const pressed = state == 1;

wl.input_state.keys.setValue(@intFromEnum(key), pressed);
const key_event = KeyEvent{ .key = toMachKey(scancode), .mods = wl.modifiers };

if (pressed) {
wl.state.pushEvent(Core.Event{ .key_press = .{
.key = key,
.mods = wl.modifiers,
} });
wl.state.pushEvent(.{ .key_press = key_event });

var keysyms: ?[*]c.xkb_keysym_t = undefined;
//Get the keysym from the keycode (scancode + 8)
Expand All @@ -505,10 +500,7 @@ const keyboard_listener = struct {
}
}
} else {
wl.state.pushEvent(Core.Event{ .key_release = .{
.key = key,
.mods = wl.modifiers,
} });
wl.state.pushEvent(.{ .key_release = key_event });
}
}

Expand Down Expand Up @@ -638,7 +630,6 @@ const pointer_listener = struct {
const y = c.wl_fixed_to_double(fixed_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 {
Expand All @@ -649,20 +640,20 @@ const pointer_listener = struct {

const mouse_button: Core.MouseButton = @enumFromInt(button - c.BTN_LEFT);
const pressed = state == c.WL_POINTER_BUTTON_STATE_PRESSED;

wl.input_state.mouse_buttons.setValue(@intFromEnum(mouse_button), pressed);
const x = wl.state.input_state.mouse_position.x;
const y = wl.state.input_state.mouse_position.y;

if (pressed) {
wl.state.pushEvent(Core.Event{ .mouse_press = .{
.button = mouse_button,
.mods = wl.modifiers,
.pos = wl.input_state.mouse_position,
.pos = .{ .x = x, .y = y },
} });
} else {
wl.state.pushEvent(Core.Event{ .mouse_release = .{
.button = mouse_button,
.mods = wl.modifiers,
.pos = wl.input_state.mouse_position,
.pos = .{ .x = x, .y = y },
} });
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/core/linux/X11.zig
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,6 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {

switch (event.type) {
c.KeyPress => {
x11.state.input_state.keys.set(@intFromEnum(key_event.key));
x11.state.pushEvent(.{ .key_press = key_event });

const codepoint = x11.libxkbcommon.xkb_keysym_to_utf32(@truncate(keysym));
Expand All @@ -510,7 +509,6 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
}
},
c.KeyRelease => {
x11.state.input_state.keys.unset(@intFromEnum(key_event.key));
x11.state.pushEvent(.{ .key_release = key_event });
},
else => unreachable,
Expand All @@ -536,7 +534,6 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
.mods = toMachMods(event.xbutton.state),
};

x11.state.input_state.mouse_buttons.set(@intFromEnum(mouse_button.button));
x11.state.pushEvent(.{ .mouse_press = mouse_button });
},
c.ButtonRelease => {
Expand All @@ -548,7 +545,6 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
.mods = toMachMods(event.xbutton.state),
};

x11.state.input_state.mouse_buttons.unset(@intFromEnum(mouse_button.button));
x11.state.pushEvent(.{ .mouse_release = mouse_button });
},
c.ClientMessage => {
Expand Down Expand Up @@ -578,13 +574,11 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
c.EnterNotify => {
const x: f32 = @floatFromInt(event.xcrossing.x);
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 } } });
},
c.MotionNotify => {
const x: f32 = @floatFromInt(event.xmotion.x);
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 } } });
},
c.ConfigureNotify => {
Expand Down

0 comments on commit f220494

Please sign in to comment.