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

mintty is slow to grab focus on startup, which interacts badly with focus-stealing prevention #1113

Closed
benrg opened this issue Jul 31, 2021 · 11 comments

Comments

@benrg
Copy link

benrg commented Jul 31, 2021

There's a human-perceptible delay between the mintty process being created and the appearance of the terminal window on the desktop. It's only a fraction of a second, but it creates a bigger problem. I have a hotkey that starts a mintty terminal, and I tend to press the hotkey and then immediately start typing what I want to go into the terminal. Sometimes, the first keystroke precedes the appearance of the terminal window, so it goes to another window. Then (I assume this is what is happening) Windows' focus-stealing prevention algorithm ironically decides that I really wanted to be typing into that other window, and never gives focus to the new terminal. By the time I notice, a number of additional keystrokes have gone to a window that, in many cases, I can't even see any more because the new terminal has appeared on top of it. Cleaning up after this is a pain.

I'm able to consistently reproduce this by pressing my mintty-launching hotkey and then quickly pressing a random key. If I make the same hotkey launch notepad.exe instead, I can't reproduce it, so I think the problem is mintty being slow to grab focus.

I don't know if fixing this is feasible, but it would be nice.

@mintty
Copy link
Owner

mintty commented Jul 31, 2021

There is hardly any terminal that would start "immediately". There are typically a number of parameters, e.g. command-line parameters, that impact the appearance of the terminal, unlike simple system programs like notepad, which may explain some tiny difference.
Most people would simply wait until a window appears before they type "into it", so this is not a priority issue either.
However it's a challenge and I may find some time to look into it but don't expect miracles.

@mintty
Copy link
Owner

mintty commented Jul 31, 2021

On my system, it takes ~130ms from process start until display of the window.
~80ms are taken by fork(), a notoriously slow system call in cygwin, unfortunately.
Now it might be feasible to create the child process after the window is shown, with unforeseen side effects however.

@mintty
Copy link
Owner

mintty commented Aug 29, 2021

Applied a few tweaks to get the window to display earlier:
Font initialisation could be optimised, saving ~40ms.
Drag-and-drop setup can be done after showing the window, postponing another 7ms.
Showing the tabbar (if configured) can also be moved after window display, for another 12ms deferred.
ShowWindow() itself takes 18ms, CreateWindow() 4ms, both unavoidable.
The major delay remains starting the shell with fork() taking 80ms which are out of control of mintty.
I tried creating the shell after displaying the window but it has unpleasant side effects (as commented in the code).

@mintty
Copy link
Owner

mintty commented Aug 29, 2021

Actually it seems to be possible to grab the focus even before showing the window, as a workaround to evade the child creation delay for keyboard handling.
Please try the uploaded repository version.

@mintty
Copy link
Owner

mintty commented Sep 3, 2021

Released 3.5.1.

@dscho
Copy link
Contributor

dscho commented Aug 10, 2022

At least for me, this seems not to be working as intended (I am on Windows 11, if that makes any difference). I wonder whether we should actually add back the old SetFocus() without removing the new one.

@mintty
Copy link
Owner

mintty commented Aug 23, 2022

So your focus is still grabbed with a delay, causing symptoms as described above?
As I don't have Windows 11, I can neither reproduce nor test the issue, but if you confirm the patch below helps, I'll commit it:

--- a/src/winmain.c
+++ b/src/winmain.c
@@ -6872,6 +6872,9 @@ static int dynfonts = 0;

   // Finally show the window.
   ShowWindow(wnd, show_cmd);
+  // and grab focus again, just in case and for Windows 11
+  // (https://github.com/mintty/mintty/issues/1113#issuecomment-1210278957)
+  SetFocus(wnd);
   // Cloning fullscreen window
   if (run_max == 2)
     win_maximise(2);

@dscho
Copy link
Contributor

dscho commented Aug 23, 2022

It is an intermittent issue, for sure. For example, right now I cannot reproduce the issue (I suspect it is less likely to occur directly after a fresh reboot).

Once I can reproduce the issue again, I will make sure to try that patch.

@dscho
Copy link
Contributor

dscho commented Aug 31, 2022

Once I can reproduce the issue again, I will make sure to try that patch.

I was just able to reproduce the issue again, and this patch indeed fixed the issue!

@mintty
Copy link
Owner

mintty commented Nov 14, 2022

Released 3.6.2.

@apodtele
Copy link

apodtele commented Apr 26, 2023

None of these tweaks create any focus races when starting Windows calc, I suppose. I just want to mention issue #1206 here.

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

4 participants