From 9a45249f997ce657b434f5a242ae006afe2aae2a Mon Sep 17 00:00:00 2001 From: Werner Sembach Date: Fri, 18 Dec 2020 17:13:25 +0100 Subject: [PATCH] Cleanup --- .gitignore | 6 + CMakeLists.txt | 12 +- gsettings-test.c | 34 ------ tuxedo-touchpad-switch-deamon.cpp | 179 ------------------------------ tuxedo-touchpad-switch.cpp | 90 ++++++++++++--- 5 files changed, 85 insertions(+), 236 deletions(-) delete mode 100644 gsettings-test.c delete mode 100644 tuxedo-touchpad-switch-deamon.cpp diff --git a/.gitignore b/.gitignore index 84c048a..c6232b6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,7 @@ /build/ +/debug/ + +/.idea/ +/.cproject +/.project +/*.kdev4 diff --git a/CMakeLists.txt b/CMakeLists.txt index c6d21ae..7fbfc47 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,14 +3,6 @@ cmake_minimum_required(VERSION 3.6) 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) -target_link_libraries(tuxedo-touchpad-switch udev) - -pkg_check_modules(GIO2 REQUIRED IMPORTED_TARGET gio-2.0) - -add_executable(gsettings-test gsettings-test.c) -target_link_libraries(gsettings-test PkgConfig::GIO2) - -add_executable(tuxedo-touchpad-switch-deamon tuxedo-touchpad-switch-deamon.cpp) -target_link_libraries(tuxedo-touchpad-switch-deamon udev PkgConfig::GIO2) +target_link_libraries(tuxedo-touchpad-switch udev PkgConfig::deps) diff --git a/gsettings-test.c b/gsettings-test.c deleted file mode 100644 index e8d8227..0000000 --- a/gsettings-test.c +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include - -void send_events_handler(GSettings *settings, char* key, gpointer user_data) { - const gchar *send_events_string = g_settings_get_string(settings, key); - if (!send_events_string) { - perror("g_settings_get_string"); - } - printf("send_events_string: %s\n", send_events_string); -} - -int main(int argc, char *argv[]) { - GSettings *settings_touchpad = g_settings_new("org.gnome.desktop.peripherals.touchpad"); - if (!settings_touchpad) { - perror("g_settings_new"); - return EXIT_FAILURE; - } - - if (g_signal_connect(settings_touchpad, "changed::send-events", G_CALLBACK(send_events_handler), NULL) < 1) { - perror("g_signal_connect"); - return EXIT_FAILURE; - } - - GMainLoop *app = g_main_loop_new(NULL, TRUE); - if (!app) { - perror("g_main_loop_new"); - return EXIT_FAILURE; - } - - g_main_loop_run(app); - // g_main_loop_run should not return - perror("g_main_loop_run"); - return EXIT_FAILURE; -} diff --git a/tuxedo-touchpad-switch-deamon.cpp b/tuxedo-touchpad-switch-deamon.cpp deleted file mode 100644 index ea9defd..0000000 --- a/tuxedo-touchpad-switch-deamon.cpp +++ /dev/null @@ -1,179 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -const char *bus_str(int bus); - -static int get_touchpad_hidraw_devices(std::vector *devnodes) { - struct udev *udev_context = udev_new(); - if (!udev_context) { - perror("udev_new"); - return -1; - } - - struct udev_enumerate *hidraw_devices = udev_enumerate_new(udev_context); - if (!hidraw_devices) { - perror("udev_enumerate_new"); - udev_unref(udev_context); - return -1; - } - - if (udev_enumerate_add_match_subsystem(hidraw_devices, "hidraw") < 0) { - perror("udev_enumerate_add_match_subsystem"); - udev_enumerate_unref(hidraw_devices); - udev_unref(udev_context); - return -1; - } - - if (udev_enumerate_scan_devices(hidraw_devices) < 0) { - perror("udev_enumerate_scan_devices"); - udev_enumerate_unref(hidraw_devices); - udev_unref(udev_context); - return -1; - } - - struct udev_list_entry *hidraw_devices_iterator = udev_enumerate_get_list_entry(hidraw_devices); - if (!hidraw_devices_iterator) { - udev_enumerate_unref(hidraw_devices); - udev_unref(udev_context); - return -1; - } - 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")) { - struct udev_device *hidraw_device = udev_device_new_from_syspath(udev_context, udev_list_entry_get_name(hidraw_device_entry)); - if (!hidraw_device) { - perror("udev_device_new_from_syspath"); - continue; - } - - std::string devnode = udev_device_get_devnode(hidraw_device); - devnodes->push_back(devnode); - - udev_device_unref(hidraw_device); - } - } - - udev_enumerate_unref(hidraw_devices); - udev_unref(udev_context); - - return devnodes->size();; -} - -void send_events_handler(GSettings *settings, const char* key, gpointer user_data) { - const gchar *send_events_string = g_settings_get_string(settings, key); - if (!send_events_string) { - perror("g_settings_get_string"); - return; - } - - std::vector devnodes; - int touchpad_count = get_touchpad_hidraw_devices(&devnodes); - if (touchpad_count < 0) { - perror("get_touchpad_hidraw_devices"); - return; - } - if (touchpad_count == 0) { - printf("No compatible touchpads found.\n"); - return; - } - - for (auto it = devnodes.begin(); it != devnodes.end(); ++it) { - int hidraw = open((*it).c_str(), O_RDWR|O_NONBLOCK); - if (hidraw < 0) { - perror("open"); - continue; - } - - char buffer[2] = {0x07, 0x03}; - if (send_events_string[0] == 'd') { - buffer[1] = 0x00; - } - int result = ioctl(hidraw, HIDIOCSFEATURE(sizeof(buffer)/sizeof(buffer[0])), buffer); - if (result < 0) { - perror("ioctl"); - close(hidraw); - continue; - } - - close(hidraw); - } -} - -void gracefull_exit(int signum) { - GSettings *settings_touchpad = g_settings_new("org.gnome.desktop.peripherals.touchpad"); - if (!settings_touchpad) { - perror("g_settings_new"); - exit(EXIT_FAILURE); - } - - std::vector devnodes; - int touchpad_count = get_touchpad_hidraw_devices(&devnodes); - if (touchpad_count < 0) { - perror("get_touchpad_hidraw_devices"); - exit(EXIT_FAILURE); - } - if (touchpad_count == 0) { - printf("No compatible touchpads found.\n"); - exit(EXIT_SUCCESS); - } - - for (auto it = devnodes.begin(); it != devnodes.end(); ++it) { - int hidraw = open((*it).c_str(), O_RDWR|O_NONBLOCK); - if (hidraw < 0) { - perror("open"); - continue; - } - - char buffer[2] = {0x07, 0x03}; - int result = ioctl(hidraw, HIDIOCSFEATURE(sizeof(buffer)/sizeof(buffer[0])), buffer); - if (result < 0) { - perror("ioctl"); - close(hidraw); - continue; - } - - close(hidraw); - } - exit(EXIT_SUCCESS); -} - -int main(int argc, char *argv[]) { - signal(SIGINT, gracefull_exit); - signal(SIGTERM, gracefull_exit); - - GSettings *settings_touchpad = g_settings_new("org.gnome.desktop.peripherals.touchpad"); - if (!settings_touchpad) { - perror("g_settings_new"); - return EXIT_FAILURE; - } - - // sync on start - send_events_handler(settings_touchpad, "send-events", NULL); - - if (g_signal_connect(settings_touchpad, "changed::send-events", G_CALLBACK(send_events_handler), NULL) < 1) { - perror("g_signal_connect"); - return EXIT_FAILURE; - } - - GMainLoop *app = g_main_loop_new(NULL, TRUE); - if (!app) { - perror("g_main_loop_new"); - return EXIT_FAILURE; - } - - g_main_loop_run(app); - // g_main_loop_run should not return - perror("g_main_loop_run"); - return EXIT_FAILURE; -} diff --git a/tuxedo-touchpad-switch.cpp b/tuxedo-touchpad-switch.cpp index b4a2449..ea9defd 100644 --- a/tuxedo-touchpad-switch.cpp +++ b/tuxedo-touchpad-switch.cpp @@ -5,10 +5,13 @@ #include #include #include +#include #include #include +#include + const char *bus_str(int bus); static int get_touchpad_hidraw_devices(std::vector *devnodes) { @@ -67,49 +70,110 @@ static int get_touchpad_hidraw_devices(std::vector *devnodes) { return devnodes->size();; } -int main(int argc, char *argv[]) { - if (argc < 2) { - printf("Usage: %s \n", argv[0]); - return EXIT_SUCCESS; +void send_events_handler(GSettings *settings, const char* key, gpointer user_data) { + const gchar *send_events_string = g_settings_get_string(settings, key); + if (!send_events_string) { + perror("g_settings_get_string"); + return; } std::vector devnodes; int touchpad_count = get_touchpad_hidraw_devices(&devnodes); if (touchpad_count < 0) { perror("get_touchpad_hidraw_devices"); - return EXIT_FAILURE; + return; } if (touchpad_count == 0) { printf("No compatible touchpads found.\n"); - return EXIT_SUCCESS; + return; } - int result = EXIT_SUCCESS; for (auto it = devnodes.begin(); it != devnodes.end(); ++it) { int hidraw = open((*it).c_str(), O_RDWR|O_NONBLOCK); if (hidraw < 0) { perror("open"); - result = EXIT_FAILURE; continue; } char buffer[2] = {0x07, 0x03}; - if (argv[1][0] == 'd') { + if (send_events_string[0] == 'd') { buffer[1] = 0x00; } int result = ioctl(hidraw, HIDIOCSFEATURE(sizeof(buffer)/sizeof(buffer[0])), buffer); if (result < 0) { perror("ioctl"); close(hidraw); - result = EXIT_FAILURE; continue; } - else { - printf("ioctl returned: %d\n", result); + + close(hidraw); + } +} + +void gracefull_exit(int signum) { + GSettings *settings_touchpad = g_settings_new("org.gnome.desktop.peripherals.touchpad"); + if (!settings_touchpad) { + perror("g_settings_new"); + exit(EXIT_FAILURE); + } + + std::vector devnodes; + int touchpad_count = get_touchpad_hidraw_devices(&devnodes); + if (touchpad_count < 0) { + perror("get_touchpad_hidraw_devices"); + exit(EXIT_FAILURE); + } + if (touchpad_count == 0) { + printf("No compatible touchpads found.\n"); + exit(EXIT_SUCCESS); + } + + for (auto it = devnodes.begin(); it != devnodes.end(); ++it) { + int hidraw = open((*it).c_str(), O_RDWR|O_NONBLOCK); + if (hidraw < 0) { + perror("open"); + continue; + } + + char buffer[2] = {0x07, 0x03}; + int result = ioctl(hidraw, HIDIOCSFEATURE(sizeof(buffer)/sizeof(buffer[0])), buffer); + if (result < 0) { + perror("ioctl"); + close(hidraw); + continue; } close(hidraw); } + exit(EXIT_SUCCESS); +} + +int main(int argc, char *argv[]) { + signal(SIGINT, gracefull_exit); + signal(SIGTERM, gracefull_exit); + + GSettings *settings_touchpad = g_settings_new("org.gnome.desktop.peripherals.touchpad"); + if (!settings_touchpad) { + perror("g_settings_new"); + return EXIT_FAILURE; + } + + // sync on start + send_events_handler(settings_touchpad, "send-events", NULL); + + if (g_signal_connect(settings_touchpad, "changed::send-events", G_CALLBACK(send_events_handler), NULL) < 1) { + perror("g_signal_connect"); + return EXIT_FAILURE; + } + + GMainLoop *app = g_main_loop_new(NULL, TRUE); + if (!app) { + perror("g_main_loop_new"); + return EXIT_FAILURE; + } - return result; + g_main_loop_run(app); + // g_main_loop_run should not return + perror("g_main_loop_run"); + return EXIT_FAILURE; }