2022-09-07 17:43:25 +00:00
-- This file is part of Reno desktop.
--
-- Reno desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
--
-- Reno desktop is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with Reno desktop. If not, see <https://www.gnu.org/licenses/>.
-- library for constructing bindable keys
local asckey = {
keymap = { }
}
local awful = require ( " awful " )
2023-03-12 21:08:23 +00:00
local gears = require ( " gears " )
2022-09-16 18:42:50 +00:00
asckey.set_keymap = function ( keymap )
for k , v in pairs ( keymap ) do
asckey.keymap [ v ] = k
end
end
2022-09-07 17:43:25 +00:00
asckey.get_keycomb = function ( name )
local modifiers = { }
name = name : gsub ( " [^%s%+]+%+ " , function ( v )
if v == " modkey+ " then v = global.modkey .. " + " end
table.insert ( modifiers , v : sub ( 1 , - 2 ) )
return " "
end )
return modifiers , name
end
2023-05-06 08:56:25 +00:00
asckey.k = function ( name , callback , description , release )
2022-09-07 17:43:25 +00:00
if not asckey.keymap [ name ] then
return { }
end
local modifiers , key = asckey.get_keycomb ( asckey.keymap [ name ] )
2023-05-06 08:56:25 +00:00
if key : match ( " ^Mouse(%d+)$ " ) then
-- After several experiments, the best solution to making the extra mouse buttons bindable
-- was this. The drawback is that all bindings for these are global, and they do not return
-- the button object itself. Crappy hack, but it will suffice for now.
local callback_wrap = function ( ... )
callback ( ... )
-- For some ungodly reason binding to mouse drops all keybindings
gears.timer . delayed_call ( function ( )
mousegrabber.run ( function ( ) return false end , " bogosity " )
mousegrabber.stop ( )
end )
end
local new_button = awful.button ( modifiers , tonumber ( key : match ( " ^Mouse(%d+)$ " ) ) ,
callback_wrap , release )
awful.rules . rules [ 1 ] . properties.buttons = gears.table . join (
awful.rules . rules [ 1 ] . properties.buttons ,
new_button
)
root.buttons ( gears.table . join (
root.buttons ( ) ,
new_button
) )
return { }
else
local callback_wrap = ( description.release_pre and function ( ... )
root.fake_input ( " key_release " , key )
callback ( ... )
end ) or callback
if release then
return awful.key ( modifiers , key ,
callback_wrap ,
description )
else
return awful.key ( modifiers , key ,
callback_wrap ,
release , description )
end
end
2022-09-07 17:43:25 +00:00
end
asckey.custom_binds = function ( )
local custom_keys = { }
2023-03-05 13:26:33 +00:00
for comm , _ in pairs ( asckey.keymap ) do
2022-09-07 17:43:25 +00:00
if not comm : match ( " ^:.* " ) then
table.insert ( custom_keys , asckey.k ( comm , function ( )
awful.spawn ( comm )
end , { description = comm , group = " custom " } ) )
end
end
return custom_keys
end
2023-03-12 21:08:23 +00:00
asckey.context = function ( keys )
local context = { keys = keys , key_lookup = { } , active = false }
for _ , v in pairs ( keys ) do
context.key_lookup [ v ] = true
end
function context : activate ( )
context.active = true
root.keys ( gears.table . join (
root.keys ( ) ,
self.keys
) )
end
function context : deactivate ( )
context.active = false
local root_keys = root.keys ( )
for i = # root_keys , 1 , - 1 do
if self.key_lookup [ root_keys [ i ] ] then
table.remove ( root_keys , i )
end
end
root.keys ( root_keys )
end
return context
end
2022-09-07 17:43:25 +00:00
return asckey