From d66608f77b5e59f5297738884755f012e1873590 Mon Sep 17 00:00:00 2001 From: GH Cao Date: Tue, 4 Feb 2020 22:29:01 +0800 Subject: [PATCH] win rawinput: move all GetRawInputData into one --- src/win/win.h | 8 +++--- src/win/win_joystick.cpp | 1 + ...stick_xinput.cpp => win_joystick_xinput.c} | 3 +- src/win/win_keyboard.c | 25 ++--------------- src/win/win_mouse.c | 28 ++----------------- src/win/win_ui.c | 28 +++++++++++++++---- 6 files changed, 34 insertions(+), 59 deletions(-) rename src/win/{win_joystick_xinput.cpp => win_joystick_xinput.c} (99%) diff --git a/src/win/win.h b/src/win/win.h index 6c8444a33..5bbb6ec95 100644 --- a/src/win/win.h +++ b/src/win/win.h @@ -114,13 +114,13 @@ extern int get_vidpause(void); extern void show_cursor(int); extern void keyboard_getkeymap(void); -extern void keyboard_handle(LPARAM lParam, int infocus); +extern void keyboard_handle(PRAWINPUT raw); extern void win_mouse_init(void); extern void win_mouse_close(void); -#ifndef USE_DINPUT -extern void win_mouse_handle(LPARAM lParam, int infocus); -#endif +extern void win_mouse_handle(PRAWINPUT raw); + +extern void win_joystick_handle(PRAWINPUT raw); extern void win_notify_dlg_open(void); extern void win_notify_dlg_closed(void); diff --git a/src/win/win_joystick.cpp b/src/win/win_joystick.cpp index 499f19903..dea7c70a6 100644 --- a/src/win/win_joystick.cpp +++ b/src/win/win_joystick.cpp @@ -336,3 +336,4 @@ void joystick_process(void) } } +void win_joystick_handle(PRAWINPUT raw) {} diff --git a/src/win/win_joystick_xinput.cpp b/src/win/win_joystick_xinput.c similarity index 99% rename from src/win/win_joystick_xinput.cpp rename to src/win/win_joystick_xinput.c index 237cab23c..cd0b1cea3 100644 --- a/src/win/win_joystick_xinput.cpp +++ b/src/win/win_joystick_xinput.c @@ -18,7 +18,7 @@ * Copyright 2016-2018 Miran Grca. * Copyright 2019 GH Cao. */ -#include +#include #include #include #include @@ -260,3 +260,4 @@ void joystick_process(void) } } +void win_joystick_handle(PRAWINPUT raw) {} diff --git a/src/win/win_keyboard.c b/src/win/win_keyboard.c index eaf4ddac4..b1700b79d 100644 --- a/src/win/win_keyboard.c +++ b/src/win/win_keyboard.c @@ -108,30 +108,12 @@ keyboard_getkeymap(void) void -keyboard_handle(LPARAM lParam, int infocus) +keyboard_handle(PRAWINPUT raw) { - uint32_t ri_size = 0; - UINT size; - RAWINPUT *raw; USHORT scancode; static int recv_lalt = 0, recv_ralt = 0, recv_tab = 0; - if (! infocus) return; - - GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, - &size, sizeof(RAWINPUTHEADER)); - - raw = malloc(size); - if (raw == NULL) return; - - /* Here we read the raw input data for the keyboard */ - ri_size = GetRawInputData((HRAWINPUT)(lParam), RID_INPUT, - raw, &size, sizeof(RAWINPUTHEADER)); - if (ri_size != size) return; - - /* If the input is keyboard, we process it */ - if (raw->header.dwType == RIM_TYPEKEYBOARD) { - RAWKEYBOARD rawKB = raw->data.keyboard; + RAWKEYBOARD rawKB = raw->data.keyboard; scancode = rawKB.MakeCode; /* If it's not a scan code that starts with 0xE1 */ @@ -217,7 +199,4 @@ keyboard_handle(LPARAM lParam, int infocus) if (scancode != 0xFFFF) keyboard_input(!(rawKB.Flags & RI_KEY_BREAK), scancode); } - } - - free(raw); } diff --git a/src/win/win_mouse.c b/src/win/win_mouse.c index 2ca004e9f..b2cf2eeba 100644 --- a/src/win/win_mouse.c +++ b/src/win/win_mouse.c @@ -58,32 +58,11 @@ win_mouse_init(void) } void -win_mouse_handle(LPARAM lParam, int infocus) +win_mouse_handle(PRAWINPUT raw) { - uint32_t ri_size = 0; - UINT size; - RAWINPUT *raw; - RAWMOUSE state; + RAWMOUSE state = raw->data.mouse; static int x, y; - if (! infocus) return; - - GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, - &size, sizeof(RAWINPUTHEADER)); - - raw = (RAWINPUT*)malloc(size); - if (raw == NULL) return; - - /* Here we read the raw input data for the mouse */ - ri_size = GetRawInputData((HRAWINPUT)(lParam), RID_INPUT, - raw, &size, sizeof(RAWINPUTHEADER)); - if (ri_size != size) goto err; - - /* If the input is not a mouse, we ignore it */ - if (raw->header.dwType != RIM_TYPEMOUSE) goto err; - - state = raw->data.mouse; - /* read mouse buttons and wheel */ if (state.usButtonFlags & RI_MOUSE_LEFT_BUTTON_DOWN) mousestate.buttons |= 1; @@ -120,9 +99,6 @@ win_mouse_handle(LPARAM lParam, int infocus) mousestate.dx += state.lLastX; mousestate.dy += state.lLastY; } - - err: - free(raw); } void diff --git a/src/win/win_ui.c b/src/win/win_ui.c index 812e4c2cb..608ccb775 100644 --- a/src/win/win_ui.c +++ b/src/win/win_ui.c @@ -1238,12 +1238,30 @@ input_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INPUT: - keyboard_handle(lParam, infocus); -#ifndef USE_DINPUT - win_mouse_handle(lParam, infocus); -#endif - break; + if (infocus) { + UINT size = 0; + PRAWINPUT raw = NULL; + /* Here we read the raw input data */ + GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER)); + raw = (PRAWINPUT)malloc(size); + if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, raw, &size, sizeof(RAWINPUTHEADER)) == size) { + switch(raw->header.dwType) + { + case RIM_TYPEKEYBOARD: + keyboard_handle(raw); + break; + case RIM_TYPEMOUSE: + win_mouse_handle(raw); + break; + case RIM_TYPEHID: + win_joystick_handle(raw); + break; + } + } + free(raw); + } + break; case WM_SETFOCUS: infocus = 1; if (! hook_enabled) {