From 878c0c49c98a15b276a02f30abf3877c5a0708b5 Mon Sep 17 00:00:00 2001 From: adamws Date: Sun, 24 Nov 2024 09:09:53 +0100 Subject: [PATCH] Remove symbol pointer from `KeyData` It can be derived from `keysym` field later, when needed. This change will allow to store `KeyData` to files (the upcoming change in recording process). --- src/main.zig | 13 ++++++------- src/win32.zig | 13 ++++++++----- src/x11.zig | 5 ++++- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main.zig b/src/main.zig index 08d3853..406cc3f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -109,8 +109,6 @@ pub const KeyData = struct { repeated: bool, keycode: u8, keysym: c_ulong, - status: c_int, - symbol: [*c]const u8, // owned by x11, in static area. Must not be modified. string: [32]u8, comptime { @@ -754,14 +752,15 @@ pub fn main() !void { if (app_state.keys.pop()) |k| { app_state.updateKeyStates(@intCast(k.keycode), k.pressed); - if (k.symbol == null) continue; - std.debug.print("Consumed: '{s}'\n", .{k.symbol}); + const symbol = backend.keysymToString(k.keysym); + if (symbol == null) continue; + std.debug.print("Consumed: '{s}'\n", .{symbol}); var text_: [*:0]const u8 = undefined; - if (symbols_lookup.get(std.mem.sliceTo(k.symbol, 0))) |symbol| { - std.debug.print("Replacement: '{s}'\n", .{symbol}); - text_ = symbol; + if (symbols_lookup.get(std.mem.sliceTo(symbol, 0))) |lookup| { + std.debug.print("Replacement: '{s}'\n", .{lookup}); + text_ = lookup; } else { text_ = @ptrCast(&k.string); } diff --git a/src/win32.zig b/src/win32.zig index d7aa673..0265e5c 100644 --- a/src/win32.zig +++ b/src/win32.zig @@ -50,15 +50,14 @@ fn lowLevelKeyboardProc(nCode: c.INT, wParam: c.WPARAM, lParam: c.LPARAM) callco keyEventToString(keyboard.vkCode, keyboard.scanCode, &key.string) catch {}; const extended: bool = ((keyboard.flags & c.LLKHF_EXTENDED) != 0); - var index = @as(usize, @intCast(keyboard.scanCode)); + var index = @as(c_ulong, @intCast(keyboard.scanCode)); if (extended) index += 0x100; if (index >= kbd_en_vscname.len) { index = 0; } - key.symbol = kbd_en_vscname[index].ptr; - - std.debug.print("Pressed vk: '{}', scancode: '{}' extended: {}, string: '{s}', symbol: '{s}'\n", .{ - keyboard.vkCode, keyboard.scanCode, extended, std.mem.sliceTo(&key.string, 0), key.symbol + key.keysym = index; + std.debug.print("Pressed vk: '{}', scancode: '{}' extended: {}, string: '{s}', symbol: '{}'\n", .{ + keyboard.vkCode, keyboard.scanCode, extended, std.mem.sliceTo(&key.string, 0), key.keysym }); } else { key.pressed = false; @@ -94,6 +93,10 @@ pub fn listener(app_state: *AppState, window_handle: *anyopaque, record_file: ?[ } } +pub fn keysymToString(keysym: c_ulong) [*c]const u8 { + return kbd_en_vscname[@as(usize, @intCast(keysym))].ptr; +} + // uses events stored in file to reproduce them // assumes that only expected event types are recorded pub fn producer(app_state: *AppState, window_handle: *anyopaque, replay_file: []const u8, loop: bool) !void { diff --git a/src/x11.zig b/src/x11.zig index 3e6ff8d..4c781b6 100644 --- a/src/x11.zig +++ b/src/x11.zig @@ -78,7 +78,6 @@ const X11InputContext = struct { &key_data.keysym, &status, ); - key_data.symbol = x11.XKeysymToString(key_data.keysym); return len; } }; @@ -209,6 +208,10 @@ pub fn listener(app_state: *AppState, window_handle: *anyopaque, record_file: ?[ _ = x11.XCloseDisplay(display); } +pub fn keysymToString(keysym: c_ulong) [*c]const u8 { + return x11.XKeysymToString(keysym); +} + // uses events stored in file to reproduce them // assumes that only expected event types are recorded pub fn producer(app_state: *AppState, window_handle: *anyopaque, replay_file: []const u8, loop: bool) !void {