Skip to content

Commit

Permalink
Fix crash when receiving abs input events prior to the display touchport
Browse files Browse the repository at this point in the history
  • Loading branch information
cgutman committed Apr 13, 2024
1 parent f87bc86 commit a4d9ee3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/input.cpp
Expand Up @@ -469,15 +469,19 @@ namespace input {
* @param input The input context.
* @param val The cartesian coordinate pair to convert.
* @param size The size of the client's surface containing the value.
* @return The host-relative coordinate pair.
* @return The host-relative coordinate pair if a touchport is available.
*/
std::pair<float, float>
std::optional<std::pair<float, float>>
client_to_touchport(std::shared_ptr<input_t> &input, const std::pair<float, float> &val, const std::pair<float, float> &size) {
auto &touch_port_event = input->touch_port_event;
auto &touch_port = input->touch_port;
if (touch_port_event->peek()) {
touch_port = *touch_port_event->pop();
}
if (!touch_port) {
BOOST_LOG(verbose) << "Ignoring early absolute input without a touch port"sv;
return std::nullopt;
}

auto scalarX = touch_port.width / size.first;
auto scalarY = touch_port.height / size.second;
Expand All @@ -491,7 +495,7 @@ namespace input {
x = std::clamp(x, offsetX, (size.first * scalarX) - offsetX);
y = std::clamp(y, offsetY, (size.second * scalarY) - offsetY);

return { (x - offsetX) * touch_port.scalar_inv, (y - offsetY) * touch_port.scalar_inv };
return std::pair { (x - offsetX) * touch_port.scalar_inv, (y - offsetY) * touch_port.scalar_inv };
}

/**
Expand Down Expand Up @@ -561,14 +565,17 @@ namespace input {
auto height = (float) util::endian::big(packet->height);

auto tpcoords = client_to_touchport(input, { x, y }, { width, height });
if (!tpcoords) {
return;
}

auto &touch_port = input->touch_port;
platf::touch_port_t abs_port {
touch_port.offset_x, touch_port.offset_y,
touch_port.env_width, touch_port.env_height
};

platf::abs_mouse(platf_input, abs_port, tpcoords.first, tpcoords.second);
platf::abs_mouse(platf_input, abs_port, tpcoords->first, tpcoords->second);
}

void
Expand Down Expand Up @@ -918,6 +925,9 @@ namespace input {
{ from_clamped_netfloat(packet->x, 0.0f, 1.0f) * 65535.f,
from_clamped_netfloat(packet->y, 0.0f, 1.0f) * 65535.f },
{ 65535.f, 65535.f });
if (!coords) {
return;
}

auto &touch_port = input->touch_port;
platf::touch_port_t abs_port {
Expand All @@ -926,8 +936,8 @@ namespace input {
};

// Renormalize the coordinates
coords.first /= abs_port.width;
coords.second /= abs_port.height;
coords->first /= abs_port.width;
coords->second /= abs_port.height;

// Normalize rotation value to 0-359 degree range
auto rotation = util::endian::little(packet->rotation);
Expand All @@ -946,8 +956,8 @@ namespace input {
packet->eventType,
rotation,
util::endian::little(packet->pointerId),
coords.first,
coords.second,
coords->first,
coords->second,
from_clamped_netfloat(packet->pressureOrDistance, 0.0f, 1.0f),
contact_area.first,
contact_area.second,
Expand All @@ -972,6 +982,9 @@ namespace input {
{ from_clamped_netfloat(packet->x, 0.0f, 1.0f) * 65535.f,
from_clamped_netfloat(packet->y, 0.0f, 1.0f) * 65535.f },
{ 65535.f, 65535.f });
if (!coords) {
return;
}

auto &touch_port = input->touch_port;
platf::touch_port_t abs_port {
Expand All @@ -980,8 +993,8 @@ namespace input {
};

// Renormalize the coordinates
coords.first /= abs_port.width;
coords.second /= abs_port.height;
coords->first /= abs_port.width;
coords->second /= abs_port.height;

// Normalize rotation value to 0-359 degree range
auto rotation = util::endian::little(packet->rotation);
Expand All @@ -1002,8 +1015,8 @@ namespace input {
packet->penButtons,
packet->tilt,
rotation,
coords.first,
coords.second,
coords->first,
coords->second,
from_clamped_netfloat(packet->pressureOrDistance, 0.0f, 1.0f),
contact_area.first,
contact_area.second,
Expand Down
5 changes: 5 additions & 0 deletions src/input.h
Expand Up @@ -32,6 +32,11 @@ namespace input {
float client_offsetX, client_offsetY;

float scalar_inv;

explicit
operator bool() const {
return width != 0 && height != 0 && env_width != 0 && env_height != 0;
}
};

std::pair<float, float>
Expand Down

0 comments on commit a4d9ee3

Please sign in to comment.