(winraw_input) Simplify code

This commit is contained in:
libretroadmin 2024-09-10 18:18:26 +02:00
parent 5892551f75
commit 8ba57c77e0

View File

@ -105,16 +105,13 @@ static HWND winraw_create_window(WNDPROC wnd_proc)
{ {
HWND wnd; HWND wnd;
WNDCLASSA wc = {0}; WNDCLASSA wc = {0};
if (!(wc.hInstance = GetModuleHandleA(NULL))) if (!(wc.hInstance = GetModuleHandleA(NULL)))
return NULL; return NULL;
wc.lpfnWndProc = wnd_proc; wc.lpfnWndProc = wnd_proc;
wc.lpszClassName = "winraw-input"; wc.lpszClassName = "winraw-input";
if ( !RegisterClassA(&wc) if ( !RegisterClassA(&wc)
&& GetLastError() != ERROR_CLASS_ALREADY_EXISTS) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
return NULL; return NULL;
if (!(wnd = CreateWindowExA(0, wc.lpszClassName, if (!(wnd = CreateWindowExA(0, wc.lpszClassName,
NULL, 0, 0, 0, 0, 0, NULL, 0, 0, 0, 0, 0,
HWND_MESSAGE, NULL, NULL, NULL))) HWND_MESSAGE, NULL, NULL, NULL)))
@ -122,42 +119,13 @@ static HWND winraw_create_window(WNDPROC wnd_proc)
UnregisterClassA(wc.lpszClassName, NULL); UnregisterClassA(wc.lpszClassName, NULL);
return NULL; return NULL;
} }
return wnd; return wnd;
} }
static void winraw_destroy_window(HWND wnd)
{
if (!wnd)
return;
DestroyWindow(wnd);
UnregisterClassA("winraw-input", NULL);
}
static BOOL winraw_set_keyboard_input(HWND window)
{
RAWINPUTDEVICE rid;
settings_t *settings;
settings = config_get_ptr();
rid.dwFlags = window ? 0 : RIDEV_REMOVE;
rid.hwndTarget = window;
rid.usUsagePage = 0x01; /* Generic desktop */
rid.usUsage = 0x06; /* Keyboard */
if (settings->bools.input_nowinkey_enable)
rid.dwFlags |= RIDEV_NOHOTKEYS; /* Disable win keys while focused */
return RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE));
}
static void winraw_log_mice_info(winraw_mouse_t *mice, unsigned mouse_cnt) static void winraw_log_mice_info(winraw_mouse_t *mice, unsigned mouse_cnt)
{ {
unsigned i; unsigned i;
char name[256]; char name[256];
char prod_name[128];
wchar_t prod_buf[128];
UINT name_size = sizeof(name); UINT name_size = sizeof(name);
name[0] = '\0'; name[0] = '\0';
@ -169,9 +137,6 @@ static void winraw_log_mice_info(winraw_mouse_t *mice, unsigned mouse_cnt)
if (r == (UINT)-1 || r == 0) if (r == (UINT)-1 || r == 0)
name[0] = '\0'; name[0] = '\0';
prod_name[0] = '\0';
prod_buf[0] = '\0';
if (name[0]) if (name[0])
{ {
HANDLE hhid = CreateFile(name, HANDLE hhid = CreateFile(name,
@ -180,13 +145,13 @@ static void winraw_log_mice_info(winraw_mouse_t *mice, unsigned mouse_cnt)
if (hhid != INVALID_HANDLE_VALUE) if (hhid != INVALID_HANDLE_VALUE)
{ {
wchar_t prod_buf[128];
prod_buf[0] = '\0';
if (HidD_GetProductString(hhid, prod_buf, sizeof(prod_buf))) if (HidD_GetProductString(hhid, prod_buf, sizeof(prod_buf)))
wcstombs(prod_name, prod_buf, sizeof(prod_name)); wcstombs(name, prod_buf, sizeof(name));
} }
CloseHandle(hhid); CloseHandle(hhid);
} }
if (prod_name[0])
strlcpy(name, prod_name, sizeof(name));
if (!name[0]) if (!name[0])
strlcpy(name, "<name not found>", sizeof(name)); strlcpy(name, "<name not found>", sizeof(name));
@ -261,25 +226,11 @@ error:
return false; return false;
} }
static BOOL winraw_set_mouse_input(HWND window)
{
RAWINPUTDEVICE rid;
rid.dwFlags = (window) ? 0 : RIDEV_REMOVE;
rid.hwndTarget = window;
rid.usUsagePage = 0x01; /* generic desktop */
rid.usUsage = 0x02; /* mouse */
return RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE));
}
static int16_t winraw_lightgun_aiming_state(winraw_input_t *wr, static int16_t winraw_lightgun_aiming_state(winraw_input_t *wr,
winraw_mouse_t *mouse, winraw_mouse_t *mouse,
unsigned port, unsigned id) unsigned port, unsigned id)
{ {
struct video_viewport vp; struct video_viewport vp;
const int edge_detect = 32700;
bool inside = false;
int16_t res_x = 0; int16_t res_x = 0;
int16_t res_y = 0; int16_t res_y = 0;
int16_t res_screen_x = 0; int16_t res_screen_x = 0;
@ -292,12 +243,12 @@ static int16_t winraw_lightgun_aiming_state(winraw_input_t *wr,
vp.full_width = 0; vp.full_width = 0;
vp.full_height = 0; vp.full_height = 0;
if (!(video_driver_translate_coord_viewport_wrap( if ((video_driver_translate_coord_viewport_wrap(
&vp, mouse->x, mouse->y, &vp, mouse->x, mouse->y,
&res_x, &res_y, &res_screen_x, &res_screen_y))) &res_x, &res_y, &res_screen_x, &res_screen_y)))
return 0; {
const int edge_detect = 32700;
inside = (res_x >= -edge_detect) bool inside = (res_x >= -edge_detect)
&& (res_y >= -edge_detect) && (res_y >= -edge_detect)
&& (res_x <= edge_detect) && (res_x <= edge_detect)
&& (res_y <= edge_detect); && (res_y <= edge_detect);
@ -317,6 +268,7 @@ static int16_t winraw_lightgun_aiming_state(winraw_input_t *wr,
default: default:
break; break;
} }
}
return 0; return 0;
} }
@ -524,25 +476,22 @@ static void winraw_update_mouse_state(winraw_input_t *wr,
static LRESULT CALLBACK winraw_callback( static LRESULT CALLBACK winraw_callback(
HWND wnd, UINT msg, WPARAM wpar, LPARAM lpar) HWND wnd, UINT msg, WPARAM wpar, LPARAM lpar)
{ {
unsigned i;
unsigned mcode, flags, down, mod;
static uint8_t data[1024]; static uint8_t data[1024];
RAWINPUT *ri = (RAWINPUT*)data; RAWINPUT *ri = (RAWINPUT*)data;
UINT size = sizeof(data); UINT size = sizeof(data);
winraw_input_t *wr = (winraw_input_t*)(LONG_PTR)
GetWindowLongPtr(wnd, GWLP_USERDATA);
if (msg != WM_INPUT) if (msg != WM_INPUT)
return DefWindowProcA(wnd, msg, wpar, lpar); return DefWindowProcA(wnd, msg, wpar, lpar);
if ( if (!(
GET_RAWINPUT_CODE_WPARAM(wpar) != RIM_INPUT /* app is in the background */ GET_RAWINPUT_CODE_WPARAM(wpar) != RIM_INPUT /* app is in the background */
|| GetRawInputData((HRAWINPUT)lpar, RID_INPUT, || GetRawInputData((HRAWINPUT)lpar, RID_INPUT,
data, &size, sizeof(RAWINPUTHEADER)) == (UINT)-1) data, &size, sizeof(RAWINPUTHEADER)) == (UINT)-1))
{ {
DefWindowProcA(wnd, msg, wpar, lpar); unsigned i;
return 0; unsigned mcode, flags, down, mod;
} winraw_input_t *wr = (winraw_input_t*)(LONG_PTR)
GetWindowLongPtr(wnd, GWLP_USERDATA);
switch (ri->header.dwType) switch (ri->header.dwType)
{ {
@ -613,6 +562,7 @@ static LRESULT CALLBACK winraw_callback(
} }
break; break;
} }
}
DefWindowProcA(wnd, msg, wpar, lpar); DefWindowProcA(wnd, msg, wpar, lpar);
return 0; return 0;
@ -620,6 +570,8 @@ static LRESULT CALLBACK winraw_callback(
static void *winraw_init(const char *joypad_driver) static void *winraw_init(const char *joypad_driver)
{ {
RAWINPUTDEVICE rid;
settings_t *settings = config_get_ptr();
winraw_input_t *wr = (winraw_input_t *) winraw_input_t *wr = (winraw_input_t *)
calloc(1, sizeof(winraw_input_t)); calloc(1, sizeof(winraw_input_t));
@ -642,10 +594,22 @@ static void *winraw_init(const char *joypad_driver)
memcpy(wr->mice, g_mice, wr->mouse_cnt * sizeof(winraw_mouse_t)); memcpy(wr->mice, g_mice, wr->mouse_cnt * sizeof(winraw_mouse_t));
} }
if (!winraw_set_keyboard_input(wr->window)) rid.dwFlags = (wr->window) ? 0 : RIDEV_REMOVE;
rid.hwndTarget = wr->window;
rid.usUsagePage = 0x01; /* Generic desktop */
rid.usUsage = 0x06; /* Keyboard */
if (settings->bools.input_nowinkey_enable)
rid.dwFlags |= RIDEV_NOHOTKEYS; /* Disable win keys while focused */
if (!RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE)))
goto error; goto error;
if (!winraw_set_mouse_input(wr->window)) rid.dwFlags = wr->window ? 0 : RIDEV_REMOVE;
rid.hwndTarget = wr->window;
rid.usUsagePage = 0x01; /* generic desktop */
rid.usUsage = 0x02; /* mouse */
if (!RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE)))
goto error; goto error;
SetWindowLongPtr(wr->window, GWLP_USERDATA, (LONG_PTR)wr); SetWindowLongPtr(wr->window, GWLP_USERDATA, (LONG_PTR)wr);
@ -655,9 +619,23 @@ static void *winraw_init(const char *joypad_driver)
error: error:
if (wr && wr->window) if (wr && wr->window)
{ {
winraw_set_mouse_input(NULL); rid.dwFlags = RIDEV_REMOVE;
winraw_set_keyboard_input(NULL); rid.hwndTarget = NULL;
winraw_destroy_window(wr->window); rid.usUsagePage = 0x01; /* generic desktop */
rid.usUsage = 0x02; /* mouse */
RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE));
rid.dwFlags = RIDEV_REMOVE;
rid.hwndTarget = NULL;
rid.usUsagePage = 0x01; /* Generic desktop */
rid.usUsage = 0x06; /* Keyboard */
if (settings->bools.input_nowinkey_enable)
rid.dwFlags |= RIDEV_NOHOTKEYS; /* Disable win keys while focused */
RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE));
DestroyWindow(wr->window);
UnregisterClassA("winraw-input", NULL);
} }
free(g_mice); free(g_mice);
if (wr) if (wr)
@ -744,13 +722,12 @@ static int16_t winraw_input_state(
unsigned idx, unsigned idx,
unsigned id) unsigned id)
{ {
int16_t ret = 0;
settings_t *settings = NULL;
winraw_mouse_t *mouse = NULL; winraw_mouse_t *mouse = NULL;
winraw_input_t *wr = (winraw_input_t*)data;
if (port < MAX_USERS) if (port < MAX_USERS)
{ {
int16_t ret = 0;
winraw_input_t *wr = (winraw_input_t*)data;
bool process_mouse = bool process_mouse =
(device == RETRO_DEVICE_JOYPAD) (device == RETRO_DEVICE_JOYPAD)
|| (device == RETRO_DEVICE_MOUSE) || (device == RETRO_DEVICE_MOUSE)
@ -762,7 +739,7 @@ static int16_t winraw_input_state(
if (process_mouse) if (process_mouse)
{ {
unsigned i; unsigned i;
settings = config_get_ptr(); settings_t *settings = config_get_ptr();
for (i = 0; i < wr->mouse_cnt; ++i) for (i = 0; i < wr->mouse_cnt; ++i)
{ {
if (i == settings->uints.input_mouse_index[port]) if (i == settings->uints.input_mouse_index[port])
@ -1071,12 +1048,32 @@ bool winraw_handle_message(UINT msg,
static void winraw_free(void *data) static void winraw_free(void *data)
{ {
RAWINPUTDEVICE rid;
settings_t *settings = config_get_ptr();
winraw_input_t *wr = (winraw_input_t*)data; winraw_input_t *wr = (winraw_input_t*)data;
winraw_set_mouse_input(NULL); rid.dwFlags = RIDEV_REMOVE;
winraw_set_keyboard_input(NULL); rid.hwndTarget = NULL;
rid.usUsagePage = 0x01; /* generic desktop */
rid.usUsage = 0x02; /* mouse */
RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE));
rid.dwFlags = RIDEV_REMOVE;
rid.hwndTarget = NULL;
rid.usUsagePage = 0x01; /* Generic desktop */
rid.usUsage = 0x06; /* Keyboard */
if (settings->bools.input_nowinkey_enable)
rid.dwFlags |= RIDEV_NOHOTKEYS; /* Disable win keys while focused */
RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE));
SetWindowLongPtr(wr->window, GWLP_USERDATA, 0); SetWindowLongPtr(wr->window, GWLP_USERDATA, 0);
winraw_destroy_window(wr->window); if (wr->window)
{
DestroyWindow(wr->window);
UnregisterClassA("winraw-input", NULL);
}
free(g_mice); free(g_mice);
free(wr->mice); free(wr->mice);
@ -1095,24 +1092,29 @@ static uint64_t winraw_get_capabilities(void *u)
static void winraw_grab_mouse(void *d, bool state) static void winraw_grab_mouse(void *d, bool state)
{ {
RAWINPUTDEVICE rid;
winraw_input_t *wr = (winraw_input_t*)d; winraw_input_t *wr = (winraw_input_t*)d;
bool curr_state = (wr->flags & WRAW_INP_FLG_MOUSE_GRAB) > 0; bool curr_state = (wr->flags & WRAW_INP_FLG_MOUSE_GRAB) > 0;
if (curr_state == state) if (curr_state == state)
return; return;
if (!winraw_set_mouse_input(wr->window)) rid.dwFlags = (wr->window) ? 0 : RIDEV_REMOVE;
return; rid.hwndTarget = wr->window;
rid.usUsagePage = 0x01; /* generic desktop */
rid.usUsage = 0x02; /* mouse */
if (RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE)))
{
if (state) if (state)
wr->flags |= WRAW_INP_FLG_MOUSE_GRAB; wr->flags |= WRAW_INP_FLG_MOUSE_GRAB;
else else
wr->flags &= ~WRAW_INP_FLG_MOUSE_GRAB; wr->flags &= ~WRAW_INP_FLG_MOUSE_GRAB;
#ifndef _XBOX #ifndef _XBOX
win32_clip_window(state); win32_clip_window(state);
#endif #endif
} }
}
input_driver_t input_winraw = { input_driver_t input_winraw = {
winraw_init, winraw_init,