Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: ScrCpy either steals focus or closes itself completely when orientation changes and it has no focus #3947

Closed
AndroidDeveloperLB opened this issue Apr 24, 2023 · 15 comments

Comments

@AndroidDeveloperLB
Copy link

AndroidDeveloperLB commented Apr 24, 2023

  • [V ] I have read the FAQ.
  • [V ] I have searched in existing issues.

Environment

  • OS: [e.g. Debian, Windows, macOS...]
    Windows

  • scrcpy version: [e.g. 1.12.1]
    2.0

  • installation method: [e.g. manual build, apt, snap, brew, Windows release...]
    Extract from the ZIP file

  • device model:
    Pixel 6

  • Android version: [e.g. 10]
    13

Describe the bug
I've launched an app that decided to change the device orientation after some time. During this time, I was actually using a very different window on Windows OS.
When it changed the orientation, ScrCpy stole focus. As it was in a full screen online game, it was very annoying because I lost the context and had to quickly switch back.
When I tried to reproduce it via a sample, I've also noticed that if ScrCpy is just minimized to the taskbar, it will close itself completely when the orientation changes.
The command I use for ScrCpy is:

C:\android\scrcpy\scrcpy-noconsole.vbs -S --always-on-top --stay-awake --no-audio

Here's a sample app to simulate this, which changes the orientation after 5 seconds since shown. During this time, switch to another window, and wait for it to happen.

My Application.zip
app-debug.zip

Code:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Handler(mainLooper).postDelayed({
            requestedOrientation = if (resources.configuration.orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
                ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            else
                ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        },  5000L)
    }
}

On errors, please provide the output of the console (and adb logcat if relevant).
There are no errors.

However, here's a video showing the issue:
2023-04-25_00-34-32.zip

And here's a video showing the issue when there is another window, on top, that needs to stay on focus (a video player app) :

2023-04-25_00-42-13_1.zip

@rom1v
Copy link
Collaborator

rom1v commented Apr 24, 2023

When it changed the orientation, ScrCpy stole focus

Does it also happen when you don't pass --always-on-top? In any case, this is a behavior from SDL.

I've also noticed that if ScrCpy is just minimized to the taskbar, it will close itself completely when the orientation changes.

100% reproducible? If you keep a terminal, what is the error message?

@AndroidDeveloperLB
Copy link
Author

AndroidDeveloperLB commented Apr 24, 2023

What do you mean "behavior from SDL." ? I don't want it to ever steal focus, let alone just because of orientation change.

I can always reproduce this.

The closing of ScrCpy happens even without being on top, using this command:

C:\android\scrcpy\scrcpy-noconsole.vbs

As for stealing focus, because the other app is on top, ScrCpy can't be on top of it when stealing focus, so removing "--always-on-top" won't show the issue.

Which command do you want me to try? Please show me the exact command to write

@rom1v
Copy link
Collaborator

rom1v commented Apr 25, 2023

You are reporting two different problems.

The one about focus stealing with "always on top" is out of control of scrcpy: it is the behavior from your window manager (or maybe SDL). For example, it does not happen on my window manager on Linux/XFCE.

I can reproduce your second issue about rotation while minimized (only on Windows).

On scrcpy 2.0, the error is:

ERROR: Could not create texture: CreateTexture(D3DPOOL_DEFAULT): INVALIDCALL

On scrcpy dev branch, the error is:

ERROR: Could not update texture: LockRect(): INVALIDCALL

Scrcpy should probably not create or touch textures while a window is minimized. That will be annoying to fix, I added it on my TODO list. (Refs for myself: libsdl-org/SDL#7651)

@AndroidDeveloperLB
Copy link
Author

AndroidDeveloperLB commented Apr 25, 2023

About the "always on top" issue that steals focus, can you please fix it somehow using a workaround?
For example, you can delay the orientation change till it gets focus/shown again. It can check if another app is in full screen, or something, to trigger delaying.
Alternatively, you can minimize/hide the window during this time, to avoid focus stealing, and perhaps later restore the window.
Maybe you can check if the same occurs on the Android feature on Windows 11 (I never tried it)?

Would the fix of being minimized help with this issue too?

rom1v added a commit that referenced this issue May 9, 2023
When the scrcpy window is minimized on Windows with D3D9, texture
creation and update fail.

In that case, do not terminate scrcpy, but store the pending size or
frame to update to retry on the next update or rendering.

Fixes #3947 <#3947>
rom1v added a commit that referenced this issue May 9, 2023
On some window managers (e.g. on Windows), performing a resize while the
window is minimized does nothing (the restored window keeps its old
size).

Therefore, like for maximized and fullscreen states, wait for the window
to be restored to apply a resize.

Refs #3947 <#3947>
@rom1v
Copy link
Collaborator

rom1v commented May 9, 2023

Scrcpy should probably not create or touch textures while a window is minimized. That will be annoying to fix, I added it on my TODO list.

Please test branch fix_minimize.3. Here is a binary:

Does it fix the rotation while minimized?

@AndroidDeveloperLB
Copy link
Author

AndroidDeveloperLB commented May 9, 2023

@rom1v

The focus stealing still exists (occurs when it tries to rotate, not minimized), but the issue of it being closed (occurs when it tries to rotate while being minimized) doesn't exist.
So, to avoid the focus stealing, I can use the workaround of me minimizing ScrCpy to the taskbar, manually.
So each time I'm about to play a video or play a game that is on top or takes the full screen and shouldn't be bothered, I should minimize ScrCpy.

Please try to fix the focus stealing too.

Is it OK if I keep using this release for now? No serious issues?

@rom1v
Copy link
Collaborator

rom1v commented May 9, 2023

but the issue of it being closed (occurs when it tries to rotate while being minimized) doesn't exist.

Great! Thank you for your test 👍

The focus stealing still exists (occurs when it tries to rotate, not minimized)

Focus stealing is not a scrcpy issue. You request --always-on-top, your window manager assumes that your window is on top, so it is undefined what the focus should be, your window manager decides. Just don't use --always-on-top.

Is it OK if I keep using this release for now?

Sure, it also includes #3978, so you could also test if you want :)

@AndroidDeveloperLB
Copy link
Author

AndroidDeveloperLB commented May 9, 2023

But I want to use it as always-on-top.
Please try to think of a solution for this.
I hate when anything steals focus. Wish I could disable this behavior globally on the entire OS, or add a permission to let me choose which apps are allowed to do it.

@rom1v
Copy link
Collaborator

rom1v commented May 9, 2023

Then report to SDL that when always on top is set, changing the window size steals the focus on your system.

@AndroidDeveloperLB
Copy link
Author

  1. Who are SDL? How can I contact them?
  2. Is there no workaround possible here? Can't the app check before it steals the focus due to rotation, if another app is currently on top or full-screen, and prevent the focus-stealing?

@rom1v
Copy link
Collaborator

rom1v commented May 11, 2023

Who are SDL? How can I contact them?

It's the framework used by scrcpy to handle window/textures/etc. https://github.com/libsdl-org/SDL

For example, I opened an issue/question about your first problem: libsdl-org/SDL#7651

Can't the app check before it steals the focus due to rotation, if another app is currently on top or full-screen, and prevent the focus-stealing?

No. That would be a recipe for disaster anyway.

@AndroidDeveloperLB
Copy link
Author

AndroidDeveloperLB commented May 11, 2023

Wait, are you saying you've reported about it for me, about the focus stealing?
I don't think this is what's written there though.

About the check, is it hard?
Maybe you could have a fork of it and I could check it out ? Maybe you could have an optional flag (turned off by default) to handle this?

rom1v added a commit that referenced this issue May 15, 2023
When the scrcpy window is minimized on Windows with D3D9, texture
creation and update fail.

In that case, do not terminate scrcpy, but store the pending size or
frame to update to retry on the next update or rendering.

Fixes #3947 <#3947>
rom1v added a commit that referenced this issue May 15, 2023
On some window managers (e.g. on Windows), performing a resize while the
window is minimized does nothing (the restored window keeps its old
size).

Therefore, like for maximized and fullscreen states, wait for the window
to be restored to apply a resize.

Refs #3947 <#3947>
rom1v added a commit that referenced this issue May 22, 2023
On some window managers (e.g. on Windows), performing a resize while the
window is minimized does nothing (the restored window keeps its old
size).

Therefore, like for maximized and fullscreen states, wait for the window
to be restored to apply a resize.

Refs #3947 <#3947>
@rom1v rom1v closed this as completed in 6298ef0 Jun 21, 2023
@AndroidDeveloperLB
Copy link
Author

AndroidDeveloperLB commented Jun 22, 2023

@rom1v Can you please open a ticket where you thought about, and explain the problem there?
I don't have the technical expertise in this, so it's best that you explain it to them...

@rom1v
Copy link
Collaborator

rom1v commented Jun 22, 2023

@AndroidDeveloperLB Either report it directly to https://github.com/libsdl-org/SDL/issues, or open a new scrcpy issue with a detailed explanation of the focus stealing issue alone (so that I can reference it without all the noise about the other issue fixed in this thread). Btw, a video showing the problem could help.

@AndroidDeveloperLB
Copy link
Author

@rom1v OK here:
#4109

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants