From mboxrd@z Thu Jan  1 00:00:00 1970
Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail
From: Eli Zaretskii <eliz@gnu.org>
Newsgroups: gmane.emacs.bugs
Subject: bug#69083: Emacs's keyboard hook state is not reset on session lock
 (Windows)
Date: Mon, 12 Feb 2024 22:23:54 +0200
Message-ID: <86cyt1qvmt.fsf@gnu.org>
References: <yplmeddhpj9t.fsf@mnet-mail.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214";
	logging-data="5123"; mail-complaints-to="usenet@ciao.gmane.io"
Cc: 69083@debbugs.gnu.org
To: Raffael Stocker <r.stocker@mnet-mail.de>
Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Feb 12 21:46:10 2024
Return-path: <bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org>
Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org
Original-Received: from lists.gnu.org ([209.51.188.17])
	by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
	(Exim 4.92)
	(envelope-from <bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org>)
	id 1rZdBp-00018O-Rt
	for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 12 Feb 2024 21:46:10 +0100
Original-Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <bug-gnu-emacs-bounces@gnu.org>)
	id 1rZdBT-0004zR-Eo; Mon, 12 Feb 2024 15:45:47 -0500
Original-Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <Debian-debbugs@debbugs.gnu.org>)
 id 1rZdBR-0004xo-Is
 for bug-gnu-emacs@gnu.org; Mon, 12 Feb 2024 15:45:45 -0500
Original-Received: from debbugs.gnu.org ([2001:470:142:5::43])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <Debian-debbugs@debbugs.gnu.org>)
 id 1rZdBR-0002fk-Aw
 for bug-gnu-emacs@gnu.org; Mon, 12 Feb 2024 15:45:45 -0500
Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2)
 (envelope-from <Debian-debbugs@debbugs.gnu.org>) id 1rZdBi-0004xa-8H
 for bug-gnu-emacs@gnu.org; Mon, 12 Feb 2024 15:46:02 -0500
X-Loop: help-debbugs@gnu.org
Resent-From: Eli Zaretskii <eliz@gnu.org>
Original-Sender: "Debbugs-submit" <debbugs-submit-bounces@debbugs.gnu.org>
Resent-CC: bug-gnu-emacs@gnu.org
Resent-Date: Mon, 12 Feb 2024 20:46:02 +0000
Resent-Message-ID: <handler.69083.B69083.170777074518986@debbugs.gnu.org>
Resent-Sender: help-debbugs@gnu.org
X-GNU-PR-Message: followup 69083
X-GNU-PR-Package: emacs
Original-Received: via spool by 69083-submit@debbugs.gnu.org id=B69083.170777074518986
 (code B ref 69083); Mon, 12 Feb 2024 20:46:02 +0000
Original-Received: (at 69083) by debbugs.gnu.org; 12 Feb 2024 20:45:45 +0000
Original-Received: from localhost ([127.0.0.1]:57765 helo=debbugs.gnu.org)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <debbugs-submit-bounces@debbugs.gnu.org>)
 id 1rZdBQ-0004w7-IB
 for submit@debbugs.gnu.org; Mon, 12 Feb 2024 15:45:45 -0500
Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58636)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <eliz@gnu.org>) id 1rZdBO-0004vW-7u
 for 69083@debbugs.gnu.org; Mon, 12 Feb 2024 15:45:43 -0500
Original-Received: from fencepost.gnu.org ([2001:470:142:3::e])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <eliz@gnu.org>)
 id 1rZcqL-0007ea-Hm; Mon, 12 Feb 2024 15:23:57 -0500
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org;
 s=fencepost-gnu-org; h=MIME-version:References:Subject:In-Reply-To:To:From:
 Date; bh=zxwqDLcxDEsjgYZ8/HwbpyQj1N/pkcH5SrRfTxAbzFg=; b=i5T1AG+ObMdHGbL2hhB4
 NQnBjPdkKuG2IEm9rQ6vAw1KGXjrvTj2U7ekF3HUuenNChw34q5pYJS9cLtnfHtyztjTWTVUPJHnM
 liwt0aHo1WEu7e0D5QS2GzTWQg+WmElpv0FUJd6YRPJUWVA2u+yX+BZxvhDgvZZz9kXC3GJ8unyqi
 iHI1KEmqfXoEBc+sm8ehJQgrLJn6eNYcXIEWkXq7Y9+rxmLehAG3gVnhEL/wB/kwqTWHJ8LwnKR8o
 vDYOrG5W+H630NSsaEWc9jmXpGm+Dm5NtZl5IpfBkMYT1HKF9QV2NzzFtFdvlggckqmBdh/xR642r
 T4n2zjS4lfUrBA==;
In-Reply-To: <yplmeddhpj9t.fsf@mnet-mail.de> (message from Raffael Stocker on
 Mon, 12 Feb 2024 20:04:50 +0100)
X-BeenThere: debbugs-submit@debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
X-BeenThere: bug-gnu-emacs@gnu.org
List-Id: "Bug reports for GNU Emacs,
 the Swiss army knife of text editors" <bug-gnu-emacs.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/bug-gnu-emacs>,
 <mailto:bug-gnu-emacs-request@gnu.org?subject=unsubscribe>
List-Archive: <https://lists.gnu.org/archive/html/bug-gnu-emacs>
List-Post: <mailto:bug-gnu-emacs@gnu.org>
List-Help: <mailto:bug-gnu-emacs-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/bug-gnu-emacs>,
 <mailto:bug-gnu-emacs-request@gnu.org?subject=subscribe>
Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org
Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org
Xref: news.gmane.io gmane.emacs.bugs:279927
Archived-At: <http://permalink.gmane.org/gmane.emacs.bugs/279927>

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