all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Raffael Stocker <r.stocker@mnet-mail.de>
Cc: 69083@debbugs.gnu.org
Subject: bug#69083: Emacs's keyboard hook state is not reset on session lock (Windows)
Date: Thu, 15 Feb 2024 08:36:29 +0200	[thread overview]
Message-ID: <861q9exmhe.fsf@gnu.org> (raw)
In-Reply-To: <yplm34tupxua.fsf@mnet-mail.de> (message from Raffael Stocker on Wed, 14 Feb 2024 21:20:35 +0100)

> From: Raffael Stocker <r.stocker@mnet-mail.de>
> Cc: 69083@debbugs.gnu.org
> Date: Wed, 14 Feb 2024 21:20:35 +0100
> 
> > So we need to load that DLL at run time via LoadLibrary, ...
> 
> I have added that.

Thanks.  I suggest one optimization: instead of probing whether
LoadLibrary succeeds in two places, do it only once, where you call
WTSRegisterSessionNotification, and at that time also record the
function pointer for WTSUnRegisterSessionNotification; then use the
2nd function pointer, if non-NULL, to call the unregister API.

> > (I'm a bit surprised that Remote Desktop Services need to be used for
> > this purpose.  Are you sure there's no other way for Emacs to know
> > that the system is going to be locked?  Where did you read about the
> > need to reset the keyboard hook state in that case, and the way to do
> > it?)
> 
> For me, the need to reset the hook state is implied by the existence of
> the ‘reset_w32_kbdhook_state’ function defined in ‘w32fns.c’ and its
> call site.  This function has been there since 2016, but was never
> called, because the ‘WM_WTSSESSION_CHANGE’ message wasn't received.
> >From [0]:
> 
> MS> This message is sent only to applications that have registered to
> MS> receive this message by calling WTSRegisterSessionNotification.
> 
> This seems to be the only way to find out it's being locked; at least I
> couldn't find anything indicating otherwise on the Microsoft website and
> a quick web search didn't turn up anything either.

This URL:

  https://stackoverflow.com/questions/8606300/how-to-detect-windows-is-locked

seems to indicate that WTSRegisterSessionNotification requires
elevation on Windows 10/11.  Did you get the UAC prompt when calling
that API?  Are you running with admin privileges when you test this
code?  Triggering UAC prompts when starting Emacs would be a nuisance
to our users.

> >> To receive session notifications, one must provide a window handle,
> >> which is fine if Emacs does not run in console mode.  I don't know
> >> whether it is possible to get these notifications in console Emacs; at
> >> least using the console handle didn't work for me.
> >
> > Please try the technique used by the function find_child_console which
> > is defined in w32proc.c.
> 
> Unfortunately, this didn't work for me.  I tried calling
> ‘EnumWindows(find_child_console, ...)’ with a ‘child_process’ instance
> containing the current process id as returned by ‘GetCurrentProcessId’,
> but I don't seem to get a useful window handle.

What do you mean?  What is the result of using find_child_console?
does the condition in find_child_console, which looks at the
process_id of all windows, never match the process ID of the Emacs
session running with -nw?  Or what else goes wrong?

> I must be doing something wrong here.  OTOH, [1] says:
> 
> MS> Note For Windows 8 and later, EnumWindows enumerates only top-level
> MS> windows of desktop apps.
> 
> Does this mean that ‘EnumWindows’ it doesn't work for console windows or
> is this remark irrelevant here?  I am not familiar with the Windows
> terminology.

I think it does work with console windows, since that's what Emacs
uses it for (the sub-processes Emacs runs are almost always console
processes).

> Come to think of it, don't I need the window handle of the console
> window that Emacs is running in, which would imply that a handle for
> Emacs' pid doesn't exist?  I was starting Emacs in cmd.exe using ‘emacs
> -Q -nw’, so I would probably have to find the pid of the ‘cmd’ process
> first, right?

Maybe, I don't know.  But first, I'd like to understand what goes
wrong when you are looking for a window for which
GetWindowThreadProcessId returns the PID of Emacs.

> > Do we really need to use WTSRegisterSessionNotificationEx?  Can't we
> > use WTSRegisterSessionNotification instead?  AFAIK, the latter is
> > available since Windows XP, whereas the former only since Vista.
> 
> Yes, ‘WTSRegisterSessionNotification’ works, too.  However, [3] says
> that the minimum supported client here is Windows Vista, as with the
> ‘-Ex’ version.

That's a lie.  The MS docs frequently pretend that old versions of
Windows don't exist, and this is one such case.





  reply	other threads:[~2024-02-15  6:36 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-12 19:04 bug#69083: Emacs's keyboard hook state is not reset on session lock (Windows) Raffael Stocker
2024-02-12 20:23 ` Eli Zaretskii
2024-02-14 20:20   ` Raffael Stocker
2024-02-15  6:36     ` Eli Zaretskii [this message]
2024-02-20 18:51       ` Raffael Stocker
2024-02-21 12:37         ` Eli Zaretskii
2024-02-21 14:13           ` Raffael Stocker
2024-02-21 15:03             ` Eli Zaretskii
2024-02-26 20:50           ` Raffael Stocker
2024-02-27  7:42             ` Eli Zaretskii
2024-02-29 20:22               ` Raffael Stocker
2024-03-01  6:41                 ` Eli Zaretskii
2024-03-03 16:43                   ` Raffael Stocker
2024-03-03 17:23                     ` Eli Zaretskii
2024-03-03 17:39                       ` Eli Zaretskii
2024-03-04 18:10                       ` Raffael Stocker
2024-03-14  8:25                         ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=861q9exmhe.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=69083@debbugs.gnu.org \
    --cc=r.stocker@mnet-mail.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.