From 57f27f7cc6a232827a774c3f36035d9290f3b6ce Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Thu, 2 Apr 2020 13:44:24 +0200 Subject: [PATCH] Implement tag assignment There are a few bugs with this, but the core logic is sound --- src/cursor.zig | 12 ++++++------ src/keyboard.zig | 40 +++++++++++++++++++++++++++++++++------- src/output.zig | 14 +++++++------- src/root.zig | 44 +++++++++++++++++++++++++------------------- src/server.zig | 36 ++++++++++++++++++++++++++++++++++++ src/view.zig | 41 ++++++++++++++++++++--------------------- 6 files changed, 127 insertions(+), 60 deletions(-) diff --git a/src/cursor.zig b/src/cursor.zig index 37c2b07..e867a17 100644 --- a/src/cursor.zig +++ b/src/cursor.zig @@ -98,8 +98,8 @@ pub const Cursor = struct { // Move the grabbed view to the new position. // TODO: log on null if (self.grabbed_view) |view| { - view.current_state.x = @floatToInt(c_int, self.wlr_cursor.x - self.grab_x); - view.current_state.y = @floatToInt(c_int, self.wlr_cursor.y - self.grab_y); + view.current_box.x = @floatToInt(c_int, self.wlr_cursor.x - self.grab_x); + view.current_box.y = @floatToInt(c_int, self.wlr_cursor.y - self.grab_y); } } @@ -119,8 +119,8 @@ pub const Cursor = struct { const dx: f64 = self.wlr_cursor.x - self.grab_x; const dy: f64 = self.wlr_cursor.y - self.grab_y; - var x: f64 = @intToFloat(f64, view.current_state.x); - var y: f64 = @intToFloat(f64, view.current_state.y); + var x: f64 = @intToFloat(f64, view.current_box.x); + var y: f64 = @intToFloat(f64, view.current_box.y); var width = @intToFloat(f64, self.grab_width); var height = @intToFloat(f64, self.grab_height); @@ -143,8 +143,8 @@ pub const Cursor = struct { } else if (self.resize_edges & @intCast(u32, c.WLR_EDGE_RIGHT) != 0) { width += dx; } - view.current_state.x = @floatToInt(c_int, x); - view.current_state.y = @floatToInt(c_int, y); + view.current_box.x = @floatToInt(c_int, x); + view.current_box.y = @floatToInt(c_int, y); _ = c.wlr_xdg_toplevel_set_size( view.wlr_xdg_surface, @floatToInt(u32, width), diff --git a/src/keyboard.zig b/src/keyboard.zig index 6e4138c..57f1d37 100644 --- a/src/keyboard.zig +++ b/src/keyboard.zig @@ -81,11 +81,29 @@ pub const Keyboard = struct { // Translate libinput keycode -> xkbcommon const keycode = event.keycode + 8; - // Get a list of keysyms based on the keymap for this keyboard - var syms: ?[*]c.xkb_keysym_t = undefined; - const nsyms = c.xkb_state_key_get_syms(wlr_keyboard.xkb_state, keycode, &syms); + + // Get a list of keysyms as xkb reports them + var translated_keysyms: ?[*]c.xkb_keysym_t = undefined; + const translated_keysyms_len = c.xkb_state_key_get_syms( + wlr_keyboard.xkb_state, + keycode, + &translated_keysyms, + ); + + // Get a list of keysyms ignoring modifiers (e.g. 1 instead of !) + // Important for bindings like Mod+Shift+1 + var raw_keysyms: ?[*]c.xkb_keysym_t = undefined; + const layout_index = c.xkb_state_key_get_layout(wlr_keyboard.xkb_state, keycode); + const raw_keysyms_len = c.xkb_keymap_key_get_syms_by_level( + wlr_keyboard.keymap, + keycode, + layout_index, + 0, + &raw_keysyms, + ); var handled = false; + // TODO: These modifiers aren't properly handled, see sway's code const modifiers = c.wlr_keyboard_get_modifiers(wlr_keyboard); if (modifiers & @intCast(u32, c.WLR_MODIFIER_LOGO) != 0 and event.state == c.enum_wlr_key_state.WLR_KEY_PRESSED) @@ -93,12 +111,20 @@ pub const Keyboard = struct { // If mod is held down and this button was _pressed_, we attempt to // process it as a compositor keybinding. var i: usize = 0; - while (i < nsyms) { - handled = keyboard.seat.server.handleKeybinding(syms.?[i], modifiers); - if (handled) { + while (i < translated_keysyms_len) : (i += 1) { + if (keyboard.seat.server.handleKeybinding(translated_keysyms.?[i], modifiers)) { + handled = true; break; } - i += 1; + } + if (!handled) { + i = 0; + while (i < raw_keysyms_len) : (i += 1) { + if (keyboard.seat.server.handleKeybinding(raw_keysyms.?[i], modifiers)) { + handled = true; + break; + } + } } } diff --git a/src/output.zig b/src/output.zig index 147e890..d3b3ffe 100644 --- a/src/output.zig +++ b/src/output.zig @@ -86,7 +86,7 @@ pub const Output = struct { const view = &node.data; // Only render currently visible views - if (!view.isVisible(output.root.current_focused_tags)) { + if (view.current_tags & output.root.current_focused_tags == 0) { continue; } @@ -118,10 +118,10 @@ pub const Output = struct { // and need to render that buffer until the transaction is complete. if (view.stashed_buffer) |buffer| { var box = c.wlr_box{ - .x = view.current_state.x, - .y = view.current_state.y, - .width = @intCast(c_int, view.current_state.width), - .height = @intCast(c_int, view.current_state.height), + .x = view.current_box.x, + .y = view.current_box.y, + .width = @intCast(c_int, view.current_box.width), + .height = @intCast(c_int, view.current_box.height), }; // Scale the box to the output's current scaling factor @@ -185,8 +185,8 @@ pub const Output = struct { var ox: f64 = 0.0; var oy: f64 = 0.0; c.wlr_output_layout_output_coords(view.root.wlr_output_layout, output, &ox, &oy); - ox += @intToFloat(f64, view.current_state.x + sx); - oy += @intToFloat(f64, view.current_state.y + sy); + ox += @intToFloat(f64, view.current_box.x + sx); + oy += @intToFloat(f64, view.current_box.y + sy); var box = c.wlr_box{ .x = @floatToInt(c_int, ox), diff --git a/src/root.zig b/src/root.zig index bddee87..34cd43d 100644 --- a/src/root.zig +++ b/src/root.zig @@ -105,7 +105,7 @@ pub const Root = struct { var it = @fieldParentPtr(std.TailQueue(View).Node, "data", current_focus).next; while (it) |node| : (it = node.next) { const view = &node.data; - if (view.isVisible(self.current_focused_tags)) { + if (view.current_tags & self.current_focused_tags != 0) { view.focus(view.wlr_xdg_surface.surface); return; } @@ -117,7 +117,7 @@ pub const Root = struct { var it = self.views.first; while (it) |node| : (it = node.next) { const view = &node.data; - if (view.isVisible(self.current_focused_tags)) { + if (view.current_tags & self.current_focused_tags != 0) { view.focus(view.wlr_xdg_surface.surface); return; } @@ -132,7 +132,7 @@ pub const Root = struct { var it = @fieldParentPtr(std.TailQueue(View).Node, "data", current_focus).prev; while (it) |node| : (it = node.prev) { const view = &node.data; - if (view.isVisible(self.current_focused_tags)) { + if (view.current_tags & self.current_focused_tags != 0) { view.focus(view.wlr_xdg_surface.surface); return; } @@ -144,7 +144,7 @@ pub const Root = struct { var it = self.views.last; while (it) |node| : (it = node.prev) { const view = &node.data; - if (view.isVisible(self.current_focused_tags)) { + if (view.current_tags & self.current_focused_tags != 0) { view.focus(view.wlr_xdg_surface.surface); return; } @@ -152,12 +152,12 @@ pub const Root = struct { } // TODO: obsolete this function by using a better data structure - pub fn visibleCount(self: Self, tags: u32) u32 { + fn visibleCount(self: Self, tags: u32) u32 { var count: u32 = 0; var it = self.views.first; while (it) |node| : (it = node.next) { const view = &node.data; - if (view.isVisible(tags)) { + if (view.current_tags & tags != 0) { count += 1; } } @@ -165,12 +165,12 @@ pub const Root = struct { } pub fn arrange(self: *Self) void { - const tags = if (self.pending_focused_tags) |tags| + const root_tags = if (self.pending_focused_tags) |tags| tags else self.current_focused_tags; - const visible_count = self.visibleCount(tags); + const visible_count = self.visibleCount(root_tags); const master_count = util.min(u32, self.master_count, visible_count); const slave_count = if (master_count >= visible_count) 0 else visible_count - master_count; @@ -196,7 +196,11 @@ pub const Root = struct { while (it) |node| : (it = node.next) { const view = &node.data; - if (!view.isVisible(tags)) { + if (view.pending_tags) |tags| { + if (root_tags & tags == 0) { + continue; + } + } else if (view.current_tags & root_tags == 0) { continue; } @@ -205,22 +209,20 @@ pub const Root = struct { const master_height = @divTrunc(@intCast(u32, output_box.height), master_count); const master_height_rem = @intCast(u32, output_box.height) % master_count; - view.pending_state = View.State{ + view.pending_box = View.Box{ .x = 0, .y = @intCast(i32, i * master_height + if (i > 0) master_height_rem else 0), .width = master_column_width, .height = master_height + if (i == 0) master_height_rem else 0, - - .tags = view.current_state.tags, }; } else { // Add the remainder to the first slave to ensure every pixel of height is used const slave_height = @divTrunc(@intCast(u32, output_box.height), slave_count); const slave_height_rem = @intCast(u32, output_box.height) % slave_count; - view.pending_state = View.State{ + view.pending_box = View.Box{ .x = @intCast(i32, master_column_width), .y = @intCast(i32, (i - master_count) * slave_height + if (i > master_count) slave_height_rem else 0), @@ -228,8 +230,6 @@ pub const Root = struct { .width = slave_column_width, .height = slave_height + if (i == master_count) slave_height_rem else 0, - - .tags = view.current_state.tags, }; } @@ -314,7 +314,7 @@ pub const Root = struct { // If there were pending focused tags, make them the current focus if (self.pending_focused_tags) |tags| { - Log.Debug.log("changing current focus: {b:0>10} to {b:0>10}\n", .{ self.current_focused_tags, tags }); + Log.Debug.log("changing current focus: {b:0>10} to {b:0>10}", .{ self.current_focused_tags, tags }); self.current_focused_tags = tags; self.pending_focused_tags = null; } @@ -325,10 +325,16 @@ pub const Root = struct { // Ensure that all pending state is cleared view.pending_serial = null; - if (view.pending_state) |state| { - view.current_state = state; - view.pending_state = null; + if (view.pending_box) |state| { + view.current_box = state; + view.pending_box = null; } + + if (view.pending_tags) |tags| { + view.current_tags = tags; + view.pending_tags = null; + } + view.dropStashedBuffer(); } } diff --git a/src/server.zig b/src/server.zig index de202fd..3b6a9bc 100644 --- a/src/server.zig +++ b/src/server.zig @@ -136,6 +136,42 @@ pub const Server = struct { const child = std.ChildProcess.init(&argv, std.heap.c_allocator) catch unreachable; std.ChildProcess.spawn(child) catch unreachable; }, + c.XKB_KEY_1 => { + if (self.root.focused_view) |view| { + view.pending_tags = 1 << 0; + self.root.arrange(); + } + }, + c.XKB_KEY_2 => { + if (self.root.focused_view) |view| { + view.pending_tags = 1 << 1; + self.root.arrange(); + } + }, + c.XKB_KEY_3 => { + if (self.root.focused_view) |view| { + view.pending_tags = 1 << 2; + self.root.arrange(); + } + }, + c.XKB_KEY_4 => { + if (self.root.focused_view) |view| { + view.pending_tags = 1 << 3; + self.root.arrange(); + } + }, + c.XKB_KEY_5 => { + if (self.root.focused_view) |view| { + view.pending_tags = 1 << 4; + self.root.arrange(); + } + }, + c.XKB_KEY_6 => { + if (self.root.focused_view) |view| { + view.pending_tags = 1 << 5; + self.root.arrange(); + } + }, else => return false, } } else { diff --git a/src/view.zig b/src/view.zig index 56ee513..8344a8b 100644 --- a/src/view.zig +++ b/src/view.zig @@ -11,16 +11,18 @@ pub const View = struct { mapped: bool, - pub const State = struct { + pub const Box = struct { x: i32, y: i32, width: u32, height: u32, - tags: u32, }; - current_state: State, - pending_state: ?State, + current_box: Box, + pending_box: ?Box, + + current_tags: u32, + pending_tags: ?u32, pending_serial: ?u32, @@ -45,14 +47,16 @@ pub const View = struct { self.mapped = false; - self.current_state = State{ + self.current_box = Box{ .x = 0, .y = 0, .height = 0, .width = 0, - .tags = tags, }; - self.pending_state = null; + self.pending_box = null; + + self.current_tags = tags; + self.pending_tags = null; self.pending_serial = null; @@ -76,20 +80,20 @@ pub const View = struct { } pub fn needsConfigure(self: Self) bool { - if (self.pending_state) |pending_state| { - return pending_state.width != self.current_state.width or - pending_state.height != self.current_state.height; + if (self.pending_box) |pending_box| { + return pending_box.width != self.current_box.width or + pending_box.height != self.current_box.height; } else { return false; } } pub fn configurePending(self: *Self) void { - if (self.pending_state) |pending_state| { + if (self.pending_box) |pending_box| { self.pending_serial = c.wlr_xdg_toplevel_set_size( self.wlr_xdg_surface, - pending_state.width, - pending_state.height, + pending_box.width, + pending_box.height, ); } else { // TODO: log warning @@ -133,11 +137,6 @@ pub const View = struct { view.root.arrange(); } - /// Returns true if the view is shown given the current state of tags - pub fn isVisible(self: Self, tags: u32) bool { - return tags & self.current_state.tags != 0; - } - fn handleUnmap(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { const view = @fieldParentPtr(View, "listen_unmap", listener.?); const root = view.root; @@ -236,11 +235,11 @@ pub const View = struct { // coordinates lx and ly (in output Layout Coordinates). If so, it sets the // surface pointer to that wlr_surface and the sx and sy coordinates to the // coordinates relative to that surface's top-left corner. - const view_sx = lx - @intToFloat(f64, self.current_state.x); - const view_sy = ly - @intToFloat(f64, self.current_state.y); + const view_sx = lx - @intToFloat(f64, self.current_box.x); + const view_sy = ly - @intToFloat(f64, self.current_box.y); // This variable seems to have been unsued in TinyWL - // struct wlr_surface_state *state = &view->xdg_surface->surface->current; + // struct wlr_surface_box *state = &view->xdg_surface->surface->current; var _sx: f64 = undefined; var _sy: f64 = undefined;