From 4118ab69622ebadab8bd74211891baf5af337cf3 Mon Sep 17 00:00:00 2001 From: Werner Sembach Date: Wed, 27 Jan 2021 16:37:17 +0100 Subject: [PATCH] Merge GNOME and KDE logic and decide in programm what to run --- CMakeLists.txt | 13 +- res/tuxedo-touchpad-switch-gdm.desktop | 4 - res/tuxedo-touchpad-switch-kde.desktop | 5 - res/tuxedo-touchpad-switch.desktop | 1 - setup-gnome.cpp | 142 ++++++++++ setup-gnome.h | 20 ++ ...o-touchpad-switch-kde.cpp => setup-kde.cpp | 158 +---------- setup-kde.h | 20 ++ touchpad-control.cpp | 135 +++++++++ touchpad-control.h | 22 ++ tuxedo-touchpad-switch.cpp | 263 ++++-------------- 11 files changed, 407 insertions(+), 376 deletions(-) delete mode 100644 res/tuxedo-touchpad-switch-gdm.desktop delete mode 100644 res/tuxedo-touchpad-switch-kde.desktop create mode 100644 setup-gnome.cpp create mode 100644 setup-gnome.h rename tuxedo-touchpad-switch-kde.cpp => setup-kde.cpp (57%) create mode 100644 setup-kde.h create mode 100644 touchpad-control.cpp create mode 100644 touchpad-control.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ff128a..eba2800 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,19 +5,16 @@ project(tuxedo-touchpad-switch) find_package(PkgConfig REQUIRED) pkg_check_modules(deps REQUIRED IMPORTED_TARGET gio-2.0 udev) -add_executable(tuxedo-touchpad-switch tuxedo-touchpad-switch.cpp) +add_executable(tuxedo-touchpad-switch tuxedo-touchpad-switch.cpp setup-gnome.cpp setup-kde.cpp touchpad-control.cpp) target_link_libraries(tuxedo-touchpad-switch udev PkgConfig::deps) -add_executable(tuxedo-touchpad-switch-kde tuxedo-touchpad-switch-kde.cpp) -target_link_libraries(tuxedo-touchpad-switch-kde udev PkgConfig::deps) - -install(TARGETS tuxedo-touchpad-switch tuxedo-touchpad-switch-kde DESTINATION bin/) +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-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-gdm.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 res/tuxedo-touchpad-switch-kde.desktop DESTINATION /etc/xdg/autostart/) # absolute path on purpose: $XDG_CONFIG_DIRS does 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.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 does 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_GENERATOR DEB) SET(CPACK_PACKAGE_CONTACT "Werner Sembach ") SET(CPACK_PACKAGE_VERSION 0.0.1) SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Hardware toggle for Tongfang/Uniwill i2c touchpads. This will toggle the touchpad-disabled-led.") diff --git a/res/tuxedo-touchpad-switch-gdm.desktop b/res/tuxedo-touchpad-switch-gdm.desktop deleted file mode 100644 index 126a1d1..0000000 --- a/res/tuxedo-touchpad-switch-gdm.desktop +++ /dev/null @@ -1,4 +0,0 @@ -[Desktop Entry] -Name=TUXEDO Touchpad Switch -Exec=tuxedo-touchpad-switch -Type=Application diff --git a/res/tuxedo-touchpad-switch-kde.desktop b/res/tuxedo-touchpad-switch-kde.desktop deleted file mode 100644 index f44d835..0000000 --- a/res/tuxedo-touchpad-switch-kde.desktop +++ /dev/null @@ -1,5 +0,0 @@ -[Desktop Entry] -Name=TUXEDO Touchpad Switch -Exec=tuxedo-touchpad-switch -Type=Application -OnlyShowIn=KDE; diff --git a/res/tuxedo-touchpad-switch.desktop b/res/tuxedo-touchpad-switch.desktop index 34869a9..126a1d1 100644 --- a/res/tuxedo-touchpad-switch.desktop +++ b/res/tuxedo-touchpad-switch.desktop @@ -2,4 +2,3 @@ Name=TUXEDO Touchpad Switch Exec=tuxedo-touchpad-switch Type=Application -OnlyShowIn=GNOME; diff --git a/setup-gnome.cpp b/setup-gnome.cpp new file mode 100644 index 0000000..c8b2553 --- /dev/null +++ b/setup-gnome.cpp @@ -0,0 +1,142 @@ +// Copyright (c) 2020 TUXEDO Computers GmbH +// +// This file is part of TUXEDO Touchpad Switch. +// +// This file is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// TUXEDO Touchpad Switch is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with TUXEDO Touchpad Switch. If not, see . + +#include "setup-gnome.h" + +#include + +#include + +#include + +#include "touchpad-control.h" + +using std::cerr; +using std::endl; + +static int lockfile = -1; + +static void send_events_handler(GSettings *settings, const char* key, __attribute__((unused)) gpointer user_data) { + const gchar *send_events_string = g_settings_get_string(settings, key); + if (!send_events_string) { + cerr << "send_events_handler(...): g_settings_get_string(...) failed." << endl; + return; + } + + int enabled = 0; + if (send_events_string[0] == 'e') { + enabled = 1; + } + + if (set_touchpad_state(enabled)) { + cerr << "send_events_handler(...): set_touchpad_state(...) failed." << endl; + } +} + +static void session_manager_properties_changed_handler(__attribute__((unused)) GDBusProxy *proxy, GVariant *changed_properties, __attribute__((unused)) GStrv invalidated_properties, gpointer user_data) { + if (g_variant_is_of_type(changed_properties, G_VARIANT_TYPE_VARDICT)) { + GVariantDict changed_properties_dict; + gboolean sessionIsActive; + + 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 << "properties_changed_handler(...): 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 << "properties_changed_handler(...): flock(...) failed." << endl; + } + } + } + } +} + +static void display_config_properties_changed_handler(__attribute__((unused)) GDBusProxy *proxy, GVariant *changed_properties, __attribute__((unused)) GStrv invalidated_properties, gpointer user_data) { + if (g_variant_is_of_type(changed_properties, G_VARIANT_TYPE_VARDICT)) { + GVariantDict changed_properties_dict; + gint32 powerSaveMode; + + g_variant_dict_init (&changed_properties_dict, changed_properties); + if (g_variant_dict_lookup (&changed_properties_dict, "PowerSaveMode", "i", &powerSaveMode)) { + if (powerSaveMode == 0) { + send_events_handler((GSettings *)user_data, "send-events", NULL); + } + } + } +} + +int setup_gnome(int lockfile_arg) { + lockfile = lockfile_arg; + + // 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; + return EXIT_FAILURE; + } + + // 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; + return EXIT_FAILURE; + } + + // sync on xsession change + 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; + return EXIT_FAILURE; + } + if (g_signal_connect(session_manager_properties, "g-properties-changed", G_CALLBACK(session_manager_properties_changed_handler), touchpad_settings) < 1) { + cerr << "main(...): g_signal_connect(...) failed." << endl; + return EXIT_FAILURE; + } + + // sync on wakeup + GDBusProxy *display_config_properties = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION, + G_DBUS_PROXY_FLAGS_NONE, NULL, + "org.gnome.Mutter.DisplayConfig", + "/org/gnome/Mutter/DisplayConfig", + "org.gnome.Mutter.DisplayConfig", + NULL, NULL); + if (display_config_properties == NULL) { + cerr << "main(...): g_dbus_proxy_new_for_bus_sync(...) failed." << endl; + return EXIT_FAILURE; + } + if (g_signal_connect(display_config_properties, "g-properties-changed", G_CALLBACK(display_config_properties_changed_handler), touchpad_settings) < 1) { + cerr << "main(...): g_signal_connect(...) failed." << endl; + return EXIT_FAILURE; + } + + // sync on start + // also ensures that "send-events" setting is accessed at least once, which is required for the GSettings singal handling to be correctly initialize + send_events_handler(touchpad_settings, "send-events", NULL); + + return EXIT_SUCCESS; +} diff --git a/setup-gnome.h b/setup-gnome.h new file mode 100644 index 0000000..deeee2d --- /dev/null +++ b/setup-gnome.h @@ -0,0 +1,20 @@ +// Copyright (c) 2020 TUXEDO Computers GmbH +// +// This file is part of TUXEDO Touchpad Switch. +// +// This file is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// TUXEDO Touchpad Switch is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with TUXEDO Touchpad Switch. If not, see . + +#pragma once + +int setup_gnome(int lockfile); diff --git a/tuxedo-touchpad-switch-kde.cpp b/setup-kde.cpp similarity index 57% rename from tuxedo-touchpad-switch-kde.cpp rename to setup-kde.cpp index 7125dee..e76d5d6 100644 --- a/tuxedo-touchpad-switch-kde.cpp +++ b/setup-kde.cpp @@ -15,22 +15,16 @@ // You should have received a copy of the GNU General Public License // along with TUXEDO Touchpad Switch. If not, see . -#include -#include -#include -#include -#include -#include +#include "setup-kde.h" + +#include -#include +#include #include -#include -#include -#include +#include "touchpad-control.h" -using std::cout; using std::cerr; using std::endl; @@ -38,107 +32,7 @@ int lockfile; gboolean isMousePluggedInPrev; gboolean isEnabledSave; -static int get_touchpad_hidraw_devices(std::vector *devnodes) { - int result = -EXIT_FAILURE; - - struct udev *udev_context = udev_new(); - if (!udev_context) { - cerr << "get_touchpad_hidraw_devices(...): udev_new(...) failed." << endl; - return result; - } - - struct udev_enumerate *hidraw_devices = udev_enumerate_new(udev_context); - if (!hidraw_devices) { - cerr << "get_touchpad_hidraw_devices(...): udev_enumerate_new(...) failed." << endl; - } - else { - if (udev_enumerate_add_match_subsystem(hidraw_devices, "hidraw") < 0) { - cerr << "get_touchpad_hidraw_devices(...): udev_enumerate_add_match_subsystem(...) failed." << endl; - } - else { - if (udev_enumerate_scan_devices(hidraw_devices) < 0) { - cerr << "get_touchpad_hidraw_devices(...): udev_enumerate_scan_devices(...) failed." << endl; - } - else { - struct udev_list_entry *hidraw_devices_iterator = udev_enumerate_get_list_entry(hidraw_devices); - if (!hidraw_devices_iterator) { - cerr << "get_touchpad_hidraw_devices(...): udev_enumerate_get_list_entry(...) failed." << endl; - } - else { - struct udev_list_entry *hidraw_device_entry; - udev_list_entry_foreach(hidraw_device_entry, hidraw_devices_iterator) { - if (strstr(udev_list_entry_get_name(hidraw_device_entry), "i2c-UNIW0001:00")) { - struct udev_device *hidraw_device = udev_device_new_from_syspath(udev_context, udev_list_entry_get_name(hidraw_device_entry)); - if (!hidraw_device) { - cerr << "get_touchpad_hidraw_devices(...): udev_device_new_from_syspath(...) failed." << endl; - } - else { - std::string devnode = udev_device_get_devnode(hidraw_device); - devnodes->push_back(devnode); - - udev_device_unref(hidraw_device); - } - } - } - result = devnodes->size(); - } - } - } - udev_enumerate_unref(hidraw_devices); - } - udev_unref(udev_context); - - return result; -} - -int set_touchpad_state(int enabled) { - std::vector devnodes; - int touchpad_count = get_touchpad_hidraw_devices(&devnodes); - if (touchpad_count < 0) { - cerr << "send_events_handler(...): get_touchpad_hidraw_devices(...) failed." << endl; - return EXIT_FAILURE; - } - if (touchpad_count == 0) { - cout << "No compatible touchpads found." << endl; - return EXIT_FAILURE; - } - - for (auto it = devnodes.begin(); it != devnodes.end(); ++it) { - int hidraw = open((*it).c_str(), O_WRONLY|O_NONBLOCK); - if (hidraw < 0) { - cerr << "send_events_handler(...): open(\"" << *it << "\", O_WRONLY|O_NONBLOCK) failed." << endl; - continue; - } - - // default is to enable touchpad, for this send "0x03" as feature report nr.7 (0x07) to the touchpad hid device - char buffer[2] = {0x07, 0x00}; - // change 0x03 to 0x00 to disable touchpad when configuration string starts with [d]isable - if (enabled) { - buffer[1] = 0x03; - } - int result = ioctl(hidraw, HIDIOCSFEATURE(sizeof(buffer)/sizeof(buffer[0])), buffer); - if (result < 0) { - cerr << "send_events_handler(...): ioctl(...) on " << *it << " failed." << endl; - close(hidraw); - continue; - } - - close(hidraw); - } - - return EXIT_SUCCESS; -} - -void gracefull_exit(int signum) { - int result = set_touchpad_state(1); - if (flock(lockfile, LOCK_UN)) { - cerr << "main(...): flock(...) failed." << endl; - } - close(lockfile); - exit(result); -} - -void kded5_modules_touchpad_handler(GDBusProxy *proxy, __attribute__((unused)) char *sender_name, char *signal_name, GVariant *parameters, __attribute__((unused)) gpointer user_data) { +static void kded5_modules_touchpad_handler(GDBusProxy *proxy, __attribute__((unused)) char *sender_name, char *signal_name, GVariant *parameters, __attribute__((unused)) gpointer user_data) { if (!strcmp("enabledChanged", signal_name) && g_variant_is_of_type(parameters, (const GVariantType *)"(b)") && g_variant_n_children(parameters)) { GVariant *enabledChanged = g_variant_get_child_value(parameters, 0); @@ -185,7 +79,7 @@ void kded5_modules_touchpad_handler(GDBusProxy *proxy, __attribute__((unused)) } } -int kded5_modules_touchpad_init(GDBusProxy *proxy) { +static int kded5_modules_touchpad_init(GDBusProxy *proxy) { GVariant *isEnabledParam = g_dbus_proxy_call_sync(proxy, "isEnabled", NULL, G_DBUS_CALL_FLAGS_NONE, G_MAXINT, NULL, NULL); if (isEnabledParam != NULL && g_variant_is_of_type(isEnabledParam, (const GVariantType *)"(b)") && g_variant_n_children(isEnabledParam)) { GVariant *isEnabled = g_variant_get_child_value(isEnabledParam, 0); @@ -243,23 +137,9 @@ int kded5_modules_touchpad_init(GDBusProxy *proxy) { return EXIT_SUCCESS; } -int main() { - lockfile = open("/etc/tuxedo-touchpad-switch-lockfile", O_RDONLY); - if (lockfile < 0) { - cerr << "main(...): open(...) failed." << endl; - gracefull_exit(0); - } - - //FIXME singal(...) deprecated -> man signal - signal(SIGINT, gracefull_exit); - signal(SIGTERM, gracefull_exit); +int setup_kde(int lockfile_arg) { + lockfile = lockfile_arg; - if (flock(lockfile, LOCK_EX)) { - cerr << "main(...): flock(...) failed." << endl; - gracefull_exit(0); - } - - // sync on config change, xsession change, and wakeup kde GDBusProxy *kded5_modules_touchpad = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, NULL, @@ -269,30 +149,18 @@ int main() { NULL, NULL); if (kded5_modules_touchpad == NULL) { cerr << "main(...): g_dbus_proxy_new_for_bus_sync(...) failed." << endl; - gracefull_exit(0); + return EXIT_FAILURE; } if (g_signal_connect(kded5_modules_touchpad, "g-signal", G_CALLBACK(kded5_modules_touchpad_handler), NULL) < 1) { cerr << "main(...): g_signal_connect(...) failed." << endl; - gracefull_exit(0); + return EXIT_FAILURE; } - // sync on start if (kded5_modules_touchpad_init(kded5_modules_touchpad) == EXIT_FAILURE) { cerr << "main(...): kded5_modules_touchpad_init(...) failed." << endl; - gracefull_exit(0); - } - - - // start empty glib mainloop, required for glib signals to be catched - GMainLoop *app = g_main_loop_new(NULL, TRUE); - if (!app) { - cerr << "main(...): g_main_loop_new(...) failed." << endl; - gracefull_exit(0); + return EXIT_FAILURE; } - g_main_loop_run(app); - // g_main_loop_run only returns on error - cerr << "main(...): g_main_loop_run(...) failed." << endl; - gracefull_exit(0); + return EXIT_SUCCESS; } diff --git a/setup-kde.h b/setup-kde.h new file mode 100644 index 0000000..e95e6a5 --- /dev/null +++ b/setup-kde.h @@ -0,0 +1,20 @@ +// Copyright (c) 2020 TUXEDO Computers GmbH +// +// This file is part of TUXEDO Touchpad Switch. +// +// This file is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// TUXEDO Touchpad Switch is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with TUXEDO Touchpad Switch. If not, see . + +#pragma once + +int setup_kde(int lockfile); diff --git a/touchpad-control.cpp b/touchpad-control.cpp new file mode 100644 index 0000000..f2af8f6 --- /dev/null +++ b/touchpad-control.cpp @@ -0,0 +1,135 @@ +// Copyright (c) 2020 TUXEDO Computers GmbH +// +// This file is part of TUXEDO Touchpad Switch. +// +// This file is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// TUXEDO Touchpad Switch is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with TUXEDO Touchpad Switch. If not, see . + +#include "touchpad-control.h" + +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include + +using std::cout; +using std::cerr; +using std::endl; + +// "std::vector *devnodes" gets amended with found "/dev/hidraw*" device paths +// returns -EXIT_FAILURE on error or the number of found "i2c-UNIW0001:00"-touchpads, starting with 0, meaning no touchpads found +static int get_touchpad_hidraw_devices(std::vector *devnodes) { + int result = -EXIT_FAILURE; + + struct udev *udev_context = udev_new(); + if (!udev_context) { + cerr << "get_touchpad_hidraw_devices(...): udev_new(...) failed." << endl; + } + else { + struct udev_enumerate *hidraw_devices = udev_enumerate_new(udev_context); + if (!hidraw_devices) { + cerr << "get_touchpad_hidraw_devices(...): udev_enumerate_new(...) failed." << endl; + } + else { + if (udev_enumerate_add_match_subsystem(hidraw_devices, "hidraw") < 0) { + cerr << "get_touchpad_hidraw_devices(...): udev_enumerate_add_match_subsystem(...) failed." << endl; + } + else { + if (udev_enumerate_scan_devices(hidraw_devices) < 0) { + cerr << "get_touchpad_hidraw_devices(...): udev_enumerate_scan_devices(...) failed." << endl; + } + else { + struct udev_list_entry *hidraw_devices_iterator = udev_enumerate_get_list_entry(hidraw_devices); + if (!hidraw_devices_iterator) { + cerr << "get_touchpad_hidraw_devices(...): udev_enumerate_get_list_entry(...) failed." << endl; + } + else { + struct udev_list_entry *hidraw_device_entry; + udev_list_entry_foreach(hidraw_device_entry, hidraw_devices_iterator) { + if (strstr(udev_list_entry_get_name(hidraw_device_entry), "i2c-UNIW0001:00")) { + struct udev_device *hidraw_device = udev_device_new_from_syspath(udev_context, udev_list_entry_get_name(hidraw_device_entry)); + if (!hidraw_device) { + cerr << "get_touchpad_hidraw_devices(...): udev_device_new_from_syspath(...) failed." << endl; + } + else { + std::string devnode = udev_device_get_devnode(hidraw_device); + devnodes->push_back(devnode); + + udev_device_unref(hidraw_device); + } + } + } + + result = devnodes->size(); + } + } + } + + udev_enumerate_unref(hidraw_devices); + } + + udev_unref(udev_context); + } + + return result; +} + +int set_touchpad_state(int enabled) { + std::vector devnodes; + int touchpad_count = get_touchpad_hidraw_devices(&devnodes); + if (touchpad_count < 0) { + cerr << "send_events_handler(...): get_touchpad_hidraw_devices(...) failed." << endl; + return EXIT_FAILURE; + } + if (touchpad_count == 0) { + cout << "No compatible touchpads found." << endl; + return EXIT_FAILURE; + } + + int result = EXIT_SUCCESS; + + for (auto it = devnodes.begin(); it != devnodes.end(); ++it) { + int hidraw = open((*it).c_str(), O_WRONLY|O_NONBLOCK); + if (hidraw < 0) { + cerr << "send_events_handler(...): open(\"" << *it << "\", O_WRONLY|O_NONBLOCK) failed." << endl; + result = EXIT_FAILURE; + } + else { + // to enable touchpad send "0x03" as feature report nr.7 (0x07) to the touchpad hid device + // to disable it send "0x030" + char buffer[2] = {0x07, 0x00}; + if (enabled) { + buffer[1] = 0x03; + } + + int result = ioctl(hidraw, HIDIOCSFEATURE(sizeof(buffer)/sizeof(buffer[0])), buffer); + if (result < 0) { + cerr << "send_events_handler(...): ioctl(...) on " << *it << " failed." << endl; + result = EXIT_FAILURE; + } + // else {} + + close(hidraw); + } + } + + return result; +} diff --git a/touchpad-control.h b/touchpad-control.h new file mode 100644 index 0000000..bd1617c --- /dev/null +++ b/touchpad-control.h @@ -0,0 +1,22 @@ +// Copyright (c) 2020 TUXEDO Computers GmbH +// +// This file is part of TUXEDO Touchpad Switch. +// +// This file is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// TUXEDO Touchpad Switch is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with TUXEDO Touchpad Switch. If not, see . + +#pragma once + +// "int enable" set to 0 disables the touchpad, any other value enables it +// returns EXIT_SUCCESS or EXIT_FAILURE accordingly, on fail the activate/deactivate state of found touchpads is undefined +int set_touchpad_state(int enabled); diff --git a/tuxedo-touchpad-switch.cpp b/tuxedo-touchpad-switch.cpp index a41f8da..fe35252 100644 --- a/tuxedo-touchpad-switch.cpp +++ b/tuxedo-touchpad-switch.cpp @@ -15,262 +15,99 @@ // You should have received a copy of the GNU General Public License // along with TUXEDO Touchpad Switch. If not, see . -#include +#include + +#include +#include + #include #include -#include #include -#include - -#include #include -#include -#include -#include +#include "touchpad-control.h" +#include "setup-gnome.h" +#include "setup-kde.h" using std::cout; using std::cerr; using std::endl; -int lockfile; - -static int get_touchpad_hidraw_devices(std::vector *devnodes) { - int result = -EXIT_FAILURE; - - struct udev *udev_context = udev_new(); - if (!udev_context) { - cerr << "get_touchpad_hidraw_devices(...): udev_new(...) failed." << endl; - return result; - } - - struct udev_enumerate *hidraw_devices = udev_enumerate_new(udev_context); - if (!hidraw_devices) { - cerr << "get_touchpad_hidraw_devices(...): udev_enumerate_new(...) failed." << endl; - } - else { - if (udev_enumerate_add_match_subsystem(hidraw_devices, "hidraw") < 0) { - cerr << "get_touchpad_hidraw_devices(...): udev_enumerate_add_match_subsystem(...) failed." << endl; - } - else { - if (udev_enumerate_scan_devices(hidraw_devices) < 0) { - cerr << "get_touchpad_hidraw_devices(...): udev_enumerate_scan_devices(...) failed." << endl; - } - else { - struct udev_list_entry *hidraw_devices_iterator = udev_enumerate_get_list_entry(hidraw_devices); - if (!hidraw_devices_iterator) { - cerr << "get_touchpad_hidraw_devices(...): udev_enumerate_get_list_entry(...) failed." << endl; - } - else { - struct udev_list_entry *hidraw_device_entry; - udev_list_entry_foreach(hidraw_device_entry, hidraw_devices_iterator) { - if (strstr(udev_list_entry_get_name(hidraw_device_entry), "i2c-UNIW0001:00")) { - struct udev_device *hidraw_device = udev_device_new_from_syspath(udev_context, udev_list_entry_get_name(hidraw_device_entry)); - if (!hidraw_device) { - cerr << "get_touchpad_hidraw_devices(...): udev_device_new_from_syspath(...) failed." << endl; - } - else { - std::string devnode = udev_device_get_devnode(hidraw_device); - devnodes->push_back(devnode); - - udev_device_unref(hidraw_device); - } - } - } - result = devnodes->size(); - } - } - } - udev_enumerate_unref(hidraw_devices); - } - udev_unref(udev_context); - - return result; -} +static int lockfile = -1; -int set_touchpad_state(int enabled) { - std::vector devnodes; - int touchpad_count = get_touchpad_hidraw_devices(&devnodes); - if (touchpad_count < 0) { - cerr << "send_events_handler(...): get_touchpad_hidraw_devices(...) failed." << endl; - return EXIT_FAILURE; +static void gracefull_exit(int signum = 0) { + int result = EXIT_SUCCESS; + if (signum < 0) { + result = EXIT_FAILURE; } - if (touchpad_count == 0) { - cout << "No compatible touchpads found." << endl; - return EXIT_FAILURE; + + if (set_touchpad_state(1) != EXIT_SUCCESS) { + cerr << "gracefull_exit(...): set_touchpad_state(...) failed." << endl; + result = EXIT_FAILURE; } - for (auto it = devnodes.begin(); it != devnodes.end(); ++it) { - int hidraw = open((*it).c_str(), O_WRONLY|O_NONBLOCK); - if (hidraw < 0) { - cerr << "send_events_handler(...): open(\"" << *it << "\", O_WRONLY|O_NONBLOCK) failed." << endl; - continue; + if (lockfile >= 0) { + if (flock(lockfile, LOCK_UN)) { + cerr << "gracefull_exit(...): flock(...) failed." << endl; + result = EXIT_FAILURE; } - - // default is to enable touchpad, for this send "0x03" as feature report nr.7 (0x07) to the touchpad hid device - char buffer[2] = {0x07, 0x00}; - // change 0x03 to 0x00 to disable touchpad when configuration string starts with [d]isable - if (enabled) { - buffer[1] = 0x03; + if (close(lockfile)) { + cerr << "gracefull_exit(...): close(...) failed." << endl; + result = EXIT_FAILURE; } - int result = ioctl(hidraw, HIDIOCSFEATURE(sizeof(buffer)/sizeof(buffer[0])), buffer); - if (result < 0) { - cerr << "send_events_handler(...): ioctl(...) on " << *it << " failed." << endl; - close(hidraw); - continue; - } - - close(hidraw); } - return EXIT_SUCCESS; -} - -void gracefull_exit(int signum) { - 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) { - const gchar *send_events_string = g_settings_get_string(settings, key); - if (!send_events_string) { - cerr << "send_events_handler(...): g_settings_get_string(...) failed." << endl; - return; - } +int main() { + struct sigaction sigaction_gracefull_exit; + sigaction_gracefull_exit.sa_handler = gracefull_exit; + sigemptyset(&sigaction_gracefull_exit.sa_mask); + sigaction_gracefull_exit.sa_flags = 0; - int enabled = 0; - if (send_events_string[0] == 'e') { - enabled = 1; + if (sigaction(SIGINT, &sigaction_gracefull_exit, nullptr)) { + cerr << "main(...): sigaction(...) failed." << endl; + gracefull_exit(-EXIT_FAILURE); } - - if (set_touchpad_state(enabled)) { - cerr << "send_events_handler(...): set_touchpad_state(...) failed." << endl; + if (sigaction(SIGTERM, &sigaction_gracefull_exit, nullptr)) { + cerr << "main(...): sigaction(...) failed." << endl; + gracefull_exit(-EXIT_FAILURE); } -} - -void session_manager_properties_changed_handler(__attribute__((unused)) GDBusProxy *proxy, GVariant *changed_properties, __attribute__((unused)) GStrv invalidated_properties, gpointer user_data) { - if (g_variant_is_of_type(changed_properties, G_VARIANT_TYPE_VARDICT)) { - GVariantDict changed_properties_dict; - gboolean sessionIsActive; - - 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 << "properties_changed_handler(...): 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 << "properties_changed_handler(...): flock(...) failed." << endl; - } - } - } - } -} - -void display_config_properties_changed_handler(__attribute__((unused)) GDBusProxy *proxy, GVariant *changed_properties, __attribute__((unused)) GStrv invalidated_properties, gpointer user_data) { - if (g_variant_is_of_type(changed_properties, G_VARIANT_TYPE_VARDICT)) { - GVariantDict changed_properties_dict; - gint32 powerSaveMode; - - g_variant_dict_init (&changed_properties_dict, changed_properties); - if (g_variant_dict_lookup (&changed_properties_dict, "PowerSaveMode", "i", &powerSaveMode)) { - cout << powerSaveMode << endl; - if (powerSaveMode == 0) { - send_events_handler((GSettings *)user_data, "send-events", NULL); - } - } - } -} - -int main() { + lockfile = open("/etc/tuxedo-touchpad-switch-lockfile", O_RDONLY); - if (lockfile < 0) { + if (lockfile == -1) { cerr << "main(...): open(...) failed." << endl; - gracefull_exit(0); + gracefull_exit(-EXIT_FAILURE); } - //FIXME singal(...) deprecated -> man signal - signal(SIGINT, gracefull_exit); - signal(SIGTERM, gracefull_exit); - - if (flock(lockfile, LOCK_EX)) { + if (flock(lockfile, LOCK_EX) == -1) { cerr << "main(...): flock(...) failed." << endl; - gracefull_exit(0); + gracefull_exit(-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; - gracefull_exit(0); + char *xdg_current_desktop = getenv("XDG_CURRENT_DESKTOP"); + if (strstr(xdg_current_desktop, "GNOME")) { + setup_gnome(lockfile); } - - - // 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; - gracefull_exit(0); + else if (strstr(xdg_current_desktop, "KDE")) { + setup_kde(lockfile); } - - - // sync on xsession change - 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; - gracefull_exit(0); - } - if (g_signal_connect(session_manager_properties, "g-properties-changed", G_CALLBACK(session_manager_properties_changed_handler), touchpad_settings) < 1) { - cerr << "main(...): g_signal_connect(...) failed." << endl; - gracefull_exit(0); - } - - - // sync on wakeup - GDBusProxy *display_config_properties = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION, - G_DBUS_PROXY_FLAGS_NONE, NULL, - "org.gnome.Mutter.DisplayConfig", - "/org/gnome/Mutter/DisplayConfig", - "org.gnome.Mutter.DisplayConfig", - NULL, NULL); - if (display_config_properties == NULL) { - cerr << "main(...): g_dbus_proxy_new_for_bus_sync(...) failed." << endl; - gracefull_exit(0); - } - if (g_signal_connect(display_config_properties, "g-properties-changed", G_CALLBACK(display_config_properties_changed_handler), touchpad_settings) < 1) { - cerr << "main(...): g_signal_connect(...) failed." << endl; - gracefull_exit(0); + else { + cout << "Your desktop environment is not supported." << endl; + gracefull_exit(SIGTERM); } - - // sync on start - send_events_handler(touchpad_settings, "send-events", NULL); - - // start empty glib mainloop, required for glib signals to be catched GMainLoop *app = g_main_loop_new(NULL, TRUE); if (!app) { cerr << "main(...): g_main_loop_new(...) failed." << endl; - gracefull_exit(0); + gracefull_exit(-EXIT_FAILURE); } g_main_loop_run(app); // g_main_loop_run only returns on error cerr << "main(...): g_main_loop_run(...) failed." << endl; - gracefull_exit(0); + gracefull_exit(-EXIT_FAILURE); }