-
Notifications
You must be signed in to change notification settings - Fork 12
/
touchpad-control.cpp
194 lines (167 loc) · 8.05 KB
/
touchpad-control.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// 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/>.
#include "touchpad-control.h"
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/hidraw.h>
#include <libudev.h>
using std::cerr;
using std::endl;
// "std::vector<std::string> *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<std::string> *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;
}
static int get_hidraw_surface_button_switch_report_id(std::string devnode) {
int result = 0;
int hidraw = open(devnode.c_str(), O_WRONLY|O_NONBLOCK);
if (hidraw < 0) {
cerr << "get_hidraw_surface_button_switch_report_id(...): open(\"" << devnode << "\", O_WRONLY|O_NONBLOCK) failed." << endl;
return -EXIT_FAILURE;
}
else {
struct hidraw_report_descriptor report_descriptor;
result = ioctl(hidraw, HIDIOCGRDESCSIZE, &report_descriptor.size);
if (result < 0) {
cerr << "get_hidraw_surface_button_switch_report_id(...): ioctl(..., HIDIOCGRDESCSIZE, ...) on " << devnode << " failed." << endl;
close(hidraw);
return -EXIT_FAILURE;
}
result = ioctl(hidraw, HIDIOCGRDESC, &report_descriptor);
if (result < 0) {
cerr << "get_hidraw_surface_button_switch_report_id(...): ioctl(..., HIDIOCGRDESC, ...) on " << devnode << " failed." << endl;
close(hidraw);
return -EXIT_FAILURE;
}
close(hidraw);
__u8 surface_button_switch_collection_identifier[] = {0x05, 0x0d, 0x09, 0x22, 0xa1, 0x00, 0x09, 0x57, 0x09, 0x58};
auto it1 = std::search(std::begin(report_descriptor.value), std::end(report_descriptor.value), std::begin(surface_button_switch_collection_identifier), std::end(surface_button_switch_collection_identifier));
for (auto it2 = it1; it2 != std::end(report_descriptor.value); ++it2) {
if (*it2 == 0x85 && std::next(it2) != std::end(report_descriptor.value)) {
return *(std::next(it2));
}
}
}
return -EXIT_FAILURE;
}
int set_touchpad_state(int enabled) {
std::vector<std::string> 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) {
cerr << "No compatible touchpads found." << endl;
return EXIT_FAILURE;
}
int result = EXIT_SUCCESS;
for (auto it = devnodes.begin(); it != devnodes.end(); ++it) {
int feature_report_id = get_hidraw_surface_button_switch_report_id(*it);
if (feature_report_id < 0) {
cerr << "set_touchpad_state(...): get_hidraw_surface_button_switch_report_id(...) failed." << endl;
result = EXIT_FAILURE;
}
else {
int hidraw = open((*it).c_str(), O_WRONLY|O_NONBLOCK);
if (hidraw < 0) {
cerr << "set_touchpad_state(...): open(\"" << *it << "\", O_WRONLY|O_NONBLOCK) failed." << endl;
result = EXIT_FAILURE;
}
else {
// To enable touchpad send "0x03" as feature report to the touchpad hid device. The feature report number can be gathered from the report descriptors.
// To disable it send "0x00".
// Reference: https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/touchpad-configuration-collection#selective-reporting-feature-report
// Details:
// The two rightmost bits control the touchpad status
// In order, they are:
// 1. LED off + touchpad on/LED on + touchpad off
// 2. Clicks on/off
// So, the options are:
// 0x00 LED on, touchpad off, touchpad click off
// 0x01 LED on, touchpad off, touchpad click on
// 0x02 LED off, touchpad on, touchpad click off
// 0x03 LED off, touchpad on, touchpad click on
char buffer[2] = {static_cast<char>(feature_report_id), 0x00};
if (enabled) {
buffer[1] = 0x03;
}
result = ioctl(hidraw, HIDIOCSFEATURE(sizeof(buffer)/sizeof(buffer[0])), buffer);
if (result < 0) {
cerr << "set_touchpad_state(...): ioctl(...) on " << *it << " failed." << endl;
result = EXIT_FAILURE;
}
close(hidraw);
result = EXIT_SUCCESS;
}
}
}
return result;
}