From 57efcbd26ca87494593900b3aa51bcea8540f51d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Drahn=C3=ADk?= Date: Fri, 17 Mar 2023 11:05:15 +0100 Subject: [PATCH] #110 Fixes for used Inotify (deadlock, forgotten closing of watch,..) --- README.md | 2 +- asus_touchpad.py | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index cb80790..dccc5e8 100644 --- a/README.md +++ b/README.md @@ -410,7 +410,7 @@ top_left_icon_brightness_func_disabled = 0 | `enabled` | | `0` | NumberPad running status (enabled/disabled) | `disable_due_inactivity_time` | | `60.0` [s] | NumberPad is automatically disabled when have not received any event for this interval

decimal places allowed | `touchpad_disables_numpad` | | `1` | when is touchpad disabled is disabled NumberPad aswell, valid value is `1` or `0` (e.g. via Fn+special key)

status is taken from result of `xinput` - to toggle touchpad can be used [this script](https://github.com/ldrahnik/elementary-os-scripts/blob/master/toggle_touchpad.sh) -| `sys_numlock_enables_numpad` | | `0` | obtained via active `LED_NUML` of keyboard device

enable with `1`, by default NumberPad reflects only disabling system numlock (then is disabled) +| `sys_numlock_enables_numpad` | | `0` | obtained via active `LED_NUML` of keyboard device

enable with `1`, by default NumberPad reflects only disabling system numlock (then is disabled)

System numlock can be simulated `xdotool key Num_Lock` | `numpad_disables_sys_numlock` | | `1` | when is set to `1` is every time during inactivation of NumberPad sent `KEY_NUMLOCK`. Is useful do not send NumLock when is to laptop connected external keyboard and goal is only disable NumberPad on laptop but keep NumLock on external keyboard enabled | `enabled_touchpad_pointer` | | `3` | valid values are `0`, `1`, `2`, `3`

when is set to `1` touchpad pointer can be used to moving and for clicking and can be clicked pointer buttons left, right and middle when is NumberPad activated, `0` disable this usage and `2` allowes only pointer button clicks, `3` allowes only touchpad pointer moving without clicks | **Key layout** | | diff --git a/asus_touchpad.py b/asus_touchpad.py index 56fb002..4db52a5 100755 --- a/asus_touchpad.py +++ b/asus_touchpad.py @@ -52,6 +52,9 @@ config_file_dir = "" if len(sys.argv) > 2: config_file_dir = sys.argv[2] +# When is given config dir empty or is used default -> to ./ because inotify needs check folder (nor nothing = "") +if config_file_dir == "": + config_file_dir = "./" # Layout left_offset = getattr(model_layout, "left_offset", 0) @@ -186,6 +189,8 @@ def config_set(key, value, no_save=False, already_has_lock=False): config_save() if not already_has_lock: + # because inotify (deadlock) + sleep(0.1) config_lock.release() return value @@ -571,7 +576,7 @@ def set_touchpad_prop_tap_to_click(value): def activate_numpad(): - global brightness, device_id, default_backlight_level, enabled_touchpad_pointer, d_t, top_left_icon_brightness_func_disabled + global brightness, default_backlight_level, enabled_touchpad_pointer, top_left_icon_brightness_func_disabled config_set(CONFIG_ENABLED, True) @@ -596,7 +601,7 @@ def activate_numpad(): def deactivate_numpad(): - global brightness, device_id, enabled_touchpad_pointer, d_t + global brightness, enabled_touchpad_pointer config_set(CONFIG_ENABLED, False) @@ -754,7 +759,7 @@ def load_all_config_values(): config_lock.release() - if enabled and not numlock: + if enabled is not numlock: local_numlock_pressed() @@ -775,10 +780,14 @@ def load_all_config_values(): config = configparser.ConfigParser() load_all_config_values() +config_lock.acquire() config_save() +config_lock.release() +# because inotify (deadlock) +sleep(0.1) inotify_adapters = adapters.Inotify() -inotify_adapters.add_watch(config_file_path) +inotify_adapters.add_watch(config_file_dir) def set_tracking_id(value): try: @@ -1507,15 +1516,26 @@ def check_numpad_automatical_disable_due_inactivity(): def check_config_values_changes(): - global inotify_adapters + global inotify_adapters, config_lock try: for event in inotify_adapters.event_gen(yield_nones=False): (_, type_names, path, filename) = event - if "IN_CLOSE_WRITE" in type_names: - load_all_config_values() + if filename != CONFIG_FILE_NAME or path != config_file_dir: + continue + + if "IN_CLOSE_WRITE" in type_names or "IN_IGNORED" in type_names or "IN_MOVED_TO" in type_names: + + if not config_lock.locked(): + log.info("check_config_values_changes: detected external change of config file -> loading changes") + # because file might be read so fast that changes will not be there yet + sleep(0.1) + load_all_config_values() + else: + log.info("check_config_values_changes: detected internal change of config file -> do nothing -> would be deadlock") + except: pass @@ -1551,4 +1571,5 @@ def check_config_values_changes(): finally: if dev_k: dev_k.close() - fd_t.close() \ No newline at end of file + fd_t.close() + inotify_adapters.remove_watch(config_file_dir) \ No newline at end of file