Skip to content

Commit

Permalink
Add configurable mouse sensitivity
Browse files Browse the repository at this point in the history
The `--mouse-sensitivity`/`-s` command line option allows the user to
set the value `g_mouseSensitivity` which is used to scale the mouse
movement in `wlserver_mousemotion`.
  • Loading branch information
dexgs committed Aug 29, 2023
1 parent 23446dd commit d70bf81
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const struct option *gamescope_options = (struct option[]){
{ "rt", no_argument, nullptr, 0 },
{ "prefer-vk-device", required_argument, 0 },
{ "expose-wayland", no_argument, 0 },
{ "mouse-sensitivity", required_argument, nullptr, 's' },

{ "headless", no_argument, 0 },

Expand Down Expand Up @@ -146,6 +147,7 @@ const char usage[] =
" nis => NVIDIA Image Scaling v1.0.3\n"
" --sharpness, --fsr-sharpness upscaler sharpness from 0 (max) to 20 (min)\n"
" --expose-wayland support wayland clients using xdg-shell\n"
" -s, --mouse-sensitivity multiply mouse movement by given decimal number\n"
" --headless use headless backend (no window, no DRM output)\n"
" --cursor path to default cursor image\n"
" -R, --ready-fd notify FD when ready\n"
Expand Down Expand Up @@ -247,6 +249,8 @@ bool g_bHeadless = false;

bool g_bGrabbed = false;

float g_mouseSensitivity = 1.0;

GamescopeUpscaleFilter g_upscaleFilter = GamescopeUpscaleFilter::LINEAR;
GamescopeUpscaleScaler g_upscaleScaler = GamescopeUpscaleScaler::AUTO;

Expand Down Expand Up @@ -521,6 +525,9 @@ int main(int argc, char **argv)
case 'g':
g_bGrabbed = true;
break;
case 's':
g_mouseSensitivity = atof( optarg );
break;
case 0: // long options without a short option
opt_name = gamescope_options[opt_index].name;
if (strcmp(opt_name, "help") == 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ extern bool g_bFullscreen;

extern bool g_bGrabbed;

extern float g_mouseSensitivity;

enum class GamescopeUpscaleFilter : uint32_t
{
LINEAR = 0,
Expand Down
17 changes: 16 additions & 1 deletion src/wlserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1440,8 +1440,23 @@ void wlserver_mousemotion( int x, int y, uint32_t time )
auto server = steamcompmgr_get_focused_server();
if ( server != NULL )
{
XTestFakeRelativeMotionEvent( server->get_xdisplay(), x, y, CurrentTime );
float dx = (float) x * g_mouseSensitivity;
float dy = (float) y * g_mouseSensitivity;

// Accumulate sub-pixel increments to avoid jerky cursor movement
static float accum_x = 0.0;
static float accum_y = 0.0;

accum_x += modff( dx, &dx );
accum_y += modff( dy, &dy );

XTestFakeRelativeMotionEvent( server->get_xdisplay(), (int) (dx + accum_x), (int) (dy + accum_y), CurrentTime );
XFlush( server->get_xdisplay() );

// Integer value from `modff` is ignored, but argument can't be NULL
float _;
accum_x = modff( accum_x, &_ );
accum_y = modff( accum_y, &_ );
}
}

Expand Down

0 comments on commit d70bf81

Please sign in to comment.