summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2012-09-11 14:38:14 +0200
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2012-09-11 14:38:14 +0200
commit2421be8140898e5306d9622c7856f71adc8f4518 (patch)
tree4722fcac6e44a811cb43edcbe4c8c342b0c0c333
parentae198fa7592e7bf801f3678b11a854c5a4e6d04d (diff)
downloaddigitale-debutanten-2421be8140898e5306d9622c7856f71adc8f4518.tar.gz
added RumbleEvent class for using the "Logitech Rumble Gamepad"
* similar to NanoEvent: symbolic controller names, normalized value and scaling functions, ports
-rw-r--r--lib.ck1
-rw-r--r--lib/RumbleEvent.ck105
2 files changed, 106 insertions, 0 deletions
diff --git a/lib.ck b/lib.ck
index 2915a17..52b476f 100644
--- a/lib.ck
+++ b/lib.ck
@@ -6,6 +6,7 @@ Machine.add("lib/Element.ck");
Machine.add("lib/List.ck");
Machine.add("lib/GenEvent.ck");
Machine.add("lib/NanoEvent.ck");
+Machine.add("lib/RumbleEvent.ck");
/* platform independant Clipper classes */
//Machine.add("lib/ClipperGraph.ck");
diff --git a/lib/RumbleEvent.ck b/lib/RumbleEvent.ck
new file mode 100644
index 0000000..0992bc0
--- /dev/null
+++ b/lib/RumbleEvent.ck
@@ -0,0 +1,105 @@
+/*
+ * "Logitech Rumble Gamepad" event class
+ */
+public class RumbleEvent extends GenEvent {
+ static string @__axisToName[]; /* pseudo-private */
+ static string @__buttonToName[]; /* pseudo-private */
+
+ class Port extends Step {
+ fun void
+ fetch(int device, string wantControl)
+ {
+ Hid hid;
+
+ if (!hid.openJoystick(device)) {
+ cherr <= "Cannot open joystick device " <= device
+ <= IO.newline();
+ me.exit();
+ }
+ chout <= "Opened joystick device " <= device <= " (" <= hid.name() <= ")"
+ <= IO.newline();
+
+ while (hid => now) {
+ while (HidMsg msg => hid.recv) {
+ string control;
+ float value;
+
+ if (msg.isAxisMotion()) {
+ RumbleEvent.__axisToName[msg.which] => control;
+ msg.axisPosition => value;
+ } else if (msg.isButtonDown()) {
+ RumbleEvent.__buttonToName[msg.which] => control;
+ 1 => value;
+ } else if (msg.isButtonUp()) {
+ RumbleEvent.__buttonToName[msg.which] => control;
+ -1 => value;
+ }
+
+ if (control == wantControl)
+ value => next;
+ }
+ }
+ /* never reached */
+ }
+ }
+
+ /* should be "Generic X-Box pad" */
+ 0 => int device;
+
+ string control;
+
+ fun int
+ isControl(string c)
+ {
+ return control == c;
+ }
+
+ fun Step @
+ getPort(string control)
+ {
+ Port p;
+ spork ~ p.fetch(device, control);
+ return p;
+ }
+
+ fun void
+ __hid_loop() /* pseudo-private */
+ {
+ Hid hid;
+
+ if (!hid.openJoystick(device)) {
+ cherr <= "Cannot open joystick device " <= device
+ <= IO.newline();
+ me.exit();
+ }
+ chout <= "Opened joystick device " <= device <= " (" <= hid.name() <= ")"
+ <= IO.newline();
+
+ while (hid => now) {
+ while (HidMsg msg => hid.recv) {
+ if (msg.isAxisMotion()) {
+ __axisToName[msg.which] => control;
+ (msg.axisPosition+1)/2 => value;
+ } else if (msg.isButtonDown()) {
+ __buttonToName[msg.which] => control;
+ true => value;
+ } else if (msg.isButtonUp()) {
+ __buttonToName[msg.which] => control;
+ false => value;
+ }
+
+ if (control != "")
+ broadcast();
+ }
+ }
+ /* never reached */
+ }
+ spork ~ __hid_loop();
+}
+/* static initialization */
+["leftJoystickX", "leftJoystickY", "leftButton",
+ "rightJoystickX", "rightJoystickY", "rightButton",
+ "cursorX", "cursorY"] @=> RumbleEvent.__axisToName;
+["buttonA", "buttonB", "buttonX", "buttonY",
+ "buttonLB", "buttonRB",
+ "buttonStart", "buttonBack"] @=> RumbleEvent.__buttonToName;