core: various fixes

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-11-17 21:06:51 -07:00 committed by Emi Gutekanst
parent 714f200bc1
commit fc13b371bf
5 changed files with 41 additions and 49 deletions

View file

@ -46,7 +46,6 @@ export fn wl_proxy_destroy(proxy: ?*c.struct_wl_proxy) void {
return @call(.always_tail, libwaylandclient_global.wl_proxy_destroy, .{proxy});
}
state: *Core,
core: *Core,
surface_descriptor: *gpu.Surface.DescriptorFromWaylandSurface,
configured: bool = false,
@ -77,8 +76,7 @@ pub fn init(
libwaylandclient_global = try LibWaylandClient.load();
linux.backend = .{
.wayland = Wayland{
.core = @fieldParentPtr("platform", linux),
.state = core.state(),
.core = core,
.libxkbcommon = try LibXkbCommon.load(),
.libwaylandclient = libwaylandclient_global,
.interfaces = Interfaces{},
@ -462,7 +460,7 @@ const keyboard_listener = struct {
_ = keys;
const wl = &linux.backend.wayland;
wl.state.pushEvent(.focus_gained);
wl.core.pushEvent(.focus_gained);
}
fn keyboardHandleLeave(linux: *Linux, keyboard: ?*c.struct_wl_keyboard, serial: u32, surface: ?*c.struct_wl_surface) callconv(.C) void {
@ -471,7 +469,7 @@ const keyboard_listener = struct {
_ = surface;
const wl = &linux.backend.wayland;
wl.state.pushEvent(.focus_lost);
wl.core.pushEvent(.focus_lost);
}
fn keyboardHandleKey(linux: *Linux, keyboard: ?*c.struct_wl_keyboard, serial: u32, time: u32, scancode: u32, state: u32) callconv(.C) void {
@ -485,7 +483,7 @@ const keyboard_listener = struct {
const key_event = KeyEvent{ .key = toMachKey(scancode), .mods = wl.modifiers };
if (pressed) {
wl.state.pushEvent(.{ .key_press = key_event });
wl.core.pushEvent(.{ .key_press = key_event });
var keysyms: ?[*]c.xkb_keysym_t = undefined;
//Get the keysym from the keycode (scancode + 8)
@ -496,11 +494,11 @@ const keyboard_listener = struct {
//Try to convert that keysym to a unicode codepoint
const codepoint = wl.libxkbcommon.xkb_keysym_to_utf32(keysym);
if (codepoint != 0) {
wl.state.pushEvent(Core.Event{ .char_input = .{ .codepoint = @truncate(codepoint) } });
wl.core.pushEvent(Core.Event{ .char_input = .{ .codepoint = @truncate(codepoint) } });
}
}
} else {
wl.state.pushEvent(.{ .key_release = key_event });
wl.core.pushEvent(.{ .key_release = key_event });
}
}
@ -629,7 +627,7 @@ const pointer_listener = struct {
const x = c.wl_fixed_to_double(fixed_x);
const y = c.wl_fixed_to_double(fixed_y);
wl.state.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } });
wl.core.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } });
}
fn handlePointerButton(linux: *Linux, pointer: ?*c.struct_wl_pointer, serial: u32, time: u32, button: u32, state: u32) callconv(.C) void {
@ -644,13 +642,13 @@ const pointer_listener = struct {
const y = wl.state.input_state.mouse_position.y;
if (pressed) {
wl.state.pushEvent(Core.Event{ .mouse_press = .{
wl.core.pushEvent(Core.Event{ .mouse_press = .{
.button = mouse_button,
.mods = wl.modifiers,
.pos = .{ .x = x, .y = y },
} });
} else {
wl.state.pushEvent(Core.Event{ .mouse_release = .{
wl.core.pushEvent(Core.Event{ .mouse_release = .{
.button = mouse_button,
.mods = wl.modifiers,
.pos = .{ .x = x, .y = y },

View file

@ -34,7 +34,6 @@ pub const X11 = @This();
allocator: std.mem.Allocator,
core: *Core,
state: *Core,
libx11: LibX11,
libxrr: ?LibXRR,
@ -141,8 +140,7 @@ pub fn init(
.window = @intCast(window),
};
linux.backend = .{ .x11 = X11{
.core = @fieldParentPtr("platform", linux),
.state = core.state(),
.core = core,
.allocator = options.allocator,
.display = display,
.libx11 = libx11,
@ -501,15 +499,15 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
switch (event.type) {
c.KeyPress => {
x11.state.pushEvent(.{ .key_press = key_event });
x11.core.pushEvent(.{ .key_press = key_event });
const codepoint = x11.libxkbcommon.xkb_keysym_to_utf32(@truncate(keysym));
if (codepoint != 0) {
x11.state.pushEvent(.{ .char_input = .{ .codepoint = @truncate(codepoint) } });
x11.core.pushEvent(.{ .char_input = .{ .codepoint = @truncate(codepoint) } });
}
},
c.KeyRelease => {
x11.state.pushEvent(.{ .key_release = key_event });
x11.core.pushEvent(.{ .key_release = key_event });
},
else => unreachable,
}
@ -524,7 +522,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
7 => .{ -1.0, 0.0 },
else => unreachable,
};
x11.state.pushEvent(.{ .mouse_scroll = .{ .xoffset = scroll[0], .yoffset = scroll[1] } });
x11.core.pushEvent(.{ .mouse_scroll = .{ .xoffset = scroll[0], .yoffset = scroll[1] } });
return;
};
const cursor_pos = x11.getCursorPos();
@ -534,7 +532,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
.mods = toMachMods(event.xbutton.state),
};
x11.state.pushEvent(.{ .mouse_press = mouse_button });
x11.core.pushEvent(.{ .mouse_press = mouse_button });
},
c.ButtonRelease => {
const button = toMachButton(event.xbutton.button) orelse return;
@ -545,7 +543,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
.mods = toMachMods(event.xbutton.state),
};
x11.state.pushEvent(.{ .mouse_release = mouse_button });
x11.core.pushEvent(.{ .mouse_release = mouse_button });
},
c.ClientMessage => {
if (event.xclient.message_type == c.None) return;
@ -555,7 +553,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
if (protocol == c.None) return;
if (protocol == x11.wm_delete_window) {
x11.state.pushEvent(.close);
x11.core.pushEvent(.close);
} else if (protocol == x11.net_wm_ping) {
// The window manager is pinging the application to ensure
// it's still responding to events
@ -574,12 +572,12 @@ 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.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } });
x11.core.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.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } });
x11.core.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } });
},
c.ConfigureNotify => {
if (event.xconfigure.width != linux.size.width or
@ -588,7 +586,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
linux.size.width = @intCast(event.xconfigure.width);
linux.size.height = @intCast(event.xconfigure.height);
x11.core.swap_chain_update.set();
x11.state.pushEvent(.{
x11.core.pushEvent(.{
.framebuffer_resize = .{
.width = linux.size.width,
.height = linux.size.height,
@ -605,7 +603,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
return;
}
x11.state.pushEvent(.focus_gained);
x11.core.pushEvent(.focus_gained);
},
c.FocusOut => {
if (event.xfocus.mode == c.NotifyGrab or
@ -616,7 +614,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
return;
}
x11.state.pushEvent(.focus_lost);
x11.core.pushEvent(.focus_lost);
},
else => {},
}