Fixed bug #901 SDL-1.2
authorSam Lantinga <slouken@libsdl.org>
Mon, 14 Dec 2009 22:41:31 +0000
branchSDL-1.2
changeset 4393 9afe12fb4c41
parent 4392 2b8c1aea633b
child 4394 42012a6afb5b
Fixed bug #901

Tim Angus 2009-12-11 11:45:46 PST

Disable mouse event generation when state is not SDL_APPMOUSEFOCUS

If a Windows SDL application is minimised by using alt-tab, SDL_APPMOUSEFOCUS
is lost as part of the minimisation. Unfortunately, the directx driver doesn't
pay any attention to this state when generating mouse button events, so
clicking on the Desktop can cause mouse clicks in the SDL application, while
it's still minimised. The attached patch fixes this. It looks much more
complicated than it actually is due to indentation; here it is ignoring
whitespace:

tma@abraxas:~/sources/SDL-1.2-svn$ svn diff -x -b
Index: src/video/windx5/SDL_dx5events.c
===================================================================
--- src/video/windx5/SDL_dx5events.c (revision 5376)
+++ src/video/windx5/SDL_dx5events.c (working copy)
@@ -374,10 +374,9 @@
if ( !(SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
mouse_lost = 1;
ClipCursor(NULL);
- }
-
+ } else {
/* If the mouse was lost, regain some sense of mouse state */
- if ( mouse_lost && (SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
+ if ( mouse_lost ) {
POINT mouse_pos;
Uint8 old_state;
Uint8 new_state;
@@ -548,6 +547,7 @@
if ( xrel || yrel ) {
post_mouse_motion(1, xrel, yrel);
}
+ }
}

/* The main Win32 event handler */
src/video/windx5/SDL_dx5events.c
--- a/src/video/windx5/SDL_dx5events.c	Fri Dec 11 15:31:37 2009 +0000
+++ b/src/video/windx5/SDL_dx5events.c	Mon Dec 14 22:41:31 2009 +0000
@@ -374,179 +374,179 @@
 	if ( !(SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
 		mouse_lost = 1;
 		ClipCursor(NULL);
-	}
+	} else {
+		/* If the mouse was lost, regain some sense of mouse state */
+		if ( mouse_lost ) {
+			POINT mouse_pos;
+			Uint8 old_state;
+			Uint8 new_state;
 
-	/* If the mouse was lost, regain some sense of mouse state */
-	if ( mouse_lost && (SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
-		POINT mouse_pos;
-		Uint8 old_state;
-		Uint8 new_state;
+			/* Set ourselves up with the current cursor position */
+			GetCursorPos(&mouse_pos);
+			ScreenToClient(SDL_Window, &mouse_pos);
+			post_mouse_motion( 0, (Sint16)mouse_pos.x, (Sint16)mouse_pos.y);
 
-		/* Set ourselves up with the current cursor position */
-		GetCursorPos(&mouse_pos);
-		ScreenToClient(SDL_Window, &mouse_pos);
-		post_mouse_motion( 0, (Sint16)mouse_pos.x, (Sint16)mouse_pos.y);
+			/* Check for mouse button changes */
+			old_state = SDL_GetMouseState(NULL, NULL);
+			new_state = 0;
+			{ /* Get the new DirectInput button state for the mouse */
+	#if DIRECTINPUT_VERSION >= 0x700
+				DIMOUSESTATE2 distate;
+	#else
+				DIMOUSESTATE distate;
+	#endif
+				HRESULT result;
 
-		/* Check for mouse button changes */
-		old_state = SDL_GetMouseState(NULL, NULL);
-		new_state = 0;
-		{ /* Get the new DirectInput button state for the mouse */
-#if DIRECTINPUT_VERSION >= 0x700
-			DIMOUSESTATE2 distate;
-#else
-			DIMOUSESTATE distate;
-#endif
-			HRESULT result;
+				result=IDirectInputDevice2_GetDeviceState(SDL_DIdev[1],
+							sizeof(distate), &distate);
+				if ( result != DI_OK ) {
+					/* Try again next time */
+					SetDIerror(
+					"IDirectInputDevice2::GetDeviceState", result);
+					return;
+				}
+				for ( i=3; i>=0; --i ) {
+					if ( (distate.rgbButtons[i]&0x80) == 0x80 ) {
+						new_state |= 0x01;
+					}
+					new_state <<= 1;
+				}
+			}
+			for ( i=0; i<8; ++i ) {
+				if ( (old_state&0x01) != (new_state&0x01) ) {
+					button = (Uint8)(i+1);
+					/* Map DI button numbers to SDL */
+					switch ( button ) {
+						case 2: button = SDL_BUTTON_RIGHT; break;
+						case 3: button = SDL_BUTTON_MIDDLE; break;
+						case 4: button = SDL_BUTTON_X1; break;
+						case 5: button = SDL_BUTTON_X2; break;
+						default: break;
+					}
+					if ( new_state & 0x01 ) {
+						/* Grab mouse so we get mouse-up */
+						if ( ++mouse_pressed > 0 ) {
+							SetCapture(SDL_Window);
+						}
+						state = SDL_PRESSED;
+					} else {
+						/* Release mouse after all mouse-ups */
+						if ( --mouse_pressed <= 0 ) {
+							ReleaseCapture();
+							mouse_pressed = 0;
+						}
+						state = SDL_RELEASED;
+					}
+					if ( mouse_buttons_swapped ) {
+						if ( button == 1 ) button = 3;
+						else
+						if ( button == 3 ) button = 1;
+					}
+					posted = SDL_PrivateMouseButton(state, button,
+										0, 0);
+				}
+				old_state >>= 1;
+				new_state >>= 1;
+			}
+			mouse_lost = 0;
+			return;
+		}
 
-			result=IDirectInputDevice2_GetDeviceState(SDL_DIdev[1],
-						sizeof(distate), &distate);
-			if ( result != DI_OK ) {
-				/* Try again next time */
-				SetDIerror(
-				"IDirectInputDevice2::GetDeviceState", result);
-				return;
-			}
-			for ( i=3; i>=0; --i ) {
-				if ( (distate.rgbButtons[i]&0x80) == 0x80 ) {
-					new_state |= 0x01;
-				}
-				new_state <<= 1;
-			}
-		}
-		for ( i=0; i<8; ++i ) {
-			if ( (old_state&0x01) != (new_state&0x01) ) {
-				button = (Uint8)(i+1);
-				/* Map DI button numbers to SDL */
-				switch ( button ) {
-					case 2: button = SDL_BUTTON_RIGHT; break;
-					case 3: button = SDL_BUTTON_MIDDLE; break;
-					case 4: button = SDL_BUTTON_X1; break;
-					case 5: button = SDL_BUTTON_X2; break;
-					default: break;
-				}
-				if ( new_state & 0x01 ) {
-					/* Grab mouse so we get mouse-up */
-					if ( ++mouse_pressed > 0 ) {
-						SetCapture(SDL_Window);
+		/* Translate mouse messages */
+		xrel = 0;
+		yrel = 0;
+		for ( i=0; i<(int)numevents; ++i ) {
+			switch (ptrbuf[i].dwOfs) {
+				case DIMOFS_X:
+					if ( timestamp != ptrbuf[i].dwTimeStamp ) {
+						if ( xrel || yrel ) {
+							post_mouse_motion(1, xrel, yrel);
+							xrel = 0;
+							yrel = 0;
+						}
+						timestamp = ptrbuf[i].dwTimeStamp;
 					}
-					state = SDL_PRESSED;
-				} else {
-					/* Release mouse after all mouse-ups */
-					if ( --mouse_pressed <= 0 ) {
-						ReleaseCapture();
-						mouse_pressed = 0;
+					xrel += (Sint16)ptrbuf[i].dwData;
+					break;
+				case DIMOFS_Y:
+					if ( timestamp != ptrbuf[i].dwTimeStamp ) {
+						if ( xrel || yrel ) {
+							post_mouse_motion(1, xrel, yrel);
+							xrel = 0;
+							yrel = 0;
+						}
+						timestamp = ptrbuf[i].dwTimeStamp;
 					}
-					state = SDL_RELEASED;
-				}
-				if ( mouse_buttons_swapped ) {
-					if ( button == 1 ) button = 3;
-					else
-					if ( button == 3 ) button = 1;
-				}
-				posted = SDL_PrivateMouseButton(state, button,
-									0, 0);
-			}
-			old_state >>= 1;
-			new_state >>= 1;
-		}
-		mouse_lost = 0;
-		return;
-	}
-
-	/* Translate mouse messages */
-	xrel = 0;
-	yrel = 0;
-	for ( i=0; i<(int)numevents; ++i ) {
-		switch (ptrbuf[i].dwOfs) {
-			case DIMOFS_X:
-				if ( timestamp != ptrbuf[i].dwTimeStamp ) {
+					yrel += (Sint16)ptrbuf[i].dwData;
+					break;
+				case DIMOFS_Z:
 					if ( xrel || yrel ) {
 						post_mouse_motion(1, xrel, yrel);
 						xrel = 0;
 						yrel = 0;
 					}
-					timestamp = ptrbuf[i].dwTimeStamp;
-				}
-				xrel += (Sint16)ptrbuf[i].dwData;
-				break;
-			case DIMOFS_Y:
-				if ( timestamp != ptrbuf[i].dwTimeStamp ) {
+					timestamp = 0;
+					if((int)ptrbuf[i].dwData > 0)
+						button = SDL_BUTTON_WHEELUP;
+					else
+						button = SDL_BUTTON_WHEELDOWN;
+					posted = SDL_PrivateMouseButton(
+							SDL_PRESSED, button, 0, 0);
+					posted |= SDL_PrivateMouseButton(
+							SDL_RELEASED, button, 0, 0);
+					break;
+				case DIMOFS_BUTTON0:
+				case DIMOFS_BUTTON1:
+				case DIMOFS_BUTTON2:
+				case DIMOFS_BUTTON3:
+	#if DIRECTINPUT_VERSION >= 0x700
+				case DIMOFS_BUTTON4:
+				case DIMOFS_BUTTON5:
+				case DIMOFS_BUTTON6:
+				case DIMOFS_BUTTON7:
+	#endif
 					if ( xrel || yrel ) {
 						post_mouse_motion(1, xrel, yrel);
 						xrel = 0;
 						yrel = 0;
 					}
-					timestamp = ptrbuf[i].dwTimeStamp;
-				}
-				yrel += (Sint16)ptrbuf[i].dwData;
-				break;
-			case DIMOFS_Z:
-				if ( xrel || yrel ) {
-					post_mouse_motion(1, xrel, yrel);
-					xrel = 0;
-					yrel = 0;
-				}
-				timestamp = 0;
-				if((int)ptrbuf[i].dwData > 0)
-					button = SDL_BUTTON_WHEELUP;
-				else
-					button = SDL_BUTTON_WHEELDOWN;
-				posted = SDL_PrivateMouseButton(
-						SDL_PRESSED, button, 0, 0);
-				posted |= SDL_PrivateMouseButton(
-						SDL_RELEASED, button, 0, 0);
-				break;
-			case DIMOFS_BUTTON0:
-			case DIMOFS_BUTTON1:
-			case DIMOFS_BUTTON2:
-			case DIMOFS_BUTTON3:
-#if DIRECTINPUT_VERSION >= 0x700
-			case DIMOFS_BUTTON4:
-			case DIMOFS_BUTTON5:
-			case DIMOFS_BUTTON6:
-			case DIMOFS_BUTTON7:
-#endif
-				if ( xrel || yrel ) {
-					post_mouse_motion(1, xrel, yrel);
-					xrel = 0;
-					yrel = 0;
-				}
-				timestamp = 0;
-				button = (Uint8)(ptrbuf[i].dwOfs-DIMOFS_BUTTON0)+1;
-				/* Map DI button numbers to SDL */
-				switch ( button ) {
-					case 2: button = SDL_BUTTON_RIGHT; break;
-					case 3: button = SDL_BUTTON_MIDDLE; break;
-					case 4: button = SDL_BUTTON_X1; break;
-					case 5: button = SDL_BUTTON_X2; break;
-					default: break;
-				}
-				if ( ptrbuf[i].dwData & 0x80 ) {
-					/* Grab mouse so we get mouse-up */
-					if ( ++mouse_pressed > 0 ) {
-						SetCapture(SDL_Window);
+					timestamp = 0;
+					button = (Uint8)(ptrbuf[i].dwOfs-DIMOFS_BUTTON0)+1;
+					/* Map DI button numbers to SDL */
+					switch ( button ) {
+						case 2: button = SDL_BUTTON_RIGHT; break;
+						case 3: button = SDL_BUTTON_MIDDLE; break;
+						case 4: button = SDL_BUTTON_X1; break;
+						case 5: button = SDL_BUTTON_X2; break;
+						default: break;
 					}
-					state = SDL_PRESSED;
-				} else {
-					/* Release mouse after all mouse-ups */
-					if ( --mouse_pressed <= 0 ) {
-						ReleaseCapture();
-						mouse_pressed = 0;
+					if ( ptrbuf[i].dwData & 0x80 ) {
+						/* Grab mouse so we get mouse-up */
+						if ( ++mouse_pressed > 0 ) {
+							SetCapture(SDL_Window);
+						}
+						state = SDL_PRESSED;
+					} else {
+						/* Release mouse after all mouse-ups */
+						if ( --mouse_pressed <= 0 ) {
+							ReleaseCapture();
+							mouse_pressed = 0;
+						}
+						state = SDL_RELEASED;
 					}
-					state = SDL_RELEASED;
-				}
-				if ( mouse_buttons_swapped ) {
-					if ( button == 1 ) button = 3;
-					else
-					if ( button == 3 ) button = 1;
-				}
-				posted = SDL_PrivateMouseButton(state, button,
-									0, 0);
-				break;
+					if ( mouse_buttons_swapped ) {
+						if ( button == 1 ) button = 3;
+						else
+						if ( button == 3 ) button = 1;
+					}
+					posted = SDL_PrivateMouseButton(state, button,
+										0, 0);
+					break;
+			}
 		}
-	}
-	if ( xrel || yrel ) {
-		post_mouse_motion(1, xrel, yrel);
+		if ( xrel || yrel ) {
+			post_mouse_motion(1, xrel, yrel);
+		}
 	}
 }