2012-03-10 09:28:54 +00:00
|
|
|
require "keys"
|
|
|
|
|
2012-03-13 21:51:25 +00:00
|
|
|
Keydef = {
|
|
|
|
keycode = nil,
|
|
|
|
modifier = nil,
|
|
|
|
descr = nil
|
|
|
|
}
|
2012-03-29 12:52:57 +00:00
|
|
|
|
2012-03-13 21:51:25 +00:00
|
|
|
function Keydef:_new(obj)
|
|
|
|
-- obj definition
|
|
|
|
obj = obj or {}
|
|
|
|
setmetatable(obj, self)
|
|
|
|
self.__index = self
|
|
|
|
self.__tostring=Keydef.tostring
|
2012-03-20 19:15:24 +00:00
|
|
|
return obj
|
2012-03-10 09:28:54 +00:00
|
|
|
end
|
2012-03-29 12:52:57 +00:00
|
|
|
|
2012-03-13 21:51:25 +00:00
|
|
|
function Keydef:new(keycode,modifier,descr)
|
|
|
|
obj = Keydef:_new()
|
|
|
|
obj.keycode = keycode
|
|
|
|
obj.modifier = modifier
|
2012-03-20 19:15:24 +00:00
|
|
|
obj.descr = descr
|
|
|
|
return obj
|
2012-03-10 09:28:54 +00:00
|
|
|
end
|
2012-03-29 12:52:57 +00:00
|
|
|
|
2012-03-13 21:51:25 +00:00
|
|
|
function Keydef:display()
|
2012-04-15 14:07:39 +00:00
|
|
|
return (self.modifier or "")..(self.descr or "")
|
2012-03-13 21:51:25 +00:00
|
|
|
end
|
2012-03-29 12:52:57 +00:00
|
|
|
|
2012-03-13 21:51:25 +00:00
|
|
|
function Keydef:tostring()
|
|
|
|
return ((self.modifier and self.modifier.."+") or "").."["..(self.keycode or "").."]"..(self.descr or "")
|
2012-03-10 09:28:54 +00:00
|
|
|
end
|
|
|
|
|
2012-03-29 12:52:57 +00:00
|
|
|
|
2012-03-13 21:51:25 +00:00
|
|
|
Command = {
|
|
|
|
keydef = nil,
|
|
|
|
keygroup = nil,
|
|
|
|
func = nil,
|
|
|
|
help = nil,
|
|
|
|
order = nil
|
|
|
|
}
|
2012-03-29 12:52:57 +00:00
|
|
|
|
2012-03-13 21:51:25 +00:00
|
|
|
function Command:_new(obj)
|
|
|
|
-- obj definition
|
|
|
|
obj = obj or {}
|
|
|
|
setmetatable(obj, self)
|
|
|
|
self.__index = self
|
|
|
|
self.__tostring=Command.tostring
|
2012-03-20 19:15:24 +00:00
|
|
|
return obj
|
2012-03-13 21:51:25 +00:00
|
|
|
end
|
2012-03-29 12:52:57 +00:00
|
|
|
|
2012-03-13 21:51:25 +00:00
|
|
|
function Command:new(keydef, func, help, keygroup, order)
|
|
|
|
obj = Command:_new()
|
|
|
|
obj.keydef = keydef
|
|
|
|
obj.func = func
|
|
|
|
obj.help = help
|
|
|
|
obj.keygroup = keygroup
|
|
|
|
obj.order = order
|
2012-04-18 16:16:49 +00:00
|
|
|
--debug("creating command: ["..tostring(keydef).."] keygroup:["..(keygroup or "").."] help:"..help)
|
2012-03-13 21:51:25 +00:00
|
|
|
return obj
|
|
|
|
end
|
2012-03-29 12:52:57 +00:00
|
|
|
|
2012-03-13 21:51:25 +00:00
|
|
|
function Command:tostring()
|
|
|
|
return tostring(self.keydef)..": "..(self.help or "<no help defined>")
|
2012-03-10 09:28:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
Commands = {
|
2012-03-13 21:51:25 +00:00
|
|
|
map = {},
|
|
|
|
size = 0
|
2012-03-10 09:28:54 +00:00
|
|
|
}
|
2012-03-29 12:52:57 +00:00
|
|
|
|
2012-03-10 09:28:54 +00:00
|
|
|
function Commands:add(keycode,modifier,keydescr,help,func)
|
2012-03-27 17:58:29 +00:00
|
|
|
if type(keycode) == "table" then
|
|
|
|
for i=1,#keycode,1 do
|
|
|
|
local keydef = Keydef:new(keycode[i],modifier,keydescr)
|
|
|
|
self:_addImpl(keydef,help,func)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local keydef = Keydef:new(keycode,modifier,keydescr)
|
|
|
|
self:_addImpl(keydef,help,func)
|
|
|
|
end
|
2012-03-20 19:15:24 +00:00
|
|
|
end
|
2012-03-29 12:52:57 +00:00
|
|
|
|
2012-03-20 19:15:24 +00:00
|
|
|
function Commands:addGroup(keygroup,keys,help,func)
|
2012-03-10 09:28:54 +00:00
|
|
|
for _k,keydef in pairs(keys) do
|
2012-03-20 19:15:24 +00:00
|
|
|
self:_addImpl(keydef,help,func,keygroup)
|
2012-03-10 09:28:54 +00:00
|
|
|
end
|
2012-03-13 21:51:25 +00:00
|
|
|
end
|
2012-03-29 12:52:57 +00:00
|
|
|
|
2012-04-06 12:13:46 +00:00
|
|
|
--@TODO handle MOD_ANY 06.04 2012 (houqp)
|
2012-03-30 04:01:59 +00:00
|
|
|
function Commands:del(keycode, modifier, keydescr)
|
|
|
|
local keydef = nil
|
|
|
|
|
|
|
|
if not keydescr then
|
|
|
|
for k,v in pairs(self.map) do
|
|
|
|
if v.keydef.keycode == keycode
|
|
|
|
and v.keydef.modifier == modifier then
|
|
|
|
keydef = k
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end -- EOF for
|
|
|
|
else
|
|
|
|
keydef = Keydef:new(keycode, modifier, keydescr)
|
|
|
|
end -- EOF if
|
|
|
|
|
|
|
|
self.map[keydef] = nil
|
|
|
|
end
|
|
|
|
|
2012-04-06 12:13:46 +00:00
|
|
|
function Commands:delGroup(keygroup)
|
|
|
|
if keygroup then
|
|
|
|
for k,v in pairs(self.map) do
|
|
|
|
if v.keygroup == keygroup then
|
|
|
|
self.map[k] = nil
|
|
|
|
end
|
|
|
|
end -- EOF for
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-20 19:15:24 +00:00
|
|
|
function Commands:_addImpl(keydef,help,func,keygroup)
|
2012-03-13 21:51:25 +00:00
|
|
|
if keydef.modifier==MOD_ANY then
|
2012-03-20 19:15:24 +00:00
|
|
|
self:addGroup(keygroup or keydef.descr,{Keydef:new(keydef.keycode,nil), Keydef:new(keydef.keycode,MOD_SHIFT), Keydef:new(keydef.keycode,MOD_ALT)},help,func)
|
2012-03-13 21:51:25 +00:00
|
|
|
elseif keydef.modifier==MOD_SHIFT_OR_ALT then
|
2012-04-15 14:07:39 +00:00
|
|
|
self:addGroup(keygroup or (MOD_SHIFT..MOD_ALT..(keydef.descr or "")),{Keydef:new(keydef.keycode,MOD_SHIFT), Keydef:new(keydef.keycode,MOD_ALT)},help,func)
|
2012-03-13 21:51:25 +00:00
|
|
|
else
|
|
|
|
local command = self.map[keydef]
|
|
|
|
if command == nil then
|
|
|
|
self.size = self.size + 1
|
2012-03-20 19:15:24 +00:00
|
|
|
command = Command:new(keydef,func,help,keygroup,self.size)
|
2012-03-13 21:51:25 +00:00
|
|
|
self.map[keydef] = command
|
|
|
|
else
|
|
|
|
command.func = func
|
|
|
|
command.help = help
|
|
|
|
command.keygroup = keygroup
|
|
|
|
end
|
2012-03-20 19:15:24 +00:00
|
|
|
end
|
|
|
|
end
|
2012-03-29 12:52:57 +00:00
|
|
|
|
2012-03-10 09:28:54 +00:00
|
|
|
function Commands:get(keycode,modifier)
|
2012-03-13 21:51:25 +00:00
|
|
|
return self.map[Keydef:new(keycode, modifier)]
|
2012-03-10 09:28:54 +00:00
|
|
|
end
|
2012-03-29 12:52:57 +00:00
|
|
|
|
2012-03-20 19:15:24 +00:00
|
|
|
function Commands:getByKeydef(keydef)
|
2012-03-10 09:28:54 +00:00
|
|
|
return self.map[keydef]
|
|
|
|
end
|
2012-03-29 12:52:57 +00:00
|
|
|
|
2012-03-10 09:28:54 +00:00
|
|
|
function Commands:new(obj)
|
2012-03-29 12:52:57 +00:00
|
|
|
-- obj definition
|
|
|
|
obj = obj or {}
|
|
|
|
obj.map = {}
|
|
|
|
obj.size = 0
|
|
|
|
setmetatable(obj, self)
|
|
|
|
self.__index = self
|
|
|
|
|
2012-03-10 09:28:54 +00:00
|
|
|
-- payload
|
|
|
|
local mt = {}
|
2012-03-29 12:52:57 +00:00
|
|
|
mt.__index = function(table, key)
|
2012-03-10 09:28:54 +00:00
|
|
|
return rawget(table,(key.modifier or "").."@#@"..(key.keycode or ""))
|
2012-03-20 19:15:24 +00:00
|
|
|
end
|
2012-03-29 12:52:57 +00:00
|
|
|
mt.__newindex = function(table, key, value)
|
2012-03-10 09:28:54 +00:00
|
|
|
return rawset(table,(key.modifier or "").."@#@"..(key.keycode or ""),value)
|
2012-03-20 19:15:24 +00:00
|
|
|
end
|
2012-03-29 12:52:57 +00:00
|
|
|
setmetatable(obj.map, mt)
|
|
|
|
|
2012-04-10 12:45:22 +00:00
|
|
|
obj:add(KEY_INTO_SCREEN_SAVER, nil, "Slider",
|
2012-04-08 04:46:33 +00:00
|
|
|
"toggle screen saver",
|
|
|
|
function()
|
|
|
|
Screen:saveCurrentBB()
|
|
|
|
Screen.kpv_rotation_mode = Screen.cur_rotation_mode
|
|
|
|
fb:setOrientation(Screen.native_rotation_mode)
|
2012-04-18 11:43:55 +00:00
|
|
|
util.sleep(1)
|
|
|
|
os.execute("killall -cont cvm")
|
2012-04-08 04:46:33 +00:00
|
|
|
end
|
|
|
|
)
|
2012-04-10 12:45:22 +00:00
|
|
|
obj:add(KEY_OUTOF_SCREEN_SAVER, nil, "Slider",
|
2012-04-08 04:46:33 +00:00
|
|
|
"toggle screen saver",
|
|
|
|
function()
|
2012-04-18 11:43:55 +00:00
|
|
|
util.usleep(1500000)
|
|
|
|
os.execute("killall -stop cvm")
|
2012-04-08 04:46:33 +00:00
|
|
|
fb:setOrientation(Screen.kpv_rotation_mode)
|
2012-04-14 17:28:59 +00:00
|
|
|
Screen:restoreFromSavedBB()
|
2012-04-08 04:46:33 +00:00
|
|
|
fb:refresh(0)
|
|
|
|
end
|
|
|
|
)
|
2012-03-10 09:28:54 +00:00
|
|
|
return obj
|
2012-03-13 21:51:25 +00:00
|
|
|
end
|