refactor(gtk): move device modifier state handling to key.zig

This commit is contained in:
nnyyxxxx
2025-01-25 18:17:45 -05:00
parent cc154404d3
commit 862da9be90
3 changed files with 22 additions and 24 deletions

View File

@ -49,6 +49,22 @@ pub fn translateMods(state: c.GdkModifierType) input.Mods {
return mods;
}
pub fn deviceMods(device: ?*c.GdkDevice, gtk_mods: c.GdkModifierType) ?input.Mods {
if (device) |dev| {
const device_mods = c.gdk_device_get_modifier_state(dev);
if (device_mods != gtk_mods) {
return input.Mods{
.shift = (device_mods & c.GDK_SHIFT_MASK) != 0,
.ctrl = (device_mods & c.GDK_CONTROL_MASK) != 0,
.alt = (device_mods & c.GDK_ALT_MASK) != 0,
.super = (device_mods & c.GDK_SUPER_MASK) != 0,
.caps_lock = (device_mods & c.GDK_LOCK_MASK) != 0,
};
}
}
return null;
}
// Get the unshifted unicode value of the keyval. This is used
// by the Kitty keyboard protocol.
pub fn keyvalUnicodeUnshifted(

View File

@ -5,6 +5,7 @@ const Allocator = std.mem.Allocator;
const c = @import("../c.zig").c;
const Config = @import("../../../config.zig").Config;
const input = @import("../../../input.zig");
const key = @import("../key.zig");
const wl = wayland.client.wl;
const org = wayland.client.org;
@ -78,20 +79,9 @@ pub const App = struct {
pub fn eventMods(
_: App,
device: ?*c.GdkDevice,
_: c.GdkModifierType,
gtk_mods: c.GdkModifierType,
) ?input.Mods {
if (device) |dev| {
const device_mods = c.gdk_device_get_modifier_state(dev);
return input.Mods{
.shift = (device_mods & c.GDK_SHIFT_MASK) != 0,
.ctrl = (device_mods & c.GDK_CONTROL_MASK) != 0,
.alt = (device_mods & c.GDK_ALT_MASK) != 0,
.super = (device_mods & c.GDK_SUPER_MASK) != 0,
.caps_lock = (device_mods & c.GDK_LOCK_MASK) != 0,
};
}
return null;
return key.deviceMods(device, gtk_mods);
}
fn registryListener(

View File

@ -7,6 +7,7 @@ const c = @import("../c.zig").c;
const input = @import("../../../input.zig");
const Config = @import("../../../config.zig").Config;
const adwaita = @import("../adwaita.zig");
const key = @import("../key.zig");
const log = std.log.scoped(.gtk_x11);
@ -122,17 +123,8 @@ pub const App = struct {
device: ?*c.GdkDevice,
gtk_mods: c.GdkModifierType,
) ?input.Mods {
if (device) |dev| {
const device_mods = c.gdk_device_get_modifier_state(dev);
if (device_mods != gtk_mods) {
return input.Mods{
.shift = (device_mods & c.GDK_SHIFT_MASK) != 0,
.ctrl = (device_mods & c.GDK_CONTROL_MASK) != 0,
.alt = (device_mods & c.GDK_ALT_MASK) != 0,
.super = (device_mods & c.GDK_SUPER_MASK) != 0,
.caps_lock = (device_mods & c.GDK_LOCK_MASK) != 0,
};
}
if (key.deviceMods(device, gtk_mods)) |mods| {
return mods;
}
// Shoutout to Mozilla for figuring out a clean way to do this, this is