-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.lua
209 lines (164 loc) · 4.44 KB
/
core.lua
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
-- commands are indexed by a string representing the button code
-- for now
local function new_command_table()
local utilities = {
-- inserts a command in the current button
insert = function (self, cmd_table)
-- check for errors
assert(self.current , 'Set a current button first!')
--local button_name = 'Button ' .. self.current.name .. ': '
assert( cmd_table.down or cmd_table.up , 'Set at least one command (down/up)!')
-- setups null objects (empty functions) if methods are absent
if not cmd_table.down then cmd_table.down = function () end end
if not cmd_table.up then cmd_table.up = function () end end
table.insert(self.current, cmd_table)
end
}
local meta = {
-- the call metamethod will execute in sequence all the commands for a button
-- state is either down or up (indicating a press or release repectively)
__call = function (t,button,state)
for _,cmd in ipairs(t[button]) do
cmd[state]()
end
end ,
__index = utilities
}
-- a new command table with no current button
local cmd_table = { current = nil }
return setmetatable( cmd_table , meta )
end
-- TODO should be moved to some kind of config file
base_dir = 'mapping/'
current_file = base_dir .. 'example.lua'
commands = new_command_table()
local function do_event(x,state)
x = tostring(x)
if commands[x] ~= nil then
commands(x,state)
else
-- dont crash
end
end
-- called from C
-- receives joystick events and performs actions
function __event_button(x,down)
do_event(x, down and 'down' or 'up')
end
-- TODO: document the API
function button(b)
-- creates the command table for this button
-- and sets as the current one
commands[b] = { }
commands.current = commands[b]
end
-- cmd is polymorphic and stuff
-- it can receive strings, tables and functions
local function construct_cmd(param)
local ret = nil
if type(param) == 'table' then
-- already ready and formated like a
-- table with button (down|up) functions, we hope
ret = param
elseif type(param) == 'function' then
-- a function to be executed,
-- just need to make a button down table
ret = {
down = param
}
elseif type(param) == 'string' then
-- a string is an os command,
-- we encapsulate it in a function
ret = {
down = function ()
print(param)
os.execute(param)
end
}
end
return ret
end
local function execute_cmd(param)
construct_cmd(param)['down']()
end
function cmd(param)
commands:insert( construct_cmd(param) )
end
-- TODO
-- for now this uses xdotool to send the commands
-- in the future I want to have a portable module
-- that sends the keypresses
function key(k)
cmd {
down = function ()
print(k .. ' down')
execute_cmd('xdotool keydown ' .. k)
--__send_key_event(k,true)
end ,
up = function()
print(k .. ' up')
execute_cmd('xdotool keyup ' .. k)
--__send_key_event(k,false)
end
}
end
function toggle(cmd_table)
-- TODO assert for errors
-- closures rock
local on = true
cmd( function ()
if on then
execute_cmd(cmd_table.on)
on = false
else
execute_cmd(cmd_table.off)
on = true
end
end
)
end
-- loads the commands stored in a file and displays a visual confirmation
-- the nodoc param should tell it not to display
-- the visual, but for now it does nothing
function load(filename,nodoc)
cmd ( function ()
commands = new_command_table()
if filename ~= 'self' then current_file = base_dir .. filename end
dofile(current_file)
print('Loaded ' .. current_file)
report(commands)
end
)
end
function explain(str)
commands.current.explain = str
end
-- TODO modularize this to work everywhere
-- quick and dirty visual notification
-- works only where libnotify-bin package is available (ubuntu...)
local function notify_impl(t)
t.icon = t.icon or "`pwd`/awesome.png"
t.title = t.title or 'Empty'
t.message = t.message or ''
t.timeout = t.timeout or 1000
return ('notify-send ' .. '"' .. t.title .. '" "' ..
'\n' .. t.message .. '" -i ' .. t.icon .. ' -t ' .. t.timeout )
end
local function notify_real(t)
os.execute(notify_impl(t))
end
function notify(t)
cmd( notify_impl(t) )
end
-- crafts a notification message with the contents of a file
-- and displays it
function report(commands)
local note = { title = current_file, message = '', timeout = 2000 }
for i,cmd in pairs(commands) do
if i ~= 'current' then
note.message = note.message .. i .. ': ' .. (cmd.explain or 'Nothing') .. '\n'
end
end
notify_real(note)
end
dofile(current_file)