I’m currently working on a mouse injector for emulators project I’ve forked on github.
The issue:
When after some seconds the application is minimized I get sluggish gamepad inputs and input delay.
When the application focus is restored, gamepad inputs works as normal after some seconds if I move the mouse a little. The application uses ManyMouse to read mouse inputs instead of SDL. Mouse inputs works flawless all the time.
It runs on Windows Terminal (console), I don’t know if this influences SDL behaviour.
I’m on Windows 11 version 24H2 BTW.
Code that uses SDL:
#include <stdint.h>
#include <stdbool.h>
#include <windows.h>
#include "joystick.h"
#include <SDL2/SDL.h>
#include <stdio.h>
SDL_Event event;
int quit = 0;
int16_t rx, ry;
void JOYSTICK_Quit(void);
SDL_GameController* controller = NULL;
uint8_t JOYSTICK_Init()
{
SDL_SetHint(SDL_HINT_JOYSTICK_THREAD, "1");
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,"1");
if (SDL_Init(SDL_INIT_GAMECONTROLLER) < 0) {
fprintf(stderr, "SDL could not initialize! SDL Error: %s\n", SDL_GetError());
return 1;
}
printf("SDL initialized successfully.\n");
controller = SDL_GameControllerOpen(0);
}
uint8_t JOYSTICK_check()
{
if(SDL_NumJoysticks() < 1){
return 1;
}else{
return 0; //joystick connected
}
}
void JOYSTICK_Quit(void)
{
SDL_Quit();
}
void handle_joystick_event(const SDL_Event* event) {
if (event->type == SDL_CONTROLLERAXISMOTION){
printf("%s %d axis %d moved to value: %d\n", SDL_GameControllerName(controller), event->caxis.which, event->caxis.axis, event->caxis.value);
if(event->caxis.axis == SDL_CONTROLLER_AXIS_RIGHTX)
if(event->caxis.value > 6000 || event->caxis.value < -6000)
rx = event->caxis.value;
else
rx = 0;
if(event->caxis.axis == SDL_CONTROLLER_AXIS_RIGHTY)
if(event->caxis.value > 6000 || event->caxis.value < -6000)
ry = event->caxis.value;
else
ry = 0;
}
}
void JOYSTICK_UPDATE(){
while (SDL_PollEvent(&event) != 0) {
handle_joystick_event(&event);
}
}
Additional info:
Application originally reads mouse input using ManyMouse library and injects into game memory (mainly used for game camera control).
On some cases, it is necessary to disable some native in-game functions for the injection function properly (eg. auto center camera functions). By doing that, usually, makes the game’s native gamepad support broken on that aspect of the game (eg. can’t move the camera using the gamepad anymore).
So, my approach was to implement gamepad input reading using SDL2 (I couldn’t make SDL3 work, I’m a noob programmer and got help from Google AI :sweat_smile: to get SDL working). I read gamepad inputs and use in parallel with the mouse inputs for the injector application.
Update:
I've removed SDL implementation and added Xinput gamepad read via #include <xinput.h> instead and got the same results as SDL did.
My guess it's ManyMouse does something specific on this file (windows_wminput.c) that make it function properly all the time.