-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmacro.lua
More file actions
161 lines (140 loc) · 4.11 KB
/
macro.lua
File metadata and controls
161 lines (140 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
--local socket = require("socket")
local print = print
local table = table
local glib = require("lgi").GLib
local capi = {
mousegrabber = mousegrabber, timer = timer,
keygrabber = keygrabber , root = root ,
mouse = mouse ,
}
local module = {}
local function get_time()
local time2 = glib.TimeVal()
glib.get_current_time(time2)
return time2.tv_sec,time2.tv_usec
end
local function get_delta(sec1,usec1,sec2,usec2)
return (sec2-sec1)*(1000000)+(usec2-usec1)
end
local function exec_key_ev(data)
if data.event == "release" then
capi.root.fake_input("key_release",data.key)
else
capi.root.fake_input("key_press",data.key)
end
end
local buttons = {}
local function exec_mouse_buttons(ev)
for i=1, #ev.buttons do
if ev.buttons[i] == true and buttons[i] ~= true then
capi.root.fake_input("button_press",i)
buttons[i] = true
elseif ev.buttons[i] == false and buttons[i] == true then
capi.root.fake_input("button_release",i)
buttons[i] = nil
end
end
end
local function exec_mouse_ev(ev,x,y)
exec_mouse_buttons(ev)
if x ~= ev.x or y ~= ev.y then
capi.mouse.coords({x=ev.x,y = ev.y})
end
end
local function release_all()
for i=1,20 do
if buttons[i] then
capi.root.fake_input("button_release",i)
end
end
buttons = {}
end
local function meta_macro_namespace()
local start_sec,start_usec,last_sec,last_usec,x,y,m
local mouse_fct = nil
mouse_fct = function(mouse)
print("Mouse",mouse.buttons[1],buttons[1])
local new_sec,new_usec = get_time()
local delta = get_delta(last_sec,last_usec,new_sec,new_usec)
last_sec,last_usec = new_sec,new_usec
table.insert(m,{type="m", data = mouse,delta=delta})
-- Pause the grabbing to execute some buttons events --TODO buggy
-- capi.mousegrabber.stop()
-- exec_mouse_buttons(mouse)
-- capi.mousegrabber.run(mouse_fct,"fleur")
return true
end
local key_fct = nil
key_fct = function(mod, key, event)
print("Key",key)
if key == 'Escape' then
print("STOP")
capi.mousegrabber.stop()
capi.keygrabber.stop()
release_all()
m.callback_fct(m)
return false
end
local new_sec,new_usec = get_time()
local delta = get_delta(last_sec,last_usec,new_sec,new_usec)
last_sec,last_usec = new_sec,new_usec
table.insert(m,{type="k", mod = mod, key = key, event= event, delta = delta})
--Pause the grabber the time to execute the key
capi.keygrabber.stop()
exec_key_ev(m[#m])
capi.keygrabber.run(key_fct)
return true
end
function module.record(callback_fct)
print("Start recording")
x = capi.mouse.coords().x
y = capi.mouse.coords().y
m = {x=x,y=y,callback_fct=callback_fct}
start_sec,start_usec = get_time()
last_sec,last_usec = start_sec,start_usec
-- Mouse
capi.mousegrabber.run(mouse_fct,"fleur")
-- Keyboard
capi.keygrabber.run(key_fct)
return m
end
end
--Old JavaScript hack, this create a virtual object with local vars
meta_macro_namespace()
function module.play(m)
print("Playing macro")
local timer = capi.timer({ timeout = m[1].delta/1000000 or 0.1 })
local index = 1
-- In case the first event is a key, reset the mouse
local x = m.x
local y = m.y
capi.mouse.coords({x=x ,y=y})
timer:connect_signal("timeout", function()
if index <= #m then
local data = m[index]
if data.type == "m" then
exec_mouse_ev(data.data,x,y)
elseif data.type == "k" then
exec_key_ev(data)
end
--print("Playing: "..data.type,data.event,'"'..(data.key or "N/A")..'"')
index = index + 1
-- Set the timer correctly
data = m[index]
if data then
local delta = data.delta
timer:stop()
timer.timeout = delta/1000000
timer:start()
end
else
release_all()
print("That's all folk")
timer:stop()
timer = nil
end
end)
timer:start()
end
return setmetatable(module, { __call = function(_, ...) return new(...) end })
-- kate: space-indent on; indent-width 2; replace-tabs on;