You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tuxedo-touchpad-switch/tuxedo-touchpad-switch.cpp

164 lines
6.3 KiB

4 years ago
// Copyright (c) 2020 TUXEDO Computers GmbH <tux@tuxedocomputers.com>
//
// 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 <https://www.gnu.org/licenses/>.
4 years ago
#include <linux/hidraw.h>
#include <fcntl.h>
4 years ago
#include <unistd.h>
#include <sys/ioctl.h>
4 years ago
4 years ago
#include <libudev.h>
4 years ago
4 years ago
#include <gio/gio.h>
4 years ago
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::cerr;
using std::endl;
4 years ago
static int get_touchpad_hidraw_devices(std::vector<std::string> *devnodes) {
4 years ago
int result = -1;
4 years ago
struct udev *udev_context = udev_new();
if (!udev_context) {
4 years ago
cerr << "get_touchpad_hidraw_devices(...): udev_new(...) failed." << endl;
return result;
4 years ago
}
struct udev_enumerate *hidraw_devices = udev_enumerate_new(udev_context);
if (!hidraw_devices) {
4 years ago
cerr << "get_touchpad_hidraw_devices(...): udev_enumerate_new(...) failed." << endl;
4 years ago
}
4 years ago
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")) {
4 years ago
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();
}
4 years ago
}
}
4 years ago
udev_enumerate_unref(hidraw_devices);
4 years ago
}
udev_unref(udev_context);
4 years ago
return result;
4 years ago
}
4 years ago
void send_events_handler(GSettings *settings, const char* key, __attribute__((unused)) gpointer user_data) {
4 years ago
const gchar *send_events_string = g_settings_get_string(settings, key);
if (!send_events_string) {
4 years ago
cerr << "send_events_handler(...): g_settings_get_string(...) failed." << endl;
4 years ago
return;
4 years ago
}
std::vector<std::string> devnodes;
int touchpad_count = get_touchpad_hidraw_devices(&devnodes);
if (touchpad_count < 0) {
4 years ago
cerr << "send_events_handler(...): get_touchpad_hidraw_devices(...) failed." << endl;
4 years ago
return;
4 years ago
}
if (touchpad_count == 0) {
4 years ago
cout << "No compatible touchpads found." << endl;
4 years ago
return;
4 years ago
}
for (auto it = devnodes.begin(); it != devnodes.end(); ++it) {
int hidraw = open((*it).c_str(), O_WRONLY|O_NONBLOCK);
4 years ago
if (hidraw < 0) {
4 years ago
cerr << "send_events_handler(...): open(\"" << *it << "\", O_RDWR|O_NONBLOCK) failed." << endl;
4 years ago
continue;
}
4 years ago
// default is to enable touchpad, for this send "0x03" as feature report nr.7 (0x07) to the touchpad hid device
4 years ago
char buffer[2] = {0x07, 0x03};
4 years ago
// change 0x03 to 0x00 to disable touchpad when configuration string starts with [d]isable
4 years ago
if (send_events_string[0] == 'd') {
4 years ago
buffer[1] = 0x00;
}
int result = ioctl(hidraw, HIDIOCSFEATURE(sizeof(buffer)/sizeof(buffer[0])), buffer);
if (result < 0) {
4 years ago
cerr << "send_events_handler(...): ioctl(...) on " << *it << " failed." << endl;
4 years ago
close(hidraw);
continue;
}
4 years ago
close(hidraw);
}
}
4 years ago
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.
4 years ago
4 years ago
// 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;
4 years ago
return EXIT_FAILURE;
}
4 years ago
// 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;
4 years ago
return EXIT_FAILURE;
}
4 years ago
// sync on start
send_events_handler(touchpad_settings, "send-events", NULL);
4 years ago
// TODO sync on xsession change
// start empty glib mainloop, required for glib signals to be catched
4 years ago
GMainLoop *app = g_main_loop_new(NULL, TRUE);
if (!app) {
4 years ago
cerr << "main(...): g_main_loop_new(...) failed." << endl;
4 years ago
return EXIT_FAILURE;
}
4 years ago
4 years ago
g_main_loop_run(app);
// g_main_loop_run should not return
4 years ago
cerr << "main(...): g_main_loop_run(...) failed." << endl;
4 years ago
return EXIT_FAILURE;
4 years ago
}