2023-05-27 14:57:46 +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/>.
2023-05-06 08:56:25 +00:00
local awful = require ( " awful " )
local gears = require ( " gears " )
local ask = require ( " asckey " )
local fs = gears.filesystem
_G.MacroTimer = { }
_G.MacroActive = { }
local mconf = config.macros
local macro_loop = false
local macro_delay = mconf.step or 10
local macro_base_dir = mconf.base_dir or ( root_path .. " /macros/ " )
if not fs.dir_readable ( root_path .. " /macros/ " ) then
fs.make_directories ( root_path .. " /macros/ " )
end
awesome.connect_signal ( " macro::record " , function ( filename )
if not filename then return end
local timeline = " loop= " .. tostring ( macro_loop ) .. " \n " ..
" delay= " .. tostring ( macro_delay ) .. " \n "
require ( " naughty " ) . notify ( { title =
" Recording macro " ..
tostring ( filename ) ..
" (Escape to stop) "
} )
awful.keygrabber {
autostart = true ,
stop_key = " Escape " ,
keypressed_callback = function ( self , mod , key , event )
if key == " " then
key = " space "
end
timeline = timeline .. " + " .. key .. " \n "
end ,
keyreleased_callback = function ( self , mod , key , event )
if key == " " then
key = " space "
end
timeline = timeline .. " - " .. key .. " \n "
end ,
stop_callback = function ( )
local macrofile = io.open ( filename , " w " )
if macrofile then
macrofile : write ( timeline )
macrofile : close ( )
require ( " naughty " ) . notify ( { title = " Macro saved as " .. tostring ( filename ) } )
else
require ( " naughty " ) . notify ( { title = " Failed to save macro " } )
end
-- For some reason after keygrabber is done input doesn't work
gears.timer . delayed_call ( function ( )
awful.keygrabber : stop ( )
end )
end
}
end )
awesome.connect_signal ( " macro::play " , function ( filename )
if ( not filename ) or ( not fs.file_readable ( filename ) ) then
return
end
local macrofile = io.open ( filename , " r " )
local macrodata = macrofile : read ( " *a " )
macrofile : close ( )
local macro = { }
local step = 0
macrodata : gsub ( " ([^ \n ]+) " , function ( line )
table.insert ( macro , line )
end )
local delay = tonumber ( macro [ 2 ] : match ( " delay=(%d+) " ) )
_G.MacroActive [ filename ] = ( macro [ 1 ] : match ( " loop=(.+) " ) == " true " )
table.remove ( macro , 2 )
table.remove ( macro , 1 )
local macro_length = # macro
_G.MacroTimer [ filename ] = gears.timer . start_new ( ( 1 / 1000 ) * delay , function ( )
step = step + 1
local instruction = macro [ step ]
local event = ( instruction : match ( " ^%+ " ) and " key_press " ) or " key_release "
root.fake_input ( event , instruction : match ( " ^[%+%-](.*) " ) )
if ( step == macro_length ) and ( _G.MacroActive [ filename ] ) then
step = 0
return true
elseif ( step < macro_length ) then
return true
else
_G.MacroTimer [ filename ] = nil
_G.MacroActive [ filename ] = nil
return false
end
end )
end )
root.keys ( gears.table . join (
root.keys ( ) ,
ask.k ( " :macro.record_1 " , function ( )
awesome.emit_signal ( " macro::record " , macro_base_dir .. " macro1 " )
end , { group = " macros " , description = " Record macro 1 " } ) ,
ask.k ( " :macro.record_2 " , function ( )
awesome.emit_signal ( " macro::record " , macro_base_dir .. " macro2 " )
end , { group = " macros " , description = " Record macro 2 " } ) ,
ask.k ( " :macro.record_3 " , function ( )
awesome.emit_signal ( " macro::record " , macro_base_dir .. " macro3 " )
end , { group = " macros " , description = " Record macro 3 " } ) ,
ask.k ( " :macro.record_4 " , function ( )
awesome.emit_signal ( " macro::record " , macro_base_dir .. " macro4 " )
end , { group = " macros " , description = " Record macro 4 " } ) ,
ask.k ( " :macro.record_5 " , function ( )
awesome.emit_signal ( " macro::record " , macro_base_dir .. " macro5 " )
end , { group = " macros " , description = " Record macro 5 " } ) ,
ask.k ( " :macro.play_1 " , function ( )
if not _G.MacroActive [ macro_base_dir .. " macro1 " ] then
awesome.emit_signal ( " macro::play " , macro_base_dir .. " macro1 " )
else
_G.MacroActive [ macro_base_dir .. " macro1 " ] = false
end
end , { group = " macros " , description = " Play macro 1 " , release_pre = true } ) ,
ask.k ( " :macro.play_2 " , function ( )
if not _G.MacroActive [ macro_base_dir .. " macro2 " ] then
awesome.emit_signal ( " macro::play " , macro_base_dir .. " macro2 " )
else
_G.MacroActive [ macro_base_dir .. " macro2 " ] = false
end
end , { group = " macros " , description = " Play macro 2 " , release_pre = true } ) ,
ask.k ( " :macro.play_3 " , function ( )
if not _G.MacroActive [ macro_base_dir .. " macro3 " ] then
awesome.emit_signal ( " macro::play " , macro_base_dir .. " macro3 " )
else
_G.MacroActive [ macro_base_dir .. " macro3 " ] = false
end
end , { group = " macros " , description = " Play macro 3 " , release_pre = true } ) ,
ask.k ( " :macro.play_4 " , function ( )
if not _G.MacroActive [ macro_base_dir .. " macro4 " ] then
awesome.emit_signal ( " macro::play " , macro_base_dir .. " macro4 " )
else
_G.MacroActive [ macro_base_dir .. " macro4 " ] = false
end
end , { group = " macros " , description = " Play macro 4 " , release_pre = true } ) ,
ask.k ( " :macro.play_5 " , function ( )
if not _G.MacroActive [ macro_base_dir .. " macro5 " ] then
awesome.emit_signal ( " macro::play " , macro_base_dir .. " macro5 " )
else
_G.MacroActive [ macro_base_dir .. " macro5 " ] = false
end
end , { group = " macros " , description = " Play macro 5 " , release_pre = true } ) ,
ask.k ( " :macro.loop " , function ( )
macro_loop = not macro_loop
if macro_loop then
require ( " naughty " ) . notify ( { title = " Macro looping turned on " } )
else
require ( " naughty " ) . notify ( { title = " Macro looping turned off " } )
end
end , { group = " macros " , description = " turn looping on/off " } )
) )