Skip to content

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

WIP: diagnose keyboard layout group resets

Mel88083190f2d1
diff --git a/src/Keyboard.zig b/src/Keyboard.zig
index c3bf976..627e581 100644
--- a/src/Keyboard.zig
+++ b/src/Keyboard.zig
@@ -103,6 +103,18 @@ fn handleModifiers(listener: *wl.Listener(*wlr.Keyboard), wlr_keyboard: *wlr.Key
_ = keyboard.server.world.peekTarget();
keyboard.server.scheduleFrames();
const mods = wlr_keyboard.getModifiers();
+ if (keyboard.server.input_sources) |sources| {
+ const state = wlr_keyboard.xkb_state.?;
+ log.info(
+ "layout state: effective={d} locked={d} wlr={d} manager={d}",
+ .{
+ state.serializeLayout(@enumFromInt(xkb.State.Component.layout_effective)),
+ state.serializeLayout(@enumFromInt(xkb.State.Component.layout_locked)),
+ wlr_keyboard.modifiers.group,
+ sources.active_group,
+ },
+ );
+ }
if (keyboard.server.locker == null) {
if (keyboard.server.input.open_control_center) |cc| {
if (cc.hasKeyboardFocus() and cc.page == .shortcuts and cc.shortcuts.editing != null and keyboard.server.input.open_start_menu == null) {
diff --git a/src/control_center/sections/input_sources.zig b/src/control_center/sections/input_sources.zig
index e2effa9..90e76b4 100644
--- a/src/control_center/sections/input_sources.zig
+++ b/src/control_center/sections/input_sources.zig
@@ -11,6 +11,7 @@ const W = ui.layout.Widget;
const config = @import("config");
const config_sources = config.input_sources;
const taskbar_items = config.taskbar_items;
+const fold = ui.fold;
const panel = @import("../panel.zig");
const gpa = @import("../../main.zig").gpa;
@@ -60,7 +61,7 @@ pub const Section = struct {
search_focus: bool = false,
results: []Result = &.{},
language_keys: []const []const u8 = &.{},
- selected_language: [16]u8 = @splat(0),
+ selected_language: [128]u8 = @splat(0),
selected_language_len: usize = 0,
message: []const u8 = "",
@@ -188,17 +189,15 @@ fn contentWidth(cc: *panel.ControlCenter) f32 {
}
fn picker(s: *Section, cc: *panel.ControlCenter, manager: anytype, a: std.mem.Allocator, saved: ?ui.layout.TextInputData, layouts: []const config_sources.Layout) !W {
- const field = try a.create(W);
- field.* = .{
+ const field: W = .{
.name = "input_source_search",
.kind = .{ .text_input = saved orelse .{ .field = .{ .leading_icon = .search }, .placeholder = tr("Search layouts or languages…"), .value = try gpa.dupe(u8, ""), .owner = cc, .on_change = searchChanged } },
.height = .{ .fixed = 36 },
.width = .{ .percent = 1 },
};
- s.search = field;
const query = std.mem.trim(u8, field.kind.text_input.value, " ");
var children: std.ArrayList(W) = .empty;
- try children.append(a, field.*);
+ try children.append(a, field);
var groups: std.ArrayList(LanguageGroup) = .empty;
var results: std.ArrayList(Result) = .empty;
var rows: std.ArrayList(W) = .empty;
@@ -210,7 +209,7 @@ fn picker(s: *Section, cc: *panel.ControlCenter, manager: anytype, a: std.mem.Al
var id_buf: [160]u8 = undefined;
const spelled = (config_sources.Layout{ .layout = entry.layout, .variant = entry.variant }).format(&id_buf);
if (!matches(query, &.{ entry.description, entry.layout, entry.variant, entry.brief, entry.languages, spelled })) continue;
- try appendLanguageGroup(a, &groups, if (entry.brief.len > 0) entry.brief else entry.layout, languageName(entry.description));
+ try appendLanguageGroup(a, &groups, languageName(entry.description), languageName(entry.description));
},
.unavailable => for (fallback) |entry| {
if (configured(layouts, entry.layout, entry.variant)) continue;
@@ -224,7 +223,7 @@ fn picker(s: *Section, cc: *panel.ControlCenter, manager: anytype, a: std.mem.Al
for (groups.items, 0..) |group, i| {
keys[i] = group.key;
labels[i] = group.label;
- if (s.selected_language_len > 0 and std.mem.eql(u8, group.key, s.selected_language[0..s.selected_language_len])) selected = i;
+ if (s.selected_language_len > 0 and fold.eql(group.key, s.selected_language[0..s.selected_language_len], .{})) selected = i;
}
if (selected == null and groups.items.len > 0) selected = 0;
s.language_keys = keys;
@@ -252,8 +251,8 @@ fn picker(s: *Section, cc: *panel.ControlCenter, manager: anytype, a: std.mem.Al
.loading => {},
.ready => for (manager.catalog.?.entries) |entry| {
if (configured(layouts, entry.layout, entry.variant)) continue;
- const key = if (entry.brief.len > 0) entry.brief else entry.layout;
- if (!std.mem.eql(u8, key, s.selected_language[0..s.selected_language_len])) continue;
+ const key = languageName(entry.description);
+ if (!fold.eql(key, s.selected_language[0..s.selected_language_len], .{})) continue;
var id_buf: [160]u8 = undefined;
const spelled = (config_sources.Layout{ .layout = entry.layout, .variant = entry.variant }).format(&id_buf);
if (!matches(query, &.{ entry.description, entry.layout, entry.variant, entry.brief, entry.languages, spelled })) continue;
@@ -264,7 +263,7 @@ fn picker(s: *Section, cc: *panel.ControlCenter, manager: anytype, a: std.mem.Al
},
.unavailable => for (fallback) |entry| {
if (configured(layouts, entry.layout, entry.variant)) continue;
- if (!std.mem.eql(u8, languageName(entry.name), s.selected_language[0..s.selected_language_len])) continue;
+ if (!fold.eql(languageName(entry.name), s.selected_language[0..s.selected_language_len], .{})) continue;
if (!matches(query, &.{ entry.name, entry.layout, entry.variant })) continue;
total += 1;
var id_buf: [160]u8 = undefined;
@@ -283,12 +282,14 @@ fn picker(s: *Section, cc: *panel.ControlCenter, manager: anytype, a: std.mem.Al
try children.append(a, try column(a, rows.items, 2));
if (total > rows.items.len) try children.append(a, text(try i18n.allocPrint(a, tr("{shown} of {total} layouts; type to narrow the list."), .{ .shown = rows.items.len, .total = total }), 11, true));
}
- return column(a, children.items, 8);
+ var result = try column(a, children.items, 8);
+ s.search = &result.children[0];
+ return result;
}
fn appendLanguageGroup(a: std.mem.Allocator, groups: *std.ArrayList(LanguageGroup), key: []const u8, label: []const u8) !void {
for (groups.items) |group| {
- if (std.mem.eql(u8, group.key, key)) return;
+ if (fold.eql(group.key, key, .{})) return;
}
try groups.append(a, .{ .key = key, .label = label });
}
@@ -352,6 +353,16 @@ fn matches(query: []const u8, fields: []const []const u8) bool {
return true;
}
+test "language groups merge identical visible language names" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ var groups: std.ArrayList(LanguageGroup) = .empty;
+ try appendLanguageGroup(arena.allocator(), &groups, "English", "English");
+ try appendLanguageGroup(arena.allocator(), &groups, "english", "english");
+ try appendLanguageGroup(arena.allocator(), &groups, "Croatian", "Croatian");
+ try std.testing.expectEqual(@as(usize, 2), groups.items.len);
+}
+
fn containsIgnoreCase(haystack: []const u8, needle: []const u8) bool {
if (needle.len > haystack.len) return false;
for (0..haystack.len - needle.len + 1) |i| {
diff --git a/src/control_center/tree.zig b/src/control_center/tree.zig
index a342fc3..ba95fce 100644
--- a/src/control_center/tree.zig
+++ b/src/control_center/tree.zig
@@ -279,13 +279,15 @@ pub fn buildTree(cc: *ControlCenter) void {
if (!cc.search.empty() and cc.page == .services and cc.services.search_focus) {
if (cc.services.search) |field| ui.input.current.focus(&cc.root, field);
}
- if (!cc.search.empty() and cc.page == .region and cc.input_sources.search_focus) {
+ if (cc.page == .region and cc.input_sources.search_focus) {
if (cc.input_sources.search) |field| ui.input.current.focus(&cc.root, field);
}
if (!cc.search.empty() and cc.page == .users) {
if (users_section.initialFormFocus(&cc.users)) |field| ui.input.current.focus(&cc.root, field);
}
- if (cc.search.focused or cc.search.request_focus) {
+ if (cc.page == .region and cc.input_sources.picker_open and cc.input_sources.search_focus) {
+ // The Region & Language picker owns focus while it is open.
+ } else if (cc.search.focused or cc.search.request_focus) {
if (cc.search.initialized) ui.input.current.focus(&cc.root, &cc.search.field);
cc.search.request_focus = false;
}
diff --git a/src/input/layouts.zig b/src/input/layouts.zig
index 1619e0f..22a98f0 100644
--- a/src/input/layouts.zig
+++ b/src/input/layouts.zig
@@ -70,9 +70,5 @@ pub fn switchLayout(server: *Server, target: protocol.LayoutTarget) !void {
.index => |value| if (value < count) value else return error.InvalidLayoutIndex,
};
if (next == idx) return;
- // wlroots applies this through XKB and emits modifiers, preserving held,
- // latched and locked modifiers and normal client/IME routing.
- var mods = kb.modifiers;
- mods.group = next;
- kb.notifyModifiers(mods);
+ kb.setLayoutGroup(next);
}
diff --git a/src/input_sources.zig b/src/input_sources.zig
index efc660e..aba491d 100644
--- a/src/input_sources.zig
+++ b/src/input_sources.zig
@@ -339,9 +339,7 @@ pub const Manager = struct {
if (!self.follows(keyboard)) return;
const kb = keyboard.device.toKeyboard();
if (kb.modifiers.group == self.active_group) return;
- var mods = kb.modifiers;
- mods.group = self.active_group;
- kb.notifyModifiers(mods);
+ kb.setLayoutGroup(self.active_group);
}
pub fn scheduleSync(self: *Manager) void {
diff --git a/src/ui/input.zig b/src/ui/input.zig
index d9ecce2..61ab854 100644
--- a/src/ui/input.zig
+++ b/src/ui/input.zig
@@ -453,6 +453,12 @@ pub const Caret = input_caret.Caret;
/// it at paint time, so there is nothing to thread through the tree.
pub var caret_config: CaretConfig = .{};
+const TypeSelect = struct {
+ buffer: [64]u8 = @splat(0),
+ len: usize = 0,
+ last_ms: i64 = 0,
+};
+
pub const Dispatcher = struct {
surface: interaction_surface.Surface = .{},
hovered: ?*Widget = null,
@@ -470,6 +476,8 @@ pub const Dispatcher = struct {
/// rebuilds its tree (and so resets its dispatcher) on every keystroke,
/// which is precisely when a caret must *not* forget where it was.
caret: Caret = .{},
+ /// Dropdown type-ahead survives page rebuilds without retaining tree data.
+ type_select: TypeSelect = .{},
/// Text input the pointer is currently sweeping a selection across.
text_drag: ?*Widget = null,
/// Multi-click run: a second press close enough in time and space to the
@@ -482,12 +490,12 @@ pub const Dispatcher = struct {
/// Drop every pointer into a widget tree. Safe after that tree has
/// been freed — unlike `pointerMotion`, this does not dereference.
pub fn reset(self: *Dispatcher) void {
- self.* = .{ .caret = self.caret };
+ self.* = .{ .caret = self.caret, .type_select = self.type_select };
}
pub fn bindTree(self: *Dispatcher, root: *Widget) void {
if (self.tree == root) return;
- self.* = .{ .tree = root, .caret = self.caret };
+ self.* = .{ .tree = root, .caret = self.caret, .type_select = self.type_select };
}
/// The pointer is over something else now, so no scrollbar thumb of `root`
diff --git a/src/ui/input_keys.zig b/src/ui/input_keys.zig
index 45a6316..e954b89 100644
--- a/src/ui/input_keys.zig
+++ b/src/ui/input_keys.zig
@@ -3,6 +3,8 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const anim = @import("anim.zig");
+const fold = @import("fold.zig");
+const Widget = @import("layout.zig").Widget;
const text_input_widget = @import("widgets/text_input.zig");
const select_widget = @import("widgets/select.zig");
const input_mod = @import("input.zig");
@@ -11,6 +13,75 @@ const Key = input_mod.Key;
const Mods = input_mod.Mods;
const KeyOutcome = input_mod.KeyOutcome;
+const type_select_timeout_ms: i64 = 1000;
+
+fn sameFolded(a: []const u8, b: []const u8) bool {
+ return fold.eql(a, b, .{ .turkish = @import("i18n").isTurkish() });
+}
+
+fn typeSelect(self: *Dispatcher, widget: *Widget, chars: []const u8) void {
+ const state = &self.type_select;
+ const now = anim.nowMs();
+ const options: fold.Options = .{ .turkish = @import("i18n").isTurkish() };
+ const expired = state.len == 0 or now - state.last_ms > type_select_timeout_ms;
+ const data = widget.kind.select;
+
+ // Repeating one character cycles through choices with that prefix.
+ if (!expired and state.len == chars.len and sameFolded(state.buffer[0..state.len], chars)) {
+ state.last_ms = now;
+ for (0..data.labels.len) |offset| {
+ const index = (data.highlighted + 1 + offset) % data.labels.len;
+ if (std.mem.indexOfScalar(usize, data.disabled_options, index) != null) continue;
+ if (fold.startsWith(data.labels[index], chars, options)) {
+ select_widget.highlight(widget, index);
+ return;
+ }
+ }
+ return;
+ }
+
+ if (expired or chars.len > state.buffer.len - state.len) state.len = 0;
+ if (chars.len > state.buffer.len) return;
+ @memcpy(state.buffer[state.len..][0..chars.len], chars);
+ state.len += chars.len;
+ state.last_ms = now;
+
+ const prefix = state.buffer[0..state.len];
+ var found: ?usize = null;
+ for (data.labels, 0..) |label, index| {
+ if (std.mem.indexOfScalar(usize, data.disabled_options, index) != null) continue;
+ if (fold.startsWith(label, prefix, options)) {
+ found = index;
+ break;
+ }
+ }
+
+ // If the accumulated prefix has no match, treat this key as a fresh query.
+ if (found == null and state.len > chars.len) {
+ @memcpy(state.buffer[0..chars.len], chars);
+ state.len = chars.len;
+ for (data.labels, 0..) |label, index| {
+ if (std.mem.indexOfScalar(usize, data.disabled_options, index) != null) continue;
+ if (fold.startsWith(label, chars, options)) {
+ found = index;
+ break;
+ }
+ }
+ }
+ if (found) |index| select_widget.highlight(widget, index);
+}
+
+test "dropdown type-ahead accumulates multiple characters" {
+ var dispatcher: Dispatcher = .{};
+ var widget: Widget = .{ .kind = .{ .select = .{
+ .labels = &.{ "Russian", "Croatian", "Czech" },
+ } } };
+ typeSelect(&dispatcher, &widget, "C");
+ typeSelect(&dispatcher, &widget, "R");
+ typeSelect(&dispatcher, &widget, "O");
+ try std.testing.expectEqual(@as(usize, 1), widget.kind.select.highlighted);
+}
+
pub fn keyEvent(self: *Dispatcher, allocator: Allocator, key: Key, mods: Mods) !KeyOutcome {
const widget = self.focused orelse return .{};
if (widget.kind == .select) {
@@ -27,14 +98,7 @@ pub fn keyEvent(self: *Dispatcher, allocator: Allocator, key: Key, mods: Mods) !
if (widget.kind.select.open) select_widget.commit(widget, widget.kind.select.highlighted) else select_widget.open(widget);
} else if (chars.len > 0) {
if (!widget.kind.select.open) select_widget.open(widget);
- const d = widget.kind.select;
- for (0..d.labels.len) |offset| {
- const i = (d.highlighted + 1 + offset) % d.labels.len;
- if (std.ascii.startsWithIgnoreCase(d.labels[i], chars)) {
- select_widget.highlight(widget, i);
- break;
- }
- }
+ typeSelect(self, widget, chars);
}
},
.up, .down, .home, .end, .page_up, .page_down => {
diff --git a/src/wlr/keyboard.zig b/src/wlr/keyboard.zig
index 9989966..e61b453 100644
--- a/src/wlr/keyboard.zig
+++ b/src/wlr/keyboard.zig
@@ -145,4 +145,27 @@ pub const Keyboard = extern struct {
modifiers.group,
);
}
+
+ /// Change the locked XKB layout without round-tripping server state
+ /// through xkb_state_update_mask(). Keep wlroots' cached group in sync
+ /// and emit its normal modifier signal for seat/client notification.
+ pub fn setLayoutGroup(keyboard: *Keyboard, group: xkb.LayoutIndex) void {
+ const state = keyboard.xkb_state orelse return;
+ _ = xkb.State.updateLatchedLocked(
+ state,
+ 0,
+ 0,
+ false,
+ 0,
+ 0,
+ 0,
+ true,
+ @intCast(group),
+ );
+
+ const effective = state.serializeLayout(@enumFromInt(xkb.State.Component.layout_effective));
+ if (keyboard.modifiers.group == effective) return;
+ keyboard.modifiers.group = effective;
+ keyboard.events.modifiers.emit(keyboard);
+ }
};
diff --git a/src/wlr/xkbcommon.zig b/src/wlr/xkbcommon.zig
index 042e9fc..0c84f8f 100644
--- a/src/wlr/xkbcommon.zig
+++ b/src/wlr/xkbcommon.zig
@@ -88,6 +88,7 @@ pub const Keymap = opaque {
pub const State = opaque {
pub const Component = enum(c_int) {
_,
+ pub const layout_locked = 1 << 6;
pub const layout_effective = 1 << 7;
};
@@ -111,6 +112,19 @@ pub const State = opaque {
) Component;
pub const updateMask = xkb_state_update_mask;
+ extern fn xkb_state_update_latched_locked(
+ state: *State,
+ affect_latched_mods: ModMask,
+ latched_mods: ModMask,
+ affect_latched_layout: bool,
+ latched_layout: i32,
+ affect_locked_mods: ModMask,
+ locked_mods: ModMask,
+ affect_locked_layout: bool,
+ locked_layout: i32,
+ ) Component;
+ pub const updateLatchedLocked = xkb_state_update_latched_locked;
+
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;