Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_macro(self):
"""
Tests if Macro can be converted to string and parsed back to
same action.
"""
assert _parses_as_itself(Macro(
ButtonAction(Keys.BTN_LEFT),
ButtonAction(Keys.BTN_RIGHT),
ButtonAction(Keys.BTN_MIDDLE)
))
def test_dpad(self):
"""
Tests if DPadAction can be converted to string and
parsed back to same action.
"""
# Default diagonal rage
assert _parses_as_itself(DPadAction(
ButtonAction(Keys.BTN_LEFT),
ButtonAction(Keys.BTN_RIGHT),
ButtonAction(Keys.BTN_MIDDLE),
ButtonAction(Keys.KEY_A)
))
# Modified diagonal rage
assert _parses_as_itself(DPadAction(33,
ButtonAction(Keys.BTN_RIGHT),
ButtonAction(Keys.KEY_A),
ButtonAction(Keys.BTN_LEFT),
ButtonAction(Keys.BTN_MIDDLE),
))
from scc.gui.controller_widget import ControllerButton
from scc.gui.gdk_to_key import keyevent_to_key
from scc.gui.editor import Editor
from scc.actions import Action, ButtonAction, NoAction
from scc.macros import Macro, Repeat, SleepAction, PressAction, ReleaseAction
from scc.modifiers import ModeModifier
from scc.constants import SCButtons
from scc.profile import Profile
from scc.uinput import Keys
from gi.repository import Gtk, Gdk, GLib
import os, logging
log = logging.getLogger("KeyGrabber")
MODIFIERS = [ Keys.KEY_LEFTCTRL, Keys.KEY_LEFTMETA, Keys.KEY_LEFTALT,
Keys.KEY_RIGHTALT, Keys.KEY_RIGHTMETA, Keys.KEY_RIGHTCTRL,
Keys.KEY_LEFTSHIFT, Keys.KEY_RIGHTSHIFT
]
def merge_modifiers(mods):
return "+".join([ key.name.split("_")[-1] for key in mods ])
# Just to speed shit up, KeyGrabber is singleton
class KeyGrabber(object):
GLADE = "key_grabber.glade"
_singleton = None
def __new__(cls, *a):
if cls._singleton is None:
'Keys' : Keys,
'Axes' : Axes,
'Rels' : Rels,
'HapticPos' : HapticPos,
'None' : NoAction(),
'True' : True,
'False' : False,
}
for c in PARSER_CONSTANTS:
rv[c] = c
for tpl in (Keys, Axes, Rels, SCButtons, HapticPos):
for x in tpl:
rv[x.name] = x
for b in ("A", "B", "X", "Y", "START", "SELECT"):
name = "BTN_%s" % (b,)
rv[name] = getattr(Keys, name)
return rv
'button_back_right' : SCButtons.RGRIP,
'button_menu' : SCButtons.BACK,
'button_escape' : SCButtons.START, # what what what
'left_bumper' : SCButtons.LB,
'right_bumper' : SCButtons.RB,
'left_click' : SCButtons.LPAD,
'right_click' : SCButtons.RPAD,
}
SPECIAL_KEYS = {
# Maps some key names from vdf file to Keys.* constants.
# Rest of key names are converted in convert_key_name.
'FORWARD_SLASH' : Keys.KEY_SLASH,
'VOLUME_DOWN' : Keys.KEY_VOLUMEDOWN,
'VOLUME_UP' : Keys.KEY_VOLUMEUP,
'NEXT_TRACK' : Keys.KEY_NEXTSONG,
'PREV_TRACK' : Keys.KEY_PREVIOUSSONG,
'PAGE_UP' : Keys.KEY_PAGEUP,
'PAGE_DOWN' : Keys.KEY_PAGEDOWN,
'SINGLE_QUOTE' : Keys.KEY_APOSTROPHE,
'RETURN' : Keys.KEY_ENTER,
'ESCAPE' : Keys.KEY_ESC,
'PERIOD' : Keys.KEY_DOT,
'KEYPAD_DASH' : Keys.KEY_KPMINUS,
'KEYPAD_FORWARD_SLASH' : Keys.KEY_KPSLASH,
'LEFT_CONTROL' : Keys.KEY_LEFTCTRL,
'RIGHT_CONTROL' : Keys.KEY_RIGHTCTRL,
}
SPECIAL_BUTTONS = {
# As SPECIAL_KEYS, but for buttons.
'shoulder_left' : Keys.BTN_TL,
def update(self):
cb = self.builder.get_object("cbActionType")
scl = self.builder.get_object("sclDiagonalRange")
key = cb.get_model().get_value(cb.get_active_iter(), 1)
if key == "dpad8":
# 8-way dpad
self.editor.set_action(DPad8Action(scl.get_value(), *self.actions))
elif key == "dpad":
# 4-way dpad
self.editor.set_action(DPadAction(scl.get_value(),
*self.actions[0:4]))
elif key == "wsad":
# special case of 4-way dpad
a = DPadAction(scl.get_value(),
ButtonAction(Keys.KEY_W), ButtonAction(Keys.KEY_S),
ButtonAction(Keys.KEY_A), ButtonAction(Keys.KEY_D))
self.actions = [ NoAction() ] * 8
self.editor.set_action(a)
self.update_button_desc(a)
elif key == "arrows":
# special case of 4-way dpad
a = DPadAction(scl.get_value(),
ButtonAction(Keys.KEY_UP), ButtonAction(Keys.KEY_DOWN),
ButtonAction(Keys.KEY_LEFT), ButtonAction(Keys.KEY_RIGHT))
self.actions = [ NoAction() ] * 8
self.editor.set_action(a)
self.update_button_desc(a)
elif key == "actual_dpad":
# maps to dpad as real gamepad usually has
a = DPadAction(scl.get_value(),
HatUpAction(Axes.ABS_HAT0Y), HatDownAction(Axes.ABS_HAT0Y),
"""
if name in VDFProfile.SPECIAL_KEYS:
return VDFProfile.SPECIAL_KEYS[name]
elif name.endswith("_ARROW"):
key = "KEY_%s" % (name[:-6],)
elif "KEYPAD_" in name:
key = "KEY_%s" % (name.replace("KEYPAD_", "KP"),)
elif "LEFT_" in name:
key = "KEY_%s" % (name.replace("LEFT_", "LEFT"),)
elif "RIGHT_" in name:
key = "KEY_%s" % (name.replace("RIGHT_", "RIGHT"),)
else:
key = "KEY_%s" % (name,)
if hasattr(Keys, key):
return getattr(Keys, key)
if hasattr(Keys, key.upper()):
return getattr(Keys, key.upper())
raise ParseError("Unknown key: '%s'" % (name,))
def build_action_constants():
""" Generates dicts for ActionParser.CONSTS """
rv = {
'Keys' : Keys,
'Axes' : Axes,
'Rels' : Rels,
'HapticPos' : HapticPos,
'None' : NoAction(),
'True' : True,
'False' : False,
}
for c in PARSER_CONSTANTS:
rv[c] = c
for tpl in (Keys, Axes, Rels, SCButtons, HapticPos):
for x in tpl:
rv[x.name] = x
for b in ("A", "B", "X", "Y", "START", "SELECT"):
name = "BTN_%s" % (b,)
rv[name] = getattr(Keys, name)
return rv
def set_action(self, mode, action):
cb = self.builder.get_object("cbActionType")
if isinstance(action, CloseOSKAction):
self.set_cb(cb, "OSK.close()")
elif isinstance(action, OSKCursorAction) and action.side == LEFT:
self.set_cb(cb, "OSK.cursor(LEFT)")
elif isinstance(action, OSKCursorAction): # and action.side == RIGHT:
self.set_cb(cb, "OSK.cursor(RIGHT)")
elif isinstance(action, OSKPressAction) and action.side == LEFT:
self.set_cb(cb, "OSK.press(LEFT)")
elif isinstance(action, OSKPressAction): # and action.side == RIGHT:
self.set_cb(cb, "OSK.press(RIGHT)")
elif isinstance(action, MoveOSKAction):
self.set_cb(cb, "OSK.move()")
if isinstance(action, ButtonAction):
if action.button == Keys.BTN_LEFT:
self.set_cb(cb, "button(Keys.BTN_LEFT)")
elif action.button == Keys.BTN_RIGHT:
self.set_cb(cb, "button(Keys.BTN_RIGHT)")
else:
self.set_cb(cb, "None")
'ABS_RZ' : (AxisAction, Axes.ABS_RZ),
# Mouse
'MOUSE_LEFT' : (MouseAction, Rels.REL_X, -1),
'MOUSE_RIGHT' : (MouseAction, Rels.REL_X, 1),
'MOUSE_UP' : (MouseAction, Rels.REL_Y, -1),
'MOUSE_DOWN' : (MouseAction, Rels.REL_Y, 1,),
'MOUSE_X' : (MouseAction, Rels.REL_X, 1),
'MOUSE_Y' : (MouseAction, Rels.REL_Y, 1),
'MOUSE_WHEEL' : (MouseAction, Rels.REL_WHEEL, 1),
'MOUSE_HWHEEL' : (MouseAction, Rels.REL_HWHEEL, 1),
# Mouse buttons
'MOUSE1' : (ButtonAction, Keys.BTN_LEFT),
'MOUSE2' : (ButtonAction, Keys.BTN_MIDDLE),
'MOUSE3' : (ButtonAction, Keys.BTN_RIGHT),
'MOUSE4' : (MouseAction, Rels.REL_WHEEL, 1),
'MOUSE5' : (MouseAction, Rels.REL_WHEEL, -1),
'MOUSE8' : (ButtonAction, Keys.BTN_SIDE),
'MOUSE9' : (ButtonAction, Keys.BTN_EXTRA),
}
_CLS_TO_AREA = {}
for x in AREA_TO_ACTION:
cls, params = AREA_TO_ACTION[x][0], AREA_TO_ACTION[x][1:]
if not cls in _CLS_TO_AREA:
_CLS_TO_AREA[cls] = []
_CLS_TO_AREA[cls].append((x, params))
def action_to_area(action):
"""