From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#69083: Emacs's keyboard hook state is not reset on session lock (Windows) Date: Thu, 15 Feb 2024 08:36:29 +0200 Message-ID: <861q9exmhe.fsf@gnu.org> References: <86cyt1qvmt.fsf@gnu.org> 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="8819"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 69083@debbugs.gnu.org To: Raffael Stocker Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Feb 15 07:40:06 2024 Return-path: 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 ) id 1raVPh-00027L-RK for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 15 Feb 2024 07:40:06 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raVPP-0001dK-W6; Thu, 15 Feb 2024 01:39:48 -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 ) id 1raVPM-0001d1-7r for bug-gnu-emacs@gnu.org; Thu, 15 Feb 2024 01:39: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 ) id 1raVPL-0002I1-Tz for bug-gnu-emacs@gnu.org; Thu, 15 Feb 2024 01:39:43 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1raVPe-00038C-Cc for bug-gnu-emacs@gnu.org; Thu, 15 Feb 2024 01:40:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 15 Feb 2024 06:40:02 +0000 Resent-Message-ID: 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.170797915311967 (code B ref 69083); Thu, 15 Feb 2024 06:40:02 +0000 Original-Received: (at 69083) by debbugs.gnu.org; 15 Feb 2024 06:39:13 +0000 Original-Received: from localhost ([127.0.0.1]:53856 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1raVOq-00036x-Op for submit@debbugs.gnu.org; Thu, 15 Feb 2024 01:39:13 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:44260) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1raVOo-00036i-2H for 69083@debbugs.gnu.org; Thu, 15 Feb 2024 01:39:10 -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 ) id 1raVMH-0001Xs-0p; Thu, 15 Feb 2024 01:36:33 -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=8YmV262C+Y51OKb3Ai/gpjyGfRI7XAUv9IBzP1SdJZU=; b=PTR9pmC4n5K2S64YL7jo UdSBa/UHUSdS/7/KHdalgFzLL4uLz0hT+9sgon1rEmat0luwuCSaRmaKExSc06EUNIdtjXaZHT2DY 4aQhYi2qRPy2RR27b0sUcDyexqPLQnu/xu5WnlOJ7kR4dj3fjfTZk5TWEGTDlPjnkqmg5hAAQ6G5L KLVXEbwQAaQSOAQohMCVTn5GKtX2PQ8cd+h/8A+EN2YwIqiLqhV10v8GFz1rijDOjZmV2MJ08wmYI cfEWZsfBeoI/aeZMV2LezQ8V5n/uElj0UVSixnMzPWSPBbpSme6JNQscI/i4Jcvvzz8IzMVO0Fx7v mvNJ8+bcQBOeEw==; In-Reply-To: (message from Raffael Stocker on Wed, 14 Feb 2024 21:20:35 +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" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-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:280039 Archived-At: > From: Raffael Stocker > 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.