Skip to content

Commit 2729d18

Browse files
committed
apps: daemon: Create a proper touchpad device
1 parent 8f2b345 commit 2729d18

File tree

2 files changed

+77
-20
lines changed

2 files changed

+77
-20
lines changed

src/apps/daemon/daemon.hpp

+15-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace iptsd::apps::daemon {
2020

2121
class Daemon : public core::Application {
2222
private:
23-
// The touchscreen device.
23+
// The touch device.
2424
TouchDevice m_touch;
2525

2626
// The stylus device.
@@ -34,20 +34,26 @@ class Daemon : public core::Application {
3434

3535
void on_start() override
3636
{
37-
if (m_config.touchscreen_disable)
37+
if (m_config.touchpad_disable && m_info.is_touchpad())
38+
spdlog::warn("Touchpad is disabled!");
39+
40+
if (m_config.touchscreen_disable && m_info.is_touchscreen())
3841
spdlog::warn("Touchscreen is disabled!");
3942

40-
if (m_config.stylus_disable)
43+
if (m_config.stylus_disable && m_info.is_touchpad())
4144
spdlog::warn("Stylus is disabled!");
4245
}
4346

4447
void on_touch(const std::vector<contacts::Contact<f64>> &contacts) override
4548
{
46-
if (m_config.touchscreen_disable)
49+
if (m_config.touchpad_disable && m_info.is_touchpad())
50+
return;
51+
52+
if (m_config.touchscreen_disable && m_info.is_touchscreen())
4753
return;
4854

4955
// Enable the touchscreen if it was disabled by a stylus that is no longer active.
50-
if (m_config.touchscreen_disable_on_stylus) {
56+
if (m_config.touchscreen_disable_on_stylus && m_info.is_touchscreen()) {
5157
if (!m_stylus.active() && !m_touch.enabled())
5258
m_touch.enable();
5359
}
@@ -60,8 +66,10 @@ class Daemon : public core::Application {
6066
if (m_config.stylus_disable)
6167
return;
6268

63-
if (m_config.touchscreen_disable_on_stylus && m_touch.enabled())
64-
m_touch.disable();
69+
if (m_config.touchscreen_disable_on_stylus && m_info.is_touchscreen()) {
70+
if (m_touch.enabled())
71+
m_touch.disable();
72+
}
6573

6674
m_stylus.update(stylus);
6775
}

src/apps/daemon/touch.hpp

+62-13
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ class TouchDevice {
4343
// The daemon configuration.
4444
core::Config m_config;
4545

46+
// Information about the device that the daemon is reading from.
47+
core::DeviceInfo m_info;
48+
49+
// How far a contact can be outside of the touch area and still get registered.
50+
f64 m_overshoot = 0;
51+
52+
// Whether all inputs will be lifted once a palm is registered.
53+
bool m_disable_on_palm = false;
54+
4655
// The indices of the contacts in the current frame.
4756
std::set<usize> m_current {};
4857

@@ -59,18 +68,41 @@ class TouchDevice {
5968
bool m_enabled = true;
6069

6170
public:
62-
TouchDevice(const core::Config &config, const core::DeviceInfo &info) : m_config {config}
71+
TouchDevice(const core::Config &config, const core::DeviceInfo &info)
72+
: m_config {config},
73+
m_info {info}
6374
{
64-
m_uinput->set_name("Touchscreen");
75+
if (info.is_touchscreen())
76+
m_uinput->set_name("Touchscreen");
77+
else
78+
m_uinput->set_name("Touchpad");
79+
6580
m_uinput->set_vendor(info.vendor);
6681
m_uinput->set_product(info.product);
6782

6883
m_uinput->set_evbit(EV_ABS);
6984
m_uinput->set_evbit(EV_KEY);
7085

71-
m_uinput->set_propbit(INPUT_PROP_DIRECT);
7286
m_uinput->set_keybit(BTN_TOUCH);
7387

88+
if (info.is_touchpad()) {
89+
m_uinput->set_keybit(BTN_TOOL_FINGER);
90+
m_uinput->set_keybit(BTN_TOOL_DOUBLETAP);
91+
m_uinput->set_keybit(BTN_TOOL_TRIPLETAP);
92+
m_uinput->set_keybit(BTN_TOOL_QUADTAP);
93+
m_uinput->set_keybit(BTN_TOOL_QUINTTAP);
94+
95+
m_uinput->set_propbit(INPUT_PROP_POINTER);
96+
97+
m_overshoot = config.touchpad_overshoot;
98+
m_disable_on_palm = config.touchpad_disable_on_palm;
99+
} else {
100+
m_uinput->set_propbit(INPUT_PROP_DIRECT);
101+
102+
m_overshoot = config.touchscreen_overshoot;
103+
m_disable_on_palm = config.touchscreen_disable_on_palm;
104+
}
105+
74106
const f64 diag = std::hypot(config.width, config.height);
75107

76108
// Resolution for X / Y is expected to be units/mm.
@@ -98,7 +130,7 @@ class TouchDevice {
98130
*/
99131
void update(const std::vector<contacts::Contact<f64>> &contacts)
100132
{
101-
// If the touchscreen is disabled ignore all inputs.
133+
// If the touch device is disabled ignore all inputs.
102134
if (!m_enabled)
103135
return;
104136

@@ -114,7 +146,7 @@ class TouchDevice {
114146
}
115147

116148
/*!
117-
* Disables the touchscreen and lifts all contacts.
149+
* Disables the touch device and lifts all contacts.
118150
*/
119151
void disable()
120152
{
@@ -130,25 +162,25 @@ class TouchDevice {
130162
}
131163

132164
/*!
133-
* Enables the touchscreen.
165+
* Enables the touch device.
134166
*/
135167
void enable()
136168
{
137169
m_enabled = true;
138170
}
139171

140172
/*!
141-
* Whether the touchscreen is disabled or enabled.
173+
* Whether the touch device is disabled or enabled.
142174
*
143-
* @return true if the touchscreen is enabled.
175+
* @return true if the touch device is enabled.
144176
*/
145177
[[nodiscard]] bool enabled() const
146178
{
147179
return m_enabled;
148180
}
149181

150182
/*!
151-
* Whether the touchscreen is currently active.
183+
* Whether the touch device is currently active.
152184
*
153185
* @return true if there are any active inputs.
154186
*/
@@ -189,14 +221,14 @@ class TouchDevice {
189221
}
190222

191223
/*!
192-
* Checks if the touchscreen should be disabled because of a palm on the screen.
224+
* Checks if the touch device should be disabled because of a palm.
193225
*
194226
* @param[in] contacts All currently active contacts.
195227
* @return true if all contacts should be lifted.
196228
*/
197229
[[nodiscard]] bool is_blocked(const std::vector<contacts::Contact<f64>> &contacts) const
198230
{
199-
if (!m_config.touchscreen_disable_on_palm)
231+
if (!m_disable_on_palm)
200232
return false;
201233

202234
return std::any_of(contacts.cbegin(), contacts.cend(), [&](const auto &c) {
@@ -213,8 +245,8 @@ class TouchDevice {
213245
{
214246
bool reset_singletouch = true;
215247

216-
const f64 ox = m_config.touchscreen_overshoot / m_config.width;
217-
const f64 oy = m_config.touchscreen_overshoot / m_config.height;
248+
const f64 ox = m_overshoot / m_config.width;
249+
const f64 oy = m_overshoot / m_config.height;
218250

219251
for (const contacts::Contact<f64> &contact : contacts) {
220252
// Ignore contacts without an index
@@ -366,6 +398,14 @@ class TouchDevice {
366398
void lift_singletouch() const
367399
{
368400
m_uinput->emit(EV_KEY, BTN_TOUCH, 0);
401+
402+
if (m_info.is_touchpad()) {
403+
m_uinput->emit(EV_KEY, BTN_TOOL_FINGER, 0);
404+
m_uinput->emit(EV_KEY, BTN_TOOL_DOUBLETAP, 0);
405+
m_uinput->emit(EV_KEY, BTN_TOOL_TRIPLETAP, 0);
406+
m_uinput->emit(EV_KEY, BTN_TOOL_QUADTAP, 0);
407+
m_uinput->emit(EV_KEY, BTN_TOOL_QUINTTAP, 0);
408+
}
369409
}
370410

371411
/*!
@@ -384,6 +424,15 @@ class TouchDevice {
384424
const i32 y = casts::to<i32>(std::round(mean.y() * MAX_Y));
385425

386426
m_uinput->emit(EV_KEY, BTN_TOUCH, 1);
427+
428+
if (m_info.is_touchpad()) {
429+
m_uinput->emit(EV_KEY, BTN_TOOL_FINGER, m_current.size() == 1 ? 1 : 0);
430+
m_uinput->emit(EV_KEY, BTN_TOOL_DOUBLETAP, m_current.size() == 2 ? 1 : 0);
431+
m_uinput->emit(EV_KEY, BTN_TOOL_TRIPLETAP, m_current.size() == 3 ? 1 : 0);
432+
m_uinput->emit(EV_KEY, BTN_TOOL_QUADTAP, m_current.size() == 4 ? 1 : 0);
433+
m_uinput->emit(EV_KEY, BTN_TOOL_QUINTTAP, m_current.size() >= 5 ? 1 : 0);
434+
}
435+
387436
m_uinput->emit(EV_ABS, ABS_X, x);
388437
m_uinput->emit(EV_ABS, ABS_Y, y);
389438
}

0 commit comments

Comments
 (0)