From 077495424aa80c7e45061a8698a0b4100b20142b Mon Sep 17 00:00:00 2001 From: Werner Sembach Date: Tue, 19 Jan 2021 16:37:29 +0100 Subject: [PATCH] Unlock touchpad when switching away from supported xsession --- CMakeLists.txt | 6 ++-- res/tuxedo-touchpad-switch-lockfile | 0 tuxedo-touchpad-switch.cpp | 43 +++++++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 res/tuxedo-touchpad-switch-lockfile diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a396ae..6caea1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,9 +10,9 @@ target_link_libraries(tuxedo-touchpad-switch udev PkgConfig::deps) install(TARGETS tuxedo-touchpad-switch DESTINATION bin/) install(FILES res/99-tuxedo-touchpad-switch.rules DESTINATION lib/udev/rules.d/) -install(FILES res/tuxedo-touchpad-switch.desktop DESTINATION /etc/xdg/autostart/) # absolute path on purpose for this file only: - # $XDG_CONFIG_DIRS dos not include a folder under /usr/ by default - # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables +install(FILES res/tuxedo-touchpad-switch-lockfile DESTINATION /etc/ PERMISSIONS OWNER_READ GROUP_READ WORLD_READ) # absolute path on purpose: implemented as such in tuxedo-touchpad-switch.cpp +install(FILES res/tuxedo-touchpad-switch.desktop DESTINATION /usr/share/gdm/greeter/autostart/) # absolute path on purpose: gdm has no config dir in /usr/local/ +install(FILES res/tuxedo-touchpad-switch.desktop DESTINATION /etc/xdg/autostart/) # absolute path on purpose: $XDG_CONFIG_DIRS dos not include a folder under /usr/ by default https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables SET(CPACK_GENERATOR DEB RPM) SET(CPACK_PACKAGE_CONTACT "Werner Sembach ") diff --git a/res/tuxedo-touchpad-switch-lockfile b/res/tuxedo-touchpad-switch-lockfile new file mode 100644 index 0000000..e69de29 diff --git a/tuxedo-touchpad-switch.cpp b/tuxedo-touchpad-switch.cpp index 63e3d52..07e2324 100644 --- a/tuxedo-touchpad-switch.cpp +++ b/tuxedo-touchpad-switch.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -33,6 +34,8 @@ using std::cout; using std::cerr; using std::endl; +int lockfile; + static int get_touchpad_hidraw_devices(std::vector *devnodes) { int result = -EXIT_FAILURE; @@ -125,7 +128,12 @@ int set_touchpad_state(int enabled) { } void gracefull_exit(int signum) { - exit(set_touchpad_state(1)); + int result = set_touchpad_state(1); + if (flock(lockfile, LOCK_UN)) { + cerr << "main(...): flock(...) failed." << endl; + } + close(lockfile); + exit(result); } void send_events_handler(GSettings *settings, const char* key, __attribute__((unused)) gpointer user_data) { @@ -153,8 +161,19 @@ void properties_changed_handler(__attribute__((unused)) GDBusProxy *proxy, GVar g_variant_dict_init (&changed_properties_dict, changed_properties); if (g_variant_dict_lookup (&changed_properties_dict, "SessionIsActive", "b", &sessionIsActive)) { if (sessionIsActive) { + if (flock(lockfile, LOCK_EX)) { + cerr << "main(...): flock(...) failed." << endl; + } send_events_handler((GSettings *)user_data, "send-events", NULL); } + else { + if (set_touchpad_state(1)) { + cerr << "properties_changed_handler(...): set_touchpad_state(...) failed." << endl; + } + if (flock(lockfile, LOCK_UN)) { + cerr << "main(...): flock(...) failed." << endl; + } + } } } } @@ -162,13 +181,26 @@ void properties_changed_handler(__attribute__((unused)) GDBusProxy *proxy, GVar int main() { // Currently this programm works on desktop environments using Gnome settings only (Gnome/Budgie/Cinnamon/etc.). // A KDE configuration version will be developt once this works flawless. - signal(SIGINT, gracefull_exit); - signal(SIGTERM, gracefull_exit); + lockfile = open("/etc/tuxedo-touchpad-switch-lockfile", O_RDONLY); + if (lockfile < 0) { + cerr << "main(...): open(...) failed." << endl; + return EXIT_FAILURE; + } + + signal(SIGINT, gracefull_exit); + signal(SIGTERM, gracefull_exit); + + if (flock(lockfile, LOCK_EX)) { + cerr << "main(...): flock(...) failed." << endl; + close(lockfile); + return EXIT_FAILURE; + } // get a new glib settings context to read the touchpad configuration of the current user GSettings *touchpad_settings = g_settings_new("org.gnome.desktop.peripherals.touchpad"); if (!touchpad_settings) { cerr << "main(...): g_settings_new(...) failed." << endl; + close(lockfile); return EXIT_FAILURE; } @@ -176,6 +208,7 @@ int main() { // sync on config change if (g_signal_connect(touchpad_settings, "changed::send-events", G_CALLBACK(send_events_handler), NULL) < 1) { cerr << "main(...): g_signal_connect(...) failed." << endl; + close(lockfile); return EXIT_FAILURE; } @@ -188,11 +221,13 @@ int main() { GDBusProxy *session_manager_properties = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, NULL, "org.gnome.SessionManager", "/org/gnome/SessionManager", "org.gnome.SessionManager", NULL, NULL); if (session_manager_properties == NULL) { cerr << "main(...): g_dbus_proxy_new_for_bus_sync(...) failed." << endl; + close(lockfile); return EXIT_FAILURE; } if (g_signal_connect(session_manager_properties, "g-properties-changed", G_CALLBACK(properties_changed_handler), touchpad_settings) < 1) { cerr << "main(...): g_signal_connect(...) failed." << endl; + close(lockfile); g_object_unref(session_manager_properties); return EXIT_FAILURE; } @@ -202,6 +237,7 @@ int main() { GMainLoop *app = g_main_loop_new(NULL, TRUE); if (!app) { cerr << "main(...): g_main_loop_new(...) failed." << endl; + close(lockfile); g_object_unref(session_manager_properties); return EXIT_FAILURE; } @@ -209,6 +245,7 @@ int main() { g_main_loop_run(app); // g_main_loop_run should not return cerr << "main(...): g_main_loop_run(...) failed." << endl; + close(lockfile); g_object_unref(session_manager_properties); return EXIT_FAILURE; }