Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tuxedo-touchpad-toggle executable #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ pkg_check_modules(deps REQUIRED IMPORTED_TARGET gio-2.0 udev)
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-toggle tuxedo-touchpad-toggle.cpp touchpad-control.cpp)
target_link_libraries(tuxedo-touchpad-toggle udev PkgConfig::deps)

install(TARGETS tuxedo-touchpad-switch DESTINATION bin/)
install(TARGETS tuxedo-touchpad-toggle 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.desktop DESTINATION /usr/share/gdm/greeter/autostart/) # absolute path on purpose: gdm has no config dir in /usr/local/
Expand Down
45 changes: 45 additions & 0 deletions touchpad-control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,48 @@ int set_touchpad_state(int enabled) {

return result;
}

int toggle_touchpad_state() {
std::vector<std::string> devnodes;
int touchpad_count = get_touchpad_hidraw_devices(&devnodes);
if (touchpad_count < 0) {
cerr << "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 << "open(\"" << *it << "\", O_WRONLY|O_NONBLOCK) failed." << endl;
result = EXIT_FAILURE;
}
else {
// get the device's state first (feature report nr.7 - 0x07)
char buffer[2] = {0x07, 0x00};
ioctl(hidraw, HIDIOCGFEATURE(2), buffer);

// toggle the state
if (buffer[1] == 0x00) {
buffer[1] = 0x03; // enable touchpad
} else {
buffer[1] = 0x00; // disable touchpad
}

int result = ioctl(hidraw, HIDIOCSFEATURE(2), buffer);
if (result < 0) {
cerr << "ioctl on " << *it << " failed." << endl;
result = EXIT_FAILURE;
}

close(hidraw);
}
}

return result;
}
2 changes: 2 additions & 0 deletions touchpad-control.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@
// "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);

int toggle_touchpad_state();
5 changes: 5 additions & 0 deletions tuxedo-touchpad-toggle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "touchpad-control.h"

int main() {
return toggle_touchpad_state();
}