diff --git a/java/src/mimis/Device.java b/java/src/mimis/Device.java index f0ecd5e..cac9717 100644 --- a/java/src/mimis/Device.java +++ b/java/src/mimis/Device.java @@ -1,58 +1,37 @@ package mimis; +import mimis.device.EventMapCycle; import mimis.event.EventHandler; -import mimis.event.Task; import mimis.exception.worker.ActivateException; import mimis.exception.worker.DeactivateException; import mimis.manager.Exitable; import mimis.manager.Titled; -import mimis.sequence.Sequence; +import mimis.sequence.EventMap; import mimis.sequence.SequenceListener; import mimis.sequence.State; -import mimis.sequence.state.Press; public abstract class Device extends EventHandler implements Titled, Exitable { protected String title; + protected EventMapCycle eventMapCycle; protected SequenceListener sequenceListener; public Device(String title) { this.title = title; } + public String title() { + return title; + } + + /* Worker */ public void activate() throws ActivateException { super.activate(); sequenceListener = new SequenceListener(this); } - /* Register macro's */ - protected void add(Sequence sequence, Task task) { - sequenceListener.add(sequence, task); - } - - protected void add(State state, Task task) { - add(new Sequence(state), task); - } - - protected void add(Press press, Task task, boolean macro) { - if (macro) { - add(new Macro(press), task); - } else { - add((State) press, task); - } - } - - protected void add(Press press, Task task) { - add(press, task, true); - } - - /* Recognize events */ - protected void add(State state) { - log.debug(state.toString() + " " + state.getButton()); - sequenceListener.add(state); - } - - public String title() { - return title; + public void deactivate() throws DeactivateException { + super.deactivate(); + sequenceListener.reset(); } public void stop() { @@ -65,4 +44,21 @@ public abstract class Device extends EventHandler implements Titled, Exitable { } super.stop(); } + + /* SequenceListener */ + protected void add(EventMap eventMap) { + sequenceListener.add(eventMap); + } + + protected void remove(EventMap eventMap) { + sequenceListener.remove(eventMap); + } + + protected void add(State state) { + sequenceListener.add(state); + } + + protected void reset() { + sequenceListener.reset(); + } } diff --git a/java/src/mimis/Mimis.java b/java/src/mimis/Mimis.java index 132d945..7a60a5c 100644 --- a/java/src/mimis/Mimis.java +++ b/java/src/mimis/Mimis.java @@ -85,7 +85,6 @@ public class Mimis extends EventHandler { } protected void end(Action action) { - log.debug(String.format("action(%s)", action)); switch (action) { case NEXT: eventRouter.set(applicationCycle.next()); diff --git a/java/src/mimis/device/EventMapCycle.java b/java/src/mimis/device/EventMapCycle.java new file mode 100644 index 0000000..a7d2812 --- /dev/null +++ b/java/src/mimis/device/EventMapCycle.java @@ -0,0 +1,13 @@ +package mimis.device; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import mimis.sequence.EventMap; +import mimis.util.ArrayCycle; + +public class EventMapCycle extends ArrayCycle { + protected static final long serialVersionUID = 1L; + + protected Log log = LogFactory.getLog(getClass()); +} diff --git a/java/src/mimis/device/jintellitype/JIntellitypeDevice.java b/java/src/mimis/device/jintellitype/JIntellitypeDevice.java index 71f4952..66c9491 100644 --- a/java/src/mimis/device/jintellitype/JIntellitypeDevice.java +++ b/java/src/mimis/device/jintellitype/JIntellitypeDevice.java @@ -3,23 +3,19 @@ package mimis.device.jintellitype; import java.util.ArrayList; import mimis.Device; -import mimis.event.Task; import mimis.exception.button.UnknownButtonException; import mimis.exception.worker.ActivateException; +import mimis.exception.worker.DeactivateException; import mimis.sequence.state.Press; import mimis.sequence.state.Release; -import mimis.value.Action; -import mimis.value.Key; -import mimis.value.Target; - import com.melloware.jintellitype.HotkeyListener; import com.melloware.jintellitype.IntellitypeListener; import com.melloware.jintellitype.JIntellitype; - public class JIntellitypeDevice extends Device implements HotkeyListener, IntellitypeListener { protected static final String TITLE = "JIntellitype"; + protected JIntellitypeEventMapCycle eventMapCycle; protected ArrayList hotkeyList; protected JIntellitype jit; @@ -28,45 +24,15 @@ public class JIntellitypeDevice extends Device implements HotkeyListener, Intell hotkeyList = new ArrayList(); jit = JIntellitype.getInstance(); Hotkey.initialise(hotkeyList, jit); + eventMapCycle = new JIntellitypeEventMapCycle(); } public void activate() throws ActivateException { super.activate(); jit.addHotKeyListener(this); jit.addIntellitypeListener(this); - add( - new Hotkey(Key.PRIOR), - new Task(Target.MIMIS, Action.PREVIOUS)); - add( - new Hotkey(Key.NEXT), - new Task(Target.MIMIS, Action.NEXT)); - add( - new Press(CommandButton.VOLUME_DOWN), - new Task(Target.APPLICATIONS, Action.VOLUME_DOWN)); - add( - new Press(CommandButton.VOLUME_UP), - new Task(Target.APPLICATIONS, Action.VOLUME_UP)); - add( - new Hotkey(Modifier.CTRL | Modifier.WIN, 'x'), - new Task(Target.MIMIS, Action.EXIT)); - add( - new Hotkey(Modifier.CTRL | Modifier.SHIFT | Modifier.WIN, 'n'), - new Task(Target.APPLICATION, Action.NEXT)); - add( - new Hotkey(Modifier.CTRL | Modifier.SHIFT | Modifier.WIN, 'p'), - new Task(Target.APPLICATION, Action.PREVIOUS)); - /*add( - new Hotkey(Modifier.CTRL | Modifier.WIN, 't'), - new Task(Action.TEST, Target.MAIN)); - add( - new Hotkey(Modifier.CTRL | Modifier.WIN, 'r'), - new Hotkey(Modifier.CTRL | Modifier.WIN, 's'), - new Continuous(Action.REPEAT, Target.APPLICATIONS, 500));*/ - - } - - protected void add(Hotkey hotkey, Task task) { - add(new Press(hotkey), task); + add(eventMapCycle.mimis); + add(eventMapCycle.player); } public void onIntellitype(int command) { @@ -89,8 +55,8 @@ public class JIntellitypeDevice extends Device implements HotkeyListener, Intell } } - public void stop() { - super.stop(); + public void deactivate() throws DeactivateException { + super.deactivate(); jit.removeHotKeyListener(this); jit.removeIntellitypeListener(this); jit.cleanUp(); diff --git a/java/src/mimis/device/jintellitype/JIntellitypeEventMapCycle.java b/java/src/mimis/device/jintellitype/JIntellitypeEventMapCycle.java new file mode 100644 index 0000000..41dfea6 --- /dev/null +++ b/java/src/mimis/device/jintellitype/JIntellitypeEventMapCycle.java @@ -0,0 +1,44 @@ +package mimis.device.jintellitype; + +import mimis.device.EventMapCycle; +import mimis.event.Task; +import mimis.sequence.EventMap; +import mimis.sequence.state.Press; +import mimis.value.Action; +import mimis.value.Key; +import mimis.value.Target; + +public class JIntellitypeEventMapCycle extends EventMapCycle { + protected static final long serialVersionUID = 1L; + + public EventMap mimis, player; + + public JIntellitypeEventMapCycle() { + /* Mimis */ + mimis = new EventMap(); + mimis.add( + new Hotkey(Key.PRIOR), + new Task(Target.MIMIS, Action.PREVIOUS)); + mimis.add( + new Hotkey(Key.NEXT), + new Task(Target.MIMIS, Action.NEXT)); + + /* Player */ + player = new EventMap(); + player.add( + new Press(CommandButton.VOLUME_DOWN), + new Task(Target.APPLICATIONS, Action.VOLUME_DOWN)); + player.add( + new Press(CommandButton.VOLUME_UP), + new Task(Target.APPLICATIONS, Action.VOLUME_UP)); + player.add( + new Hotkey(Modifier.CTRL | Modifier.WIN, 'x'), + new Task(Target.MIMIS, Action.EXIT)); + player.add( + new Hotkey(Modifier.CTRL | Modifier.SHIFT | Modifier.WIN, 'n'), + new Task(Target.APPLICATION, Action.NEXT)); + player.add( + new Hotkey(Modifier.CTRL | Modifier.SHIFT | Modifier.WIN, 'p'), + new Task(Target.APPLICATION, Action.PREVIOUS)); + } +} diff --git a/java/src/mimis/device/wiimote/WiimoteDevice.java b/java/src/mimis/device/wiimote/WiimoteDevice.java index 1815af0..2fa9328 100644 --- a/java/src/mimis/device/wiimote/WiimoteDevice.java +++ b/java/src/mimis/device/wiimote/WiimoteDevice.java @@ -4,7 +4,6 @@ import mimis.Button; import mimis.Device; import mimis.device.wiimote.gesture.GestureDevice; import mimis.event.Feedback; -import mimis.event.Task; import mimis.exception.button.UnknownButtonException; import mimis.exception.device.DeviceNotFoundException; import mimis.exception.worker.ActivateException; @@ -12,31 +11,26 @@ import mimis.exception.worker.DeactivateException; import mimis.sequence.state.Press; import mimis.sequence.state.Release; import mimis.value.Action; - import org.wiigee.event.GestureEvent; import org.wiigee.event.GestureListener; import org.wiigee.util.Log; import wiiusej.Wiimote; -import wiiusej.values.Calibration; import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent; import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent; public class WiimoteDevice extends Device implements GestureListener { protected static final String TITLE = "Wiimote"; - - protected static final int CONNECT_MAX = 10; protected static final int RUMBLE = 150; - public static final int TIMEOUT = 200; + protected static final int TIMEOUT = 200; protected static WiimoteService wiimoteService; - - protected Wiimote wiimote; - protected Calibration calibration; - protected GestureDevice gestureDevice; - protected int gestureId; + protected WiimoteEventMapCycle eventMapCycle; protected WiimoteDiscovery wiimoteDiscovery; protected boolean connected; + protected Wiimote wiimote; + protected GestureDevice gestureDevice; + protected int gestureId; static { WiimoteDevice.wiimoteService = new WiimoteService(); @@ -45,6 +39,7 @@ public class WiimoteDevice extends Device implements GestureListener { public WiimoteDevice() { super(TITLE); + eventMapCycle = new WiimoteEventMapCycle(); wiimoteDiscovery = new WiimoteDiscovery(this); gestureDevice = new GestureDevice(); gestureDevice.add(this); @@ -55,69 +50,11 @@ public class WiimoteDevice extends Device implements GestureListener { public void activate() throws ActivateException { super.activate(); connect(); - /*add( - new Hold(WiimoteButton.A), - new Task(Action.TRAIN), - new Task(Action.STOP));*/ - add( - new Press(WiimoteButton.B), - new Task(Action.SAVE)); - add( - new Press(WiimoteButton.DOWN), - new Task(Action.LOAD)); - /*add( - new Hold(WiimoteButton.HOME), - new Task(Action.RECOGNIZE), - new Task(Action.STOP));/* - add( - new Press(WiimoteButton.A), - new Task(Target.APPLICATION, Action.PLAY)); - add( - new Press(WiimoteButton.B), - new Task(Target.APPLICATION, Action.MUTE)); - add( - new Press(WiimoteButton.ONE), - new Task(Target.APPLICATION, Action.SHUFFLE)); - add( - new Press(WiimoteButton.TWO), - new Task(Target.APPLICATION, Action.REPEAT)); - add( - new Press(WiimoteButton.UP), - new Task(Target.APPLICATION, Action.NEXT)); - add( - new Press(WiimoteButton.DOWN), - new Task(Target.APPLICATION, Action.PREVIOUS)); - add( - new Hold(WiimoteButton.RIGHT), - new Dynamic(Action.FORWARD, Target.APPLICATION, 200, -30)); - add( - new Hold(WiimoteButton.LEFT), - new Dynamic(Action.REWIND, Target.APPLICATION, 200, -30)); - add( - new Hold(WiimoteButton.MINUS), - new Continuous(Action.VOLUME_DOWN, Target.APPLICATION, 100)); - add( - new Hold(WiimoteButton.PLUS), - new Continuous(Action.VOLUME_UP, Target.APPLICATION, 100)); - add( - new Press(WiimoteButton.HOME), - new Task(Target.MANAGER, Action.NEXT)); - try { - add( - new Macro( - new Hold(WiimoteButton.TWO), - new Press(WiimoteButton.PLUS), - new Release(WiimoteButton.TWO)), - new Task(Target.APPLICATION, Action.LIKE)); - add( - new Macro( - new Hold(WiimoteButton.TWO), - new Press(WiimoteButton.MINUS), - new Release(WiimoteButton.TWO)), - new Task(Target.APPLICATION, Action.DISLIKE)); - } catch (StateOrderException e) {}*/ + add(eventMapCycle.mimis); + add(eventMapCycle.player); + add(eventMapCycle.like); } - + public void deactivate() throws DeactivateException { super.deactivate(); if (wiimote != null) { @@ -132,32 +69,40 @@ public class WiimoteDevice extends Device implements GestureListener { } /* Events */ - public void action(Action action) { + public void begin(Action action) { switch (action) { case TRAIN: - System.out.println("Wiimote Train"); + log.debug("Gesture train"); gestureDevice.train(); break; - case STOP: - System.out.println("Wiimote Stop"); - gestureDevice.stop(); - break; case SAVE: - System.out.println("Wiimote Save"); + log.debug("Gesture save"); gestureDevice.close(); gestureDevice.saveGesture(gestureId, "C:\\gesture-" + gestureId); ++gestureId; break; case LOAD: + log.debug("Gesture load"); for (int i = 0; i < gestureId; ++i) { gestureDevice.loadGesture("C:\\gesture-" + i); } break; case RECOGNIZE: + log.debug("Gesture recognize"); gestureDevice.recognize(); break; } } + + public void end(Action action) { + switch (action) { + case TRAIN: + case RECOGNIZE: + log.debug("Gesture stop"); + gestureDevice.stop(); + break; + } + } public void feedback(Feedback feedback) { System.out.println("Wiimote feedback"); diff --git a/java/src/mimis/device/wiimote/WiimoteEventMapCycle.java b/java/src/mimis/device/wiimote/WiimoteEventMapCycle.java new file mode 100644 index 0000000..466a998 --- /dev/null +++ b/java/src/mimis/device/wiimote/WiimoteEventMapCycle.java @@ -0,0 +1,96 @@ +package mimis.device.wiimote; + +import mimis.Macro; +import mimis.device.EventMapCycle; +import mimis.event.Task; +import mimis.exception.macro.StateOrderException; +import mimis.sequence.EventMap; +import mimis.sequence.state.Hold; +import mimis.sequence.state.Press; +import mimis.sequence.state.Release; +import mimis.value.Action; +import mimis.value.Target; + +public class WiimoteEventMapCycle extends EventMapCycle { + protected static final long serialVersionUID = 1L; + + public EventMap mimis, player, gesture, like; + + public WiimoteEventMapCycle() { + /* Mimis */ + mimis = new EventMap(); + mimis.add( + new Press(WiimoteButton.HOME), + new Task(Target.MIMIS, Action.NEXT)); + + /* Gesture */ + gesture = new EventMap(); + gesture.add( + new Press(WiimoteButton.A), + new Task(Action.TRAIN)); + gesture.add( + new Press(WiimoteButton.B), + new Task(Action.SAVE)); + gesture.add( + new Press(WiimoteButton.DOWN), + new Task(Action.LOAD)); + gesture.add( + new Press(WiimoteButton.HOME), + new Task(Action.RECOGNIZE)); + add(gesture); + + /* Player */ + player = new EventMap(); + player.add( + new Press(WiimoteButton.A), + new Task(Target.APPLICATION, Action.PLAY)); + player.add( + new Press(WiimoteButton.B), + new Task(Target.APPLICATION, Action.MUTE)); + player.add( + new Press(WiimoteButton.ONE), + new Task(Target.APPLICATION, Action.SHUFFLE)); + player.add( + new Press(WiimoteButton.TWO), + new Task(Target.APPLICATION, Action.REPEAT)); + player.add( + new Press(WiimoteButton.UP), + new Task(Target.APPLICATION, Action.NEXT)); + player.add( + new Press(WiimoteButton.DOWN), + new Task(Target.APPLICATION, Action.PREVIOUS)); + player.add( + new Press(WiimoteButton.RIGHT), + new Task(Target.APPLICATION, Action.FORWARD)); + player.add( + new Press(WiimoteButton.LEFT), + new Task(Target.APPLICATION, Action.REWIND)); + player.add( + new Press(WiimoteButton.MINUS), + new Task(Target.APPLICATION, Action.VOLUME_DOWN)); + player.add( + new Press(WiimoteButton.PLUS), + new Task(Target.APPLICATION, Action.VOLUME_UP)); + add(player); + + /* Like */ + try { + like = new EventMap(); + like.add( + new Macro( + new Hold(WiimoteButton.TWO), + new Press(WiimoteButton.PLUS), + new Release(WiimoteButton.TWO)), + new Task(Target.APPLICATION, Action.LIKE)); + like.add( + new Macro( + new Hold(WiimoteButton.TWO), + new Press(WiimoteButton.MINUS), + new Release(WiimoteButton.TWO)), + new Task(Target.APPLICATION, Action.DISLIKE)); + add(like); + } catch (StateOrderException e) { + log.error(e); + } + } +} diff --git a/java/src/mimis/event/EventHandler.java b/java/src/mimis/event/EventHandler.java index 507100e..db0d093 100644 --- a/java/src/mimis/event/EventHandler.java +++ b/java/src/mimis/event/EventHandler.java @@ -21,7 +21,6 @@ public abstract class EventHandler extends EventListener { protected void feedback(Feedback feedback) {} protected final void task(Task task) { - log.debug("Signal: " + task.signal); Action action = task.getAction(); switch (task.getSignal()) { case BEGIN: diff --git a/java/src/mimis/event/EventListener.java b/java/src/mimis/event/EventListener.java index e4baae9..1e9e2ca 100644 --- a/java/src/mimis/event/EventListener.java +++ b/java/src/mimis/event/EventListener.java @@ -16,7 +16,6 @@ public abstract class EventListener extends Worker { } public void add(Event event) { - log.info("event " + event + " " + active); eventQueue.add(event); synchronized (work) { work.notifyAll(); diff --git a/java/src/mimis/event/router/LocalRouter.java b/java/src/mimis/event/router/LocalRouter.java index 7b62f86..83c8024 100644 --- a/java/src/mimis/event/router/LocalRouter.java +++ b/java/src/mimis/event/router/LocalRouter.java @@ -17,7 +17,6 @@ public class LocalRouter extends EventRouter { default: for (EventListener eventListener : eventListenerList) { if (event.compatible(eventListener)) { - log.trace(eventListener); eventListener.add(event); } } diff --git a/java/src/mimis/sequence/EventMap.java b/java/src/mimis/sequence/EventMap.java new file mode 100644 index 0000000..db50a68 --- /dev/null +++ b/java/src/mimis/sequence/EventMap.java @@ -0,0 +1,37 @@ +package mimis.sequence; + +import java.util.HashMap; + +import mimis.Button; +import mimis.Event; +import mimis.Macro; +import mimis.event.Task; +import mimis.sequence.state.Press; + +public class EventMap extends HashMap { + protected static final long serialVersionUID = 1L; + + public void add(Button button, Task task) { + add(new Press(button), task); + } + + public void add(Press press, Task task) { + add(press, task, true); + } + + protected void add(Press press, Task task, boolean macro) { + if (macro) { + add(new Macro(press), task); + } else { + add((State) press, task); + } + } + + protected void add(State state, Task task) { + add(new Sequence(state), task); + } + + public void add(Sequence sequence, Task task) { + put(sequence, task); + } +} diff --git a/java/src/mimis/sequence/SequenceListener.java b/java/src/mimis/sequence/SequenceListener.java index f572a27..ed31e33 100644 --- a/java/src/mimis/sequence/SequenceListener.java +++ b/java/src/mimis/sequence/SequenceListener.java @@ -1,7 +1,7 @@ package mimis.sequence; import java.util.ArrayList; -import java.util.HashMap; +import java.util.Map.Entry; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -17,31 +17,44 @@ public class SequenceListener { protected Log log = LogFactory.getLog(getClass()); protected EventHandler self; + protected EventMap eventMap; protected ArrayList sequenceList; - protected HashMap eventMap; protected ArrayList activeList; - + protected static EventListener eventListener; public SequenceListener(EventHandler self) { this.self = self; + eventMap = new EventMap(); sequenceList = new ArrayList(); - eventMap = new HashMap(); activeList = new ArrayList(); } public static void initialise(EventListener eventListener) { SequenceListener.eventListener = eventListener; } - - public int add(Sequence sequence, Task task) { - int id = sequenceList.size(); - sequenceList.add(sequence); - eventMap.put(sequence, task); - return id; + + public synchronized void add(EventMap eventMap) { + this.eventMap.putAll(eventMap); + sequenceList.addAll(eventMap.keySet()); } - public void add(State state) { + public void remove(EventMap eventMap) { + for (Entry entry : eventMap.entrySet()) { + Sequence sequence = entry.getKey(); + this.eventMap.remove(sequence); + sequenceList.remove(sequence); + activeList.remove(sequence); + } + } + + public synchronized void reset() { + eventMap.clear(); + sequenceList.clear(); + activeList.clear(); + } + + public synchronized void add(State state) { for (Sequence sequence : sequenceList) { activeList.add(new Active(sequence)); }