Only check for keyboard focus if the video subsystem was initialized.
authorSam Lantinga <slouken@libsdl.org>
Fri, 28 Jun 2013 23:29:13 -0700
changeset 73375ba1fafc6a94
parent 7336 b0ef4d043c7b
child 7338 9962dfd88567
Only check for keyboard focus if the video subsystem was initialized.
Check the hint at initialization time, as an optimization. This isn't something we expect the application to change at runtime, and if it is we should add an API for it.
src/joystick/SDL_joystick.c
     1.1 --- a/src/joystick/SDL_joystick.c	Fri Jun 28 22:49:03 2013 -0700
     1.2 +++ b/src/joystick/SDL_joystick.c	Fri Jun 28 23:29:13 2013 -0700
     1.3 @@ -22,6 +22,7 @@
     1.4  
     1.5  /* This is the joystick API for Simple DirectMedia Layer */
     1.6  
     1.7 +#include "SDL.h"
     1.8  #include "SDL_events.h"
     1.9  #include "SDL_sysjoystick.h"
    1.10  #include "SDL_assert.h"
    1.11 @@ -31,17 +32,25 @@
    1.12  #include "../events/SDL_events_c.h"
    1.13  #endif
    1.14  
    1.15 +static SDL_bool SDL_joystick_allows_background_events = SDL_FALSE;
    1.16  static SDL_Joystick *SDL_joysticks = NULL;
    1.17  static SDL_Joystick *SDL_updating_joystick = NULL;
    1.18  
    1.19  int
    1.20  SDL_JoystickInit(void)
    1.21  {
    1.22 +    const char *hint;
    1.23      int status;
    1.24 +	
    1.25 +    /* Check to see if we should allow joystick events while in the background */
    1.26 +    hint = SDL_GetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS);
    1.27 +    if (hint && *hint == '1') {
    1.28 +        SDL_joystick_allows_background_events = SDL_TRUE;
    1.29 +    }
    1.30  
    1.31      status = SDL_SYS_JoystickInit();
    1.32      if (status >= 0) {
    1.33 -      status = 0;
    1.34 +        status = 0;
    1.35      }
    1.36      return (status);
    1.37  }
    1.38 @@ -455,17 +464,22 @@
    1.39  static SDL_bool
    1.40  SDL_PrivateJoystickShouldIgnoreEvent()
    1.41  {
    1.42 -    const char *hint;
    1.43 -    if (SDL_GetKeyboardFocus() != NULL) {
    1.44 +    if (SDL_joystick_allows_background_events)
    1.45 +    {
    1.46          return SDL_FALSE;
    1.47      }
    1.48  
    1.49 -    hint = SDL_GetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS);
    1.50 -    if (hint && *hint == '1') {
    1.51 -        return SDL_FALSE;
    1.52 +    if (SDL_WasInit(SDL_INIT_VIDEO)) {
    1.53 +        if (SDL_GetKeyboardFocus() == NULL) {
    1.54 +            // Video is initialized and we don't have focus, ignore the event.
    1.55 +            return SDL_TRUE;
    1.56 +        } else {
    1.57 +            return SDL_FALSE;
    1.58 +        }
    1.59      }
    1.60  
    1.61 -    return SDL_TRUE;
    1.62 +    // Video subsystem wasn't initialized, always allow the event
    1.63 +    return SDL_FALSE;
    1.64  }
    1.65  
    1.66  /* These are global for SDL_sysjoystick.c and SDL_events.c */