def _convert(self, from_version): """ Performs conversion from older profile version """ if from_version < 1: from scc.modifiers import ModeModifier # Add 'display Default.menu if center button is held' for old profiles c = self.buttons[SCButtons.C] if not c: # Nothing set to C button self.buttons[SCButtons.C] = HoldModifier( MenuAction("Default.menu"), normalaction=MenuAction("Default.menu")) elif hasattr(c, "holdaction") and c.holdaction: # Already set to something, don't overwrite it pass elif c.to_string().startswith("OSK."): # Special case, don't touch this either pass else: self.buttons[SCButtons.C] = HoldModifier( MenuAction("Default.menu"), normalaction=self.buttons[SCButtons.C]) if from_version < 1.1: # Convert old scrolling wheel to new representation from scc.modifiers import FeedbackModifier, BallModifier from scc.actions import MouseAction, XYAction from scc.uinput import Rels iswheelaction = ( lambda x: isinstance(x, MouseAction) and x.parameters[0] in (Rels.REL_HWHEEL, Rels.REL_WHEEL)) for p in (Profile.LEFT, Profile.RIGHT): a, feedback = self.pads[p], None if isinstance(a, FeedbackModifier): feedback = a.haptic.get_position() a = a.action if isinstance(a, XYAction): if iswheelaction(a.x) or iswheelaction(a.y): n = BallModifier(XYAction(a.x, a.y)) if feedback is not None: n = FeedbackModifier(feedback, 4096, 16, n) self.pads[p] = n log.info("Converted %s to %s", a.to_string(), n.to_string()) if from_version < 1.2: # Convert old trigger settings that were done with ButtonAction # to new TriggerAction from scc.constants import TRIGGER_HALF, TRIGGER_MAX, TRIGGER_CLICK from scc.actions import ButtonAction, TriggerAction, MultiAction from scc.uinput import Keys for p in (Profile.LEFT, Profile.RIGHT): if isinstance(self.triggers[p], ButtonAction): buttons, numbers = [], [] n = None # There were one or two keys and zero to two numeric # parameters for old button action for param in self.triggers[p].parameters: if param in Keys: buttons.append(param) elif type(param) in (int, float): numbers.append(int(param)) if len(numbers) == 0: # Trigger range was not specified, assume defaults numbers = (TRIGGER_HALF, TRIGGER_CLICK) elif len(numbers) == 1: # Only lower range was specified, add default upper range numbers.append(TRIGGER_CLICK) if len(buttons) == 1: # If only one button was set, trigger should work like # one big button n = TriggerAction(numbers[0], ButtonAction(buttons[0])) elif len(buttons) == 2: # Both buttons were set n = MultiAction( TriggerAction(numbers[0], numbers[1], ButtonAction(buttons[0])), TriggerAction(numbers[1], TRIGGER_MAX, ButtonAction(buttons[1]))) if n: log.info("Converted %s to %s", self.triggers[p].to_string(), n.to_string()) self.triggers[p] = n
def send(self): self.editor.set_action(XYAction(self.x, self.y))
def parse_group(self, group, side): """ Parses output (group) from vdf profile. Returns Action. """ if not "mode" in group: raise ParseError("Group without mode") mode = group["mode"] inputs = VDFProfile.get_inputs(group) settings = group["settings"] if "settings" in group else {} for o in ("output_trigger", "output_joystick"): if o in settings: if int(settings[o]) <= 1: side = Profile.LEFT else: side = Profile.RIGHT if mode == "dpad": keys = [] for k in ("dpad_north", "dpad_south", "dpad_east", "dpad_west"): if k in inputs: keys.append(self.parse_button(inputs[k])) else: keys.append(NoAction()) action = DPadAction(*keys) elif mode == "four_buttons": keys = [] for k in ("button_y", "button_a", "button_x", "button_b"): if k in inputs: keys.append(self.parse_button(inputs[k])) else: keys.append(NoAction()) action = DPadAction(*keys) elif mode == "joystick_move": if side == Profile.LEFT: # Left action = XYAction(AxisAction(Axes.ABS_X), AxisAction(Axes.ABS_Y)) else: # Right action = XYAction(AxisAction(Axes.ABS_RX), AxisAction(Axes.ABS_RY)) elif mode == "joystick_camera": output_joystick = 0 if 'output_joystick' in settings: output_joystick = int(settings['output_joystick']) if output_joystick == 0: action = BallModifier( XYAction(AxisAction(Axes.ABS_X), AxisAction(Axes.ABS_Y))) elif output_joystick == 1: action = BallModifier( XYAction(AxisAction(Axes.ABS_RX), AxisAction(Axes.ABS_RY))) else: # TODO: Absolute mouse? Doesn't seems to do anything in Steam action = BallModifier( SensitivityModifier(0.1, 0.1, MouseAction())) elif mode == "mouse_joystick": action = BallModifier( XYAction(AxisAction(Axes.ABS_RX), AxisAction(Axes.ABS_RY))) elif mode == "scrollwheel": action = BallModifier( XYAction(MouseAction(Rels.REL_HWHEEL), MouseAction(Rels.REL_WHEEL))) elif mode == "touch_menu": # Touch menu is converted to GridMenu items = [] next_item_id = 1 for k in inputs: action = self.parse_button(inputs[k]) items.append( MenuItem("item_%s" % (next_item_id, ), action.describe(Action.AC_BUTTON), action)) next_item_id += 1 # Menu is stored in profile, with generated ID menu_id = "menu_%s" % (self.next_menu_id, ) self.next_menu_id += 1 self.menus[menu_id] = MenuData(*items) action = GridMenuAction( menu_id, 'LEFT' if side == Profile.LEFT else 'RIGHT', SCButtons.LPAD if side == Profile.LEFT else SCButtons.RPAD) elif mode == "absolute_mouse": if "click" in inputs: if side == Profile.LEFT: self.add_by_binding(SCButtons.LPAD, self.parse_button(inputs["click"])) else: self.add_by_binding(SCButtons.RPAD, self.parse_button(inputs["click"])) if "gyro_axis" in settings: if int(settings["gyro_axis"]) == 1: action = MouseAction(ROLL) else: action = MouseAction(YAW) else: action = MouseAction() elif mode == "mouse_wheel": action = BallModifier( XYAction(MouseAction(Rels.REL_HWHEEL), ouseAction(Rels.REL_WHEEL))) elif mode == "trigger": actions = [] if "click" in inputs: actions.append( TriggerAction(TRIGGER_CLICK, self.parse_button(inputs["click"]))) if side == Profile.LEFT: actions.append(AxisAction(Axes.ABS_Z)) else: actions.append(AxisAction(Axes.ABS_RZ)) action = MultiAction.make(*actions) elif mode == "mouse_region": # Read value and assume dafaults scale = float(settings["scale"]) if "scale" in settings else 100.0 x = float( settings["position_x"]) if "position_x" in settings else 50.0 y = float( settings["position_y"]) if "position_y" in settings else 50.0 w = float(settings["sensitivity_horiz_scale"] ) if "sensitivity_horiz_scale" in settings else 100.0 h = float(settings["sensitivity_vert_scale"] ) if "sensitivity_vert_scale" in settings else 100.0 # Apply scale w = w * scale / 100.0 h = h * scale / 100.0 # Convert to (0, 1) range x, y = x / 100.0, 1.0 - (y / 100.0) w, h = w / 100.0, h / 100.0 # Convert to rectangle x1 = max(0.0, x - (w * VDFProfile.REGION_IMPORT_FACTOR)) x2 = min(1.0, x + (w * VDFProfile.REGION_IMPORT_FACTOR)) y1 = max(0.0, y - (h * VDFProfile.REGION_IMPORT_FACTOR)) y2 = min(1.0, y + (h * VDFProfile.REGION_IMPORT_FACTOR)) action = RelAreaAction(x1, y1, x2, y2) else: raise ParseError("Unknown mode: '%s'" % (group["mode"], )) action = VDFProfile.parse_modifiers(group, action, side) return action
def from_json_data(self, data, key=None): """ Converts dict stored in profile file into action. May throw ParseError. """ if key is not None: # Don't fail if called for non-existent key, return NoAction instead. # Using this is sorter than # calling 'if button in data["buttons"]: ...' everywhere if key in data: return self.from_json_data(data[key], None) else: return NoAction() a = NoAction() if "action" in data: a = self.restart(data["action"]).parse() or NoAction() if "X" in data or "Y" in data: # "action" is ignored if either "X" or "Y" is there x = self.from_json_data(data["X"]) if "X" in data else NoAction() y = self.from_json_data(data["Y"]) if "Y" in data else NoAction() a = XYAction(x, y) if "deadzone" in data: lower = data["deadzone"]["lower"] if "lower" in data["deadzone"] else STICK_PAD_MIN upper = data["deadzone"]["upper"] if "upper" in data["deadzone"] else STICK_PAD_MAX a = DeadzoneModifier(lower, upper, a) if "sensitivity" in data: args = data["sensitivity"] args.append(a) a = SensitivityModifier(*args) if "feedback" in data: args = data["feedback"] if hasattr(HapticPos, args[0]): args[0] = getattr(HapticPos, args[0]) args.append(a) a = FeedbackModifier(*args) if "osd" in data: a = OSDAction(a) if data["osd"] is not True: a.timeout = float(data["osd"]) if "click" in data: a = ClickModifier(a) if "name" in data: a.name = data["name"] if "modes" in data: args = [] for button in data['modes']: if hasattr(SCButtons, button): args += [ getattr(SCButtons, button), self.from_json_data(data['modes'][button]) ] if a: args += [ a ] a = ModeModifier(*args) if "doubleclick" in data: args = [ self.from_json_data(data['doubleclick']), a ] a = DoubleclickModifier(*args) if "hold" in data: if isinstance(a, DoubleclickModifier): a.holdaction = self.from_json_data(data['hold']) else: args = [ self.from_json_data(data['hold']), a ] a = HoldModifier(*args) return a