From b77c6302ac5351e00537d74a912573a361376056 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Tue, 16 Jan 2024 20:39:35 -0700 Subject: [PATCH] Add xkb-common --- .gitmodules | 2 ++ build.zig | 7 +++++++ deps/zig-xkbcommon | 1 + examples/01_client_connect.zig | 38 ++++++++++++++++++++++++++++++++++ src/main.zig | 22 ++++++++++++++++++++ 5 files changed, 70 insertions(+) create mode 100644 .gitmodules create mode 160000 deps/zig-xkbcommon diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..1b17242 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,2 @@ +[submodule "https://codeberg.org/ifreund/zig-xkbcommon"] + url = deps/zig-xkbcommon/ diff --git a/build.zig b/build.zig index 3218d4d..f8a1057 100644 --- a/build.zig +++ b/build.zig @@ -4,6 +4,10 @@ pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const xkbcommon_module = b.createModule(.{ + .root_source_file = .{ .path = "deps/zig-xkbcommon/src/xkbcommon.zig" }, + }); + const module = b.addModule("wayland", .{ .root_source_file = .{ .path = "src/main.zig" }, }); @@ -42,5 +46,8 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); client_connect_exe.root_module.addImport("wayland", module); + client_connect_exe.root_module.addImport("xkbcommon", xkbcommon_module); + client_connect_exe.linkLibC(); + client_connect_exe.linkSystemLibrary("xkbcommon"); b.installArtifact(client_connect_exe); } diff --git a/deps/zig-xkbcommon b/deps/zig-xkbcommon new file mode 160000 index 0000000..7b188de --- /dev/null +++ b/deps/zig-xkbcommon @@ -0,0 +1 @@ +Subproject commit 7b188de0ba794b52eb70340abf2469b858630816 diff --git a/examples/01_client_connect.zig b/examples/01_client_connect.zig index b347fdb..ed85632 100644 --- a/examples/01_client_connect.zig +++ b/examples/01_client_connect.zig @@ -1,5 +1,6 @@ const std = @import("std"); const wayland = @import("wayland"); +const xkbcommon = @import("xkbcommon"); pub fn main() !void { var general_allocator = std.heap.GeneralPurposeAllocator(.{}){}; @@ -258,6 +259,17 @@ pub fn main() !void { ); var window_size: [2]u32 = [2]u32{ @intCast(framebuffer_size[0]), @intCast(framebuffer_size[1]) }; + const xkb_ctx = xkbcommon.Context.new(.no_flags) orelse return error.XKBInit; + defer xkb_ctx.unref(); + + var xkb_keymap_opt: ?*xkbcommon.Keymap = null; + defer if (xkb_keymap_opt) |xkb_keymap| { + xkb_keymap.unref(); + }; + var xkb_state_opt: ?*xkbcommon.State = null; + defer if (xkb_state_opt) |xkb_state| { + xkb_state.unref(); + }; var running = true; while (running) { @@ -379,6 +391,32 @@ pub fn main() !void { keymap.size, fd, }); + const mem = try std.os.mmap( + null, + keymap.size, + std.os.PROT.READ, + std.os.MAP.PRIVATE, + fd, + 0, + ); + std.debug.print("---START xkb file---\n{s}\n---END xkb file---\n", .{mem}); + xkb_keymap_opt = xkbcommon.Keymap.newFromString(xkb_ctx, @ptrCast(mem), .text_v1, .no_flags) orelse return error.XKBKeymap; + xkb_state_opt = xkbcommon.State.new(xkb_keymap_opt.?) orelse return error.XKBStateInit; + }, + .key => |key| { + if (xkb_state_opt) |xkb_state| { + const keycode: xkbcommon.Keycode = key.key + 8; + const keysym: xkbcommon.Keysym = xkb_state.keyGetOneSym(keycode); + var buf: [64]u8 = undefined; + const name_len = keysym.getName(&buf, buf.len); + std.debug.print("{s}\n", .{buf[0..@intCast(name_len)]}); + + const changed = if (key.state == .pressed) + xkb_state.updateKey(keycode, .down) + else + xkb_state.updateKey(keycode, .up); + _ = changed; + } }, else => { std.debug.print("<- wl_keyboard@{}\n", .{event}); diff --git a/src/main.zig b/src/main.zig index 43e747a..75e869d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -589,3 +589,25 @@ pub const Conn = struct { return .{ header, message }; } }; + +// pub const XKBKeymap = struct { +// keycodes: Keycodes, +// types: Types, +// compatability: Compatability, +// symbols: Symbols, +// +// const Keycodes = struct { +// min: usize, +// max: usize, +// map: std.AutoHashMap(usize, usize), +// indicators: std.StringHashMap(usize), +// }; +// +// const ISOKEY = [4]u8; +// +// const Types = struct {}; +// const Compatability = struct {}; +// const Symbols = struct {}; +// +// pub fn parse() {} +// };