Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

Replace zig-xkbcommon with an in-tree facade

oxydizer593f520536ea
diff --git a/build.zig b/build.zig
index 9e589e1..ead8c6d 100644
--- a/build.zig
+++ b/build.zig
@@ -39,9 +39,10 @@ pub fn build(b: *std.Build) void {
const c_headers = support.cHeaders(b, target, optimize);
const wayland = protocols.module(b);
- const xkbcommon = b.dependency("xkbcommon", .{}).module("xkbcommon");
- // Hand-written facade over pixman's region API (the only part this
- // compositor uses); see src/wlr/pixman.zig. No package dependency.
+ // Hand-written facades over the parts of xkbcommon and pixman this
+ // compositor actually calls; see src/wlr/xkbcommon.zig and
+ // src/wlr/pixman.zig. Neither has a package dependency.
+ const xkbcommon = b.createModule(.{ .root_source_file = b.path("src/wlr/xkbcommon.zig") });
const pixman = b.createModule(.{ .root_source_file = b.path("src/wlr/pixman.zig") });
// One header-backed module supplies every compositor wlroots binding.
const wlr_facade = b.createModule(.{
diff --git a/build.zig.zon b/build.zig.zon
index 6a621da..207ade1 100644
--- a/build.zig.zon
+++ b/build.zig.zon
@@ -21,10 +21,6 @@
.url = "https://codeberg.org/ifreund/zig-wayland/archive/v0.7.0.tar.gz",
.hash = "wayland-0.7.0-lQa1kuP8AQD5dW7_o-VKLVD_QbqXe7Zob78r2su7Pyex",
},
- .xkbcommon = .{
- .url = "https://codeberg.org/ifreund/zig-xkbcommon/archive/v0.5.0.tar.gz",
- .hash = "xkbcommon-0.5.0-VDqIe8y3AgDU-cGL02xZplwABH3WbpTimrrgbgTgD_3g",
- },
},
.fingerprint = 0x6c5977b13cbceb1,
}
diff --git a/src/wlr/AGENTS.md b/src/wlr/AGENTS.md
index cff3c0c..9abde26 100644
--- a/src/wlr/AGENTS.md
+++ b/src/wlr/AGENTS.md
@@ -13,6 +13,17 @@ real `pixman_region32_t`, since `Region32` is embedded by value in
`node.zig`'s `Buffer` and crosses into wlroots calls via `abi.bind`. Extend
it rather than reintroducing the package if more of the API is needed.
+`@import("xkbcommon")` resolves to `xkbcommon.zig`, a hand-written facade
+over the part of xkbcommon this compositor calls: building a keymap from
+RMLVO names, tracking modifier/layout state, and keysym lookups. There is no
+zig-xkbcommon package dependency either (keep `LICENSE.zig-xkbcommon`).
+Unlike `Region32`, `Context`/`Keymap`/`State` are opaque handles — pointers
+only, never embedded by value — so they need no layout check to cross the
+wlroots boundary. `Keysym` is a plain non-exhaustive `u32` enum: add a named
+constant (value from xkbcommon-keysyms.h) rather than growing a full keysym
+table. Extend this file rather than reintroducing the package if more of the
+API is needed.
+
## Boundary and layouts
* `c.h` lists every wlroots header; the build translates it once into
diff --git a/src/wlr/LICENSE.zig-xkbcommon b/src/wlr/LICENSE.zig-xkbcommon
new file mode 100644
index 0000000..6588f3b
--- /dev/null
+++ b/src/wlr/LICENSE.zig-xkbcommon
@@ -0,0 +1,19 @@
+Copyright 2020 Isaac Freund
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the "Software"),
+to deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/src/wlr/xkbcommon.zig b/src/wlr/xkbcommon.zig
new file mode 100644
index 0000000..042e9fc
--- /dev/null
+++ b/src/wlr/xkbcommon.zig
@@ -0,0 +1,213 @@
+//! Minimal facade over the part of xkbcommon this compositor actually calls:
+//! building a keymap from RMLVO names, tracking modifier/layout state, and
+//! looking up keysyms. Adapted from zig-xkbcommon; see LICENSE.zig-xkbcommon.
+//! `Context`/`Keymap`/`State` are opaque handles (pointers only, never
+//! embedded by value), so unlike pixman's `Region32` they need no layout
+//! check to cross the wlroots boundary via `abi.bind` (src/wlr/keyboard.zig).
+//! Extend this file rather than reintroducing the package if more of the
+//! API is needed.
+
+pub const Keycode = u32;
+pub const LayoutIndex = u32;
+pub const LevelIndex = u32;
+pub const ModIndex = u32;
+pub const ModMask = u32;
+pub const LED_Index = u32;
+
+pub const mod_invalid = 0xffff_ffff;
+
+pub const RuleNames = extern struct {
+ rules: ?[*:0]const u8,
+ model: ?[*:0]const u8,
+ layout: ?[*:0]const u8,
+ variant: ?[*:0]const u8,
+ options: ?[*:0]const u8,
+};
+
+pub const Context = opaque {
+ pub const Flags = enum(c_int) {
+ no_flags = 0,
+ };
+
+ extern fn xkb_context_new(flags: Flags) ?*Context;
+ pub const new = xkb_context_new;
+
+ extern fn xkb_context_unref(context: *Context) void;
+ pub const unref = xkb_context_unref;
+};
+
+pub const Keymap = opaque {
+ pub const CompileFlags = enum(c_int) {
+ no_flags = 0,
+ };
+
+ extern fn xkb_keymap_new_from_names(context: *Context, names: ?*const RuleNames, flags: CompileFlags) ?*Keymap;
+ pub const newFromNames = xkb_keymap_new_from_names;
+
+ extern fn xkb_keymap_unref(keymap: *Keymap) void;
+ pub const unref = xkb_keymap_unref;
+
+ extern fn xkb_keymap_min_keycode(keymap: *Keymap) Keycode;
+ pub const minKeycode = xkb_keymap_min_keycode;
+
+ extern fn xkb_keymap_max_keycode(keymap: *Keymap) Keycode;
+ pub const maxKeycode = xkb_keymap_max_keycode;
+
+ extern fn xkb_keymap_key_get_name(keymap: *Keymap, key: Keycode) ?[*:0]const u8;
+ pub const keyGetName = xkb_keymap_key_get_name;
+
+ extern fn xkb_keymap_key_by_name(keymap: *Keymap, name: [*:0]const u8) Keycode;
+ pub const keyByName = xkb_keymap_key_by_name;
+
+ extern fn xkb_keymap_mod_get_index(keymap: *Keymap, name: [*:0]const u8) ModIndex;
+ pub const modGetIndex = xkb_keymap_mod_get_index;
+
+ extern fn xkb_keymap_num_layouts(keymap: *Keymap) LayoutIndex;
+ pub const numLayouts = xkb_keymap_num_layouts;
+
+ extern fn xkb_keymap_layout_get_name(keymap: *Keymap, idx: LayoutIndex) ?[*:0]const u8;
+ pub const layoutGetName = xkb_keymap_layout_get_name;
+
+ extern fn xkb_keymap_num_layouts_for_key(keymap: *Keymap, key: Keycode) LayoutIndex;
+ pub const numLayoutsForKey = xkb_keymap_num_layouts_for_key;
+
+ extern fn xkb_keymap_num_levels_for_key(keymap: *Keymap, key: Keycode, layout: LayoutIndex) LevelIndex;
+ pub const numLevelsForKey = xkb_keymap_num_levels_for_key;
+
+ extern fn xkb_keymap_key_get_syms_by_level(keymap: *Keymap, key: Keycode, layout: LayoutIndex, level: LevelIndex, syms_out: *?[*]const Keysym) c_int;
+ pub fn keyGetSymsByLevel(keymap: *Keymap, key: Keycode, layout: LayoutIndex, level: LevelIndex) []const Keysym {
+ var ptr: ?[*]const Keysym = undefined;
+ const len = xkb_keymap_key_get_syms_by_level(keymap, key, layout, level, &ptr);
+ return if (len == 0) &[0]Keysym{} else ptr.?[0..@intCast(len)];
+ }
+
+ extern fn xkb_keymap_key_repeats(keymap: *Keymap, key: Keycode) c_int;
+ pub const keyRepeats = xkb_keymap_key_repeats;
+};
+
+pub const State = opaque {
+ pub const Component = enum(c_int) {
+ _,
+ pub const layout_effective = 1 << 7;
+ };
+
+ extern fn xkb_state_new(keymap: *Keymap) ?*State;
+ pub const new = xkb_state_new;
+
+ extern fn xkb_state_unref(state: *State) void;
+ pub const unref = xkb_state_unref;
+
+ extern fn xkb_state_get_keymap(state: *State) *Keymap;
+ pub const getKeymap = xkb_state_get_keymap;
+
+ extern fn xkb_state_update_mask(
+ state: *State,
+ depressed_mods: ModMask,
+ latched_mods: ModMask,
+ locked_mods: ModMask,
+ depressed_layout: LayoutIndex,
+ latched_layout: LayoutIndex,
+ locked_layout: LayoutIndex,
+ ) Component;
+ pub const updateMask = xkb_state_update_mask;
+
+ extern fn xkb_state_key_get_syms(state: *State, key: Keycode, syms_out: *?[*]const Keysym) c_int;
+ pub fn keyGetSyms(state: *State, key: Keycode) []const Keysym {
+ var ptr: ?[*]const Keysym = undefined;
+ const len = xkb_state_key_get_syms(state, key, &ptr);
+ return if (len == 0) &[0]Keysym{} else ptr.?[0..@intCast(len)];
+ }
+
+ extern fn xkb_state_key_get_utf8(state: *State, key: Keycode, buffer: [*]u8, size: usize) c_int;
+ pub fn keyGetUtf8(state: *State, key: Keycode, buffer: []u8) usize {
+ return @intCast(xkb_state_key_get_utf8(state, key, buffer.ptr, buffer.len));
+ }
+
+ extern fn xkb_state_key_get_one_sym(state: *State, key: Keycode) Keysym;
+ pub const keyGetOneSym = xkb_state_key_get_one_sym;
+
+ extern fn xkb_state_key_get_layout(state: *State, key: Keycode) LayoutIndex;
+ pub const keyGetLayout = xkb_state_key_get_layout;
+
+ extern fn xkb_state_key_get_level(state: *State, key: Keycode, layout: LayoutIndex) LevelIndex;
+ pub const keyGetLevel = xkb_state_key_get_level;
+
+ extern fn xkb_state_serialize_layout(state: *State, components: Component) LayoutIndex;
+ pub const serializeLayout = xkb_state_serialize_layout;
+
+ extern fn xkb_state_led_name_is_active(state: *State, name: [*:0]const u8) c_int;
+ pub const ledNameIsActive = xkb_state_led_name_is_active;
+};
+
+/// `xkb_keysym_t`: a 32-bit symbol code, not just the named values below —
+/// `keyGetSyms`/`keyGetOneSym` return arbitrary runtime values, hence `_`.
+pub const Keysym = enum(u32) {
+ NoSymbol = 0x000000,
+ _,
+
+ pub const BackSpace: Keysym = @enumFromInt(0xff08);
+ pub const Tab: Keysym = @enumFromInt(0xff09);
+ pub const Return: Keysym = @enumFromInt(0xff0d);
+ pub const Sys_Req: Keysym = @enumFromInt(0xff15);
+ pub const Escape: Keysym = @enumFromInt(0xff1b);
+ pub const Delete: Keysym = @enumFromInt(0xffff);
+ pub const Home: Keysym = @enumFromInt(0xff50);
+ pub const Left: Keysym = @enumFromInt(0xff51);
+ pub const Up: Keysym = @enumFromInt(0xff52);
+ pub const Right: Keysym = @enumFromInt(0xff53);
+ pub const Down: Keysym = @enumFromInt(0xff54);
+ pub const Page_Up: Keysym = @enumFromInt(0xff55);
+ pub const Page_Down: Keysym = @enumFromInt(0xff56);
+ pub const End: Keysym = @enumFromInt(0xff57);
+ pub const Print: Keysym = @enumFromInt(0xff61);
+ pub const Num_Lock: Keysym = @enumFromInt(0xff7f);
+ pub const KP_Enter: Keysym = @enumFromInt(0xff8d);
+ pub const F1: Keysym = @enumFromInt(0xffbe);
+ pub const Shift_L: Keysym = @enumFromInt(0xffe1);
+ pub const Shift_R: Keysym = @enumFromInt(0xffe2);
+ pub const Control_L: Keysym = @enumFromInt(0xffe3);
+ pub const Control_R: Keysym = @enumFromInt(0xffe4);
+ pub const Caps_Lock: Keysym = @enumFromInt(0xffe5);
+ pub const Alt_L: Keysym = @enumFromInt(0xffe9);
+ pub const Alt_R: Keysym = @enumFromInt(0xffea);
+ pub const Super_L: Keysym = @enumFromInt(0xffeb);
+ pub const Super_R: Keysym = @enumFromInt(0xffec);
+ pub const ISO_Left_Tab: Keysym = @enumFromInt(0xfe20);
+ pub const space: Keysym = @enumFromInt(0x0020);
+ pub const plus: Keysym = @enumFromInt(0x002b);
+ pub const minus: Keysym = @enumFromInt(0x002d);
+ pub const equal: Keysym = @enumFromInt(0x003d);
+ pub const A: Keysym = @enumFromInt(0x0041);
+ pub const C: Keysym = @enumFromInt(0x0043);
+ pub const F: Keysym = @enumFromInt(0x0046);
+ pub const T: Keysym = @enumFromInt(0x0054);
+ pub const V: Keysym = @enumFromInt(0x0056);
+ pub const X: Keysym = @enumFromInt(0x0058);
+ pub const underscore: Keysym = @enumFromInt(0x005f);
+ pub const a: Keysym = @enumFromInt(0x0061);
+ pub const c: Keysym = @enumFromInt(0x0063);
+ pub const f: Keysym = @enumFromInt(0x0066);
+ pub const s: Keysym = @enumFromInt(0x0073);
+ pub const t: Keysym = @enumFromInt(0x0074);
+ pub const u: Keysym = @enumFromInt(0x0075);
+ pub const v: Keysym = @enumFromInt(0x0076);
+ pub const x: Keysym = @enumFromInt(0x0078);
+ pub const eacute: Keysym = @enumFromInt(0x00e9);
+ pub const Cyrillic_ie: Keysym = @enumFromInt(0x06c5);
+ pub const XF86Switch_VT_1: Keysym = @enumFromInt(0x1008fe01);
+ pub const XF86Switch_VT_12: Keysym = @enumFromInt(0x1008fe0c);
+
+ pub const Flags = enum(c_int) {
+ no_flags = 0,
+ case_insensitive = 1 << 0,
+ };
+
+ extern fn xkb_keysym_from_name(name: [*:0]const u8, flags: Flags) Keysym;
+ pub const fromName = xkb_keysym_from_name;
+
+ extern fn xkb_utf32_to_keysym(ucs: u32) Keysym;
+ pub const fromUTF32 = xkb_utf32_to_keysym;
+
+ extern fn xkb_keysym_to_lower(ks: Keysym) Keysym;
+ pub const toLower = xkb_keysym_to_lower;
+};