tests: Add keygrabber tests.
This commit is contained in:
parent
e05222088a
commit
a961c5f002
|
@ -0,0 +1,22 @@
|
||||||
|
|
||||||
|
local awful = { keygrabber = require("awful.keygrabber") } --DOC_HIDE
|
||||||
|
|
||||||
|
local allowed_keys_works = false --DOC_HIDE
|
||||||
|
|
||||||
|
awful.keygrabber {
|
||||||
|
autostart = true,
|
||||||
|
allowed_keys = {"a", "w", "e", "s", "o", "m", "e"},
|
||||||
|
stop_callback = function() --DOC_HIDE
|
||||||
|
allowed_keys_works = true --DOC_HIDE
|
||||||
|
end, --DOC_HIDE
|
||||||
|
}
|
||||||
|
|
||||||
|
root.fake_input("key_press" , "w")--DOC_HIDE
|
||||||
|
root.fake_input("key_release", "w")--DOC_HIDE
|
||||||
|
|
||||||
|
assert(not allowed_keys_works) --DOC_HIDE
|
||||||
|
|
||||||
|
root.fake_input("key_press" , "q")--DOC_HIDE
|
||||||
|
root.fake_input("key_release", "q")--DOC_HIDE
|
||||||
|
|
||||||
|
assert(allowed_keys_works) --DOC_HIDE
|
|
@ -0,0 +1,41 @@
|
||||||
|
--DOC_HEADER --DOC_NO_USAGE
|
||||||
|
|
||||||
|
local was_called = {} --DOC_HIDE
|
||||||
|
|
||||||
|
local awful = {keygrabber = require("awful.keygrabber"), --DOC_HIDE
|
||||||
|
client={focus={history={--DOC_HIDE
|
||||||
|
disable_tracking = function() was_called[1] = true end, --DOC_HIDE
|
||||||
|
enable_tracking = function() was_called[2] = true end, --DOC_HIDE
|
||||||
|
select_next = function() was_called[3] = true end, --DOC_HIDE
|
||||||
|
select_previous = function() was_called[4] = true end, --DOC_HIDE
|
||||||
|
}}}}--DOC_HIDE
|
||||||
|
|
||||||
|
awful.keygrabber {
|
||||||
|
keybindings = {
|
||||||
|
{{"Mod1" }, "Tab", awful.client.focus.history.select_previous},
|
||||||
|
{{"Mod1", "Shift"}, "Tab", awful.client.focus.history.select_next },
|
||||||
|
},
|
||||||
|
-- Note that it is using the key name and not the modifier name.
|
||||||
|
stop_key = "Alt_L",
|
||||||
|
release_event = "release",
|
||||||
|
start_callback = awful.client.focus.history.disable_tracking,
|
||||||
|
stop_callback = awful.client.focus.history.enable_tracking,
|
||||||
|
export_keybindings = true,
|
||||||
|
}
|
||||||
|
|
||||||
|
--DOC_HIDE Trigger the keybinging
|
||||||
|
awesome.emit_signal("refresh") --DOC_HIDE `export_keybindings` is async
|
||||||
|
root.fake_input("key_press", "Alt_L")--DOC_HIDE
|
||||||
|
root.fake_input("key_press", "Tab")--DOC_HIDE
|
||||||
|
root.fake_input("key_release", "Tab")--DOC_HIDE
|
||||||
|
root.fake_input("key_release", "Alt_L")--DOC_HIDE
|
||||||
|
assert(was_called[1] and was_called[1] and was_called[2] and was_called[4])--DOC_HIDE
|
||||||
|
assert(not was_called[3]) --DOC_HIDE
|
||||||
|
|
||||||
|
--DOC_HIDE Now make sure it can be triggered again
|
||||||
|
root.fake_input("key_press", "Alt_L")--DOC_HIDE
|
||||||
|
root.fake_input("key_press", "Shift_L")--DOC_HIDE
|
||||||
|
root.fake_input("key_press", "Tab")--DOC_HIDE
|
||||||
|
root.fake_input("key_release", "Tab")--DOC_HIDE
|
||||||
|
|
||||||
|
assert(was_called[3]) --DOC_HIDE
|
|
@ -0,0 +1,26 @@
|
||||||
|
|
||||||
|
local awful = { keygrabber = require("awful.keygrabber") } --DOC_HIDE
|
||||||
|
|
||||||
|
local naughty = { notify = function() end } --DOC_HIDE
|
||||||
|
|
||||||
|
local autostart_works = false --DOC_HIDE
|
||||||
|
|
||||||
|
awful.keygrabber {
|
||||||
|
autostart = true,
|
||||||
|
stop_key = "Return",
|
||||||
|
stop_callback = function(_, _, _, sequence)
|
||||||
|
autostart_works = true --DOC_HIDE
|
||||||
|
assert(sequence == "abc") --DOC_HIDE
|
||||||
|
naughty.notify{text="The keys were:"..sequence}
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v in ipairs {"a", "b", "c"} do--DOC_HIDE
|
||||||
|
root.fake_input("key_press" , v)--DOC_HIDE
|
||||||
|
root.fake_input("key_release", v)--DOC_HIDE
|
||||||
|
end--DOC_HIDE
|
||||||
|
|
||||||
|
root.fake_input("key_press" , "Return")--DOC_HIDE
|
||||||
|
root.fake_input("key_release", "Return")--DOC_HIDE
|
||||||
|
|
||||||
|
assert(autostart_works) --DOC_HIDE
|
|
@ -0,0 +1,20 @@
|
||||||
|
|
||||||
|
local awful = { keygrabber = require("awful.keygrabber") } --DOC_HIDE
|
||||||
|
|
||||||
|
local allowed_keys_works = false --DOC_HIDE
|
||||||
|
|
||||||
|
awful.keygrabber {
|
||||||
|
autostart = true, --DOC_HIDE
|
||||||
|
stop_key = "Return",
|
||||||
|
stop_callback = function() --DOC_HIDE
|
||||||
|
allowed_keys_works = true --DOC_HIDE
|
||||||
|
end, --DOC_HIDE
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(not allowed_keys_works) --DOC_HIDE
|
||||||
|
root.fake_input("key_press" , "a")--DOC_HIDE
|
||||||
|
root.fake_input("key_release", "a")--DOC_HIDE
|
||||||
|
assert(not allowed_keys_works) --DOC_HIDE
|
||||||
|
root.fake_input("key_press" , "Return")--DOC_HIDE
|
||||||
|
root.fake_input("key_release", "Return")--DOC_HIDE
|
||||||
|
assert(allowed_keys_works) --DOC_HIDE
|
|
@ -0,0 +1,20 @@
|
||||||
|
--DOC_NO_USAGE
|
||||||
|
local awful = { keygrabber = require("awful.keygrabber") } --DOC_HIDE
|
||||||
|
|
||||||
|
local allowed_keys_works = false --DOC_HIDE
|
||||||
|
|
||||||
|
awful.keygrabber {
|
||||||
|
autostart = true, --DOC_HIDE
|
||||||
|
stop_key = {"Return", "Escape"},
|
||||||
|
stop_callback = function() --DOC_HIDE
|
||||||
|
allowed_keys_works = true --DOC_HIDE
|
||||||
|
end, --DOC_HIDE
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(not allowed_keys_works) --DOC_HIDE
|
||||||
|
root.fake_input("key_press" , "a")--DOC_HIDE
|
||||||
|
root.fake_input("key_release", "a")--DOC_HIDE
|
||||||
|
assert(not allowed_keys_works) --DOC_HIDE
|
||||||
|
root.fake_input("key_press" , "Escape")--DOC_HIDE
|
||||||
|
root.fake_input("key_release", "Escape")--DOC_HIDE
|
||||||
|
assert(allowed_keys_works) --DOC_HIDE
|
|
@ -0,0 +1,64 @@
|
||||||
|
--DOC_GEN_OUTPUT --DOC_HIDE
|
||||||
|
local awful = { keygrabber = require("awful.keygrabber") } --DOC_HIDE
|
||||||
|
|
||||||
|
local keybinding_works = {} --DOC_HIDE
|
||||||
|
|
||||||
|
local g = --DOC_HIDE
|
||||||
|
awful.keygrabber {
|
||||||
|
mask_modkeys = true,
|
||||||
|
root_keybindings = {
|
||||||
|
{{"Mod4"}, "i", function(self)
|
||||||
|
print("Is now active!", self)
|
||||||
|
keybinding_works[1] = true --DOC_HIDE
|
||||||
|
end},
|
||||||
|
},
|
||||||
|
keybindings = {
|
||||||
|
{{"Mod4", "Shift"}, "i", function(self)
|
||||||
|
print("Called again!")
|
||||||
|
keybinding_works[3] = true --DOC_HIDE
|
||||||
|
self:stop()
|
||||||
|
end},
|
||||||
|
},
|
||||||
|
keypressed_callback = function(_, modifiers, key)
|
||||||
|
print("A key was pressed:", key, "with", #modifiers, "modifier!")
|
||||||
|
keybinding_works[2] = keybinding_works[2] and keybinding_works[2] + 1 or 1 --DOC_HIDE
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
--DOC_NEWLINE
|
||||||
|
-- The following will **NOT** trigger the keygrabbing because it isn't exported
|
||||||
|
-- to the root (global) keys. Adding `export_keybindings` would solve that
|
||||||
|
awesome.emit_signal("refresh") --DOC_HIDE `root_keybindings` is async
|
||||||
|
root._execute_keybinding({"Mod4", "Shift"}, "i")
|
||||||
|
assert(#keybinding_works == 0)
|
||||||
|
|
||||||
|
--DOC_NEWLINE
|
||||||
|
-- But this will start the keygrabber because it is part of the root_keybindings
|
||||||
|
root._execute_keybinding({"Mod4"}, "i")
|
||||||
|
assert(keybinding_works[1]) --DOC_HIDE
|
||||||
|
assert(not keybinding_works[2]) --DOC_HIDE
|
||||||
|
|
||||||
|
--DOC_NEWLINE
|
||||||
|
-- Note that that keygrabber is running, all callbacks should work:
|
||||||
|
root.fake_input("key_press" , "a")
|
||||||
|
root.fake_input("key_release" , "a")
|
||||||
|
assert(keybinding_works[2] == 1) --DOC_HIDE
|
||||||
|
|
||||||
|
--DOC_NEWLINE
|
||||||
|
-- Calling the root keybindings now wont work because they are not part of
|
||||||
|
-- the keygrabber internal (own) keybindings, so `keypressed_callback` will
|
||||||
|
-- be called.
|
||||||
|
root._execute_keybinding({"Mod4"}, "i")
|
||||||
|
assert(keybinding_works[2] == 2) --DOC_HIDE because mask_modkeys is set
|
||||||
|
assert(g == awful.keygrabber.current_instance) --DOC_HIDE
|
||||||
|
assert(not keybinding_works[3])--DOC_HIDE
|
||||||
|
|
||||||
|
|
||||||
|
--DOC_NEWLINE
|
||||||
|
-- Now the keygrabber own keybindings will work
|
||||||
|
root._execute_keybinding({"Mod4", "Shift"}, "i")
|
||||||
|
assert(keybinding_works[3])--DOC_HIDE
|
||||||
|
keybinding_works[2] = 0--DOC_HIDE
|
||||||
|
assert(not awful.keygrabber.current_instance) --DOC_HIDE
|
||||||
|
root.fake_input("key_press" , "a") --DOC_HIDE
|
||||||
|
root.fake_input("key_release" , "a") --DOC_HIDE
|
||||||
|
assert(keybinding_works[2] == 0) --DOC_HIDE
|
|
@ -0,0 +1,4 @@
|
||||||
|
Is now active! nil
|
||||||
|
A key was pressed: a with 0 modifier!
|
||||||
|
A key was pressed: i with 1 modifier!
|
||||||
|
Called again!
|
|
@ -0,0 +1,24 @@
|
||||||
|
|
||||||
|
local awful = { keygrabber = require("awful.keygrabber") } --DOC_HIDE
|
||||||
|
|
||||||
|
local timeout_works = false --DOC_HIDE
|
||||||
|
|
||||||
|
local function print() end --DOC_HIDE be silent
|
||||||
|
|
||||||
|
local g = --DOC_HIDE
|
||||||
|
|
||||||
|
awful.keygrabber {
|
||||||
|
autostart = true,
|
||||||
|
timeout = 1, -- second
|
||||||
|
timeout_callback = function()
|
||||||
|
print("The keygrabber has expired")
|
||||||
|
timeout_works = true --DOC_HIDE
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(g) --DOC_HIDE Returning the object works
|
||||||
|
assert(g._private.timer)--DOC_HIDE The timer has been created
|
||||||
|
assert(g._private.timer.started) --DOC_HIDE The timer is started
|
||||||
|
assert(not timeout_works) --DOC_HIDE The callback isn't called by accident
|
||||||
|
g._private.timer:emit_signal("timeout") --DOC_HIDE
|
||||||
|
assert(timeout_works) --DOC_HIDE The callback has been called
|
|
@ -0,0 +1,49 @@
|
||||||
|
--DOC_HEADER --DOC_NO_USAGE
|
||||||
|
|
||||||
|
local gears = {table = require("gears.table")} --DOC_HIDE
|
||||||
|
|
||||||
|
local awful = { keygrabber = require("awful.keygrabber") } --DOC_HIDE
|
||||||
|
|
||||||
|
local map, actions = {
|
||||||
|
verbs = {
|
||||||
|
m = "move" , f = "focus" , d = "delete" , a = "append",
|
||||||
|
w = "swap" , p = "print" , n = "new" ,
|
||||||
|
},
|
||||||
|
adjectives = { h = "left" , j = "down" , k = "up" , l = "right" , },
|
||||||
|
nouns = { c = "client", t = "tag" , s = "screen", y = "layout", },
|
||||||
|
}, {}
|
||||||
|
--DOC_NEWLINE
|
||||||
|
function actions.client(action, adj) print("IN CLIENT!") end --luacheck: no unused args
|
||||||
|
function actions.tag (action, adj) print("IN TAG!" ) end --luacheck: no unused args
|
||||||
|
function actions.screen(action, adj) print("IN SCREEN!") end --luacheck: no unused args
|
||||||
|
function actions.layout(action, adj) print("IN LAYOUT!") end --luacheck: no unused args
|
||||||
|
--DOC_NEWLINE
|
||||||
|
local function parse(_, stop_key, _, sequence)
|
||||||
|
local parsed, count = { verbs = "", adjectives = "", nouns = "", }, ""
|
||||||
|
sequence = sequence..stop_key
|
||||||
|
--DOC_NEWLINE
|
||||||
|
for i=1, #sequence do
|
||||||
|
local char = sequence:sub(i,i)
|
||||||
|
if char >= "0" and char <= "9" then
|
||||||
|
count = count .. char
|
||||||
|
else
|
||||||
|
for kind in pairs(parsed) do
|
||||||
|
parsed[kind] = map[kind][char] or parsed[kind]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
--DOC_NEWLINE
|
||||||
|
if parsed.nouns == "" then return end
|
||||||
|
|
||||||
|
for _=1, count == "" and 1 or tonumber(count) do
|
||||||
|
actions[parsed.nouns](parsed.verbs, parsed.adjectives)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
--DOC_NEWLINE
|
||||||
|
awful.keygrabber {
|
||||||
|
stop_callback = parse,
|
||||||
|
stop_key = gears.table.keys(map.verbs),
|
||||||
|
root_keybingins = {
|
||||||
|
{{"Mod4"}, "v"}
|
||||||
|
},
|
||||||
|
}
|
Loading…
Reference in New Issue