~mil/sxmo-devel

sxmo-utils: V2 Tentative to modularize inputhandler v1 PROPOSED

Florent de Lamotte: 1
 V2 Tentative to modularize inputhandler

 18 files changed, 303 insertions(+), 235 deletions(-)
#1216705 test.yml failed
Hey,

Sorry for the delay. The time pass quickly..

I still consider it can be valuable to modularize inputhandler and
context menu. But the fact is that atm our migration process works well,
and is reliable. So to apply those refactorizations, we have to also
extends sxmo_migrate.sh to support this. In the same time, as you
said, the status-quo works well.

As always, those non-critical patches, and/or low-value
refactorisations, tends to stay queued in the ML for some time. They
stay available, if someone want to apply this on their tree. And
hopefully me, or another maintainer, would take the needed time to dig
into the topic, and improve over it to make the transition as smooth as
possible.
Hi,

Totally agree with you ... my work is available and can be carried on,
that is more than I ask ;)

And in the meantime I'll document it for the ones wanting to have it on
their side ...

Regards,
--
Florent
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~mil/sxmo-devel/patches/51757/mbox | git am -3
Learn more about email & git

[PATCH sxmo-utils] V2 Tentative to modularize inputhandler Export this patch

Hi,

Here is a new tentative, trying to take into account your comments. I
hope code quality is better, tried to be a little cleaner ;)

I have not yet dealt with migration as I'd need to study a little how
to set this up ...

The biggest change is the sourcing of action scripts, this simplifies a
lot of things and added some speed gains.

I have also tried different approaches for the loop :
 1- for action_path in "$action_dir"/*; do
 2- find "$action_dir" -type f | while read -r action_path; do
 3- for action_path in $(find "$action_dir" -type f); do

The first is the fastest (108ms on motog3) but might introduce other
issues (one is the handling of empty directories, and subdirectories)

The second was proposed by Stacy and I liked it much, it is slower
(116ms). One big issue is that it spawn a subshell where the action
files are executed, so the exit does not exit the parent shell and it
continues evaluating global actions (there were noticeable side-effects). 
It can surely be handled but complicates things ...

The third one has the disavantages of 1 and 2, it is slower (same as 2) 
and uses a for loop. But I found it to be a "good" compromise after all.

As said in my previous mail, I went for changing the name of directories
and actions for the sake of simplicity ...

Regards,
--
Florent

---
 .../default_hooks/sxmo_hook_inputhandler.sh   | 259 ++----------------
 configs/inputhandler.d/terminal               |  28 ++
 configs/inputhandler.d/wmclass/acme           |  12 +
 configs/inputhandler.d/wmclass/foot           |   3 +
 configs/inputhandler.d/wmclass/mpv            |  20 ++
 configs/inputhandler.d/wmclass/st             |  68 +++++
 configs/inputhandler.d/wmclass/terminal       |   3 +
 configs/inputhandler.d/wmclass/vte            |   3 +
 configs/inputhandler.d/wmname/amfora          |  28 ++
 configs/inputhandler.d/wmname/epr             |  16 ++
 configs/inputhandler.d/wmname/epy             |   3 +
 configs/inputhandler.d/wmname/less            |  20 ++
 configs/inputhandler.d/wmname/lf              |  12 +
 configs/inputhandler.d/wmname/mpv             |  20 ++
 configs/inputhandler.d/wmname/nnn             |  12 +
 configs/inputhandler.d/wmname/sms             |  10 +
 configs/inputhandler.d/wmname/tuir            |   9 +
 configs/inputhandler.d/wmname/weechat         |  12 +
 18 files changed, 303 insertions(+), 235 deletions(-)
 create mode 100644 configs/inputhandler.d/terminal
 create mode 100644 configs/inputhandler.d/wmclass/acme
 create mode 100644 configs/inputhandler.d/wmclass/foot
 create mode 100644 configs/inputhandler.d/wmclass/mpv
 create mode 100644 configs/inputhandler.d/wmclass/st
 create mode 100644 configs/inputhandler.d/wmclass/terminal
 create mode 100644 configs/inputhandler.d/wmclass/vte
 create mode 100644 configs/inputhandler.d/wmname/amfora
 create mode 100644 configs/inputhandler.d/wmname/epr
 create mode 100644 configs/inputhandler.d/wmname/epy
 create mode 100644 configs/inputhandler.d/wmname/less
 create mode 100644 configs/inputhandler.d/wmname/lf
 create mode 100644 configs/inputhandler.d/wmname/mpv
 create mode 100644 configs/inputhandler.d/wmname/nnn
 create mode 100644 configs/inputhandler.d/wmname/sms
 create mode 100644 configs/inputhandler.d/wmname/tuir
 create mode 100644 configs/inputhandler.d/wmname/weechat

diff --git a/configs/default_hooks/sxmo_hook_inputhandler.sh b/configs/default_hooks/sxmo_hook_inputhandler.sh
index b0bba6d..5db7acd 100755
--- a/configs/default_hooks/sxmo_hook_inputhandler.sh
+++ b/configs/default_hooks/sxmo_hook_inputhandler.sh
@@ -15,9 +15,30 @@ stop_proximity_lock() {
	sxmo_jobs.sh stop proximity_lock
}

run_actions() {
	action_id="$1"
	action_dir="$2"

	if [ -d "$action_dir" ] && [ -n "$action_id" ]; then
		for action_path in $(find "$action_dir" -type f); do
			action_name="${action_path##*/}"
			# Checks if $action_id contains $action_name
			# by removing $action_name (and anything before and
			# after) from $action_id
			if [ -z "${action_id##*"$action_name"*}" ]; then
				. "$action_path"
				return 1
			fi
		done
	fi
	return 0
}

XPROPOUT="$(sxmo_wm.sh focusedwindow)"
WMCLASS="$(printf %s "$XPROPOUT" | grep app: | cut -d" " -f2- | tr '[:upper:]' '[:lower:]')"
WMNAME="$(printf %s "$XPROPOUT" | grep title: | cut -d" " -f2- | tr '[:upper:]' '[:lower:]')"
HOME_INPUTHANDLER_D="$XDG_CONFIG_HOME"/sxmo/inputhandler.d
SXMO_INPUTHANDLER_D="$(xdg_data_path "sxmo/inputhandler.d")"

sxmo_debug "STATE: $(sxmo_state.sh get) ACTION: $ACTION WMCLASS: $WMCLASS WMNAME: $WMNAME"

@@ -75,241 +96,9 @@ if ! sxmo_state.sh get | grep -q unlock; then
	exit 0
fi

#special context-sensitive handling
case "$WMCLASS" in
	*"mpv"*)
		case "$ACTION" in
			"oneright")
				sxmo_type.sh -k Left
				exit 0
				;;
			"oneleft")
				sxmo_type.sh -k Right
				exit 0
				;;
			"oneup")
				sxmo_type.sh m
				exit 0
				;;
			"onedown")
				sxmo_type.sh p
				exit 0
				;;
		esac
		;;
	*"acme"*)
		case "$ACTION" in
			"volup_one")
				xdotool click 3
				exit 0
				;;
			"voldown_one")
				xdotool click 2
				exit 0
				;;
		esac
		;;
	*"foot"*|*"st"*|*"vte"*|"terminal") # Terminals
		case "$WMCLASS" in # Handle programs without touch support
			*"st"*)
				case "$WMNAME" in
					*"weechat"*|*'gomuks'*)
						case "$ACTION" in
							*"onedown")
								sxmo_type.sh -k Page_Up
								exit 0
								;;
							*"oneup")
								sxmo_type.sh -k Page_Down
								exit 0
								;;
						esac
						;;
					*"less"*|*"amfora"*)
						case "$ACTION" in
							*"onedown")
								sxmo_type.sh u
								exit 0
								;;
							*"oneup")
								sxmo_type.sh d
								exit 0
								;;
						esac
						;;
					*'irssi'*)
						case "$ACTION" in
							"onedown")
								sxmo_type.sh -M Alt p
								exit 0
								;;
							"oneup")
								sxmo_type.sh -M Alt n
								exit 0
								;;
						esac
						;;
					*'epy'*|*'epr'*)
						case "$ACTION" in
							"onedown")
								sxmo_type.sh h
								exit 0
								;;
							"oneup")
								sxmo_type.sh l
								exit 0
								;;
						esac
						;;
					*'nnn'*|'lf')
						case "$ACTION" in
							"onedown")
								sxmo_type.sh -k Down
								exit 0
								;;
							"oneup")
								sxmo_type.sh -k Up
								exit 0
								;;
						esac
						;;
				esac
				;;
		esac

		case "$WMNAME" in # Handle programs
			*"weechat"*)
				case "$ACTION" in
					*"oneleft")
						sxmo_type.sh -M Alt -k a
						exit 0
						;;
					*"oneright")
						sxmo_type.sh -M Alt -k less
						exit 0
						;;
				esac
				;;
			*" sms")
				case "$ACTION" in
					*"upbottomedge")
						number="$(printf %s "$WMNAME" | sed -e 's|^\"||' -e 's|\"$||' | cut -f1 -d' ')"
						sxmo_terminal.sh sxmo_modemtext.sh conversationloop "$number"
						exit 0
						;;
				esac
				;;
			*"tuir"*)
				if [ "$ACTION" = "rightbottomedge" ]; then
					sxmo_type.sh o
					exit 0
				elif [ "$ACTION" = "leftbottomedge" ]; then
					sxmo_type.sh s
					exit 0
				fi
				;;
			*"less"*)
				case "$ACTION" in
					"leftbottomedge")
						sxmo_type.sh q
						exit 0
						;;
					"leftrightedge_short")
						sxmo_type.sh q
						exit 0
						;;
					*"oneleft")
						sxmo_type.sh ":n" -k Return
						exit 0
						;;
					*"oneright")
						sxmo_type.sh ":p" -k Return
						exit 0
						;;
				esac
				;;
			*"amfora"*)
				case "$ACTION" in
					"downright")
						sxmo_type.sh -k Tab
						exit 0
						;;
					"upleft")
						sxmo_type.sh -M Shift -k Tab
						exit 0
						;;
					*"oneright")
						sxmo_type.sh -k Return
						exit 0
						;;
					"upright")
						sxmo_type.sh -M Ctrl t
						exit 0
						;;
					*"oneleft")
						sxmo_type.sh b
						exit 0
						;;
					"downleft")
						sxmo_type.sh -M Ctrl w
						exit 0
						;;
				esac
				;;
			*"epy"*|*"epr"*)
				case "$ACTION" in
					*"left"|"voldown_one")
						sxmo_type.sh l
						exit 0
						;;
					*"right"|"volup_one")
						sxmo_type.sh h
						exit 0
						;;
					"voldown_three"|"twodownbottomedge")
						sxmo_type.sh q
						exit
						;;
				esac
				;;
			*'nnn'*|'lf')
				case "$ACTION" in
					*"left")
						sxmo_type.sh -k Right
						exit 0
						;;
					*"right")
						sxmo_type.sh -k Left
						exit 0
						;;
				esac
				;;
		esac

		case "$WMCLASS" in # Handle general scrolling without touch support
			*"st"*)
				case "$ACTION" in
					*"onedown")
						sxmo_type.sh -M Ctrl -M Shift -k b
						exit 0
						;;
					*"oneup")
						sxmo_type.sh -M Ctrl -M Shift -k f
						exit 0
						;;
				esac
				;;
			"org.gnome.vte.application"|"terminal")
				case "$ACTION" in
					# For VTE, fallback to doing nothing,
					# you're probably dragging the scrollbar
					*"uprightedge") exit 0 ;;
					*"downrightedge") exit 0 ;;
				esac
				;;
		esac
		;;
esac
# Process actions for current application
run_actions "$WMCLASS" "$HOME_INPUTHANDLER_D"/wmclass &&\
run_actions "$WMCLASS" "$SXMO_INPUTHANDLER_D"/wmclass

#standard handling
case "$ACTION" in
diff --git a/configs/inputhandler.d/terminal b/configs/inputhandler.d/terminal
new file mode 100644
index 0000000..98c8bc1
--- /dev/null
+++ b/configs/inputhandler.d/terminal
@@ -0,0 +1,28 @@
#!/bin/sh

run_actions "$WMNAME" "$HOME_INPUTHANDLER_D"/wmname &&\
run_actions "$WMNAME" "$SXMO_INPUTHANDLER_D"/wmname

# Now we try generic actions for terminal
case "$WMCLASS" in # Handle general scrolling without touch support
	*"st"*)
		case "$ACTION" in
			*"onedown")
				sxmo_type.sh -M Ctrl -M Shift -k b
				exit 0
				;;
			*"oneup")
				sxmo_type.sh -M Ctrl -M Shift -k f
				exit 0
				;;
		esac
		;;
	"org.gnome.vte.application"|"terminal")
		case "$ACTION" in
			# For VTE, fallback to doing nothing,
			# you're probably dragging the scrollbar
			*"uprightedge") exit 0 ;;
			*"downrightedge") exit 0 ;;
		esac
		;;
esac
diff --git a/configs/inputhandler.d/wmclass/acme b/configs/inputhandler.d/wmclass/acme
new file mode 100644
index 0000000..e55c3d4
--- /dev/null
+++ b/configs/inputhandler.d/wmclass/acme
@@ -0,0 +1,12 @@
#/bin/sh

case "$ACTION" in
	"volup_one")
		xdotool click 3
		exit 0
		;;
	"voldown_one")
		xdotool click 2
		exit 0
		;;
esac
diff --git a/configs/inputhandler.d/wmclass/foot b/configs/inputhandler.d/wmclass/foot
new file mode 100644
index 0000000..8da44f4
--- /dev/null
+++ b/configs/inputhandler.d/wmclass/foot
@@ -0,0 +1,3 @@
#!/bin/sh

. "$SXMO_INPUTHANDLER_D"/terminal
diff --git a/configs/inputhandler.d/wmclass/mpv b/configs/inputhandler.d/wmclass/mpv
new file mode 100644
index 0000000..3813f82
--- /dev/null
+++ b/configs/inputhandler.d/wmclass/mpv
@@ -0,0 +1,20 @@
#!/bin/sh

case "$ACTION" in
	"oneright")
		sxmo_type.sh -k Left
		exit 0
		;;
	"oneleft")
		sxmo_type.sh -k Right
		exit 0
		;;
	"oneup")
		sxmo_type.sh m
		exit 0
		;;
	"onedown")
		sxmo_type.sh p
		exit 0
		;;
esac
diff --git a/configs/inputhandler.d/wmclass/st b/configs/inputhandler.d/wmclass/st
new file mode 100644
index 0000000..c60715b
--- /dev/null
+++ b/configs/inputhandler.d/wmclass/st
@@ -0,0 +1,68 @@
#!/bin/sh

# specific actions to st (for handling scrolling)
case "$WMNAME" in
	*"weechat"*|*'gomuks'*)
		case "$ACTION" in
			*"onedown")
				sxmo_type.sh -k Page_Up
				exit 0
				;;
			*"oneup")
				sxmo_type.sh -k Page_Down
				exit 0
				;;
		esac
		;;
	*"less"*|*"amfora"*)
		case "$ACTION" in
			*"onedown")
				sxmo_type.sh u
				exit 0
				;;
			*"oneup")
				sxmo_type.sh d
				exit 0
				;;
		esac
		;;
	*'irssi'*)
		case "$ACTION" in
			"onedown")
				sxmo_type.sh -M Alt p
				exit 0
				;;
			"oneup")
				sxmo_type.sh -M Alt n
				exit 0
				;;
		esac
		;;
	*'epy'*|*'epr'*)
		case "$ACTION" in
			"onedown")
				sxmo_type.sh h
				exit 0
				;;
			"oneup")
				sxmo_type.sh l
				exit 0
				;;
		esac
		;;
	*'nnn'*|'lf')
		case "$ACTION" in
			"onedown")
				sxmo_type.sh -k Down
				exit 0
				;;
			"oneup")
				sxmo_type.sh -k Up
				exit 0
				;;
		esac
		;;
esac

# handle generic terminal actions
. "$SXMO_INPUTHANDLER_D"/terminal
diff --git a/configs/inputhandler.d/wmclass/terminal b/configs/inputhandler.d/wmclass/terminal
new file mode 100644
index 0000000..8da44f4
--- /dev/null
+++ b/configs/inputhandler.d/wmclass/terminal
@@ -0,0 +1,3 @@
#!/bin/sh

. "$SXMO_INPUTHANDLER_D"/terminal
diff --git a/configs/inputhandler.d/wmclass/vte b/configs/inputhandler.d/wmclass/vte
new file mode 100644
index 0000000..8da44f4
--- /dev/null
+++ b/configs/inputhandler.d/wmclass/vte
@@ -0,0 +1,3 @@
#!/bin/sh

. "$SXMO_INPUTHANDLER_D"/terminal
diff --git a/configs/inputhandler.d/wmname/amfora b/configs/inputhandler.d/wmname/amfora
new file mode 100644
index 0000000..31d9245
--- /dev/null
+++ b/configs/inputhandler.d/wmname/amfora
@@ -0,0 +1,28 @@
#!/bin/sh

case "$ACTION" in
	"downright")
		sxmo_type.sh -k Tab
		exit 0
		;;
	"upleft")
		sxmo_type.sh -M Shift -k Tab
		exit 0
		;;
	*"oneright")
		sxmo_type.sh -k Return
		exit 0
		;;
	"upright")
		sxmo_type.sh -M Ctrl t
		exit 0
		;;
	*"oneleft")
		sxmo_type.sh b
		exit 0
		;;
	"downleft")
		sxmo_type.sh -M Ctrl w
		exit 0
		;;
esac
diff --git a/configs/inputhandler.d/wmname/epr b/configs/inputhandler.d/wmname/epr
new file mode 100644
index 0000000..36b6a4a
--- /dev/null
+++ b/configs/inputhandler.d/wmname/epr
@@ -0,0 +1,16 @@
#!/bin/sh

case "$ACTION" in
	*"left"|"voldown_one")
		sxmo_type.sh l
		exit 0
		;;
	*"right"|"volup_one")
		sxmo_type.sh h
		exit 0
		;;
	"voldown_three"|"twodownbottomedge")
		sxmo_type.sh q
		exit
		;;
esac
diff --git a/configs/inputhandler.d/wmname/epy b/configs/inputhandler.d/wmname/epy
new file mode 100644
index 0000000..5a824ce
--- /dev/null
+++ b/configs/inputhandler.d/wmname/epy
@@ -0,0 +1,3 @@
#!/bin/sh

. "$SXMO_INPUTHANDLER_D"/wmname/epr
diff --git a/configs/inputhandler.d/wmname/less b/configs/inputhandler.d/wmname/less
new file mode 100644
index 0000000..07daec7
--- /dev/null
+++ b/configs/inputhandler.d/wmname/less
@@ -0,0 +1,20 @@
#!/bin/sh

case "$ACTION" in
	"leftbottomedge")
		sxmo_type.sh q
		exit 0
		;;
	"leftrightedge_short")
		sxmo_type.sh q
		exit 0
		;;
	*"oneleft")
		sxmo_type.sh ":n" -k Return
		exit 0
		;;
	*"oneright")
		sxmo_type.sh ":p" -k Return
		exit 0
		;;
esac
diff --git a/configs/inputhandler.d/wmname/lf b/configs/inputhandler.d/wmname/lf
new file mode 100644
index 0000000..8ecd5e1
--- /dev/null
+++ b/configs/inputhandler.d/wmname/lf
@@ -0,0 +1,12 @@
#!/bin/sh

case "$ACTION" in
	*"left")
		sxmo_type.sh -k Right
		exit 0
		;;
	*"right")
		sxmo_type.sh -k Left
		exit 0
		;;
esac
diff --git a/configs/inputhandler.d/wmname/mpv b/configs/inputhandler.d/wmname/mpv
new file mode 100644
index 0000000..3813f82
--- /dev/null
+++ b/configs/inputhandler.d/wmname/mpv
@@ -0,0 +1,20 @@
#!/bin/sh

case "$ACTION" in
	"oneright")
		sxmo_type.sh -k Left
		exit 0
		;;
	"oneleft")
		sxmo_type.sh -k Right
		exit 0
		;;
	"oneup")
		sxmo_type.sh m
		exit 0
		;;
	"onedown")
		sxmo_type.sh p
		exit 0
		;;
esac
diff --git a/configs/inputhandler.d/wmname/nnn b/configs/inputhandler.d/wmname/nnn
new file mode 100644
index 0000000..8ecd5e1
--- /dev/null
+++ b/configs/inputhandler.d/wmname/nnn
@@ -0,0 +1,12 @@
#!/bin/sh

case "$ACTION" in
	*"left")
		sxmo_type.sh -k Right
		exit 0
		;;
	*"right")
		sxmo_type.sh -k Left
		exit 0
		;;
esac
diff --git a/configs/inputhandler.d/wmname/sms b/configs/inputhandler.d/wmname/sms
new file mode 100644
index 0000000..0b7804c
--- /dev/null
+++ b/configs/inputhandler.d/wmname/sms
@@ -0,0 +1,10 @@
#!/bin/sh

number="$(printf %s "$WMNAME" | sed -e 's|^\"||' -e 's|\"$||' | cut -f1 -d' ')"
case "$ACTION" in
	*"upbottomedge")
		number="$(printf %s "$WMNAME" | sed -e 's|^\"||' -e 's|\"$||' | cut -f1 -d' ')"
		sxmo_terminal.sh sxmo_modemtext.sh conversationloop "$number" &
		exit 0
		;;
esac
diff --git a/configs/inputhandler.d/wmname/tuir b/configs/inputhandler.d/wmname/tuir
new file mode 100644
index 0000000..2dc0e1b
--- /dev/null
+++ b/configs/inputhandler.d/wmname/tuir
@@ -0,0 +1,9 @@
#!/bin/sh

if [ "$ACTION" = "rightbottomedge" ]; then
	sxmo_type.sh o
	exit 0
elif [ "$ACTION" = "leftbottomedge" ]; then
	sxmo_type.sh s
	exit 0
fi
diff --git a/configs/inputhandler.d/wmname/weechat b/configs/inputhandler.d/wmname/weechat
new file mode 100644
index 0000000..7bd3c9c
--- /dev/null
+++ b/configs/inputhandler.d/wmname/weechat
@@ -0,0 +1,12 @@
#!/bin/sh

case "$ACTION" in
	*"oneleft")
		sxmo_type.sh -M Alt -k a
		exit 0
		;;
	*"oneright")
		sxmo_type.sh -M Alt -k less
		exit 0
		;;
esac
-- 
2.43.0
sxmo-utils/patches/test.yml: FAILED in 22s

[V2 Tentative to modularize inputhandler][0] from [Florent de Lamotte][1]

[0]: https://lists.sr.ht/~mil/sxmo-devel/patches/51757
[1]: mailto:florent@frizoncorrea.fr

✗ #1216705 FAILED sxmo-utils/patches/test.yml https://builds.sr.ht/~mil/job/1216705
Hi,

Just a friendly ping since I have had no response about this thread ;)

The build failed but it did not seem too bad (I've made it pass on my
side) :
 - one reason is from the "dynamic" sourcing of scripts
 - the other came from the for loop (and I still don't know which way is
   better for writing it)

In the last month I had some time to think about this refactoring and
while it is necessary on my phone because I have defined a lot of
gestures It might not be the case for the default install, where
gestures does not seem to be used a lot ...

So I am considering documenting this solution on the pmos wiki and let
people use it if they find some usage to it (it is easy to apply).

However I think this work could be used for the contextmenus where it
would be interesting already in the default install ... (but I don't use
contextmenus much and I have not felt the urge to do the test).

Regard,
--
Florent