Add xkb-common

dev
Louis Pearson 2024-01-16 20:39:35 -07:00
parent e39f8e6ec8
commit b77c6302ac
5 changed files with 70 additions and 0 deletions

2
.gitmodules vendored Normal file
View File

@ -0,0 +1,2 @@
[submodule "https://codeberg.org/ifreund/zig-xkbcommon"]
url = deps/zig-xkbcommon/

View File

@ -4,6 +4,10 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const xkbcommon_module = b.createModule(.{
.root_source_file = .{ .path = "deps/zig-xkbcommon/src/xkbcommon.zig" },
});
const module = b.addModule("wayland", .{ const module = b.addModule("wayland", .{
.root_source_file = .{ .path = "src/main.zig" }, .root_source_file = .{ .path = "src/main.zig" },
}); });
@ -42,5 +46,8 @@ pub fn build(b: *std.Build) void {
.optimize = optimize, .optimize = optimize,
}); });
client_connect_exe.root_module.addImport("wayland", module); 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); b.installArtifact(client_connect_exe);
} }

1
deps/zig-xkbcommon vendored Submodule

@ -0,0 +1 @@
Subproject commit 7b188de0ba794b52eb70340abf2469b858630816

View File

@ -1,5 +1,6 @@
const std = @import("std"); const std = @import("std");
const wayland = @import("wayland"); const wayland = @import("wayland");
const xkbcommon = @import("xkbcommon");
pub fn main() !void { pub fn main() !void {
var general_allocator = std.heap.GeneralPurposeAllocator(.{}){}; 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]) }; 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; var running = true;
while (running) { while (running) {
@ -379,6 +391,32 @@ pub fn main() !void {
keymap.size, keymap.size,
fd, 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 => { else => {
std.debug.print("<- wl_keyboard@{}\n", .{event}); std.debug.print("<- wl_keyboard@{}\n", .{event});

View File

@ -589,3 +589,25 @@ pub const Conn = struct {
return .{ header, message }; 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() {}
// };