Skip to content

Commit

Permalink
Merge pull request #100 from asus-linux-drivers/install-driver-under-…
Browse files Browse the repository at this point in the history
…current-user
  • Loading branch information
ldrahnik authored Feb 26, 2023
2 parents e0c2420 + 869d39d commit a846f32
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 43 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions asus_touchpad.X11.service
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 34 additions & 31 deletions asus_touchpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 <leftshift>+<leftctrl>+<U>+<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
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions asus_touchpad.service
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 24 additions & 8 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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


Expand All @@ -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
Expand All @@ -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"
Expand All @@ -231,15 +248,14 @@ 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
else
echo "Asus touchpad service started"
fi


CONF_FILE="/usr/share/asus_touchpad_numpad-driver/asus_touchpad_numpad_dev"

CONFIG_FILE_DIFF=""
Expand Down
16 changes: 13 additions & 3 deletions uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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..."
Expand Down

0 comments on commit a846f32

Please sign in to comment.