Ryujinx-SDL/src/video/android
Sam Lantinga f0a4fea89f Fixed bug 4608 - Android: not getting SDL_WINDOWEVENT_FOCUS_GAINED on start of our app
Dan Ginsburg

I've seen this on several devices including Moto Z running Android 7 and a Snapdragon 845 running Android 9.

What happens is as follows:

SDLActivity.onWindowFocusChanged(true) happens pretty early on, but it's before we've done SDL_CreateWindow and so Android_Window is 0x0 thus this message does not get sent:

JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeFocusChanged)(
                                    JNIEnv *env, jclass cls, jboolean hasFocus)
{
    SDL_LockMutex(Android_ActivityMutex);

    if (Android_Window) {
        __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeFocusChanged()");
        SDL_SendWindowEvent(Android_Window, (hasFocus ? SDL_WINDOWEVENT_FOCUS_GAINED : SDL_WINDOWEVENT_FOCUS_LOST), 0, 0);
    }

    SDL_UnlockMutex(Android_ActivityMutex);
}

When the window does get created, in Android_CreateWindow it does this:

 window->flags &= ~SDL_WINDOW_RESIZABLE;     /* window is NEVER resizeable */
    window->flags &= ~SDL_WINDOW_HIDDEN;
    window->flags |= SDL_WINDOW_SHOWN;          /* only one window on Android */
    window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */

    /* One window, it always has focus */
    SDL_SetMouseFocus(window);
    SDL_SetKeyboardFocus(window);

The SDL_SetKeyboardFocus does send an SDL_WINDOWEVENT_FOCUS_GAINED message, but it gets eaten in SDL_SendWindowEvent because we've forced SDL_WINDOW_INPUT_FOCUS beforehand:

 case SDL_WINDOWEVENT_FOCUS_GAINED:
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
            return 0;
        }
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
        SDL_OnWindowFocusGained(window);
        break;

I can fix the problem if I comment out this line from Android_CreateWindow:

    window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */

I would propose that as a fix unless there is a reason not to.
2019-04-25 14:17:07 -07:00
..
SDL_androidclipboard.c Updated copyright for 2019 2019-01-04 22:01:14 -08:00
SDL_androidclipboard.h Updated copyright for 2019 2019-01-04 22:01:14 -08:00
SDL_androidevents.c Android: add static variable initialization in non blocking event loop 2019-04-23 14:24:58 +02:00
SDL_androidevents.h Android: add hint SDL_HINT_ANDROID_BLOCK_ON_PAUSE 2019-04-05 09:16:30 +02:00
SDL_androidgl.c Updated copyright for 2019 2019-01-04 22:01:14 -08:00
SDL_androidgl.h Updated copyright for 2019 2019-01-04 22:01:14 -08:00
SDL_androidkeyboard.c Updated copyright for 2019 2019-01-04 22:01:14 -08:00
SDL_androidkeyboard.h Updated copyright for 2019 2019-01-04 22:01:14 -08:00
SDL_androidmessagebox.c Updated copyright for 2019 2019-01-04 22:01:14 -08:00
SDL_androidmessagebox.h Updated copyright for 2019 2019-01-04 22:01:14 -08:00
SDL_androidmouse.c Updated copyright for 2019 2019-01-04 22:01:14 -08:00
SDL_androidmouse.h Updated copyright for 2019 2019-01-04 22:01:14 -08:00
SDL_androidtouch.c Android: remove SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH 2019-04-04 17:01:02 +02:00
SDL_androidtouch.h Updated copyright for 2019 2019-01-04 22:01:14 -08:00
SDL_androidvideo.c Android: add hint SDL_HINT_ANDROID_BLOCK_ON_PAUSE 2019-04-05 09:16:30 +02:00
SDL_androidvideo.h Android: prevent concurrency in Android_SetScreenResolution() when exiting 2019-01-17 11:05:05 +01:00
SDL_androidvulkan.c Updated copyright for 2019 2019-01-04 22:01:14 -08:00
SDL_androidvulkan.h Updated copyright for 2019 2019-01-04 22:01:14 -08:00
SDL_androidwindow.c Fixed bug 4608 - Android: not getting SDL_WINDOWEVENT_FOCUS_GAINED on start of our app 2019-04-25 14:17:07 -07:00
SDL_androidwindow.h Updated copyright for 2019 2019-01-04 22:01:14 -08:00