From 869d39d6ed8fa84c10b42794dcd963d02854fd6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Drahn=C3=ADk?= Date: Sun, 26 Feb 2023 23:27:51 +0100 Subject: [PATCH] Init --- README.md | 10 +++++- asus_touchpad.X11.service | 1 + asus_touchpad.py | 65 ++++++++++++++++++++------------------- asus_touchpad.service | 1 + install.sh | 32 ++++++++++++++----- uninstall.sh | 16 ++++++++-- 6 files changed, 82 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 2add993..3aa2600 100644 --- a/README.md +++ b/README.md @@ -61,13 +61,19 @@ Install latest dev version using `git` ```bash git clone https://github.com/asus-linux-drivers/asus-touchpad-numpad-driver cd asus-touchpad-numpad-driver +# install under current user (highly recommended) +sudo ./install.sh --user +# install as root sudo ./install.sh ``` or download latest release (stable version) from [release page](https://github.com/asus-linux-drivers/asus-touchpad-numpad-driver/releases), extract and run: ```bash +# install as root sudo ./install.sh +# install under current user (highly recommended) +sudo ./install.sh --user ``` or is available package for arch on AUR [here](https://aur.archlinux.org/packages?O=0&SeB=nd&K=asus-touchpad-numpad-driver&outdated=&SB=p&SO=d&PP=50&submit=Go) (replace model with available models, e.g. `asus-touchpad-numpad-driver-ux433fa-git`) @@ -81,7 +87,9 @@ paru -S asus-touchpad-numpad-driver-${model}-git And to uninstall, just run: ```bash -sudo ./uninstall.sh +sudo ./install.sh +# stop driver and uninstall for current user +sudo ./install.sh --user ``` ### Dependencies diff --git a/asus_touchpad.X11.service b/asus_touchpad.X11.service index 4fda42a..3cc81ba 100644 --- a/asus_touchpad.X11.service +++ b/asus_touchpad.X11.service @@ -3,6 +3,7 @@ Description=Asus Touchpad to Numpad Handler BindsTo=graphical.target [Service] +User=%i Type=idle ExecStart=/usr/bin/env python3 /usr/share/asus_touchpad_numpad-driver/asus_touchpad.py $LAYOUT $CONFIG_FILE_DIR StandardInput=tty-force diff --git a/asus_touchpad.py b/asus_touchpad.py index ad6f46a..ea210b3 100755 --- a/asus_touchpad.py +++ b/asus_touchpad.py @@ -319,6 +319,28 @@ def get_keycode_of_ascii_char(char): return keycode +def get_keycode_which_reflects_current_layout(char, reset_udev=True): + global enabled_keys_for_unicode_shortcut, udev, dev + + keycode = get_keycode_of_ascii_char(char) + key = EV_KEY.codes[int(keycode)] + if key not in enabled_keys_for_unicode_shortcut: + enabled_keys_for_unicode_shortcut.append(key) + dev.enable(key) + if reset_udev: + log.info("Old device at {} ({})".format(udev.devnode, udev.syspath)) + udev = dev.create_uinput_device() + log.info("New device at {} ({})".format(udev.devnode, udev.syspath)) + + # Sleep for a bit so udev, libinput, Xorg, Wayland, ... all have had + # a chance to see the device and initialize it. Otherwise the event + # will be sent by the kernel but nothing is ready to listen to the + # device yet + sleep(1) + + return key + + # Create a new keyboard device to send numpad events dev = Device() dev.name = "Asus Touchpad/Numpad" @@ -327,9 +349,6 @@ def get_keycode_of_ascii_char(char): dev.enable(EV_KEY.BTN_MIDDLE) dev.enable(EV_KEY.KEY_NUMLOCK) # predefined for all possible unicode characters +++<0-F> -# TODO: Wayland will stop working? -# Because python python-xlib - does support wayland? -# 2 versions of requirements.txt file? And propagate here x11/wayland so import will be not called? Or does not matter will be called? # standart is U # for FR is S @@ -354,12 +373,14 @@ def get_keycode_of_ascii_char(char): EV_KEY.KEY_F ] # for currently used keyboard -U_keycode = get_keycode_of_ascii_char("U") -U_key = EV_KEY.codes[int(U_keycode)] -if U_key not in enabled_keys_for_unicode_shortcut: - enabled_keys_for_unicode_shortcut.append(U_key) +try: + U_keycode = get_keycode_which_reflects_current_layout("U", False) +except: + pass + for key in enabled_keys_for_unicode_shortcut: dev.enable(key) + dev.enable(EV_KEY.KEY_LEFTSHIFT) dev.enable(EV_KEY.KEY_LEFTCTRL) dev.enable(EV_KEY.KEY_SPACE) @@ -793,27 +814,6 @@ def get_compose_key_end_events_for_unicode_string(): return events -def get_keycode_which_reflects_current_layout(char): - global enabled_keys_for_unicode_shortcut, udev, dev - - keycode = get_keycode_of_ascii_char(char) - key = EV_KEY.codes[int(keycode)] - if key not in enabled_keys_for_unicode_shortcut: - enabled_keys_for_unicode_shortcut.append(key) - dev.enable(key) - log.info("Old device at {} ({})".format(udev.devnode, udev.syspath)) - udev = dev.create_uinput_device() - log.info("New device at {} ({})".format(udev.devnode, udev.syspath)) - - # Sleep for a bit so udev, libinput, Xorg, Wayland, ... all have had - # a chance to see the device and initialize it. Otherwise the event - # will be sent by the kernel but nothing is ready to listen to the - # device yet - sleep(1) - - return key - - def get_compose_key_start_events_for_unicode_string(): global udev, dev @@ -857,9 +857,12 @@ def get_events_for_unicode_char(char): for hex_digit in '%X' % ord(char): - key = get_keycode_which_reflects_current_layout(hex_digit) - #key_code = getattr(ecodess, 'KEY_%s' % hex_digit) - #key = EV_KEY.codes[int(key_code)] + try: + key = get_keycode_which_reflects_current_layout(hex_digit) + except: + key = key_code = getattr(ecodess, 'KEY_%s' % hex_digit) + key = EV_KEY.codes[int(key_code)] + key_event_press = InputEvent(key, 1) key_event_unpress = InputEvent(key, 0) diff --git a/asus_touchpad.service b/asus_touchpad.service index a960fe0..1f176a1 100644 --- a/asus_touchpad.service +++ b/asus_touchpad.service @@ -2,6 +2,7 @@ Description=Asus Touchpad to Numpad Handler [Service] +User=%i Type=simple ExecStart=/usr/bin/env python3 /usr/share/asus_touchpad_numpad-driver/asus_touchpad.py $LAYOUT $CONFIG_FILE_DIR StandardInput=tty-force diff --git a/install.sh b/install.sh index 715f830..d630f75 100755 --- a/install.sh +++ b/install.sh @@ -6,6 +6,18 @@ if [[ $(id -u) != 0 ]]; then exit 1 fi +# "root" by default or when is used --user it is "current user" +RUN_UNDER_USER=$USER + +if [ "$1" = "--user" ]; then + groupadd "uinput" + echo 'KERNEL=="uinput", GROUP="uinput", MODE:="0660"' | sudo tee /etc/udev/rules.d/99-input.rules + RUN_UNDER_USER=$SUDO_USER + usermod -a -G "i2c,input,uinput" $RUN_UNDER_USER +fi + +echo "driver will run under user $RUN_UNDER_USER" + parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P ) # this works because sudo sets the environment variable SUDO_USER to the original username @@ -30,8 +42,7 @@ elif [[ $(dnf install 2>/dev/null) ]]; then fi fi -# TODO: run as non sudo: WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. -python3 -m pip install -r requirements.txt +runuser -u $RUN_UNDER_USER -- python3 -m pip install -r requirements.txt # Checking if the pip dependencies are successfuly loaded if [[ $? != 0 ]]; then @@ -189,16 +200,16 @@ if [ "$wayland_or_x11" = "x11" ]; then echo "X11 is detected" xauthority=$(/usr/bin/xauth info | grep Authority | awk '{print $3}') - cat asus_touchpad.X11.service | LAYOUT=$model CONFIG_FILE_DIR="/usr/share/asus_touchpad_numpad-driver/" XAUTHORITY=$xauthority envsubst '$LAYOUT $XAUTHORITY $CONFIG_FILE_DIR' > /etc/systemd/system/asus_touchpad_numpad.service + cat asus_touchpad.X11.service | USER=$RUN_UNDER_USER LAYOUT=$model CONFIG_FILE_DIR="/usr/share/asus_touchpad_numpad-driver/" XAUTHORITY=$xauthority envsubst '$LAYOUT $XAUTHORITY $CONFIG_FILE_DIR' > /etc/systemd/system/asus_touchpad_numpad@.service elif [ "$wayland_or_x11" = "wayland" ]; then echo "Wayland is detected, unfortunatelly you will not be able use feature: `Disabling Touchpad (e.g. Fn+special key) disables NumberPad aswell`, at this moment is supported only X11" - cat asus_touchpad.service | LAYOUT=$model CONFIG_FILE_DIR="/usr/share/asus_touchpad_numpad-driver/" envsubst '$LAYOUT $CONFIG_FILE_DIR' > /etc/systemd/system/asus_touchpad_numpad.service + cat asus_touchpad.service | USER=$RUN_UNDER_USER LAYOUT=$model CONFIG_FILE_DIR="/usr/share/asus_touchpad_numpad-driver/" envsubst '$LAYOUT $CONFIG_FILE_DIR' > /etc/systemd/system/asus_touchpad_numpad@.service else echo "Wayland or X11 is not detected" - cat asus_touchpad.service | LAYOUT=$model CONFIG_FILE_DIR="/usr/share/asus_touchpad_numpad-driver/" envsubst '$LAYOUT $CONFIG_FILE_DIR' > /etc/systemd/system/asus_touchpad_numpad.service + cat asus_touchpad.service | USER=$RUN_UNDER_USER LAYOUT=$model CONFIG_FILE_DIR="/usr/share/asus_touchpad_numpad-driver/" envsubst '$LAYOUT $CONFIG_FILE_DIR' > /etc/systemd/system/asus_touchpad_numpad@.service fi @@ -213,6 +224,12 @@ cp udev/90-numberpad-external-keyboard.rules /usr/lib/udev/rules.d/ echo "Added 90-numberpad-external-keyboard.rules" mkdir -p /usr/share/asus_touchpad_numpad-driver/udev + +chown -R $RUN_UNDER_USER /usr/share/asus_touchpad_numpad-driver +# 700: only owner can do everything (write, read, execute) +chmod -R 700 /usr/share/asus_touchpad_numpad-driver + +CONFIG_FILE_DIR="/usr/share/asus_touchpad_numpad-driver/" cat udev/external_keyboard_is_connected.sh | CONFIG_FILE_DIR="/usr/share/asus_touchpad_numpad-driver/" envsubst '$CONFIG_FILE_DIR' > /usr/share/asus_touchpad_numpad-driver/udev/external_keyboard_is_connected.sh cat udev/external_keyboard_is_disconnected.sh | CONFIG_FILE_DIR="/usr/share/asus_touchpad_numpad-driver/" envsubst '$CONFIG_FILE_DIR' > /usr/share/asus_touchpad_numpad-driver/udev/external_keyboard_is_disconnected.sh chmod +x /usr/share/asus_touchpad_numpad-driver/udev/external_keyboard_is_connected.sh @@ -222,7 +239,7 @@ udevadm control --reload-rules echo "i2c-dev" | tee /etc/modules-load.d/i2c-dev.conf >/dev/null -systemctl enable asus_touchpad_numpad +systemctl enable asus_touchpad_numpad@$RUN_UNDER_USER.service if [[ $? != 0 ]]; then echo "Something went wrong when enabling the asus_touchpad_numpad.service" @@ -231,7 +248,7 @@ else echo "Asus touchpad service enabled" fi -systemctl restart asus_touchpad_numpad +systemctl restart asus_touchpad_numpad@$RUN_UNDER_USER.service if [[ $? != 0 ]]; then echo "Something went wrong when enabling the asus_touchpad_numpad.service" exit 1 @@ -239,7 +256,6 @@ else echo "Asus touchpad service started" fi - CONF_FILE="/usr/share/asus_touchpad_numpad-driver/asus_touchpad_numpad_dev" CONFIG_FILE_DIFF="" diff --git a/uninstall.sh b/uninstall.sh index 2ca530c..eff8695 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -6,21 +6,31 @@ then exit 1 fi -systemctl stop asus_touchpad_numpad +# "root" by default or when is used --user it is "current user" +RUN_UNDER_USER=$USER + +if [ "$1" = "--user" ]; then + RUN_UNDER_USER=$SUDO_USER +fi + +echo "driver will be stopped and uninstalled for user" +echo $RUN_UNDER_USER + +systemctl stop asus_touchpad_numpad@$RUN_UNDER_USER.service if [[ $? != 0 ]] then echo "asus_touchpad_numpad.service cannot be stopped correctly..." exit 1 fi -systemctl disable asus_touchpad_numpad +systemctl disable asus_touchpad_numpad@$RUN_UNDER_USER.service if [[ $? != 0 ]] then echo "asus_touchpad_numpad.service cannot be disabled correctly..." exit 1 fi -rm -f /lib/systemd/system/asus_touchpad_numpad.service +rm -f /etc/systemd/system/asus_touchpad_numpad@.service if [[ $? != 0 ]] then echo "/lib/systemd/system/asus_touchpad_numpad.service cannot be removed correctly..."