diff options
Diffstat (limited to 'lib/RumbleEvent.ck')
-rw-r--r-- | lib/RumbleEvent.ck | 85 |
1 files changed, 20 insertions, 65 deletions
diff --git a/lib/RumbleEvent.ck b/lib/RumbleEvent.ck index 0992bc0..91aed80 100644 --- a/lib/RumbleEvent.ck +++ b/lib/RumbleEvent.ck @@ -5,63 +5,13 @@ 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" */ + /* + * Should be "Generic X-Box pad" + * Can be changed by constructor since __hid_loop() starts only + * when constructing shred passes time + */ 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 */ { @@ -72,24 +22,27 @@ public class RumbleEvent extends GenEvent { <= IO.newline(); me.exit(); } - chout <= "Opened joystick device " <= device <= " (" <= hid.name() <= ")" - <= IO.newline(); + chout <= "Opened joystick device " <= hid.num() + <= " (" <= hid.name() <= ")" <= IO.newline(); while (hid => now) { while (HidMsg msg => hid.recv) { if (msg.isAxisMotion()) { __axisToName[msg.which] => control; + /* normalize value [-1, 1] to [0, 1] */ (msg.axisPosition+1)/2 => value; - } else if (msg.isButtonDown()) { - __buttonToName[msg.which] => control; - true => value; - } else if (msg.isButtonUp()) { + } else if (msg.isButtonDown() || msg.isButtonUp()) { __buttonToName[msg.which] => control; - false => value; + msg.isButtonDown() => value; } - if (control != "") + if (control == "") { + cherr <= "Unknown joystick controller " <= msg.which + <= " (isAxisMotion=" <= msg.isAxisMotion() <= ")" + <= IO.newline(); + } else { broadcast(); + } } } /* never reached */ @@ -100,6 +53,8 @@ public class RumbleEvent extends GenEvent { ["leftJoystickX", "leftJoystickY", "leftButton", "rightJoystickX", "rightJoystickY", "rightButton", "cursorX", "cursorY"] @=> RumbleEvent.__axisToName; + ["buttonA", "buttonB", "buttonX", "buttonY", - "buttonLB", "buttonRB", - "buttonStart", "buttonBack"] @=> RumbleEvent.__buttonToName; + "buttonLB", "buttonRB", "buttonStart", + "", "", "", /* 7 to 9 are unused */ + "buttonBack"] @=> RumbleEvent.__buttonToName; |