diff --git a/README.md b/README.md index 52e0a3a..277fcfd 100644 --- a/README.md +++ b/README.md @@ -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/20170129134252.6185-1-hdegoede@redhat.com/) 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/20170129134252.6185-1-hdegoede@redhat.com/) 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`
diff --git a/asus_fliplock.py b/asus_fliplock.py index f7aabd5..55478da 100755 --- a/asus_fliplock.py +++ b/asus_fliplock.py @@ -5,8 +5,6 @@ import os import re import sys -import time -import subprocess from typing import Optional from libevdev import Device, EV_SW @@ -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 @@ -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): @@ -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: @@ -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) @@ -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: @@ -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() @@ -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() \ No newline at end of file + flip(e.value) \ No newline at end of file diff --git a/conf/default.py b/conf/default.py index 68d2a4a..b9f30de 100644 --- a/conf/default.py +++ b/conf/default.py @@ -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 \ No newline at end of file diff --git a/install.sh b/install.sh index e93bf1c..53aa4da 100755 --- a/install.sh +++ b/install.sh @@ -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 @@ -80,4 +72,6 @@ else echo "Asus fliplock service started" fi +echo "Install finished" + exit 0 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index e69de29..0000000