From 5c320c0b8c5620f7927d392f7c36eecaecdaac51 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Mon, 6 Apr 2020 22:23:30 +0200 Subject: [PATCH] Allow switching VTs --- include/render.c | 9 +++++++++ include/render.h | 3 ++- src/keyboard.zig | 35 ++++++++++++++++++++++++++++------- src/server.zig | 4 +++- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/include/render.c b/include/render.c index 10062a6..88a4437 100644 --- a/include/render.c +++ b/include/render.c @@ -1,5 +1,6 @@ #define WLR_USE_UNSTABLE #include +#include #include struct wlr_backend *river_wlr_backend_autocreate(struct wl_display *display) { @@ -13,3 +14,11 @@ struct wlr_renderer *river_wlr_backend_get_renderer(struct wlr_backend *backend) bool river_wlr_backend_start(struct wlr_backend *backend) { return wlr_backend_start(backend); } + +bool river_wlr_backend_is_multi(struct wlr_backend *backend) { + return wlr_backend_is_multi(backend); +} + +struct wlr_session *river_wlr_backend_get_session(struct wlr_backend *backend) { + return wlr_backend_get_session(backend); +} diff --git a/include/render.h b/include/render.h index cfbd1df..d7ba529 100644 --- a/include/render.h +++ b/include/render.h @@ -25,6 +25,7 @@ struct wlr_backend { struct wlr_backend *river_wlr_backend_autocreate(struct wl_display *display); struct wlr_renderer *river_wlr_backend_get_renderer(struct wlr_backend *backend); bool river_wlr_backend_start(struct wlr_backend *backend); - +bool river_wlr_backend_is_multi(struct wlr_backend *backend); +struct wlr_session *river_wlr_backend_get_session(struct wlr_backend *backend); #endif diff --git a/src/keyboard.zig b/src/keyboard.zig index 57f1d37..8be9975 100644 --- a/src/keyboard.zig +++ b/src/keyboard.zig @@ -1,6 +1,7 @@ const std = @import("std"); const c = @import("c.zig"); +const Log = @import("log.zig").Log; const Seat = @import("seat.zig").Seat; pub const Keyboard = struct { @@ -105,14 +106,13 @@ pub const Keyboard = struct { 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) - { - // If mod is held down and this button was _pressed_, we attempt to - // process it as a compositor keybinding. + if (event.state == c.enum_wlr_key_state.WLR_KEY_PRESSED) { var i: usize = 0; while (i < translated_keysyms_len) : (i += 1) { - if (keyboard.seat.server.handleKeybinding(translated_keysyms.?[i], modifiers)) { + if (keyboard.handleBuiltinKeybind(translated_keysyms.?[i])) { + handled = true; + break; + } else if (keyboard.seat.server.handleKeybinding(translated_keysyms.?[i], modifiers)) { handled = true; break; } @@ -120,7 +120,10 @@ pub const Keyboard = struct { if (!handled) { i = 0; while (i < raw_keysyms_len) : (i += 1) { - if (keyboard.seat.server.handleKeybinding(raw_keysyms.?[i], modifiers)) { + if (keyboard.handleBuiltinKeybind(raw_keysyms.?[i])) { + handled = true; + break; + } else if (keyboard.seat.server.handleKeybinding(raw_keysyms.?[i], modifiers)) { handled = true; break; } @@ -140,4 +143,22 @@ pub const Keyboard = struct { ); } } + + /// Handle any builtin, harcoded compsitor bindings such as VT switching. + /// Returns true if the keysym was handled. + fn handleBuiltinKeybind(self: Self, keysym: c.xkb_keysym_t) bool { + if (keysym >= c.XKB_KEY_XF86Switch_VT_1 and keysym <= c.XKB_KEY_XF86Switch_VT_12) { + Log.Debug.log("Switch VT keysym received", .{}); + const wlr_backend = self.seat.server.wlr_backend; + if (c.river_wlr_backend_is_multi(wlr_backend)) { + if (c.river_wlr_backend_get_session(wlr_backend)) |session| { + const vt = keysym - c.XKB_KEY_XF86Switch_VT_1 + 1; + Log.Debug.log("Switching to VT {}", .{vt}); + _ = c.wlr_session_change_vt(session, vt); + } + } + return true; + } + return false; + } }; diff --git a/src/server.zig b/src/server.zig index 469a10a..8a391f7 100644 --- a/src/server.zig +++ b/src/server.zig @@ -115,7 +115,9 @@ pub const Server = struct { /// Handle all compositor keybindings /// Note: this is a hacky initial implementation for testing and will be rewritten eventually pub fn handleKeybinding(self: *Self, sym: c.xkb_keysym_t, modifiers: u32) bool { - // This function assumes the proper modifier is held down. + if (modifiers & @intCast(u32, c.WLR_MODIFIER_LOGO) == 0) { + return false; + } if (modifiers & @intCast(u32, c.WLR_MODIFIER_SHIFT) != 0) { switch (sym) { c.XKB_KEY_H => {