Skip to content

Commit b676009

Browse files
author
loki
committed
Fix incorrect scaling for absolute mouse coordinates
1 parent 1eda45a commit b676009

File tree

6 files changed

+35
-24
lines changed

6 files changed

+35
-24
lines changed

sunshine/input.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ void free_id(std::bitset<N> &gamepad_mask, int id) {
4646
gamepad_mask[id] = false;
4747
}
4848

49-
platf::touch_port_t touch_port {
50-
0, 0, 0, 0
51-
};
52-
5349
static util::TaskPool::task_id_t task_id {};
5450
static std::unordered_map<short, bool> key_press {};
5551
static std::array<std::uint8_t, 5> mouse_press {};
@@ -88,15 +84,21 @@ struct gamepad_t {
8884
};
8985

9086
struct input_t {
91-
input_t(safe::mail_raw_t::event_t<platf::touch_port_t> touch_port_event)
92-
: active_gamepad_state {}, gamepads(MAX_GAMEPADS), touch_port_event { std::move(touch_port_event) }, mouse_left_button_timeout {} {}
87+
input_t(safe::mail_raw_t::event_t<input::touch_port_t> touch_port_event)
88+
: active_gamepad_state {},
89+
gamepads(MAX_GAMEPADS),
90+
touch_port_event { std::move(touch_port_event) },
91+
mouse_left_button_timeout {},
92+
touch_port { 0, 0, 0, 0, 0, 0, 1.0f } {}
9393

9494
std::uint16_t active_gamepad_state;
9595
std::vector<gamepad_t> gamepads;
9696

97-
safe::mail_raw_t::event_t<platf::touch_port_t> touch_port_event;
97+
safe::mail_raw_t::event_t<input::touch_port_t> touch_port_event;
9898

9999
util::ThreadPool::task_id_t mouse_left_button_timeout;
100+
101+
input::touch_port_t touch_port;
100102
};
101103

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

206208
auto &touch_port_event = input->touch_port_event;
209+
auto &touch_port = input->touch_port;
207210
if(touch_port_event->peek()) {
208211
touch_port = *touch_port_event->pop();
209212
}
@@ -233,7 +236,7 @@ void passthrough(std::shared_ptr<input_t> &input, PNV_ABS_MOUSE_MOVE_PACKET pack
233236
touch_port.env_width, touch_port.env_height
234237
};
235238

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

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

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

572575
// Workaround to ensure new frames will be captured when a client connects
573576
task_pool.pushDelayed([]() {

sunshine/input.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "platform/common.h"
99
#include "thread_safe.h"
10+
1011
namespace input {
1112

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

2122
std::shared_ptr<input_t> alloc(safe::mail_t mail);
23+
24+
struct touch_port_t : public platf::touch_port_t {
25+
int env_width, env_height;
26+
27+
// inverse of scalar used for aspect ratio
28+
float scalar_inv;
29+
};
2230
} // namespace input
2331

2432
#endif //SUNSHINE_INPUT_H

sunshine/platform/common.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ inline std::string_view from_pix_fmt(pix_fmt_e pix_fmt) {
106106
// Dimensions for touchscreen input
107107
struct touch_port_t {
108108
int offset_x, offset_y;
109-
int env_width, env_height;
110-
111109
int width, height;
112110
};
113111

sunshine/platform/linux/input.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ struct input_raw_t {
145145
void abs_mouse(input_t &input, const touch_port_t &touch_port, float x, float y) {
146146
auto touchscreen = ((input_raw_t *)input.get())->touch_input.get();
147147

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

151151
libevdev_uinput_write_event(touchscreen, EV_ABS, ABS_X, scaled_x);
152152
libevdev_uinput_write_event(touchscreen, EV_ABS, ABS_Y, scaled_y);
@@ -470,7 +470,7 @@ evdev_t touchscreen() {
470470
input_absinfo absx {
471471
0,
472472
0,
473-
target_touch_port.env_width,
473+
target_touch_port.width,
474474
1,
475475
0,
476476
28
@@ -479,7 +479,7 @@ evdev_t touchscreen() {
479479
input_absinfo absy {
480480
0,
481481
0,
482-
target_touch_port.env_height,
482+
target_touch_port.height,
483483
1,
484484
0,
485485
28

sunshine/platform/windows/input.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ void abs_mouse(input_t &input, const touch_port_t &touch_port, float x, float y)
123123
// MOUSEEVENTF_VIRTUALDESK maps to the entirety of the desktop rather than the primary desktop
124124
MOUSEEVENTF_VIRTUALDESK;
125125

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

129129
mi.dx = scaled_x;
130130
mi.dy = scaled_y;

sunshine/video.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern "C" {
1212

1313
#include "cbs.h"
1414
#include "config.h"
15+
#include "input.h"
1516
#include "main.h"
1617
#include "platform/common.h"
1718
#include "round_robin.h"
@@ -353,7 +354,7 @@ struct sync_session_ctx_t {
353354
safe::mail_raw_t::event_t<bool> shutdown_event;
354355
safe::mail_raw_t::queue_t<packet_t> packets;
355356
safe::mail_raw_t::event_t<idr_t> idr_events;
356-
safe::mail_raw_t::event_t<platf::touch_port_t> touch_port_events;
357+
safe::mail_raw_t::event_t<input::touch_port_t> touch_port_events;
357358

358359
config_t config;
359360
int frame_nr;
@@ -1061,7 +1062,7 @@ void encode_run(
10611062
}
10621063
}
10631064

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

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

1076-
return platf::touch_port_t {
1077+
return input::touch_port_t {
10771078
display->offset_x,
10781079
display->offset_y,
1079-
display->env_width,
1080-
display->env_height,
10811080
(int)w2,
10821081
(int)h2,
1082+
display->env_width,
1083+
display->env_height,
1084+
1.0f / scalar,
10831085
};
10841086
}
10851087

@@ -1315,7 +1317,7 @@ void capture_async(
13151317
int frame_nr = 1;
13161318
int key_frame_nr = 1;
13171319

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

13201322
while(!shutdown_event->peek() && images->running()) {
13211323
// Wait for the main capture event when the display is being reinitialized
@@ -1379,7 +1381,7 @@ void capture(
13791381
mail->event<bool>(mail::shutdown),
13801382
mail::man->queue<packet_t>(mail::video_packets),
13811383
std::move(idr_events),
1382-
mail->event<platf::touch_port_t>(mail::touch_port),
1384+
mail->event<input::touch_port_t>(mail::touch_port),
13831385
config,
13841386
1,
13851387
1,

0 commit comments

Comments
 (0)