<commit_before>#!/usr/bin/env python

# The MIT License (MIT)
#
# Copyright (c) 2015 <NAME> <<EMAIL>>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

"""Steam Controller XBOX360 Gamepad Emulator"""

from steamcontroller import \
    SteamController, \
    SCStatus, \
    SCButtons

import steamcontroller.uinput
import steamcontroller.tools

from steamcontroller.uinput import Keys
from steamcontroller.uinput import 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_COUNT_FILTER = 4

@static_vars(count_filter=0, prev_btn=0)
def lpad_func(x, btn, threshold, evstick, evtouch, clicked, invert):

    rmv = lpad_func.prev_btn ^ btn
    lpad_func.prev_btn = btn

    events = []

    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.count_filter -= 1
            if lpad_func.count_filter <= 0:
                events.append((evtouch, 0, False))
        else:
            lpad_func.count_filter = LPAD_OUT_COUNT_FILTER
            if invert:
                events.append((evtouch, 1 if x > 0 else -1, True))
            else:
                events.append((evtouch, 1 if x < 0 else -1, True))

    elif ((clicked and rmv & SCButtons.LPad == SCButtons.LPad) or
        (not clicked and rmv & SCButtons.LPadTouch == SCButtons.LPadTouch)):
        lpad_func.count_filter -= 1
        if lpad_func.count_filter <= 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(x, btn, 15000, Axes.ABS_X, Axes.ABS_HAT0X, False, False),
    'lpad_y' : lambda x, btn: lpad_func(x, btn, 15000, Axes.ABS_Y, Axes.ABS_HAT0Y, False, True),<commit_msg>Correct a bug introduced with dpad filter<commit_after>#!/usr/bin/env python

# The MIT License (MIT)
#
# Copyright (c) 2015 <NAME> <<EMAIL>>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

"""Steam Controller XBOX360 Gamepad Emulator"""

from steamcontroller import \
    SteamController, \
    SCStatus, \
    SCButtons

import steamcontroller.uinput
import steamcontroller.tools

from steamcontroller.uinput import Keys
from steamcontroller.uinput import 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_COUNT_FILTER = 2

@static_vars(count_filter=0, prev_btn=0)
def lpad_func(x, btn, threshold, evstick, evtouch, clicked, invert):

    rmv = lpad_func.prev_btn ^ btn
    lpad_func.prev_btn = btn

    events = []

    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.count_filter -= 1
            if lpad_func.count_filter <= 0:
                events.append((evtouch, 0, False))
        else:
            if invert:
                events.append((evtouch, 1 if x > 0 else -1, lpad_func.count_filter <= 0))
            else:
                events.append((evtouch, 1 if x < 0 else -1, lpad_func.count_filter <= 0))
            lpad_func.count_filter = LPAD_OUT_COUNT_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.count_filter -= 1
        if lpad_func.count_filter <= 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(x, btn, 15000, Axes.ABS_X, Axes.ABS_HAT0X, False, False),
    'lpad_y' : lambda x, btn: lpad_func(x, btn, 15000, Axes.ABS_Y, Axes.ABS_HAT0Y, False, True),
{'fraction_non_alphanumeric': 0.09398343066684775, 'fraction_numerical': 0.009642808637783513, 'mean_word_length': 3.132435465768799, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3903986', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2378, 'n_words': 758}