ollie/data/tools/gui_shortcuts

121 lines
8.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# args_json: {"type":"object","required":["action"],"properties":{"action":{"type":"string","description":"list|trigger|get|set"},"component":{"type":"string","description":"Component name (e.g. kwin)"},"shortcut":{"type":"string","description":"Shortcut name"},"keys":{"type":"string","description":"Key binding (for set)"}}}
# ollie:prompt
# ## gui_shortcuts
#
# Manage KDE global shortcuts.
#
# **Args**: `[action, component, shortcut, keys]`
# - Actions: `list`, `trigger`, `get`, `set`
#
# ```
# gui_shortcuts(action="list")
# gui_shortcuts(action="trigger", component="kwin", shortcut="Window Close")
# ```
# ollie:end
set -euo pipefail
SERVICE="org.kde.kglobalaccel"
OBJ_PATH="/kglobalaccel"
IFACE="org.kde.KGlobalAccel"
# Also component interface
COMP_OBJ="/component"
COMP_IFACE="org.kde.kglobalaccel.Component"
source "$(dirname "$0")/_lib/args.sh"
cmd=$(arg_get "action" "list")
component=$(arg_get "component" "")
shortcut_name=$(arg_get "shortcut" "")
keys=$(arg_get "keys" "")
case "$cmd" in
list)
# List all registered shortcut components
components=$(gdbus call --session --dest "$SERVICE" \
--object-path "$OBJ_PATH" \
--method "$IFACE.allComponents" 2>/dev/null | \
sed "s/^(\[//;s/\],)$//;s/'//g;s/, /\n/g")
if [ -z "$components" ]; then
echo "ERROR: KGlobalAccel not responding" >&2
exit 1
fi
if [ -n "$component" ]; then
# List shortcuts for specific component
comp="$component"
echo "# Shortcuts for: $comp"
gdbus call --session --dest "$SERVICE" \
--object-path "/component/$comp" \
--method "$COMP_IFACE.shortcutNames" 2>/dev/null | \
sed "s/^(\[//;s/\],)$//;s/'//g;s/, /\n/g"
else
echo "$components"
fi
;;
get)
# Get key binding for a shortcut
if [ $# -lt 2 ]; then echo '{"schema": {"type": "array", "items": [{"name": "action", "type": "string", "description": "list|get|trigger|set", "required": true}, {"name": "component", "type": "string", "description": "Component name (for get/trigger/set)", "required": false}, {"name": "shortcut", "type": "string", "description": "Shortcut name (for get/trigger/set)", "required": false}, {"name": "keys", "type": "string", "description": "Key combination (for set)", "required": false}], "examples": [["list"], ["get", "kwin", "switch window"], ["trigger", "kwin", "switch window"]]}}'; exit 1; fi
comp="$component"
if [ $# -lt 3 ]; then echo '{"schema": {"type": "array", "items": [{"name": "action", "type": "string", "description": "list|get|trigger|set", "required": true}, {"name": "component", "type": "string", "description": "Component name (for get/trigger/set)", "required": false}, {"name": "shortcut", "type": "string", "description": "Shortcut name (for get/trigger/set)", "required": false}, {"name": "keys", "type": "string", "description": "Key combination (for set)", "required": false}], "examples": [["list"], ["get", "kwin", "switch window"], ["trigger", "kwin", "switch window"]]}}'; exit 1; fi
name="${3}"
gdbus call --session --dest "$SERVICE" \
--object-path "$OBJ_PATH" \
--method "$IFACE.shortcut" \
"['$comp', '$name', '', '']" 2>/dev/null
;;
trigger)
# Trigger (invoke) a global shortcut by component/name
if [ $# -lt 2 ]; then echo '{"schema": {"type": "array", "items": [{"name": "action", "type": "string", "description": "list|get|trigger|set", "required": true}, {"name": "component", "type": "string", "description": "Component name (for get/trigger/set)", "required": false}, {"name": "shortcut", "type": "string", "description": "Shortcut name (for get/trigger/set)", "required": false}, {"name": "keys", "type": "string", "description": "Key combination (for set)", "required": false}], "examples": [["list"], ["get", "kwin", "switch window"], ["trigger", "kwin", "switch window"]]}}'; exit 1; fi
comp="$component"
if [ $# -lt 3 ]; then echo '{"schema": {"type": "array", "items": [{"name": "action", "type": "string", "description": "list|get|trigger|set", "required": true}, {"name": "component", "type": "string", "description": "Component name (for get/trigger/set)", "required": false}, {"name": "shortcut", "type": "string", "description": "Shortcut name (for get/trigger/set)", "required": false}, {"name": "keys", "type": "string", "description": "Key combination (for set)", "required": false}], "examples": [["list"], ["get", "kwin", "switch window"], ["trigger", "kwin", "switch window"]]}}'; exit 1; fi
name="${3}"
# First get the shortcut keys, then simulate them
# Or use the invokeShortcut method if available
gdbus call --session --dest "$SERVICE" \
--object-path "/component/$comp" \
--method "$COMP_IFACE.invokeShortcut" "$name" 2>/dev/null && {
echo "triggered: $comp/$name"
exit 0
}
echo "ERROR: could not trigger $comp/$name" >&2
exit 1
;;
set)
# Set key binding for a shortcut
if [ $# -lt 2 ]; then echo '{"schema": {"type": "array", "items": [{"name": "action", "type": "string", "description": "list|get|trigger|set", "required": true}, {"name": "component", "type": "string", "description": "Component name (for get/trigger/set)", "required": false}, {"name": "shortcut", "type": "string", "description": "Shortcut name (for get/trigger/set)", "required": false}, {"name": "keys", "type": "string", "description": "Key combination (for set)", "required": false}], "examples": [["list"], ["get", "kwin", "switch window"], ["trigger", "kwin", "switch window"]]}}'; exit 1; fi
comp="$component"
if [ $# -lt 3 ]; then echo '{"schema": {"type": "array", "items": [{"name": "action", "type": "string", "description": "list|get|trigger|set", "required": true}, {"name": "component", "type": "string", "description": "Component name (for get/trigger/set)", "required": false}, {"name": "shortcut", "type": "string", "description": "Shortcut name (for get/trigger/set)", "required": false}, {"name": "keys", "type": "string", "description": "Key combination (for set)", "required": false}], "examples": [["list"], ["get", "kwin", "switch window"], ["trigger", "kwin", "switch window"]]}}'; exit 1; fi
name="${3}"
if [ $# -lt 4 ]; then echo '{"schema": {"type": "array", "items": [{"name": "action", "type": "string", "description": "list|get|trigger|set", "required": true}, {"name": "component", "type": "string", "description": "Component name (for get/trigger/set)", "required": false}, {"name": "shortcut", "type": "string", "description": "Shortcut name (for get/trigger/set)", "required": false}, {"name": "keys", "type": "string", "description": "Key combination (for set)", "required": false}], "examples": [["list"], ["get", "kwin", "switch window"], ["trigger", "kwin", "switch window"]]}}'; exit 1; fi
keys="${4}"
gdbus call --session --dest "$SERVICE" \
--object-path "$OBJ_PATH" \
--method "$IFACE.setShortcut" \
"['$comp', '$name', '', '']" \
"[['$keys']]" 0 2>/dev/null && {
echo "set: $comp/$name = $keys"
exit 0
}
echo "ERROR: could not set shortcut" >&2
exit 1
;;
*)
echo '{"schema": {"type": "array", "items": [{"name": "action", "type": "string", "description": "list|get|trigger|set", "required": true}, {"name": "component", "type": "string", "description": "Component name (for get/trigger/set)", "required": false}, {"name": "shortcut", "type": "string", "description": "Shortcut name (for get/trigger/set)", "required": false}, {"name": "keys", "type": "string", "description": "Key combination (for set)", "required": false}], "examples": [["list"], ["get", "kwin", "switch window"], ["trigger", "kwin", "switch window"]]}}' >&2
echo "" >&2
echo "Examples:" >&2
echo " gui_shortcuts list - list all components" >&2
echo " gui_shortcuts list kwin - list kwin shortcuts" >&2
echo " gui_shortcuts trigger kwin 'Walk Through Windows' - trigger Alt+Tab" >&2
echo " gui_shortcuts get kwin 'Walk Through Windows'" >&2
exit 1
;;
esac