Skip to content

Commit

Permalink
Remove symbol pointer from KeyData
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
adamws committed Nov 24, 2024
1 parent eda3817 commit 878c0c4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
13 changes: 6 additions & 7 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down
13 changes: 8 additions & 5 deletions src/win32.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion src/x11.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ const X11InputContext = struct {
&key_data.keysym,
&status,
);
key_data.symbol = x11.XKeysymToString(key_data.keysym);
return len;
}
};
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 878c0c4

Please sign in to comment.