diff options
| author | Javier <dev.git@javispedro.com> | 2026-11-21 01:03:23 +0100 |
|---|---|---|
| committer | Javier <dev.git@javispedro.com> | 2026-11-21 01:03:23 +0100 |
| commit | 6385a546475e908adf01c0f53b57def1215a25ad (patch) | |
| tree | cb2a17312c3e3d792f8e12e6653989ac7abbafea /mousew16.c | |
| parent | 0950634f9afbcc753a6cbb21162df07f555785a0 (diff) | |
| download | vbados-6385a546475e908adf01c0f53b57def1215a25ad.tar.gz vbados-6385a546475e908adf01c0f53b57def1215a25ad.zip | |
mousew16: move most wheel logic from interrupt handler into a hook
this is to reduce time spend in interrupt as well as reduce
number of windows api calls during interrupt time
Diffstat (limited to 'mousew16.c')
| -rw-r--r-- | mousew16.c | 129 |
1 files changed, 105 insertions, 24 deletions
@@ -67,17 +67,27 @@ static unsigned short max_x, max_y; #if USE_WHEEL /** Some delay-loaded functions from USER.EXE to provide wheel mouse events. */ static struct { + /** Handle to the USER module itself. */ HINSTANCE hUser; + BOOL WINAPI (*EnumChildWindows)( HWND, WNDENUMPROC, LPARAM ); + LRESULT WINAPI (*DefHookProc)( int, WPARAM, LPARAM, HOOKPROC FAR * ); + HWND WINAPI (*GetActiveWindow)( void ); + int WINAPI (*GetClassName)( HWND, LPSTR, int ); void WINAPI (*GetCursorPos)( POINT FAR * ); - HWND WINAPI (*WindowFromPoint)( POINT ); HWND WINAPI (*GetParent)( HWND ); - int WINAPI (*GetClassName)( HWND, LPSTR, int ); - int WINAPI (*GetWindowText)( HWND, LPSTR, int ); LONG WINAPI (*GetWindowLong)( HWND, int ); + int WINAPI (*GetWindowText)( HWND, LPSTR, int ); BOOL WINAPI (*IsWindowEnabled)( HWND ); - BOOL WINAPI (*EnumChildWindows)( HWND, WNDENUMPROC, LPARAM ); BOOL WINAPI (*PostMessage)( HWND, UINT, WPARAM, LPARAM ); + UINT WINAPI (*RegisterWindowMessage)( LPCSTR ); + LRESULT WINAPI (*SendMessage)( HWND, UINT, WPARAM, LPARAM ); + HOOKPROC WINAPI (*SetWindowsHook)( int, HOOKPROC ); + BOOL WINAPI (*UnhookWindowsHook)( int, HOOKPROC ); + HWND WINAPI (*WindowFromPoint)( POINT ); } userapi; +static UINT wheel_msg_private; +static UINT wheel_msg_mousewheel; +static HOOKPROC wheel_hook_proc_next; #endif #if USE_WIN386 /** Previous int2f handler. */ @@ -86,8 +96,8 @@ static LPFN prev_int2f_handler; /* This is how events are delivered to Windows */ -static void send_event(unsigned short Status, short deltaX, short deltaY, short ButtonCount, short extra1, short extra2); -#pragma aux (MOUSEEVENTPROC) send_event = \ +static void post_event(unsigned short Status, short deltaX, short deltaY, short ButtonCount, short extra1, short extra2); +#pragma aux (MOUSEEVENTPROC) post_event = \ "call dword ptr [eventproc]" /* Our "CALLBACKS" segment which is fixed and non-relocatable. */ @@ -198,23 +208,20 @@ static void post_scroll_msg(HWND hWnd, UINT msg, int z, HWND hScrollBar) userapi.PostMessage(hWnd, msg, SB_ENDSCROLL, lParam); } -/** Send wheel scrolling events to the most likely candidate window. */ -static void send_wheel_movement(int8_t z, BOOL horizontal) +/** Send wheel scrolling events to the most likely candidate window. + @param cursor the cursor position at the time the wheel event occured */ +static void simulate_scroll(POINT cursor, int z, BOOL horizontal) { - POINT point; HWND hWnd; #if TRACE_WHEEL - dprintf(DPREFIX "wheel=%d %s\n", z, horizontal ? "horiz" : "vert"); + dprintf(DPREFIX "simulate_scroll @%d,%d z=%d %s\n", + cursor.x, cursor.y, + z, horizontal ? "horiz" : "vert"); #endif - // TODO It's highly unlikely that we can call this many functions from - // an interrupt handler without causing a re-entrancy issue somewhere. - // Likely it would be better to just move all of this into a hook .DLL - - // Find current window below the mosue cursor - userapi.GetCursorPos(&point); - hWnd = userapi.WindowFromPoint(point); + // Find current window below the mouse cursor + hWnd = userapi.WindowFromPoint(cursor); #if TRACE_WHEEL dprintf(DPREFIX "initial hWnd=0x%x\n", hWnd); @@ -282,9 +289,66 @@ static void send_wheel_movement(int8_t z, BOOL horizontal) } #if TRACE_WHEEL - dprintf(DPREFIX "wheel end\n"); + dprintf(DPREFIX "simulate_scroll end\n"); #endif } + +/** Send a MSH_MOUSEWHEEL message to hWnd. */ +static BOOL send_msg_mousewheel(HWND hWnd, POINT cursor, int z, BOOL horizontal) +{ + WPARAM wParam = z * WHEEL_DELTA; + LPARAM lParam = MAKELONG(cursor.x, cursor.y); + if (horizontal) return FALSE; // TODO there is no MSH_MOUSE(H)WHEEL + return userapi.SendMessage(hWnd, wheel_msg_mousewheel, wParam, lParam); +} + +/** GetMsgProc hook, called for every message before being returned by GetMessage. */ +LRESULT CALLBACK WheelHookProc(int code, WPARAM wParam, LPARAM lParam) +{ + if (code >= 0) { + // TODO Unclear if on win9x PeekMessage will also call this hook + MSG FAR *msg = (MSG FAR *)lParam; + if (msg->message == wheel_msg_private) { + // This is our private `wheel_msg` + BOOL horizontal = msg->wParam; + int z = msg->lParam; + + // First try to send zmouse's MSH_MOUSEWHEEL msg, + // which some win9x era software may handle natively + if (!send_msg_mousewheel(msg->hwnd, msg->pt, z, horizontal)) { + // Only if the window didn't handle the MSH_MOUSEWHEEL msg, + // simulate scrolling events. + simulate_scroll(msg->pt, z, horizontal); + } + + // Prevent us from intercepting this message if fwd'd + msg->message = WM_NULL; + } + } + + return userapi.DefHookProc(code, wParam, lParam, &wheel_hook_proc_next); +} + +/** Post a wheel_msg_private to the active window. + * The idea here is this function may be called during interrupt time, + * and very few winapi functions are safely callable at this point. + * So, we post a message, which will be captured by our WheelHookProc, + * at which point we will simulate all the necessary scrolling events. */ +static void post_wheel_msg(int z, BOOL horizontal) +{ + HWND hWnd; + POINT cursorpos; + + hWnd = userapi.GetActiveWindow(); + if (!hWnd) return; + +#if TRACE_WHEEL + dprintf(DPREFIX "post wheel_msg=0x%x to hWnd=0x%x : z=%d %s\n", + wheel_msg_private, hWnd, z, horizontal ? "horiz" : "vert"); +#endif + + userapi.PostMessage(hWnd, wheel_msg_private, horizontal, z); +} #endif /* USE_WHEEL */ /** Called by the int33 mouse driver. */ @@ -313,7 +377,7 @@ void FAR int33_mouse_callback(uint16_t events, uint16_t buttons, int16_t x, int1 int8_t z = (buttons & 0xFF00) >> 8; BOOL horizontal = !!(events & INT33_EVENT_MASK_HORIZ_WHEEL_MOVEMENT); if (z) { - send_wheel_movement(z, horizontal); + post_wheel_msg(z, horizontal); } } #endif @@ -354,7 +418,7 @@ void FAR int33_mouse_callback(uint16_t events, uint16_t buttons, int16_t x, int1 } #endif - send_event(status, x, y, MOUSE_NUM_BUTTONS, 0, 0); + post_event(status, x, y, MOUSE_NUM_BUTTONS, 0, 0); } #if USE_WIN386 @@ -515,16 +579,28 @@ VOID FAR PASCAL Enable(LPFN_MOUSEEVENT lpEventProc) // Detect whether the int33 driver has wheel support flags.wheel = int33_get_capabilities() & 1; if (flags.wheel) { + // Initialize functions we need userapi.hUser = (HINSTANCE) GetModuleHandle("USER"); + userapi.EnumChildWindows = (LPVOID) GetProcAddress(userapi.hUser, "EnumChildWindows"); + userapi.DefHookProc = (LPVOID) GetProcAddress(userapi.hUser, "DefHookProc"); + userapi.GetActiveWindow = (LPVOID) GetProcAddress(userapi.hUser, "GetActiveWindow"); + userapi.GetClassName = (LPVOID) GetProcAddress(userapi.hUser, "GetClassName"); userapi.GetCursorPos = (LPVOID) GetProcAddress(userapi.hUser, "GetCursorPos"); - userapi.WindowFromPoint = (LPVOID) GetProcAddress(userapi.hUser, "WindowFromPoint"); userapi.GetParent = (LPVOID) GetProcAddress(userapi.hUser, "GetParent"); - userapi.GetClassName = (LPVOID) GetProcAddress(userapi.hUser, "GetClassName"); - userapi.GetWindowText = (LPVOID) GetProcAddress(userapi.hUser, "GetWindowText"); userapi.GetWindowLong = (LPVOID) GetProcAddress(userapi.hUser, "GetWindowLong"); + userapi.GetWindowText = (LPVOID) GetProcAddress(userapi.hUser, "GetWindowText"); userapi.IsWindowEnabled = (LPVOID) GetProcAddress(userapi.hUser, "IsWindowEnabled"); - userapi.EnumChildWindows = (LPVOID) GetProcAddress(userapi.hUser, "EnumChildWindows"); userapi.PostMessage = (LPVOID) GetProcAddress(userapi.hUser, "PostMessage"); + userapi.RegisterWindowMessage = (LPVOID) GetProcAddress(userapi.hUser, "RegisterWindowMessage"); + userapi.SendMessage = (LPVOID) GetProcAddress(userapi.hUser, "SendMessage"); + userapi.SetWindowsHook = (LPVOID) GetProcAddress(userapi.hUser, "SetWindowsHook"); + userapi.UnhookWindowsHook = (LPVOID) GetProcAddress(userapi.hUser, "UnhookWindowsHook"); + userapi.WindowFromPoint = (LPVOID) GetProcAddress(userapi.hUser, "WindowFromPoint"); + + wheel_msg_private = userapi.RegisterWindowMessage("VBMOUSE_WHEEL"); + wheel_msg_mousewheel = userapi.RegisterWindowMessage(MSH_MOUSEWHEEL); + + wheel_hook_proc_next = userapi.SetWindowsHook(WH_GETMESSAGE, (HOOKPROC)WheelHookProc); } #endif @@ -557,6 +633,11 @@ VOID FAR PASCAL Enable(LPFN_MOUSEEVENT lpEventProc) VOID FAR PASCAL Disable(VOID) { if (flags.enabled) { +#if USE_WHEEL + if (flags.wheel) { + userapi.UnhookWindowsHook(WH_GETMESSAGE, (HOOKPROC)WheelHookProc); + } +#endif #if USE_WIN386 if (flags.has_win386) { // When Windows is shutting down, it's not that important to correctly |
