Macro systeem veralgemeniseerd naar Sequence. Sequence maakt het mogelijk om de automatische toegevoegde events bij macro's te omzeilen. Hier wordt gebruik van gemaakt bij de Continuous task, want deze moet alle vrijheid hebben. Nu is in de vele shortcut-sequence add functies nog geen beveiliging om oneindige Continuous tasks uit te voeren. De verantwoordelijkheid hiervoor ligt bij de gebruiker of een toekomstige controle.
This commit is contained in:
@@ -11,7 +11,7 @@ public abstract class Application extends TaskListener {
|
|||||||
initialise();
|
initialise();
|
||||||
super.run();
|
super.run();
|
||||||
} catch (ApplicationInitialiseException e) {
|
} catch (ApplicationInitialiseException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace(); // Todo: dit "over" de thread heengooien / loggen?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,10 +25,10 @@ public abstract class Application extends TaskListener {
|
|||||||
Action action = task.getAction();
|
Action action = task.getAction();
|
||||||
if (task instanceof Continuous) {
|
if (task instanceof Continuous) {
|
||||||
Continuous continuous = (Continuous) task;
|
Continuous continuous = (Continuous) task;
|
||||||
int sleep = continuous.getSleep();
|
|
||||||
do {
|
do {
|
||||||
action(action);
|
action(action);
|
||||||
sleep(sleep);
|
continuous.nextIteration();
|
||||||
|
sleep(continuous.getSleep());
|
||||||
} while (run && !continuous.getStop());
|
} while (run && !continuous.getStop());
|
||||||
continuous.reset();
|
continuous.reset();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -3,32 +3,37 @@ package pm;
|
|||||||
import pm.exception.device.DeviceExitException;
|
import pm.exception.device.DeviceExitException;
|
||||||
import pm.exception.device.DeviceInitialiseException;
|
import pm.exception.device.DeviceInitialiseException;
|
||||||
import pm.macro.Event;
|
import pm.macro.Event;
|
||||||
import pm.macro.MacroListener;
|
|
||||||
import pm.macro.event.Hold;
|
import pm.macro.event.Hold;
|
||||||
import pm.macro.event.Press;
|
import pm.macro.event.Press;
|
||||||
import pm.macro.event.Release;
|
import pm.macro.event.Release;
|
||||||
|
import pm.macro.event.Sequence;
|
||||||
|
import pm.macro.event.SequenceListener;
|
||||||
import pm.task.Continuous;
|
import pm.task.Continuous;
|
||||||
import pm.task.Stopper;
|
import pm.task.Stopper;
|
||||||
|
|
||||||
public abstract class Device {
|
public abstract class Device {
|
||||||
protected MacroListener macroListener;
|
protected SequenceListener sequenceListener;
|
||||||
|
|
||||||
public Device() {
|
public Device() {
|
||||||
macroListener = new MacroListener();
|
sequenceListener = new SequenceListener();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Register macro's */
|
/* Register macro's */
|
||||||
protected void add(Macro macro, Task task) {
|
protected void add(Sequence sequence, Task task) {
|
||||||
macroListener.add(macro, task);
|
sequenceListener.add(sequence, task);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void add(Press press, Task task) {
|
||||||
|
add(new Macro(press), task);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void add(Event event, Task task) {
|
protected void add(Event event, Task task) {
|
||||||
add(new Macro(event), task);
|
add(new Sequence(event), task);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void add(Macro startMacro, Macro stopMacro, Continuous continuous) {
|
protected void add(Sequence startSequence, Sequence stopSequence, Continuous continuous) {
|
||||||
add(startMacro, continuous);
|
add(startSequence, continuous);
|
||||||
add(stopMacro, new Stopper(continuous));
|
add(stopSequence, new Stopper(continuous));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void add(Event startEvent, Event stopEvent, Continuous continuous) {
|
protected void add(Event startEvent, Event stopEvent, Continuous continuous) {
|
||||||
@@ -36,6 +41,11 @@ public abstract class Device {
|
|||||||
add(stopEvent, new Stopper(continuous));
|
add(stopEvent, new Stopper(continuous));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void add(Press startPress, Press stopPress, Continuous continuous) {
|
||||||
|
add(new Macro(startPress), continuous);
|
||||||
|
add(new Macro(stopPress), new Stopper(continuous));
|
||||||
|
}
|
||||||
|
|
||||||
protected void add(Hold hold, Continuous continuous) {
|
protected void add(Hold hold, Continuous continuous) {
|
||||||
Button button = hold.getButton();
|
Button button = hold.getButton();
|
||||||
add(new Press(button), new Release(button), continuous);
|
add(new Press(button), new Release(button), continuous);
|
||||||
@@ -43,7 +53,7 @@ public abstract class Device {
|
|||||||
|
|
||||||
/* Recognize events */
|
/* Recognize events */
|
||||||
protected void add(Event event) {
|
protected void add(Event event) {
|
||||||
macroListener.add(event);
|
sequenceListener.add(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Device default methods */
|
/* Device default methods */
|
||||||
|
|||||||
@@ -5,15 +5,15 @@ import java.util.ArrayList;
|
|||||||
import pm.exception.MacroException;
|
import pm.exception.MacroException;
|
||||||
import pm.exception.macro.MacroEventOrderException;
|
import pm.exception.macro.MacroEventOrderException;
|
||||||
import pm.macro.Event;
|
import pm.macro.Event;
|
||||||
|
import pm.macro.event.Sequence;
|
||||||
import pm.macro.event.Hold;
|
import pm.macro.event.Hold;
|
||||||
import pm.macro.event.Press;
|
import pm.macro.event.Press;
|
||||||
import pm.macro.event.Release;
|
import pm.macro.event.Release;
|
||||||
|
|
||||||
public class Macro {
|
public class Macro extends Sequence {
|
||||||
protected Event[] eventArray;
|
public Macro(Press press) {
|
||||||
|
Button button = press.getButton();
|
||||||
public Macro(Event event) {
|
this.eventArray = new Event[] {press, new Release(button)};
|
||||||
eventArray = new Event[]{event};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Macro(Event... eventArray) throws MacroException {
|
public Macro(Event... eventArray) throws MacroException {
|
||||||
@@ -44,14 +44,6 @@ public class Macro {
|
|||||||
if (!holdList.isEmpty()) {
|
if (!holdList.isEmpty()) {
|
||||||
throw new MacroEventOrderException("One or more buttons are not released.");
|
throw new MacroEventOrderException("One or more buttons are not released.");
|
||||||
}
|
}
|
||||||
this.eventArray = (Event[]) eventList.toArray(new Event[0]);
|
eventArray = (Event[]) eventList.toArray(new Event[0]);
|
||||||
}
|
|
||||||
|
|
||||||
public int count() {
|
|
||||||
return eventArray.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Event get(int i) {
|
|
||||||
return eventArray[i];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,7 @@ import com.dt.iTunesController.iTunes;
|
|||||||
import com.dt.iTunesController.iTunesEventsInterface;
|
import com.dt.iTunesController.iTunesEventsInterface;
|
||||||
|
|
||||||
public class iTunesApplication extends Application implements iTunesEventsInterface {
|
public class iTunesApplication extends Application implements iTunesEventsInterface {
|
||||||
protected static final int POSTION_CHANGE_RATE = 5;
|
protected static final int POSTION_CHANGE_RATE = 1;
|
||||||
protected static final int VOLUME_CHANGE_RATE = 5;
|
protected static final int VOLUME_CHANGE_RATE = 5;
|
||||||
protected static final int SEEK_TIME = 1000;
|
protected static final int SEEK_TIME = 1000;
|
||||||
|
|
||||||
|
|||||||
@@ -37,10 +37,12 @@ public class RumblepadDevice extends JavaInputDevice {
|
|||||||
new Task(Action.PREVIOUS, Target.APPLICATION));
|
new Task(Action.PREVIOUS, Target.APPLICATION));
|
||||||
add(
|
add(
|
||||||
new Hold(RumblepadButton.FIVE),
|
new Hold(RumblepadButton.FIVE),
|
||||||
new Continuous(Action.FORWARD, Target.APPLICATION, 300));
|
new Continuous(Action.FORWARD, Target.APPLICATION, 200){
|
||||||
|
public int getSleep() {return sleep - 30 * iteration;}});
|
||||||
add(
|
add(
|
||||||
new Hold(RumblepadButton.SEVEN),
|
new Hold(RumblepadButton.SEVEN),
|
||||||
new Continuous(Action.REWIND, Target.APPLICATION, 300));
|
new Continuous(Action.REWIND, Target.APPLICATION, 200){
|
||||||
|
public int getSleep() {return sleep - 30 * iteration;}});
|
||||||
add(
|
add(
|
||||||
new Hold(RumblepadButton.NINE),
|
new Hold(RumblepadButton.NINE),
|
||||||
new Continuous(Action.VOLUME_DOWN, Target.APPLICATION, 100));
|
new Continuous(Action.VOLUME_DOWN, Target.APPLICATION, 100));
|
||||||
|
|||||||
@@ -1,26 +1,26 @@
|
|||||||
package pm.macro;
|
package pm.macro;
|
||||||
|
|
||||||
import pm.Macro;
|
import pm.macro.event.Sequence;
|
||||||
|
|
||||||
public class Active {
|
public class Active {
|
||||||
protected Macro macro;
|
protected Sequence sequence;
|
||||||
protected int step;
|
protected int step;
|
||||||
|
|
||||||
public Active(Macro macro) {
|
public Active(Sequence sequence) {
|
||||||
this.macro = macro;
|
this.sequence = sequence;
|
||||||
step = -1;
|
step = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Macro getMacro() {
|
public Sequence getSequence() {
|
||||||
return macro;
|
return sequence;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean next(Event event) {
|
public boolean next(Event event) {
|
||||||
Event next = macro.get(++step);
|
Event next = sequence.get(++step);
|
||||||
return next == null ? false : event.equals(next);
|
return next == null ? false : event.equals(next);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean last() {
|
public boolean last() {
|
||||||
return step == macro.count() - 1;
|
return step == sequence.count() - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
20
java/src/pm/macro/event/Sequence.java
Normal file
20
java/src/pm/macro/event/Sequence.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package pm.macro.event;
|
||||||
|
|
||||||
|
import pm.macro.Event;
|
||||||
|
|
||||||
|
|
||||||
|
public class Sequence {
|
||||||
|
protected Event[] eventArray;
|
||||||
|
|
||||||
|
public Sequence(Event... eventArray) {
|
||||||
|
this.eventArray = eventArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int count() {
|
||||||
|
return eventArray.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Event get(int i) {
|
||||||
|
return eventArray.length > 0 ? eventArray[i] : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,37 +1,38 @@
|
|||||||
package pm.macro;
|
package pm.macro.event;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import pm.Macro;
|
|
||||||
import pm.Task;
|
import pm.Task;
|
||||||
|
import pm.macro.Active;
|
||||||
|
import pm.macro.Event;
|
||||||
import pm.task.TaskGatherer;
|
import pm.task.TaskGatherer;
|
||||||
|
|
||||||
public class MacroListener {
|
public class SequenceListener {
|
||||||
public ArrayList<Macro> macroList;
|
public ArrayList<Sequence> sequenceList;
|
||||||
public HashMap<Macro, Task> taskMap;
|
public HashMap<Sequence, Task> taskMap;
|
||||||
public ArrayList<Active> activeList;
|
public ArrayList<Active> activeList;
|
||||||
|
|
||||||
public MacroListener() {
|
public SequenceListener() {
|
||||||
macroList = new ArrayList<Macro>();
|
sequenceList = new ArrayList<Sequence>();
|
||||||
taskMap = new HashMap<Macro, Task>();
|
taskMap = new HashMap<Sequence, Task>();
|
||||||
activeList = new ArrayList<Active>();
|
activeList = new ArrayList<Active>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(Macro macro, Task task) {
|
public void add(Sequence sequence, Task task) {
|
||||||
macroList.add(macro);
|
sequenceList.add(sequence);
|
||||||
taskMap.put(macro, task);
|
taskMap.put(sequence, task);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(Event event) {
|
public void add(Event event) {
|
||||||
for (Macro macro : macroList) {
|
for (Sequence sequence : sequenceList) {
|
||||||
activeList.add(new Active(macro));
|
activeList.add(new Active(sequence));
|
||||||
}
|
}
|
||||||
ArrayList<Active> removeList = new ArrayList<Active>();
|
ArrayList<Active> removeList = new ArrayList<Active>();
|
||||||
for (Active active : activeList) {
|
for (Active active : activeList) {
|
||||||
if (active.next(event)) {
|
if (active.next(event)) {
|
||||||
if (active.last()) {
|
if (active.last()) {
|
||||||
Task task = taskMap.get(active.getMacro());
|
Task task = taskMap.get(active.getSequence());
|
||||||
TaskGatherer.add(task);
|
TaskGatherer.add(task);
|
||||||
removeList.add(active);
|
removeList.add(active);
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@ import pm.Task;
|
|||||||
|
|
||||||
public class Continuous extends Task {
|
public class Continuous extends Task {
|
||||||
protected int sleep;
|
protected int sleep;
|
||||||
|
protected int iteration;
|
||||||
protected boolean stop;
|
protected boolean stop;
|
||||||
|
|
||||||
public Continuous(Action action, Target target, int sleep) {
|
public Continuous(Action action, Target target, int sleep) {
|
||||||
@@ -18,11 +19,16 @@ public class Continuous extends Task {
|
|||||||
this(action, target, 0);
|
this(action, target, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void nextIteration() {
|
||||||
|
++iteration;
|
||||||
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
stop = true;
|
stop = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
|
iteration = 0;
|
||||||
stop = false;
|
stop = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,9 @@ public abstract class TaskListener implements Runnable {
|
|||||||
|
|
||||||
protected void sleep(int time) {
|
protected void sleep(int time) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(time);
|
if (time > 0) {
|
||||||
|
Thread.sleep(time);
|
||||||
|
}
|
||||||
} catch (InterruptedException e) {}
|
} catch (InterruptedException e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user