Skip to content

Commit

Permalink
#8 Reverted considered screen orientation as it should be separated s…
Browse files Browse the repository at this point in the history
…ervice
  • Loading branch information
ldrahnik committed Feb 10, 2023
1 parent eded4b6 commit 1d6aa29
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 132 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ If you find the project useful, do not forget to give project a [![GitHub stars]

## Features

- Disable backlight of keyboard in tablet mode and use latest level of backlight when is mode changed back to laptop; NumberPad backlight does the same; CapsLock led too; history may be done for example via optional file [`brightness_hw_changed`](https://patchwork.kernel.org/project/platform-driver-x86/patch/[email protected]/) or temp file located in `/tmp`
- Disable backlight of keyboard in tablet mode and use latest level of backlight when is mode changed back to laptop mode; NumberPad backlight does the same; CapsLock led too; history may be done for example via optional file [`brightness_hw_changed`](https://patchwork.kernel.org/project/platform-driver-x86/patch/[email protected]/) or temp file located in `/tmp`
- When does not exist device `Intel HID switches` yet, driver will try find again every 5s and use for first flip event from `Asus WMI hotkeys` instead
- Customizable scripts for each display rotation (inverted, left-up, right-up and default)
- Touchpad rotates by default
- Configurable support of flip key mapping, by default `EV_KEY.KEY_PROG2`

<br/>
Expand Down
109 changes: 12 additions & 97 deletions asus_fliplock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import os
import re
import sys
import time
import subprocess
from typing import Optional
from libevdev import Device, EV_SW

Expand All @@ -24,18 +22,15 @@

fliplock_layouts = importlib.import_module('conf.'+ layout)

touchpad_name: Optional[str] = None
touchpad_id = 0
switches: Optional[str] = None
wmi_hotkeys: Optional[str] = None

touchpad_detected = 0
switches_detected = 0
wmi_hotkeys_detected = 0

def search_devices():

global touchpad_name, touchpad_id, switches, wmi_hotkeys
global switches, wmi_hotkeys
global touchpad_detected, switches_detected, wmi_hotkeys_detected

tries = 5
Expand All @@ -46,12 +41,6 @@ def search_devices():
with open('/proc/bus/input/devices', 'r') as f:
lines = f.readlines()
for line in lines:

# Look for the touchpad
if touchpad_detected == 0 and ("Name=\"ASUE" in line or "Name=\"ELAN" in line) and "Touchpad" in line:
touchpad_detected = 1
log.info('Detecting touchpad from string: \"%s\"', line.strip())
touchpad_name = line.split("\"")[1]

# Look for the device switches
if re.search("switches", line):
Expand Down Expand Up @@ -79,7 +68,7 @@ def search_devices():
log.debug('Set switches id %s from %s', switches, line.strip())
break

if switches_detected == 2 and wmi_hotkeys_detected == 2 and touchpad_detected == 1:
if switches_detected == 2 and wmi_hotkeys_detected == 2:
break

if switches_detected != 2:
Expand All @@ -100,6 +89,7 @@ def search_devices():
fd_t_wmi_hotkeys = open('/dev/input/event' + str(wmi_hotkeys), 'rb')
d_t_wmi_hotkeys = Device(fd_t_wmi_hotkeys)


def execute_cmd(cmd):
try:
os.system(cmd)
Expand All @@ -112,86 +102,12 @@ def execute_cmds_in_array(cmds):
execute_cmd(cmd)


def change_touchpad_orientation(orientation):
global touchpad_name, touchpad_id

transform_matrix = False
if orientation == "normal":
transform_matrix = "1 0 0 0 1 0 0 0 1"
elif orientation == "bottom-up":
transform_matrix = "-1 0 1 0 -1 1 0 0 1"
elif orientation == "right-up":
transform_matrix = "0 1 0 -1 0 1 0 0 1"
elif orientation == "left-up":
transform_matrix = "0 -1 1 1 0 0 0 0 1"

if transform_matrix:
transform_matrix_name = "Coordinate Transformation Matrix"
touchpad_id_regexp = "(?<=id=).*(?=\\t)"

try:
cmd_touchpad_id = "xinput | grep '" + touchpad_name + "'"
touchpad_row_with_id = subprocess.check_output(cmd_touchpad_id, shell=True)
touchpad_row_with_id_decoded = touchpad_row_with_id.decode()
matches = re.findall(touchpad_id_regexp, touchpad_row_with_id_decoded)
if matches:
touchpad_id = matches[0]

if touchpad_id:
if orientation != "bottom-up":
cmd_enable_touchpad = "xinput set-prop {} 'Device Enabled' 1".format(touchpad_id)
log.debug(cmd_enable_touchpad)
subprocess.check_output(cmd_enable_touchpad, shell=True)
else:
cmd_disable_touchpad = "xinput set-prop {} 'Device Enabled' 0".format(touchpad_id)
log.debug(cmd_disable_touchpad)
subprocess.check_output(cmd_disable_touchpad, shell=True)

cmd_rotate_touchpad = "xinput set-prop '" + touchpad_name + "' '" + transform_matrix_name + "' " + transform_matrix
log.debug(cmd_rotate_touchpad)
subprocess.check_output(cmd_rotate_touchpad, shell=True)
except subprocess.CalledProcessError as e:
log.error(e.output)


def execute_cmds_according_to_accelerometer_orientation():

# inverted when is orientation not recognized
orientation = "bottom-up"
orientation_regexp = "(?<=orientation: ).*?(?=\))"
def flip(intel_hid_switches_tablet_mode):
if intel_hid_switches_tablet_mode:
execute_cmds_in_array(fliplock_layouts.tablet_mode_actions)
else:
execute_cmds_in_array(fliplock_layouts.laptop_mode_actions)

try:
monitor_sensors = subprocess.Popen("monitor-sensor", shell=True, stdout=subprocess.PIPE)

for line in monitor_sensors.stdout:
utf8_line = line.decode()
matches = re.findall(orientation_regexp, utf8_line)
if matches:
orientation = matches[0]
log.debug(utf8_line)
break

except subprocess.CalledProcessError as e:
log.error(e.output)

# change by default matrix of touchpad
# run custom commands from conf
if orientation == "bottom-up":
change_touchpad_orientation(orientation)
execute_cmds_in_array(fliplock_layouts.orientation_bottom_up_actions)
elif orientation == "right-up":
change_touchpad_orientation(orientation)
execute_cmds_in_array(fliplock_layouts.orientation_right_up_actions)
elif orientation == "left-up":
change_touchpad_orientation(orientation)
execute_cmds_in_array(fliplock_layouts.orientation_left_up_actions)
elif orientation == "normal":
change_touchpad_orientation(orientation)
execute_cmds_in_array(fliplock_layouts.orientation_normal_actions)


# run for first time when is service started
execute_cmds_according_to_accelerometer_orientation()

# If mode has been changed, do something
if switches is None and wmi_hotkeys is not None:
Expand All @@ -203,9 +119,9 @@ def execute_cmds_according_to_accelerometer_orientation():
if switches is not None:
break

if e.matches(fliplock_layouts.flip_key):
time.sleep(2)
execute_cmds_according_to_accelerometer_orientation()
if e.matches(fliplock_layouts.flip_key) and e.value == 1:
intel_hid_switches_tablet_mode = 1
flip(intel_hid_switches_tablet_mode)

search_devices()

Expand All @@ -220,5 +136,4 @@ def execute_cmds_according_to_accelerometer_orientation():
log.debug(e)

if e.matches(EV_SW.SW_TABLET_MODE):
time.sleep(2)
execute_cmds_according_to_accelerometer_orientation()
flip(e.value)
29 changes: 8 additions & 21 deletions conf/default.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
from libevdev import EV_KEY

orientation_bottom_up_actions = [
"cat /sys/class/leds/asus::kbd_backlight/brightness | sudo tee /tmp/kbd_backlight_brightness",
"echo 0 | sudo tee /sys/class/leds/asus::kbd_backlight/brightness",
"cat /sys/class/leds/input3\:\:capslock/brightness | sudo tee /tmp/input3_capslock_brightness",
"echo 0 | sudo tee /sys/class/leds/input3\:\:capslock/brightness"
]

orientation_left_up_actions = [
"sudo rm -f /tmp/kbd_backlight_brightness",
"sudo rm -f /tmp/input3_capslock_brightness"
# TODO: enable touchpad (is disabled by default for every rotation probably)
#"xinput set-prop 17 'Device Enabled' 0"
]

orientation_right_up_actions = [
"sudo rm -f /tmp/kbd_backlight_brightness",
"sudo rm -f /tmp/input3_capslock_brightness",
# TODO: enable touchpad (is disabled by default for every rotation probably)
]

orientation_normal_actions = [
laptop_mode_actions = [
# backward setting up saved backlight brightness (only in case inverted was previous state! otherwise tmp file does not exist)
"test -f /tmp/kbd_backlight_brightness && cat /tmp/kbd_backlight_brightness | sudo tee /sys/class/leds/asus::kbd_backlight/brightness",
# backward setting up saved capslock led (only in case inverted was previous state! otherwise tmp file does not exist)
"test -f /tmp/input3_capslock_brightness && cat /tmp/input3_capslock_brightness | sudo tee /sys/class/leds/input3\:\:capslock/brightness"
]

tablet_mode_actions = [
"cat /sys/class/leds/asus::kbd_backlight/brightness | sudo tee /tmp/kbd_backlight_brightness",
"echo 0 | sudo tee /sys/class/leds/asus::kbd_backlight/brightness",
"cat /sys/class/leds/input3\:\:capslock/brightness | sudo tee /tmp/input3_capslock_brightness",
"echo 0 | sudo tee /sys/class/leds/input3\:\:capslock/brightness"
]

flip_key = EV_KEY.KEY_PROG2
16 changes: 5 additions & 11 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,11 @@ then
fi

if [[ $(apt install 2>/dev/null) ]]; then
echo 'apt is here' && apt -y install libevdev2 python3-libevdev iio-sensor-proxy
echo 'apt is here' && apt -y install libevdev2 python3-libevdev
elif [[ $(pacman -h 2>/dev/null) ]]; then
echo 'pacman is here' && pacman --noconfirm -S libevdev python-libevdev iio-sensor-proxy
echo 'pacman is here' && pacman --noconfirm -S libevdev python-libevdev
elif [[ $(dnf install 2>/dev/null) ]]; then
echo 'dnf is here' && dnf -y install libevdev python-libevdev iio-sensor-proxy
fi

python3 -m pip install -r requirements.txt

# Checking if the pip dependencies are successfuly loaded
if [[ $? != 0 ]]; then
echo "pip dependencies via file requirements.txt cannot be loaded correctly."
exit 1
echo 'dnf is here' && dnf -y install libevdev python-libevdev
fi

if [[ -d conf/__pycache__ ]] ; then
Expand Down Expand Up @@ -80,4 +72,6 @@ else
echo "Asus fliplock service started"
fi

echo "Install finished"

exit 0
Empty file removed requirements.txt
Empty file.

0 comments on commit 1d6aa29

Please sign in to comment.