Author: tom
Date: Thu Jan 8 09:18:12 2009
New Revision: 3739
URL: http://svn.slimdevices.com?rev=3739&root=Jive&view=rev
Log:
Bug: N/A
Description: Initial event model rework
Modified:
7.4/trunk/squeezeplay/src/squeezeplay/share/jive/JiveMain.lua
7.4/trunk/squeezeplay/src/squeezeplay/share/jive/ui/Event.lua
7.4/trunk/squeezeplay/src/squeezeplay/share/jive/ui/Framework.lua
7.4/trunk/squeezeplay/src/squeezeplay/src/ui/jive.h
7.4/trunk/squeezeplay/src/squeezeplay/src/ui/jive_event.c
7.4/trunk/squeezeplay/src/squeezeplay/src/ui/jive_framework.c
7.4/trunk/squeezeplay/src/squeezeplay/src/ui/lua_jiveui.c
Modified: 7.4/trunk/squeezeplay/src/squeezeplay/share/jive/JiveMain.lua
URL:
http://svn.slimdevices.com/7.4/trunk/squeezeplay/src/squeezeplay/share/jive/JiveMain.lua?rev=3739&root=Jive&r1=3738&r2=3739&view=diff
==============================================================================
--- 7.4/trunk/squeezeplay/src/squeezeplay/share/jive/JiveMain.lua (original)
+++ 7.4/trunk/squeezeplay/src/squeezeplay/share/jive/JiveMain.lua Thu Jan 8
09:18:12 2009
@@ -65,6 +65,7 @@
--require("profiler")
local EVENT_KEY_ALL = jive.ui.EVENT_KEY_ALL
+local ACTION = jive.ui.ACTION
local EVENT_KEY_PRESS = jive.ui.EVENT_KEY_PRESS
local EVENT_CHAR_PRESS = jive.ui.EVENT_CHAR_PRESS
local EVENT_KEY_HOLD = jive.ui.EVENT_KEY_HOLD
@@ -102,6 +103,7 @@
["i"] = { keyCode = KEY_UP, event = EVENT_KEY_PRESS },
["k"] = { keyCode = KEY_DOWN, event = EVENT_KEY_PRESS },
["j"] = { keyCode = KEY_LEFT, event = EVENT_KEY_PRESS },
+ ["J"] = { keyCode = KEY_LEFT, event = EVENT_KEY_HOLD },
["l"] = { keyCode = KEY_RIGHT, event = EVENT_KEY_PRESS },
["h"] = { keyCode = KEY_HOME, event = EVENT_KEY_PRESS },
["p"] = { keyCode = KEY_PLAY, event = EVENT_KEY_PRESS },
@@ -124,8 +126,35 @@
["\27"] = { keyCode = KEY_BACK, event = EVENT_KEY_PRESS } -- ESC
}
+local keyActionMappingsPress = {
+ [KEY_LEFT] = "arrow_left.press" -- using button naming convention from SC
default.map, though these are action events
+}
+
+
+local keyActionMappingsHold = {
+ [KEY_LEFT] = "arrow_left.hold",
+ [KEY_REW | KEY_PAUSE] = "take_screenshot" -- a stab at how to handle
multi-press
+}
+
local _defaultSkin
local _fullscreen
+
+local function _goHome()
+ local windowStack = Framework.windowStack
+
+ if #windowStack > 1 then
+ Framework:playSound("JUMP")
+ jiveMain:closeToHome(true)
+ else
+ Framework:playSound("BUMP")
+ windowStack[1]:bumpLeft()
+ end
+end
+
+local function _disconnectPlayer(self, event) --self, event not used in our
case, could be left out
+ appletManager:callService("setCurrentPlayer", nil)
+ _goHome()
+end
-- bring us to the home menu
local function _homeHandler(event)
@@ -143,23 +172,10 @@
return EVENT_CONSUME
- elseif (( type == EVENT_KEY_PRESS and event:getKeycode() == KEY_HOME) or
- ( type == EVENT_KEY_HOLD and event:getKeycode() == KEY_BACK)) then
-
- -- disconnect from player on press and hold left
- if type == EVENT_KEY_HOLD then
- appletManager:callService("setCurrentPlayer", nil)
- end
-
- local windowStack = Framework.windowStack
-
- if #windowStack > 1 then
- Framework:playSound("JUMP")
- jiveMain:closeToHome(true)
- else
- Framework:playSound("BUMP")
- windowStack[1]:bumpLeft()
- end
+ elseif ( type == EVENT_KEY_PRESS and event:getKeycode() == KEY_HOME)
then
+
+ _goHome()
+
return EVENT_CONSUME
end
return EVENT_UNUSED
@@ -170,6 +186,34 @@
package.path = package.path .. System.getUserDir() .. dirSeparator
.."?.lua;"
package.path = package.path .. System.getUserDir() .. dirSeparator .. "?"
.. dirSeparator .. "?.lua;"
end
+
+-- transform user input events (key, etc) to a matching action name
+local function getAction(event)
+ --naive implementation for demonstration - will be more involved later
+
+ local eventType = event:getType()
+ local action = nil
+
+ if eventType == EVENT_KEY_PRESS then
+ action = keyActionMappingsPress[event:getKeycode()]
+ elseif eventType == EVENT_KEY_HOLD then
+ action = keyActionMappingsHold[event:getKeycode()]
+ end
+
+ return action
+
+end
+
+function registerDefaultActions()
+ for key, action in pairs(keyActionMappingsPress) do
+ Framework:registerAction(action)
+ end
+ for key, action in pairs(keyActionMappingsHold) do
+ Framework:registerAction(action)
+ end
+
+end
+
-- __init
-- creates our JiveMain main object
@@ -222,6 +266,33 @@
end,
10)
+ registerDefaultActions()
+
+ -- action mapping listener, should be last listener in chain to allow
for direct access to keys/other input types if needed.
+ --todo add other input types
+ Framework:addListener(EVENT_KEY_ALL,
+ function(event)
+ local action = getAction(event)
+ if not action then
+ return EVENT_UNUSED
+ end
+
+ local actionEvent = Framework:newActionEvent(action)
+ if not actionEvent then
+ log:error("Odd, newActionEvent returned nil, but should
always return a result when match was found for action: ", action)
+ return EVENT_UNUSED
+ end
+
+ log:debug("Pushing action event (", action, ") -
event:getAction: " , actionEvent:getAction())
+ Framework:pushEvent(actionEvent)
+ return EVENT_CONSUMED
+ end,
+ 9999)
+
+
+ -- disconnect from player on press and hold left
+ Framework:addActionListener("arrow_left.hold", self, _disconnectPlayer)
+
-- show our window!
jiveMain.window:show()
Modified: 7.4/trunk/squeezeplay/src/squeezeplay/share/jive/ui/Event.lua
URL:
http://svn.slimdevices.com/7.4/trunk/squeezeplay/src/squeezeplay/share/jive/ui/Event.lua?rev=3739&root=Jive&r1=3738&r2=3739&view=diff
==============================================================================
--- 7.4/trunk/squeezeplay/src/squeezeplay/share/jive/ui/Event.lua (original)
+++ 7.4/trunk/squeezeplay/src/squeezeplay/share/jive/ui/Event.lua Thu Jan 8
09:18:12 2009
@@ -44,6 +44,14 @@
Returns the mouse x,y position for EVENT_MOUSE_* events.
+=head2 jive.ui.Event:getAction()
+
+Returns the action name for ACTION events.
+
+=head2 jive.ui.Event:getActionInternal()
+
+Returns the internal representation of the action name for ACTION events. Used
by getAction(), should not be needed for general use.
+
=back
@@ -189,13 +197,23 @@
=cut
--]]
+local require = require
local oo = require("loop.base")
module(..., oo.class)
-
--- C implementation
+local Framework = require("jive.ui.Framework")
+
+
+
+function getAction(self)
+ local actionIndex = self:getActionInternal()
+ return Framework:getActionEventNameByIndex(actionIndex)
+end
+
+-- the rest is C implementation
+
--[[
=head1 LICENSE
Modified: 7.4/trunk/squeezeplay/src/squeezeplay/share/jive/ui/Framework.lua
URL:
http://svn.slimdevices.com/7.4/trunk/squeezeplay/src/squeezeplay/share/jive/ui/Framework.lua?rev=3739&root=Jive&r1=3738&r2=3739&view=diff
==============================================================================
--- 7.4/trunk/squeezeplay/src/squeezeplay/share/jive/ui/Framework.lua (original)
+++ 7.4/trunk/squeezeplay/src/squeezeplay/share/jive/ui/Framework.lua Thu Jan
8 09:18:12 2009
@@ -39,6 +39,7 @@
local EVENT_HIDE = jive.ui.EVENT_HIDE
local EVENT_CONSUME = jive.ui.EVENT_CONSUME
local EVENT_UNUSED = jive.ui.EVENT_UNUSED
+local ACTION = jive.ui.ACTION
local FRAME_RATE = jive.ui.FRAME_RATE
@@ -97,6 +98,9 @@
screen = {}
screen.bounds = { 0, 0, 240, 320 }
screen.bpp = 16
+
+actionsByName = {}
+actionsByIndex = {}
-- Put default global settings here
default_global_settings = {
@@ -537,6 +541,100 @@
table.delete(self.unusedListeners, handle)
end
+function dumpActions(self)
+ local result = "Available Actions: "
+ for action in table.pairsByKeys(self.actionsByName) do
+ result = result .. " " .. action
+ end
+ return result
+end
+
+function _getActionEventIndexByName(self, name)
+ if (self.actionsByName[name] == nil) then
+ return nil
+ end
+
+ return self.actionsByName[name].index
+end
+
+function getActionEventNameByIndex(self, index)
+ if (index > #self.actionsByIndex) then
+ log:error("action event index out of bounds: " , index)
+ return nil
+ end
+
+ return self.actionsByIndex[index].name
+end
+
+--[[
+
+=head2 jive.ui.Framework:newActionEvent(action)
+
+Returns a new ACTION event or nil if no matching action has been registered.
+
+=cut
+--]]
+function newActionEvent(self, action)
+ local actionIndex = self:_getActionEventIndexByName(action)
+ if not actionIndex then
+ log:error("action name not registered: (" , action, "). Available
actions: ", self:dumpActions() )
+ return nil
+ end
+
+ return Event:new(ACTION, actionIndex)
+
+end
+
+--[[
+
+=head2 jive.ui.Framework:registerAction(action)
+
+Register an action. action is a unique string that represents an action.
+Each action must be registered before listeners using it can be created (for
typo prevention, and other future uses).
+
+=cut
+--]]
+function registerAction(self, actionName)
+ if (self.actionsByName[actionName]) then
+ log:error("Action already registered, doing nothing")
+ return
+ end
+
+ local actionEventDefinition = { name = actionName, index =
#self.actionsByIndex + 1 }
+
+ log:debug("Registering action: ", actionEventDefinition.name, " with
index: ", actionEventDefinition.index)
+ self.actionsByName[actionName] = actionEventDefinition
+ table.insert(self.actionsByIndex, actionEventDefinition)
+
+end
+
+
+--example: addActionListener("play", self, playAction)
+function addActionListener(self, action, obj, listener)
+ _assert(type(listener) == "function")
+
+
+ if not self:_getActionEventIndexByName(action) then
+ log:error("action name not registered:(" , action, "). Available
actions: ", self:dumpActions() )
+ return
+ end
+
+ log:debug("Creating action listener for action: (" , action, ") from
source: todo would be nice to be able to display the source of the listener in
a meaningful way for debugging")
+
+ self:addListener(ACTION,
+ function(event)
+ local eventAction = event:getAction()
+ if eventAction != action then
+ return EVENT_UNUSED
+ end
+
+ local listenerResult = listener(obj, event)
+ --default to consume unless the listener specifically wants to set
a specific event return
+ return listenerResult and listenerResult or EVENT_CONSUME
+ end
+ )
+
+end
--[[
Modified: 7.4/trunk/squeezeplay/src/squeezeplay/src/ui/jive.h
URL:
http://svn.slimdevices.com/7.4/trunk/squeezeplay/src/squeezeplay/src/ui/jive.h?rev=3739&root=Jive&r1=3738&r2=3739&view=diff
==============================================================================
--- 7.4/trunk/squeezeplay/src/squeezeplay/src/ui/jive.h (original)
+++ 7.4/trunk/squeezeplay/src/squeezeplay/src/ui/jive.h Thu Jan 8 09:18:12 2009
@@ -99,6 +99,7 @@
JIVE_EVENT_MOTION = 0x00800000,
JIVE_EVENT_CHAR_PRESS = 0x02000000,
+ JIVE_ACTION = 0x04000000,
JIVE_EVENT_CHAR_ALL = ( JIVE_EVENT_CHAR_PRESS),
JIVE_EVENT_KEY_ALL = ( JIVE_EVENT_KEY_DOWN |
JIVE_EVENT_KEY_UP | JIVE_EVENT_KEY_PRESS | JIVE_EVENT_KEY_HOLD ),
@@ -187,6 +188,10 @@
int rel;
};
+struct jive_action_event {
+ int index;
+};
+
struct jive_key_event {
JiveKey code;
};
@@ -217,6 +222,7 @@
union {
struct jive_scroll_event scroll;
+ struct jive_action_event action;
struct jive_key_event key;
struct jive_char_event text;
struct jive_mouse_event mouse;
@@ -401,6 +407,7 @@
int jiveL_event_get_keycode(lua_State *L);
int jiveL_event_get_unicode(lua_State *L);
int jiveL_event_get_mouse(lua_State *L);
+int jiveL_event_get_action_internal(lua_State *L);
int jiveL_event_get_motion(lua_State *L);
int jiveL_event_get_switch(lua_State *L);
Modified: 7.4/trunk/squeezeplay/src/squeezeplay/src/ui/jive_event.c
URL:
http://svn.slimdevices.com/7.4/trunk/squeezeplay/src/squeezeplay/src/ui/jive_event.c?rev=3739&root=Jive&r1=3738&r2=3739&view=diff
==============================================================================
--- 7.4/trunk/squeezeplay/src/squeezeplay/src/ui/jive_event.c (original)
+++ 7.4/trunk/squeezeplay/src/squeezeplay/src/ui/jive_event.c Thu Jan 8
09:18:12 2009
@@ -47,6 +47,10 @@
switch (event->type) {
case JIVE_EVENT_SCROLL:
event->u.scroll.rel = lua_tointeger(L, 3);
+ break;
+
+ case JIVE_ACTION:
+ event->u.action.index = lua_tointeger(L, 3);
break;
case JIVE_EVENT_KEY_DOWN:
@@ -177,6 +181,23 @@
return 0;
}
+int jiveL_event_get_action_internal(lua_State *L) {
+ JiveEvent* event = (JiveEvent*)lua_touserdata(L, 1);
+ if (event == NULL) {
+ luaL_error(L, "invalid Event");
+ }
+
+ switch (event->type) {
+ case JIVE_ACTION:
+ lua_pushinteger(L, event->u.action.index);
+ return 1;
+
+ default:
+ luaL_error(L, "Not an action event");
+ }
+ return 0;
+}
+
int jiveL_event_get_motion(lua_State *L) {
JiveEvent* event = (JiveEvent*)lua_touserdata(L, 1);
@@ -254,6 +275,11 @@
case JIVE_EVENT_KEY_HOLD:
lua_pushfstring(L, "KEY_HOLD code=%d", event->u.key.code);
break;
+
+ case JIVE_ACTION:
+ //todo: also show actionEventName - convert index to
actionEventName by calling Framework:getActionEventNameByIndex
+ lua_pushfstring(L, "ACTION actionIndex=%d",
event->u.action.index);
+ break;
case JIVE_EVENT_MOUSE_DOWN:
lua_pushfstring(L, "MOUSE_DOWN x=%d,y=%d", event->u.mouse.x,
event->u.mouse.y);
Modified: 7.4/trunk/squeezeplay/src/squeezeplay/src/ui/jive_framework.c
URL:
http://svn.slimdevices.com/7.4/trunk/squeezeplay/src/squeezeplay/src/ui/jive_framework.c?rev=3739&root=Jive&r1=3738&r2=3739&view=diff
==============================================================================
--- 7.4/trunk/squeezeplay/src/squeezeplay/src/ui/jive_framework.c (original)
+++ 7.4/trunk/squeezeplay/src/squeezeplay/src/ui/jive_framework.c Thu Jan 8
09:18:12 2009
@@ -1308,6 +1308,7 @@
{ "getKeycode", jiveL_event_get_keycode },
{ "getUnicode", jiveL_event_get_unicode },
{ "getMouse", jiveL_event_get_mouse },
+ { "getActionInternal", jiveL_event_get_action_internal },
{ "getMotion", jiveL_event_get_motion },
{ "getSwitch", jiveL_event_get_switch },
{ "tostring", jiveL_event_tostring },
Modified: 7.4/trunk/squeezeplay/src/squeezeplay/src/ui/lua_jiveui.c
URL:
http://svn.slimdevices.com/7.4/trunk/squeezeplay/src/squeezeplay/src/ui/lua_jiveui.c?rev=3739&root=Jive&r1=3738&r2=3739&view=diff
==============================================================================
--- 7.4/trunk/squeezeplay/src/squeezeplay/src/ui/lua_jiveui.c (original)
+++ 7.4/trunk/squeezeplay/src/squeezeplay/src/ui/lua_jiveui.c Thu Jan 8
09:18:12 2009
@@ -2203,6 +2203,7 @@
tolua_constant(tolua_S,"EVENT_SWITCH",JIVE_EVENT_SWITCH);
tolua_constant(tolua_S,"EVENT_MOTION",JIVE_EVENT_MOTION);
tolua_constant(tolua_S,"EVENT_KEY_ALL",JIVE_EVENT_KEY_ALL);
+ tolua_constant(tolua_S,"ACTION",JIVE_ACTION);
tolua_constant(tolua_S,"EVENT_MOUSE_ALL",JIVE_EVENT_MOUSE_ALL);
tolua_constant(tolua_S,"EVENT_ALL_INPUT",JIVE_EVENT_ALL_INPUT);
tolua_constant(tolua_S,"EVENT_VISIBLE_ALL",JIVE_EVENT_VISIBLE_ALL);
_______________________________________________
Jive-checkins mailing list
[email protected]
http://lists.slimdevices.com/cgi-bin/mailman/listinfo/jive-checkins