diff options
Diffstat (limited to 'mousew16.c')
| -rw-r--r-- | mousew16.c | 152 |
1 files changed, 116 insertions, 36 deletions
@@ -39,8 +39,8 @@ /** Number of lines to scroll per (vertical) wheel event. */ #define WHEEL_SCROLL_LINES 2 -/** Windows 3.x only supports 1-2 mouse buttons anyway. */ -#define MOUSE_NUM_BUTTONS 2 +/** Max number of mouse buttons supported. */ +#define MOUSE_MAX_BUTTONS 3 /** Prefix for debug messages. */ #define DPREFIX "mousew16: " @@ -60,6 +60,8 @@ static struct { bool has_win386 : 1; #endif } flags; +/** Current mouse number of buttons. */ +static unsigned short num_buttons; /** Previous deltaX, deltaY from the int33 mouse callback (for relative motion) */ static short prev_delta_x, prev_delta_y; /** Maximum X and Y coordinates expected from the int33 driver. */ @@ -67,18 +69,28 @@ 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; - void WINAPI (*GetCursorPos)( POINT FAR * ); - HWND WINAPI (*WindowFromPoint)( POINT ); - HWND WINAPI (*GetParent)( HWND ); + BOOL WINAPI (*EnumChildWindows)( HWND, WNDENUMPROC, LPARAM ); + LRESULT WINAPI (*DefHookProc)( int, WPARAM, LPARAM, HOOKPROC FAR * ); + HWND WINAPI (*GetActiveWindow)( void ); int WINAPI (*GetClassName)( HWND, LPSTR, int ); - int WINAPI (*GetWindowText)( HWND, LPSTR, int ); + HWND WINAPI (*GetParent)( HWND ); 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 ); + HOOKPROC WINAPI (*SetWindowsHook)( int, HOOKPROC ); + BOOL WINAPI (*UnhookWindowsHook)( int, HOOKPROC ); + HWND WINAPI (*WindowFromPoint)( POINT ); } userapi; -#endif +/** This is the private message that we send on wheel movement and is + * going to be intercepted by the hook, which will translate into scroll msgs. */ +static UINT wheel_msg_private; +/** The hook that comes after ours. */ +static HOOKPROC wheel_hook_proc_next; +#endif /* USE_WHEEL */ #if USE_WIN386 /** Previous int2f handler. */ static LPFN prev_int2f_handler; @@ -86,8 +98,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 +210,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 post_simulated_scroll_msgs(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 "%s @%d,%d z=%d %s\n", + __func__, 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 +291,63 @@ static void send_wheel_movement(int8_t z, BOOL horizontal) } #if TRACE_WHEEL - dprintf(DPREFIX "wheel end\n"); + dprintf(DPREFIX "%s end\n", __func__); #endif } + +/** 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; + + // TODO In theory we could try sending "native" MSH_MOUSEWHEEL msg here + // which some early nt/95 software supports (in addition to 98's WM_MOUSEWHEEL) + // but I think the fake scroll messages work well enough. + // To properly send MSH_MOUSEWHEEL, we'd need to thunk into 32-bit + // SendMessage because such win95 software will expect 32-bit WPARAM. + + post_simulated_scroll_msgs(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); +} +#else +LRESULT CALLBACK WheelHookProc(int code, WPARAM wParam, LPARAM lParam) { + // Required since we export this symbol, but not use it + (void)code; (void) wParam; (void) lParam; + return 0; +} #endif /* USE_WHEEL */ /** Called by the int33 mouse driver. */ @@ -298,10 +361,12 @@ void FAR int33_mouse_callback(uint16_t events, uint16_t buttons, int16_t x, int1 events, buttons, x, y, delta_x, delta_y); #endif - if (events & INT33_EVENT_MASK_LEFT_BUTTON_PRESSED) status |= SF_B1_DOWN; - if (events & INT33_EVENT_MASK_LEFT_BUTTON_RELEASED) status |= SF_B1_UP; - if (events & INT33_EVENT_MASK_RIGHT_BUTTON_PRESSED) status |= SF_B2_DOWN; - if (events & INT33_EVENT_MASK_RIGHT_BUTTON_RELEASED) status |= SF_B2_UP; + if (events & INT33_EVENT_MASK_LEFT_BUTTON_PRESSED) status |= SF_B1_DOWN; + if (events & INT33_EVENT_MASK_LEFT_BUTTON_RELEASED) status |= SF_B1_UP; + if (events & INT33_EVENT_MASK_RIGHT_BUTTON_PRESSED) status |= SF_B2_DOWN; + if (events & INT33_EVENT_MASK_RIGHT_BUTTON_RELEASED) status |= SF_B2_UP; + if (events & INT33_EVENT_MASK_CENTER_BUTTON_PRESSED) status |= SF_B3_DOWN; + if (events & INT33_EVENT_MASK_CENTER_BUTTON_RELEASED) status |= SF_B3_UP; if (events & INT33_EVENT_MASK_MOVEMENT) { status |= SF_MOVEMENT; @@ -313,7 +378,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 @@ -346,7 +411,7 @@ void FAR int33_mouse_callback(uint16_t events, uint16_t buttons, int16_t x, int1 } #if TRACE_EVENTS - dprintf(DPREFIX "post status=0x%x ", status); + dprintf(DPREFIX "post nb=%u status=0x%x ", num_buttons, status); if (status & SF_ABSOLUTE) { dprintf("x=%u y=%u\n", x, y); } else { @@ -354,7 +419,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, num_buttons, 0, 0); } #if USE_WIN386 @@ -484,7 +549,7 @@ WORD FAR PASCAL Inquire(LPMOUSEINFO lpMouseInfo) { lpMouseInfo->msExist = TRUE; lpMouseInfo->msRelative = 0; - lpMouseInfo->msNumButtons = MOUSE_NUM_BUTTONS; + lpMouseInfo->msNumButtons = MOUSE_MAX_BUTTONS; lpMouseInfo->msRate = 80; return sizeof(MOUSEINFO); } @@ -499,7 +564,8 @@ VOID FAR PASCAL Enable(LPFN_MOUSEEVENT lpEventProc) _enable(); if (!flags.enabled) { - int33_reset(); + int33_reset_get_buttons(&num_buttons); + num_buttons = BOUND(num_buttons, 2, MOUSE_MAX_BUTTONS); #if USE_WIN386 // If we are running under win386, hook int2f now. @@ -515,16 +581,25 @@ 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.GetCursorPos = (LPVOID) GetProcAddress(userapi.hUser, "GetCursorPos"); - userapi.WindowFromPoint = (LPVOID) GetProcAddress(userapi.hUser, "WindowFromPoint"); - userapi.GetParent = (LPVOID) GetProcAddress(userapi.hUser, "GetParent"); + 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.GetWindowText = (LPVOID) GetProcAddress(userapi.hUser, "GetWindowText"); + userapi.GetParent = (LPVOID) GetProcAddress(userapi.hUser, "GetParent"); 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.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_hook_proc_next = userapi.SetWindowsHook(WH_GETMESSAGE, (HOOKPROC)WheelHookProc); } #endif @@ -557,6 +632,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 |
