core: detect inputs that result in surface close and avoid segfault

Fixes #965

When processing keybindings that closed the surface (`close_surface`,
`close_window`), the surface and associated runtime structures would be
freed so we could segfault.

This PR introduces a new enum result for input events (only key for now)
that returns whether an event resulted in a close. In this case, callers
can properly return immediately and avoid writing to deallocated memory.
This commit is contained in:
Mitchell Hashimoto
2023-12-07 10:24:39 -08:00
parent 571170c574
commit 9de5d991a2
4 changed files with 74 additions and 20 deletions

View File

@@ -794,7 +794,7 @@ pub const Surface = struct {
} else .invalid;
// Invoke the core Ghostty logic to handle this input.
const consumed = self.core_surface.keyCallback(.{
const effect = self.core_surface.keyCallback(.{
.action = action,
.key = key,
.physical_key = physical_key,
@@ -808,11 +808,15 @@ pub const Surface = struct {
return;
};
// If we consume the key then we want to reset the dead key state.
if (consumed and is_down) {
self.keymap_state = .{};
self.core_surface.preeditCallback(null) catch {};
return;
switch (effect) {
.closed => return,
.ignored => {},
.consumed => if (is_down) {
// If we consume the key then we want to reset the dead
// key state.
self.keymap_state = .{};
self.core_surface.preeditCallback(null) catch {};
},
}
}