From 6240ec113feab8a5e24810274f4a34d560fecc2f Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Tue, 24 Jul 2018 17:13:36 -0400 Subject: [PATCH] tests: Implement keybindings, keygrabber and fake_inputs --- tests/examples/shims/keygrabber.lua | 19 +++-- tests/examples/shims/root.lua | 114 ++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 5 deletions(-) diff --git a/tests/examples/shims/keygrabber.lua b/tests/examples/shims/keygrabber.lua index c778f4ee..f19d69e5 100644 --- a/tests/examples/shims/keygrabber.lua +++ b/tests/examples/shims/keygrabber.lua @@ -1,11 +1,20 @@ +-- Needed for root.fake_inputs +local keygrabber = {_current_grabber = nil} -local keygrabber = { - run = function() end, - stop = function() end, - is_running = function() return false end, +local function stop() + keygrabber._current_grabber = nil +end + +local function run(grabber) + keygrabber._current_grabber = grabber +end + +keygrabber = { + run = run, + stop = stop, + isrunning = function() return keygrabber._current_grabber ~= nil end, } - return keygrabber -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/tests/examples/shims/root.lua b/tests/examples/shims/root.lua index dbe8754f..bc614187 100644 --- a/tests/examples/shims/root.lua +++ b/tests/examples/shims/root.lua @@ -1,9 +1,123 @@ local root = {_tags={}} +local gtable = require("gears.table") + +local hotkeys = nil + function root:tags() return root._tags end +function root:size() --TODO use the screens + return 0, 0 +end + +function root:size_mm() + return 0, 0 +end + +function root.cursor() end + +-- GLOBAL KEYBINDINGS -- + +local keys = {} + +function root.keys(k) + keys = k or keys + return keys +end + +-- FAKE INPUTS -- + +-- Turn keysym into modkey names +local conversion = { + Super_L = "Mod4", + Control_L = "Control", + Shift_L = "Shift", + Alt_L = "Mod1", + Super_R = "Mod4", + Control_R = "Control", + Shift_R = "Shift", + Alt_R = "Mod1", +} + +-- The currently pressed modkeys. +local mods = {} +local function get_mods() + local ret = {} + + for mod in pairs(mods) do + table.insert(ret, mod) + end + + return ret +end + +local function add_modkey(key) + if not conversion[key] then return end + mods[conversion[key]] = true +end + +local function remove_modkey(key) + if not conversion[key] then return end + mods[conversion[key]] = nil +end + +local function match_modifiers(mods1, mods2) + if #mods1 ~= #mods2 then return false end + + for _, mod1 in ipairs(mods1) do + if not gtable.hasitem(mods2, mod1) then + return false + end + end + + return true +end + +local function execute_keybinding(key, event) + -- It *could* be extracted from gears.object private API, but it's equally + -- ugly as using the list used by the hotkey widget. + if not hotkeys then + hotkeys = require("awful.key").hotkeys + end + + for _, v in ipairs(hotkeys) do + if key == v.key and match_modifiers(v.mod, get_mods()) and v[event] then + v[event]() + return + end + end +end + +local fake_input_handlers = { + key_press = function(key) + add_modkey(key) + if keygrabber._current_grabber then + keygrabber._current_grabber(get_mods(), key, "press") + else + execute_keybinding(key, "press") + end + end, + key_release = function(key) + remove_modkey(key) + if keygrabber._current_grabber then + keygrabber._current_grabber(get_mods(), key, "release") + else + execute_keybinding(key, "release") + end + end, + button_press = function() --[[TODO]] end, + button_release = function() --[[TODO]] end, + motion_notify = function() --[[TODO]] end, +} + +function root.fake_inputs(event_type, detail, x, y) + assert(fake_input_handlers[event_type], "Unknown event_type") + + fake_input_handlers[event_type](detail, x, y) +end + return root -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80