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: Mon, 12 Feb 2024 22:23:54 +0200	[thread overview]
Message-ID: <86cyt1qvmt.fsf@gnu.org> (raw)
In-Reply-To: <yplmeddhpj9t.fsf@mnet-mail.de> (message from Raffael Stocker on Mon, 12 Feb 2024 20:04:50 +0100)

> From: Raffael Stocker <r.stocker@mnet-mail.de>
> Date: Mon, 12 Feb 2024 20:04:50 +0100
> 
> when locking the computer using WIN+L on Windows, Emacs is supposed to
> reset its keyboard hook state.  This is done in the function
> ‘reset_w32_kbdhook_state’ in ‘w32fns.c’, which is called upon receiving
> the ‘WM_WTSSESSION_CHANGE’ message with the ‘WTS_SESSION_LOCK’
> parameter.  However, this message is never received, because to receive
> it, an application must register for session notifications, which Emacs
> doesn't do.
> 
> When Emacs can't reset its state, the handling of the windows key is
> confused after locking and unlocking the computer, but the issue seems
> to auto-correct when a windows key is pressed again.  I didn't observe
> any real problem with it, but I have not configured any non-default
> handling of the windows keys in Emacs.  It may be different in those
> cases.
> 
> Note that while this bug looks like it might be connected to bug#68914,
> I believe it's a separate issue.  The behaviour described there is
> neither triggered by this bug nor solved by the attached patch.
> 
> The attached patch rectifies the situation, but to do so, Emacs must be
> linked with ‘wtsapi32.dll’.  I am not sure whether there is a different
> way of resetting the state that doesn't require the introduction of a
> new dependency.

We can use that DLL, but we cannot link against it, because that would
make the DLL a prerequisite for running Emacs, and then it will be
impossible to start Emacs on Windows versions before XP, where that
DLL was introduced.  So we need to load that DLL at run time via
LoadLibrary, and only register for session notifications if the DLL is
available, by calling the DLL functions through function pointers --
this way, the Emacs executable doesn't have a static link-time
dependency on the DLL.  You will find many examples of this technique
in w32.c and in other w32*.c files in Emacs.

(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?)

> 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.

> I also noticed that while the keyboard hook is set up in console mode
> using ‘setup_w32_kbdhook’, there does not seem to be a corresponding
> call to ‘remove_w32_kbdhook’.

Doesn't Windows remove the hook when the process exits?

> Also, in console mode the keyboard hook is always installed, while
> in GUI Emacs it is only installed when ‘WINDOWSNT’ is defined.

That's because w32console.c is not compiled into the Cygwin build
(which does not define WINDOWSNT), whereas w32fns.c is.

> @@ -2800,17 +2801,22 @@ setup_w32_kbdhook (void)
>        /* Set the hook.  */
>        kbdhook.hook = SetWindowsHookEx (WH_KEYBOARD_LL, funhook,
>  				       GetModuleHandle (NULL), 0);
> +      if (hwnd != NULL)
> +	WTSRegisterSessionNotificationEx (WTS_CURRENT_SERVER, hwnd,
> +					  NOTIFY_FOR_THIS_SESSION);

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.

Thanks.





  reply	other threads:[~2024-02-12 20:23 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 [this message]
2024-02-14 20:20   ` Raffael Stocker
2024-02-15  6:36     ` Eli Zaretskii
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=86cyt1qvmt.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.