From 9383b34e102afc64a1dd355a84f63fba502c1afb Mon Sep 17 00:00:00 2001 From: Toni Wilen Date: Wed, 27 Mar 2024 20:18:09 +0200 Subject: [PATCH] Release rawinput devices when GUI or debugger is active. --- od-win32/dinput.cpp | 72 +++++++++++++++++-------------------------- od-win32/win32gfx.cpp | 3 +- 2 files changed, 30 insertions(+), 45 deletions(-) diff --git a/od-win32/dinput.cpp b/od-win32/dinput.cpp index 0ed0e4e9..83d78102 100644 --- a/od-win32/dinput.cpp +++ b/od-win32/dinput.cpp @@ -410,9 +410,8 @@ static void fixthings_mouse (struct didata *did) static int rawinput_available; static bool rawinput_registered; -static int rawinput_reg; -static int doregister_rawinput (bool add) +static int doregister_rawinput(void) { struct AmigaMonitor *mon = &AMonitors[0]; int num; @@ -421,37 +420,27 @@ static int doregister_rawinput (bool add) if (!rawinput_available) return 0; - rawinput_registered = add; - memset (rid, 0, sizeof rid); num = 0; /* mouse */ rid[num].usUsagePage = 1; rid[num].usUsage = 2; - if (!add) { - rid[num].dwFlags = RIDEV_REMOVE; - } else { - if (mon->hMainWnd) { - rid[num].dwFlags = RIDEV_INPUTSINK; - rid[num].hwndTarget = mon->hMainWnd; - } - rid[num].dwFlags |= RIDEV_DEVNOTIFY; + if (mon->hMainWnd) { + rid[num].dwFlags = RIDEV_INPUTSINK; + rid[num].hwndTarget = mon->hMainWnd; } + rid[num].dwFlags |= RIDEV_DEVNOTIFY; num++; /* keyboard */ if (!rp_isactive()) { rid[num].usUsagePage = 1; rid[num].usUsage = 6; - if (!add) { - rid[num].dwFlags = RIDEV_REMOVE; - } else { - if (mon->hMainWnd) { - rid[num].dwFlags = RIDEV_INPUTSINK; - rid[num].hwndTarget = mon->hMainWnd; - } - rid[num].dwFlags |= RIDEV_NOHOTKEYS | RIDEV_DEVNOTIFY; + if (mon->hMainWnd) { + rid[num].dwFlags = RIDEV_INPUTSINK; + rid[num].hwndTarget = mon->hMainWnd; } + rid[num].dwFlags |= RIDEV_NOHOTKEYS | RIDEV_DEVNOTIFY; num++; /* joystick */ @@ -460,29 +449,21 @@ static int doregister_rawinput (bool add) // game pad rid[num].usUsagePage = 1; rid[num].usUsage = 4; - if (!add) { - rid[num].dwFlags = RIDEV_REMOVE; - } else { - if (mon->hMainWnd) { - rid[num].dwFlags = RIDEV_INPUTSINK; - rid[num].hwndTarget = mon->hMainWnd; - } - rid[num].dwFlags |= RIDEV_DEVNOTIFY; + if (mon->hMainWnd) { + rid[num].dwFlags = RIDEV_INPUTSINK; + rid[num].hwndTarget = mon->hMainWnd; } + rid[num].dwFlags |= RIDEV_DEVNOTIFY; num++; // joystick rid[num].usUsagePage = 1; rid[num].usUsage = 5; - if (!add) { - rid[num].dwFlags = RIDEV_REMOVE; - } else { - if (mon->hMainWnd) { - rid[num].dwFlags = RIDEV_INPUTSINK; - rid[num].hwndTarget = mon->hMainWnd; - } - rid[num].dwFlags |= RIDEV_DEVNOTIFY; + if (mon->hMainWnd) { + rid[num].dwFlags = RIDEV_INPUTSINK; + rid[num].hwndTarget = mon->hMainWnd; } + rid[num].dwFlags |= RIDEV_DEVNOTIFY; num++; } @@ -522,28 +503,28 @@ static int doregister_rawinput (bool add) write_log (_T("RegisterRawInputDevices: NUM=%d HWND=%p\n"), num, hMainWnd); #endif - rawinput_reg = num; if (RegisterRawInputDevices (rid, num, sizeof(RAWINPUTDEVICE)) == FALSE) { - write_log (_T("RAWINPUT %sregistration failed %d\n"), - add ? _T("") : _T("un"), GetLastError ()); + write_log (_T("RAWINPUT %sregistration failed %d\n"), GetLastError()); return 0; } + rawinput_registered = 1; return 1; } void rawinput_alloc(void) { if (!rawinput_registered) { - doregister_rawinput(true); + doregister_rawinput();; + //write_log("RAWINPUT ALLOC\n"); } - //write_log("rawinput_alloc\n"); } void rawinput_release(void) { UINT num = 0; + int cnt = 0; int v = GetRegisteredRawInputDevices(NULL, &num, sizeof(RAWINPUTDEVICE)); - if (num) { + if ((v >= 0 || (v == -1 && GetLastError() == ERROR_INSUFFICIENT_BUFFER)) && num > 0) { PRAWINPUTDEVICE devs = xcalloc(RAWINPUTDEVICE, num); if (devs) { int v = GetRegisteredRawInputDevices(devs, &num, sizeof(RAWINPUTDEVICE)); @@ -552,15 +533,18 @@ void rawinput_release(void) PRAWINPUTDEVICE dev = devs + i; dev->dwFlags = RIDEV_REMOVE; dev->hwndTarget = NULL; + cnt++; } - RegisterRawInputDevices(devs, num, sizeof(RAWINPUTDEVICE)); + if (!RegisterRawInputDevices(devs, num, sizeof(RAWINPUTDEVICE))) { + write_log("RegisterRawInputDevices RIDEV_REMOVE error %08x\n", GetLastError()); + } } xfree(devs); } } rawinput_registered = false; - //write_log("rawinput_free\n"); + //write_log("RAWINPUT FREE %d\n", cnt); } static void cleardid (struct didata *did) diff --git a/od-win32/win32gfx.cpp b/od-win32/win32gfx.cpp index 39388a80..9e1d93fe 100644 --- a/od-win32/win32gfx.cpp +++ b/od-win32/win32gfx.cpp @@ -3665,6 +3665,7 @@ static int create_windows(struct AmigaMonitor *mon) return 1; } + rawinput_release(); gfx_lock(); D3D_free(mon->monitor_id, false); gfx_unlock(); @@ -3869,7 +3870,6 @@ static int create_windows(struct AmigaMonitor *mon) UpdateWindow(mon->hAmigaWnd); firstwindow = false; setDwmEnableMMCSS(true); - rawinput_alloc(); if (currprefs.win32_shutdown_notification && !rp_isactive()) { typedef BOOL(WINAPI *SHUTDOWNBLOCKREASONCREATE)(HWND, LPCWSTR); @@ -3885,6 +3885,7 @@ static int create_windows(struct AmigaMonitor *mon) } } + rawinput_alloc(); return 1; } -- 2.47.3