Fixed bug #55
authorSam Lantinga <slouken@libsdl.org>
Thu, 13 Apr 2006 14:17:48 +0000
changeset 1629 ef4a796e7f24
parent 1628 10b4827452a8
child 1630 eba962f9d603
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.
src/video/quartz/SDL_QuartzEvents.m
src/video/quartz/SDL_QuartzVideo.h
src/video/quartz/SDL_QuartzVideo.m
src/video/quartz/SDL_QuartzWM.m
src/video/quartz/SDL_QuartzWindow.m
--- a/src/video/quartz/SDL_QuartzEvents.m	Thu Apr 13 14:14:06 2006 +0000
+++ b/src/video/quartz/SDL_QuartzEvents.m	Thu Apr 13 14:17:48 2006 +0000
@@ -614,8 +614,10 @@
     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() */
     if (!cursor_should_be_visible)
         QZ_HideMouse (this);
@@ -635,7 +637,9 @@
     }
 }
 
-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 */
     QZ_GetMouseLocation (this, &cursor_loc);
@@ -753,14 +757,9 @@
             BOOL isInGameWin;
             
             #define DO_MOUSE_DOWN(button) do {                                               \
-                            if ( [ NSApp isActive ] ) {                                      \
-                                if ( isInGameWin ) {                                         \
-                                    SDL_PrivateMouseButton (SDL_PRESSED, button, 0, 0);      \
-                                    expect_mouse_up |= 1<<button;                            \
-                                }                                                            \
-                            }                                                                \
-                            else {                                                           \
-                                QZ_DoActivate (this);                                        \
+                            if ( SDL_GetAppState() & SDL_APPMOUSEFOCUS ) {                   \
+                                SDL_PrivateMouseButton (SDL_PRESSED, button, 0, 0);          \
+                                expect_mouse_up |= 1<<button;                                \
                             }                                                                \
                             [ NSApp sendEvent:event ];                                       \
             } while(0)
@@ -916,7 +915,7 @@
                             QZ_ShowMouse (this);
                     }
                     else
-                    if ( isInGameWin && !(SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
+                    if ( isInGameWin && (SDL_GetAppState() & (SDL_APPMOUSEFOCUS | SDL_APPINPUTFOCUS)) == SDL_APPINPUTFOCUS ) {
                     
                         SDL_PrivateAppActive (1, SDL_APPMOUSEFOCUS);
                         if (!cursor_should_be_visible)
@@ -950,17 +949,7 @@
                     break;
                 case NSFlagsChanged:
                     break;
-                case NSAppKitDefined:
-                    switch ( [ event subtype ] ) {
-                        case NSApplicationActivatedEventType:
-                            QZ_DoActivate (this);
-                            break;
-                        case NSApplicationDeactivatedEventType:
-                            QZ_DoDeactivate (this);
-                            break;
-                    }
-                    [ NSApp sendEvent:event ];
-                    break;
+                    /* case NSAppKitDefined: break; */
                     /* case NSApplicationDefined: break; */
                     /* case NSPeriodic: break; */
                     /* case NSCursorUpdate: break; */
--- a/src/video/quartz/SDL_QuartzVideo.h	Thu Apr 13 14:14:06 2006 +0000
+++ b/src/video/quartz/SDL_QuartzVideo.h	Thu Apr 13 14:17:48 2006 +0000
@@ -224,3 +224,5 @@
 void         QZ_PrivateGlobalToLocal (_THIS, NSPoint *p);
 void         QZ_PrivateCocoaToSDL (_THIS, NSPoint *p);
 BOOL         QZ_IsMouseInWindow (_THIS);
+void         QZ_DoActivate (_THIS);
+void         QZ_DoDeactivate (_THIS);
--- a/src/video/quartz/SDL_QuartzVideo.m	Thu Apr 13 14:14:06 2006 +0000
+++ b/src/video/quartz/SDL_QuartzVideo.m	Thu Apr 13 14:17:48 2006 +0000
@@ -560,8 +560,8 @@
     /* Save the flags to ensure correct tear-down */
     mode_flags = current->flags;
 
-    /* we're fullscreen, so flag all input states... */
-    SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS | SDL_APPINPUTFOCUS | SDL_APPACTIVE);
+    /* Set app state, hide cursor if necessary, ... */
+    QZ_DoActivate(this);
 
     return current;
 
--- a/src/video/quartz/SDL_QuartzWM.m	Thu Apr 13 14:14:06 2006 +0000
+++ b/src/video/quartz/SDL_QuartzWM.m	Thu Apr 13 14:17:48 2006 +0000
@@ -78,15 +78,14 @@
 }
 
 void QZ_HideMouse (_THIS) {
-    BOOL isInGameWin = QZ_IsMouseInWindow (this);
-    if (isInGameWin && cursor_visible) {
+    if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS) && cursor_visible) {
         [ NSCursor hide ];
         cursor_visible = NO;
     }
 }
 
 BOOL QZ_IsMouseInWindow (_THIS) {
-    if (mode_flags & SDL_FULLSCREEN) return YES;
+    if (qz_window == nil) return YES; /*fullscreen*/
     else {
         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" */
@@ -166,7 +165,7 @@
         *p = [ window_view convertPoint:*p fromView: nil ];
         
         /* 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;
         }
     }
--- a/src/video/quartz/SDL_QuartzWindow.m	Thu Apr 13 14:14:06 2006 +0000
+++ b/src/video/quartz/SDL_QuartzWindow.m	Thu Apr 13 14:17:48 2006 +0000
@@ -208,24 +208,12 @@
 
 - (void)windowDidBecomeKey:(NSNotification *)aNotification
 {
-    SDL_PrivateAppActive (1, SDL_APPINPUTFOCUS);
+    QZ_DoActivate (current_video);
 }
 
 - (void)windowDidResignKey:(NSNotification *)aNotification
 {
-    SDL_PrivateAppActive (0, SDL_APPINPUTFOCUS);
-}
-
-- (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);
+    QZ_DoDeactivate (current_video);
 }
 
 @end