Commit 95686457 authored by Sam Lantinga's avatar Sam Lantinga

Fixed bug #55

From Christian Walther:
When writing my patch for #12, I ended up doing all sorts of changes to the way
application/window activating/deactivating is handled in the Quartz backend,
resulting in the attached patch. It does make the code a bit cleaner IMHO, but
as it might be regarded as a case of "if it ain't broken, don't fix it" I'd
like to hear other people's opinion about it. Please shout if some change
strikes you as unnecessary or wrong, and I'll explain the reasons behind it. As
far as I tested it, it does not introduce any new bugs, but I may well have
missed some.

- The most fundamental change (that triggered most of the others) is irrelevant
for the usual single-window SDL applications, it only affects the people who
are crazy enough to display other Cocoa windows alongside the SDL window (I'm
actually doing this currently, although the additional window only displays
debugging info and won't be present in the final product): Before, some things
were done on the application becoming active, some on the window becoming key,
and some on the window becoming main. Conceptually, all these actions belong to
the window becoming key, so that's what I implemented. However, since in a
single-window application these three events always happen together, the
previous implementation "ain't broken".

- This slightly changed the meaning of the SDL_APPMOUSEFOCUS flag from
SDL_GetAppState(): Before, it meant "window is main and mouse is inside window
(or mode is fullscreen)". Now, it means "window is key and mouse is inside
window (or mode is fullscreen)". It makes more sense to me that way. (See
http://developer.apple.com/documentation/Cocoa/Conceptual/WinPanel/Concepts/ChangingMainKeyWindow.html
for a discussion of what key and main windows are.) The other two flags are
unchanged: SDL_APPACTIVE = application is not hidden and window is not
minimized, SDL_APPINPUTFOCUS = window is key (or mode is fullscreen).

- As a side effect, the reorganization fixes the following two issues (and
maybe others) (but they could also be fixed in less invasive ways):

* A regression that was introduced in revision 1.42 of SDL_QuartzVideo.m
(http://libsdl.org/cgi/cvsweb.cgi/SDL12/src/video/quartz/SDL_QuartzVideo.m.diff?r1=1.41&r2=1.42)
(from half-desirable to undesirable behavior):

Situation: While in windowed mode, hide the cursor using
SDL_ShowCursor(SDL_DISABLE), move the mouse outside of the window so that the
cursor becomes visible again, and SDL_SetVideoMode() to a fullscreen mode.
What happened before revision 1.42: The cursor is visible, but becomes
invisible as soon as the mouse is moved (half-desirable).
What happens in revision 1.42 and after (including current CVS): The cursor is
visible and stays visible (undesirable).
What happens after my patch: The cursor is invisible from the beginning
(desirable).

* When the cursor is hidden and grabbed, switch away from the application using
cmd-tab (which ungrabs and makes the cursor visible), move the cursor outside
of the SDL window, then cmd-tab back to the application. In 1.2.8 and in the
current CVS, the cursor is re-grabbed, but it stays visible (immovable in the
middle of the window). With my patch, the cursor is correctly re-grabbed and
hidden. (For some reason, it still doesn't work correctly if you switch back to
the application using the dock instead of cmd-tab. I haven't been able to
figure out why. I can step over [NSCursor hide] being called in the debugger,
but it seems to have no effect.)

- The patch includes my patch for #12 (it was easier to obtain using cvs diff
that way). If you apply both of them, you will end up with 6 duplicate lines in
SDL_QuartzEvents.m.

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%401636
parent 7a15c646
...@@ -614,8 +614,10 @@ static void QZ_GetMouseLocation (_THIS, NSPoint *p) { ...@@ -614,8 +614,10 @@ static void QZ_GetMouseLocation (_THIS, NSPoint *p) {
QZ_PrivateCocoaToSDL (this, p); QZ_PrivateCocoaToSDL (this, p);
} }
static void QZ_DoActivate (_THIS) void QZ_DoActivate (_THIS) {
{
SDL_PrivateAppActive (1, SDL_APPINPUTFOCUS | (QZ_IsMouseInWindow (this) ? SDL_APPMOUSEFOCUS : 0));
/* Hide the cursor if it was hidden by SDL_ShowCursor() */ /* Hide the cursor if it was hidden by SDL_ShowCursor() */
if (!cursor_should_be_visible) if (!cursor_should_be_visible)
QZ_HideMouse (this); QZ_HideMouse (this);
...@@ -635,7 +637,9 @@ static void QZ_DoActivate (_THIS) ...@@ -635,7 +637,9 @@ static void QZ_DoActivate (_THIS)
} }
} }
static void QZ_DoDeactivate (_THIS) { void QZ_DoDeactivate (_THIS) {
SDL_PrivateAppActive (0, SDL_APPINPUTFOCUS | SDL_APPMOUSEFOCUS);
/* Get the current cursor location, for restore on activate */ /* Get the current cursor location, for restore on activate */
QZ_GetMouseLocation (this, &cursor_loc); QZ_GetMouseLocation (this, &cursor_loc);
...@@ -753,14 +757,9 @@ void QZ_PumpEvents (_THIS) ...@@ -753,14 +757,9 @@ void QZ_PumpEvents (_THIS)
BOOL isInGameWin; BOOL isInGameWin;
#define DO_MOUSE_DOWN(button) do { \ #define DO_MOUSE_DOWN(button) do { \
if ( [ NSApp isActive ] ) { \ if ( SDL_GetAppState() & SDL_APPMOUSEFOCUS ) { \
if ( isInGameWin ) { \ SDL_PrivateMouseButton (SDL_PRESSED, button, 0, 0); \
SDL_PrivateMouseButton (SDL_PRESSED, button, 0, 0); \ expect_mouse_up |= 1<<button; \
expect_mouse_up |= 1<<button; \
} \
} \
else { \
QZ_DoActivate (this); \
} \ } \
[ NSApp sendEvent:event ]; \ [ NSApp sendEvent:event ]; \
} while(0) } while(0)
...@@ -916,7 +915,7 @@ void QZ_PumpEvents (_THIS) ...@@ -916,7 +915,7 @@ void QZ_PumpEvents (_THIS)
QZ_ShowMouse (this); QZ_ShowMouse (this);
} }
else else
if ( isInGameWin && !(SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) { if ( isInGameWin && (SDL_GetAppState() & (SDL_APPMOUSEFOCUS | SDL_APPINPUTFOCUS)) == SDL_APPINPUTFOCUS ) {
SDL_PrivateAppActive (1, SDL_APPMOUSEFOCUS); SDL_PrivateAppActive (1, SDL_APPMOUSEFOCUS);
if (!cursor_should_be_visible) if (!cursor_should_be_visible)
...@@ -950,17 +949,7 @@ void QZ_PumpEvents (_THIS) ...@@ -950,17 +949,7 @@ void QZ_PumpEvents (_THIS)
break; break;
case NSFlagsChanged: case NSFlagsChanged:
break; break;
case NSAppKitDefined: /* case NSAppKitDefined: break; */
switch ( [ event subtype ] ) {
case NSApplicationActivatedEventType:
QZ_DoActivate (this);
break;
case NSApplicationDeactivatedEventType:
QZ_DoDeactivate (this);
break;
}
[ NSApp sendEvent:event ];
break;
/* case NSApplicationDefined: break; */ /* case NSApplicationDefined: break; */
/* case NSPeriodic: break; */ /* case NSPeriodic: break; */
/* case NSCursorUpdate: break; */ /* case NSCursorUpdate: break; */
......
...@@ -224,3 +224,5 @@ void QZ_HideMouse (_THIS); ...@@ -224,3 +224,5 @@ void QZ_HideMouse (_THIS);
void QZ_PrivateGlobalToLocal (_THIS, NSPoint *p); void QZ_PrivateGlobalToLocal (_THIS, NSPoint *p);
void QZ_PrivateCocoaToSDL (_THIS, NSPoint *p); void QZ_PrivateCocoaToSDL (_THIS, NSPoint *p);
BOOL QZ_IsMouseInWindow (_THIS); BOOL QZ_IsMouseInWindow (_THIS);
void QZ_DoActivate (_THIS);
void QZ_DoDeactivate (_THIS);
...@@ -560,8 +560,8 @@ static SDL_Surface* QZ_SetVideoFullScreen (_THIS, SDL_Surface *current, int widt ...@@ -560,8 +560,8 @@ static SDL_Surface* QZ_SetVideoFullScreen (_THIS, SDL_Surface *current, int widt
/* Save the flags to ensure correct tear-down */ /* Save the flags to ensure correct tear-down */
mode_flags = current->flags; mode_flags = current->flags;
/* we're fullscreen, so flag all input states... */ /* Set app state, hide cursor if necessary, ... */
SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS | SDL_APPINPUTFOCUS | SDL_APPACTIVE); QZ_DoActivate(this);
return current; return current;
......
...@@ -78,15 +78,14 @@ void QZ_ShowMouse (_THIS) { ...@@ -78,15 +78,14 @@ void QZ_ShowMouse (_THIS) {
} }
void QZ_HideMouse (_THIS) { void QZ_HideMouse (_THIS) {
BOOL isInGameWin = QZ_IsMouseInWindow (this); if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS) && cursor_visible) {
if (isInGameWin && cursor_visible) {
[ NSCursor hide ]; [ NSCursor hide ];
cursor_visible = NO; cursor_visible = NO;
} }
} }
BOOL QZ_IsMouseInWindow (_THIS) { BOOL QZ_IsMouseInWindow (_THIS) {
if (mode_flags & SDL_FULLSCREEN) return YES; if (qz_window == nil) return YES; /*fullscreen*/
else { else {
NSPoint p = [ qz_window mouseLocationOutsideOfEventStream ]; NSPoint p = [ qz_window mouseLocationOutsideOfEventStream ];
p.y -= 1.0f; /* Apparently y goes from 1 to h, not from 0 to h-1 (i.e. the "location of the mouse" seems to be defined as "the location of the top left corner of the mouse pointer's hot pixel" */ p.y -= 1.0f; /* Apparently y goes from 1 to h, not from 0 to h-1 (i.e. the "location of the mouse" seems to be defined as "the location of the top left corner of the mouse pointer's hot pixel" */
...@@ -166,7 +165,7 @@ void QZ_PrivateCocoaToSDL (_THIS, NSPoint *p) { ...@@ -166,7 +165,7 @@ void QZ_PrivateCocoaToSDL (_THIS, NSPoint *p) {
*p = [ window_view convertPoint:*p fromView: nil ]; *p = [ window_view convertPoint:*p fromView: nil ];
/* We need a workaround in OpenGL mode */ /* We need a workaround in OpenGL mode */
if ( SDL_VideoSurface->flags & SDL_OPENGL ) { if ( SDL_VideoSurface != NULL && (SDL_VideoSurface->flags & SDL_OPENGL) ) {
p->y = [window_view frame].size.height - p->y; p->y = [window_view frame].size.height - p->y;
} }
} }
......
...@@ -208,24 +208,12 @@ static void QZ_SetPortAlphaOpaque () { ...@@ -208,24 +208,12 @@ static void QZ_SetPortAlphaOpaque () {
- (void)windowDidBecomeKey:(NSNotification *)aNotification - (void)windowDidBecomeKey:(NSNotification *)aNotification
{ {
SDL_PrivateAppActive (1, SDL_APPINPUTFOCUS); QZ_DoActivate (current_video);
} }
- (void)windowDidResignKey:(NSNotification *)aNotification - (void)windowDidResignKey:(NSNotification *)aNotification
{ {
SDL_PrivateAppActive (0, SDL_APPINPUTFOCUS); QZ_DoDeactivate (current_video);
}
- (void)windowDidBecomeMain:(NSNotification *)aNotification
{
SDL_VideoDevice *this = (SDL_VideoDevice*)current_video;
if (this && QZ_IsMouseInWindow (this))
SDL_PrivateAppActive (1, SDL_APPMOUSEFOCUS);
}
- (void)windowDidResignMain:(NSNotification *)aNotification
{
SDL_PrivateAppActive (0, SDL_APPMOUSEFOCUS);
} }
@end @end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment