Skip to content

Commit

Permalink
Fix incorrect scaling for absolute mouse coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
loki committed Jun 24, 2021
1 parent 1eda45a commit b676009
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 24 deletions.
21 changes: 12 additions & 9 deletions sunshine/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ void free_id(std::bitset<N> &gamepad_mask, int id) {
gamepad_mask[id] = false;
}

platf::touch_port_t touch_port {
0, 0, 0, 0
};

static util::TaskPool::task_id_t task_id {};
static std::unordered_map<short, bool> key_press {};
static std::array<std::uint8_t, 5> mouse_press {};
Expand Down Expand Up @@ -88,15 +84,21 @@ struct gamepad_t {
};

struct input_t {
input_t(safe::mail_raw_t::event_t<platf::touch_port_t> touch_port_event)
: active_gamepad_state {}, gamepads(MAX_GAMEPADS), touch_port_event { std::move(touch_port_event) }, mouse_left_button_timeout {} {}
input_t(safe::mail_raw_t::event_t<input::touch_port_t> touch_port_event)
: active_gamepad_state {},
gamepads(MAX_GAMEPADS),
touch_port_event { std::move(touch_port_event) },
mouse_left_button_timeout {},
touch_port { 0, 0, 0, 0, 0, 0, 1.0f } {}

std::uint16_t active_gamepad_state;
std::vector<gamepad_t> gamepads;

safe::mail_raw_t::event_t<platf::touch_port_t> touch_port_event;
safe::mail_raw_t::event_t<input::touch_port_t> touch_port_event;

util::ThreadPool::task_id_t mouse_left_button_timeout;

input::touch_port_t touch_port;
};

using namespace std::literals;
Expand Down Expand Up @@ -204,6 +206,7 @@ void passthrough(std::shared_ptr<input_t> &input, PNV_ABS_MOUSE_MOVE_PACKET pack
}

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();
}
Expand Down Expand Up @@ -233,7 +236,7 @@ void passthrough(std::shared_ptr<input_t> &input, PNV_ABS_MOUSE_MOVE_PACKET pack
touch_port.env_width, touch_port.env_height
};

platf::abs_mouse(platf_input, abs_port, x - offsetX, y - offsetY); //touch_port, x * scale_x + offsetX, y * scale_y + offsetY);
platf::abs_mouse(platf_input, abs_port, (x - offsetX) * touch_port.scalar_inv, (y - offsetY) * touch_port.scalar_inv);
}

void passthrough(std::shared_ptr<input_t> &input, PNV_MOUSE_BUTTON_PACKET packet) {
Expand Down Expand Up @@ -567,7 +570,7 @@ void init() {
}

std::shared_ptr<input_t> alloc(safe::mail_t mail) {
auto input = std::make_shared<input_t>(mail->event<platf::touch_port_t>(mail::touch_port));
auto input = std::make_shared<input_t>(mail->event<input::touch_port_t>(mail::touch_port));

// Workaround to ensure new frames will be captured when a client connects
task_pool.pushDelayed([]() {
Expand Down
8 changes: 8 additions & 0 deletions sunshine/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "platform/common.h"
#include "thread_safe.h"

namespace input {

struct input_t;
Expand All @@ -19,6 +20,13 @@ void passthrough(std::shared_ptr<input_t> &input, std::vector<std::uint8_t> &&in
void init();

std::shared_ptr<input_t> alloc(safe::mail_t mail);

struct touch_port_t : public platf::touch_port_t {
int env_width, env_height;

// inverse of scalar used for aspect ratio
float scalar_inv;
};
} // namespace input

#endif //SUNSHINE_INPUT_H
2 changes: 0 additions & 2 deletions sunshine/platform/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ inline std::string_view from_pix_fmt(pix_fmt_e pix_fmt) {
// Dimensions for touchscreen input
struct touch_port_t {
int offset_x, offset_y;
int env_width, env_height;

int width, height;
};

Expand Down
8 changes: 4 additions & 4 deletions sunshine/platform/linux/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ struct input_raw_t {
void abs_mouse(input_t &input, const touch_port_t &touch_port, float x, float y) {
auto touchscreen = ((input_raw_t *)input.get())->touch_input.get();

auto scaled_x = (int)std::lround((x + touch_port.offset_x) * ((float)target_touch_port.env_width / (float)touch_port.env_width));
auto scaled_y = (int)std::lround((y + touch_port.offset_y) * ((float)target_touch_port.env_height / (float)touch_port.env_height));
auto scaled_x = (int)std::lround((x + touch_port.offset_x) * ((float)target_touch_port.width / (float)touch_port.width));
auto scaled_y = (int)std::lround((y + touch_port.offset_y) * ((float)target_touch_port.height / (float)touch_port.height));

libevdev_uinput_write_event(touchscreen, EV_ABS, ABS_X, scaled_x);
libevdev_uinput_write_event(touchscreen, EV_ABS, ABS_Y, scaled_y);
Expand Down Expand Up @@ -470,7 +470,7 @@ evdev_t touchscreen() {
input_absinfo absx {
0,
0,
target_touch_port.env_width,
target_touch_port.width,
1,
0,
28
Expand All @@ -479,7 +479,7 @@ evdev_t touchscreen() {
input_absinfo absy {
0,
0,
target_touch_port.env_height,
target_touch_port.height,
1,
0,
28
Expand Down
4 changes: 2 additions & 2 deletions sunshine/platform/windows/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ void abs_mouse(input_t &input, const touch_port_t &touch_port, float x, float y)
// MOUSEEVENTF_VIRTUALDESK maps to the entirety of the desktop rather than the primary desktop
MOUSEEVENTF_VIRTUALDESK;

auto scaled_x = std::lround((x + touch_port.offset_x) * ((float)target_touch_port.env_width / (float)touch_port.env_width));
auto scaled_y = std::lround((y + touch_port.offset_y) * ((float)target_touch_port.env_height / (float)touch_port.env_height));
auto scaled_x = std::lround((x + touch_port.offset_x) * ((float)target_touch_port.width / (float)touch_port.width));
auto scaled_y = std::lround((y + touch_port.offset_y) * ((float)target_touch_port.height / (float)touch_port.height));

mi.dx = scaled_x;
mi.dy = scaled_y;
Expand Down
16 changes: 9 additions & 7 deletions sunshine/video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern "C" {

#include "cbs.h"
#include "config.h"
#include "input.h"
#include "main.h"
#include "platform/common.h"
#include "round_robin.h"
Expand Down Expand Up @@ -353,7 +354,7 @@ struct sync_session_ctx_t {
safe::mail_raw_t::event_t<bool> shutdown_event;
safe::mail_raw_t::queue_t<packet_t> packets;
safe::mail_raw_t::event_t<idr_t> idr_events;
safe::mail_raw_t::event_t<platf::touch_port_t> touch_port_events;
safe::mail_raw_t::event_t<input::touch_port_t> touch_port_events;

config_t config;
int frame_nr;
Expand Down Expand Up @@ -1061,7 +1062,7 @@ void encode_run(
}
}

platf::touch_port_t make_port(platf::display_t *display, const config_t &config) {
input::touch_port_t make_port(platf::display_t *display, const config_t &config) {
float wd = display->width;
float hd = display->height;

Expand All @@ -1073,13 +1074,14 @@ platf::touch_port_t make_port(platf::display_t *display, const config_t &config)
auto w2 = scalar * wd;
auto h2 = scalar * hd;

return platf::touch_port_t {
return input::touch_port_t {
display->offset_x,
display->offset_y,
display->env_width,
display->env_height,
(int)w2,
(int)h2,
display->env_width,
display->env_height,
1.0f / scalar,
};
}

Expand Down Expand Up @@ -1315,7 +1317,7 @@ void capture_async(
int frame_nr = 1;
int key_frame_nr = 1;

auto touch_port_event = mail->event<platf::touch_port_t>(mail::touch_port);
auto touch_port_event = mail->event<input::touch_port_t>(mail::touch_port);

while(!shutdown_event->peek() && images->running()) {
// Wait for the main capture event when the display is being reinitialized
Expand Down Expand Up @@ -1379,7 +1381,7 @@ void capture(
mail->event<bool>(mail::shutdown),
mail::man->queue<packet_t>(mail::video_packets),
std::move(idr_events),
mail->event<platf::touch_port_t>(mail::touch_port),
mail->event<input::touch_port_t>(mail::touch_port),
config,
1,
1,
Expand Down

0 comments on commit b676009

Please sign in to comment.