diff options
author | Stany MARCEL <stanypub@gmail.com> | 2015-11-18 03:48:09 +0300 |
---|---|---|
committer | Stany MARCEL <stanypub@gmail.com> | 2015-11-18 03:48:09 +0300 |
commit | 702cf4dbfaaba0718853ce37273dd7a2e2c9dfbc (patch) | |
tree | ddfae14fe7bc2e68e000fff971d7676eff8d0455 /scripts | |
parent | e1734bc4338f6b1026b78183917e8fd43f8696ed (diff) |
Rewrite xbox script with new event mapper
Signed-off-by: Stany MARCEL <stanypub@gmail.com>
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/sc-xbox.py | 164 |
1 files changed, 37 insertions, 127 deletions
diff --git a/scripts/sc-xbox.py b/scripts/sc-xbox.py index 0e1b9d7..40de1d0 100755 --- a/scripts/sc-xbox.py +++ b/scripts/sc-xbox.py @@ -26,137 +26,46 @@ from steamcontroller import \ SteamController, \ - SCStatus, \ SCButtons - -import steamcontroller.uinput -import steamcontroller.tools - -from steamcontroller.uinput import Keys -from steamcontroller.uinput import Axes +from steamcontroller.events import \ + EventMapper, \ + Pos +from steamcontroller.uinput import \ + Keys, \ + Axes from steamcontroller.daemon import Daemon -from steamcontroller.tools import static_vars - -button_map = { - SCButtons.A : Keys.BTN_A, - SCButtons.B : Keys.BTN_B, - SCButtons.X : Keys.BTN_X, - SCButtons.Y : Keys.BTN_Y, - SCButtons.LB : Keys.BTN_TL, - SCButtons.RB : Keys.BTN_TR, - SCButtons.BACK : Keys.BTN_SELECT, - SCButtons.START : Keys.BTN_START, - SCButtons.STEAM : Keys.BTN_MODE, - SCButtons.LPAD : Keys.BTN_THUMBL, - SCButtons.RPAD : Keys.BTN_THUMBR, - SCButtons.LGRIP : Keys.BTN_A, - SCButtons.RGRIP : Keys.BTN_B, -} - -LPAD_OUT_FILTER = 6 -LPAD_FB_FILTER = 20 - -@static_vars(out_flt=[0, 0], fb_flt=0, prev_btn=0) -def lpad_func(idx, x, btn, threshold, evstick, evtouch, clicked, invert): - - rmv = lpad_func.prev_btn ^ btn - lpad_func.prev_btn = btn - - events = [] - lpad_func.fb_flt -= 1 - - if btn & SCButtons.LPADTOUCH != SCButtons.LPADTOUCH: - events.append((evstick, x if not invert else -x, False)) - - if (clicked and (btn & (SCButtons.LPAD | SCButtons.LPADTOUCH)) == (SCButtons.LPAD | SCButtons.LPADTOUCH) or - not clicked and (btn & SCButtons.LPADTOUCH == SCButtons.LPADTOUCH)): - - if x >= -threshold and x <= threshold: - # dead zone - lpad_func.out_flt[idx] -= 1 - if lpad_func.out_flt[idx] <= 0: - events.append((evtouch, 0, False)) - else: - - feedback = (lpad_func.fb_flt <= 0 and lpad_func.out_flt[idx] <= 0) - if invert: - events.append((evtouch, 1 if x < 0 else -1, feedback)) - else: - events.append((evtouch, 1 if x > 0 else -1, feedback)) - if feedback: - lpad_func.fb_flt = LPAD_FB_FILTER - lpad_func.out_flt[idx] = LPAD_OUT_FILTER - - if clicked and rmv & SCButtons.LPAD == SCButtons.LPAD: - events.append((evtouch, 0, False)) - - if not clicked and btn & SCButtons.LPADTOUCH != SCButtons.LPADTOUCH: - lpad_func.out_flt[idx] -= 1 - if lpad_func.out_flt[idx] <= 0: - events.append((evtouch, 0, False)) - - return events - -axis_map = { - 'ltrig' : lambda x, btn: [(Axes.ABS_Z, x, False)], - 'rtrig' : lambda x, btn: [(Axes.ABS_RZ, x, False)], - 'lpad_x' : lambda x, btn: lpad_func(0, x, btn, 20000, Axes.ABS_X, Axes.ABS_HAT0X, False, False), - 'lpad_y' : lambda x, btn: lpad_func(1, x, btn, 20000, Axes.ABS_Y, Axes.ABS_HAT0Y, False, True), - 'rpad_x' : lambda x, btn: [(Axes.ABS_RX, x, False)], - 'rpad_y' : lambda x, btn: [(Axes.ABS_RY, -x, False)], -} - -@static_vars(prev_buttons=0, prev_key_events=set(), prev_abs_events=set()) -def scInput2Uinput(sc, sci, xb): - - if sci.status != SCStatus.INPUT: - return - - removed = scInput2Uinput.prev_buttons ^ sci.buttons - - key_events = [] - abs_events = [] - - for btn, ev in button_map.items(): - - if btn == SCButtons.LPAD and sci.buttons & SCButtons.LPADTOUCH: - key_events.append((ev, 0)) - else: - if sci.buttons & btn: - key_events.append((ev, 1)) - elif removed & btn: - key_events.append((ev, 0)) - - for name, func in axis_map.items(): - for ev, val, feedback in func(sci._asdict()[name], sci.buttons): - if ev != None: - abs_events.append((ev, val, name if feedback else None)) - - - - new = False - for ev in key_events: - if ev not in scInput2Uinput.prev_key_events: - xb.keyEvent(*ev) - new = True - - for ev in abs_events: - if ev not in scInput2Uinput.prev_abs_events: - xb.axisEvent(*ev[:2]) - sc.addFeedback(ev[2]) - new = True - if new: - xb.synEvent() - - scInput2Uinput.prev_key_events = set(key_events) - scInput2Uinput.prev_abs_events = set(abs_events) - scInput2Uinput.prev_buttons = sci.buttons +def evminit(): + evm = EventMapper() + + evm.setStickAxes(Axes.ABS_X, Axes.ABS_Y) + evm.setPadAxes(Pos.RIGHT, Axes.ABS_X, Axes.ABS_Y) + evm.setPadAxesAsButtons(Pos.LEFT, [Axes.ABS_HAT0X, + Axes.ABS_HAT0Y]) + + evm.setTrigAxis(Pos.LEFT, Axes.ABS_Z) + evm.setTrigAxis(Pos.RIGHT, Axes.ABS_RZ) + + evm.setButtonAction(SCButtons.A, Keys.BTN_A) + evm.setButtonAction(SCButtons.B, Keys.BTN_B) + evm.setButtonAction(SCButtons.X, Keys.BTN_X) + evm.setButtonAction(SCButtons.Y, Keys.BTN_Y) + evm.setButtonAction(SCButtons.LB, Keys.BTN_TL) + evm.setButtonAction(SCButtons.RB, Keys.BTN_TR) + evm.setButtonAction(SCButtons.BACK, Keys.BTN_SELECT) + evm.setButtonAction(SCButtons.START, Keys.BTN_START) + evm.setButtonAction(SCButtons.STEAM, Keys.BTN_MODE) + evm.setButtonAction(SCButtons.LPAD, Keys.BTN_THUMBL) + evm.setButtonAction(SCButtons.RPAD, Keys.BTN_THUMBR) + evm.setButtonAction(SCButtons.LGRIP, Keys.BTN_A) + evm.setButtonAction(SCButtons.RGRIP, Keys.BTN_B) + + return evm class SCDaemon(Daemon): def run(self): - xb = steamcontroller.uinput.Gamepad() - sc = SteamController(callback=scInput2Uinput, callback_args=[xb, ]) + evm = evminit() + sc = SteamController(callback=evm.process) sc.run() if __name__ == '__main__': @@ -176,9 +85,10 @@ if __name__ == '__main__': daemon.restart() elif 'debug' == args.command: try: - xb = steamcontroller.uinput.Gamepad() - sc = SteamController(callback=scInput2Uinput, callback_args=[xb, ]) + evm = evminit() + sc = SteamController(callback=evm.process) sc.run() except KeyboardInterrupt: return + _main() |