Date: Tue, 6 Jan 2004 21:54:02 +0100
authorSam Lantinga <slouken@libsdl.org>
Wed, 07 Jan 2004 15:01:51 +0000
changeset 77968c8da837fc0
parent 778 8ac3f46f9d09
child 780 7da6da0cb7cf
Date: Tue, 6 Jan 2004 21:54:02 +0100
From: Max Horn
Subject: Auto hide mouse & other changes

the attached bug adds the auto-hide-mouse feature I talked about
earlier. Turned out it was a lot simpler than I thought, simply by
using our existing code :-). I actually spent much more time on fixing
various bugs in the code and correcting (IMO) some behavior (although,
due to the lack of real specs for SDL, it's probably arguable what
'correct' means...).

* adds auto (un)hiding of mouse depending on whether it is in- or
outside the game window
* computation of course coordinates is correct now (it often and
reproducible got out of sync with the old code, since the NSEvent
window was in some cases *not* our window anymore, so locationInWindow
returned wrong results)
* added a method which at any time returns the mouse coords, relative
to our window
* fixed handling of lost/gain input/mouse/app focus "events"
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
     1.1 --- a/src/video/quartz/SDL_QuartzEvents.m	Tue Jan 06 17:18:38 2004 +0000
     1.2 +++ b/src/video/quartz/SDL_QuartzEvents.m	Wed Jan 07 15:01:51 2004 +0000
     1.3 @@ -316,8 +316,6 @@
     1.4          QZ_PrivateWarpCursor (this, cursor_loc.x, cursor_loc.y);
     1.5          QZ_ChangeGrabState (this, QZ_ENABLE_GRAB);
     1.6      }
     1.7 -
     1.8 -    SDL_PrivateAppActive (1, SDL_APPINPUTFOCUS);
     1.9  }
    1.10  
    1.11  static void QZ_DoDeactivate (_THIS) {
    1.12 @@ -334,8 +332,6 @@
    1.13      /* Show the cursor if it was hidden by SDL_ShowCursor() */
    1.14      if (!cursor_should_be_visible)
    1.15          QZ_ShowMouse (this);
    1.16 -
    1.17 -    SDL_PrivateAppActive (0, SDL_APPINPUTFOCUS);
    1.18  }
    1.19  
    1.20  void QZ_SleepNotificationHandler (void * refcon,
    1.21 @@ -462,7 +458,7 @@
    1.22              
    1.23              type = [ event type ];
    1.24              isForGameWin = (qz_window == [ event window ]);
    1.25 -            isInGameWin = (mode_flags & SDL_FULLSCREEN) ? true : NSPointInRect([event locationInWindow], [ window_view frame ]);
    1.26 +            isInGameWin = QZ_IsMouseInWindow (this);
    1.27              switch (type) {
    1.28                  case NSLeftMouseDown:
    1.29                      if ( getenv("SDL_HAS3BUTTONMOUSE") ) {
    1.30 @@ -538,7 +534,7 @@
    1.31                              provides the first known mouse position,
    1.32                              since everything after this uses deltas
    1.33                          */
    1.34 -                        NSPoint p = [ event locationInWindow ];
    1.35 +                        NSPoint p = [ qz_window mouseLocationOutsideOfEventStream ];
    1.36                          QZ_PrivateCocoaToSDL (this, &p);
    1.37                          SDL_PrivateMouseMotion (0, 0, p.x, p.y);
    1.38                          firstMouseEvent = 0;
    1.39 @@ -561,7 +557,7 @@
    1.40                      if ( grab_state == QZ_VISIBLE_GRAB &&
    1.41                           !isInGameWin ) {
    1.42                         
    1.43 -                        NSPoint p = [ event locationInWindow ]; 
    1.44 +                        NSPoint p = [ qz_window mouseLocationOutsideOfEventStream ]; 
    1.45                          QZ_PrivateCocoaToSDL (this, &p);
    1.46  
    1.47                          if ( p.x < 0.0 ) 
    1.48 @@ -582,11 +578,15 @@
    1.49                      if ( !isInGameWin && (SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
    1.50                      
    1.51                          SDL_PrivateAppActive (0, SDL_APPMOUSEFOCUS);
    1.52 +                        if (!cursor_should_be_visible)
    1.53 +                            QZ_ShowMouse (this);
    1.54                      }
    1.55                      else
    1.56                      if ( isInGameWin && !(SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
    1.57                      
    1.58                          SDL_PrivateAppActive (1, SDL_APPMOUSEFOCUS);
    1.59 +                        if (!cursor_should_be_visible)
    1.60 +                            QZ_HideMouse (this);
    1.61                      }
    1.62                      break;
    1.63                  case NSScrollWheel:
     2.1 --- a/src/video/quartz/SDL_QuartzVideo.h	Tue Jan 06 17:18:38 2004 +0000
     2.2 +++ b/src/video/quartz/SDL_QuartzVideo.h	Wed Jan 07 15:01:51 2004 +0000
     2.3 @@ -216,4 +216,4 @@
     2.4  void         QZ_HideMouse (_THIS);
     2.5  void         QZ_PrivateGlobalToLocal (_THIS, NSPoint *p);
     2.6  void         QZ_PrivateCocoaToSDL (_THIS, NSPoint *p);
     2.7 -
     2.8 +BOOL         QZ_IsMouseInWindow (_THIS);
     3.1 --- a/src/video/quartz/SDL_QuartzVideo.m	Tue Jan 06 17:18:38 2004 +0000
     3.2 +++ b/src/video/quartz/SDL_QuartzVideo.m	Wed Jan 07 15:01:51 2004 +0000
     3.3 @@ -202,6 +202,7 @@
     3.4      /* Set misc globals */
     3.5      current_grab_mode = SDL_GRAB_OFF;
     3.6      cursor_should_be_visible    = YES;
     3.7 +    cursor_visible              = YES;
     3.8      
     3.9      /* register for sleep notifications so wake from sleep generates SDL_VIDEOEXPOSE */
    3.10      QZ_RegisterForSleepNotifications (this);
     4.1 --- a/src/video/quartz/SDL_QuartzWM.m	Tue Jan 06 17:18:38 2004 +0000
     4.2 +++ b/src/video/quartz/SDL_QuartzWM.m	Wed Jan 07 15:01:51 2004 +0000
     4.3 @@ -77,12 +77,17 @@
     4.4  }
     4.5  
     4.6  void QZ_HideMouse (_THIS) {
     4.7 -    if (cursor_visible) {
     4.8 +    BOOL isInGameWin = QZ_IsMouseInWindow (this);
     4.9 +    if (isInGameWin && cursor_visible) {
    4.10          [ NSCursor hide ];
    4.11          cursor_visible = NO;
    4.12      }
    4.13  }
    4.14  
    4.15 +BOOL QZ_IsMouseInWindow (_THIS) {
    4.16 +    return (mode_flags & SDL_FULLSCREEN) ? true : NSPointInRect([ qz_window mouseLocationOutsideOfEventStream ], [ window_view frame ]);
    4.17 +}
    4.18 +
    4.19  int QZ_ShowWMCursor (_THIS, WMcursor *cursor) { 
    4.20  
    4.21      if ( cursor == NULL) {
     5.1 --- a/src/video/quartz/SDL_QuartzWindow.m	Tue Jan 06 17:18:38 2004 +0000
     5.2 +++ b/src/video/quartz/SDL_QuartzWindow.m	Wed Jan 07 15:01:51 2004 +0000
     5.3 @@ -204,4 +204,27 @@
     5.4      SDL_PrivateQuit();
     5.5      return NO;
     5.6  }
     5.7 +
     5.8 +- (void)windowDidBecomeKey:(NSNotification *)aNotification
     5.9 +{
    5.10 +    SDL_PrivateAppActive (1, SDL_APPINPUTFOCUS);
    5.11 +}
    5.12 +
    5.13 +- (void)windowDidResignKey:(NSNotification *)aNotification
    5.14 +{
    5.15 +    SDL_PrivateAppActive (0, SDL_APPINPUTFOCUS);
    5.16 +}
    5.17 +
    5.18 +- (void)windowDidBecomeMain:(NSNotification *)aNotification
    5.19 +{
    5.20 +    SDL_VideoDevice *this = (SDL_VideoDevice*)current_video;
    5.21 +    if (this && QZ_IsMouseInWindow (this))
    5.22 +        SDL_PrivateAppActive (1, SDL_APPMOUSEFOCUS);
    5.23 +}
    5.24 +
    5.25 +- (void)windowDidResignMain:(NSNotification *)aNotification
    5.26 +{
    5.27 +    SDL_PrivateAppActive (0, SDL_APPMOUSEFOCUS);
    5.28 +}
    5.29 +
    5.30  @end