Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to bind multiple actions to a single button combination. #147

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions ds4drv.conf
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ REL_WHEELDOWN = button_r1
#
# Sections contains:
# Key: A button combo
# Value: An action, see next section for valid actions.
# Value: A group of actions separated by the ';' delimeter, see next section
# for valid actions.
#
#
# Valid actions:
Expand All @@ -153,6 +154,13 @@ REL_WHEELDOWN = button_r1
# $device_addr Bluetooth address of the device
# $report.<attribute> Replace <attribute> with a valid attribute,
# use --dump-reports to see which are available
#
#
# Multiple actions can be configured for a single keypress. These actions
# should be separated with the semicolon character (';'). Note that if
# using the 'exec' or 'exec-background' actions you can not encode more
# than one console command as the semicolon would be interpretted as splitting
# the actions string.
##

[bindings]
Expand All @@ -167,7 +175,7 @@ REL_WHEELDOWN = button_r1

[bindings:exec_stuff]
# Execute a command in the foreground, blocking until it has finished
PS+Cross = exec echo '$name'
PS+Cross = exec echo '$name'; exec echo '$device_addr'

# Execute a command in the background
PS+Triangle = exec-background sh -c 'echo "disconnect $device_addr" | bluetoothctl'
Expand Down
31 changes: 16 additions & 15 deletions ds4drv/actions/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def load_options(self, options):
self.add_binding(self.controller.default_profile.profile_toggle,
lambda r: self.controller.next_profile())

def handle_binding_action(self, report, action):
def handle_binding_action(self, report, actions_raw):
info = dict(name=self.controller.device.name,
profile=self.controller.current_profile,
device_addr=self.controller.device.device_addr,
Expand All @@ -74,20 +74,21 @@ def replace_var(match):
var = getattr(var, attr, None)
return str(var)

action = re.sub(r"\$(?P<var>\w+)(\.(?P<attr>\w+))?",
replace_var, action)
action_split = shlex.split(action)
action_type = action_split[0]
action_args = action_split[1:]

func = self.actions.get(action_type)
if func:
try:
func(self.controller, *action_args)
except Exception as err:
self.logger.error("Failed to execute action: {0}", err)
else:
self.logger.error("Invalid action type: {0}", action_type)
actions = re.sub(r"\$(?P<var>\w+)(\.(?P<attr>\w+))?",
replace_var, actions_raw).split(";")
for sub_action in actions:
action_split = shlex.split(sub_action)
action_type = action_split[0]
action_args = action_split[1:]

func = self.actions.get(action_type)
if func:
try:
func(self.controller, *action_args)
except Exception as err:
self.logger.error("Failed to execute action: {0}", err)
else:
self.logger.error("Invalid action type: {0}", action_type)

def handle_report(self, report):
for binding in self.bindings:
Expand Down