From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Raffael Stocker Newsgroups: gmane.emacs.bugs Subject: bug#69083: Emacs's keyboard hook state is not reset on session lock (Windows) Date: Tue, 20 Feb 2024 19:51:21 +0100 Message-ID: References: <86cyt1qvmt.fsf@gnu.org> <861q9exmhe.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38696"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.10.8; emacs 29.2 Cc: 69083@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Feb 20 20:31:00 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 1rcVpS-0009rU-Pu for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 20 Feb 2024 20:30:58 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rcVpC-00046l-6P; Tue, 20 Feb 2024 14:30:42 -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 1rcVpA-00046b-EA for bug-gnu-emacs@gnu.org; Tue, 20 Feb 2024 14:30:40 -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 1rcVpA-0007Nb-62 for bug-gnu-emacs@gnu.org; Tue, 20 Feb 2024 14:30:40 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rcVpV-0002Dc-MK for bug-gnu-emacs@gnu.org; Tue, 20 Feb 2024 14:31:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Raffael Stocker Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 20 Feb 2024 19:31:01 +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.17084574198473 (code B ref 69083); Tue, 20 Feb 2024 19:31:01 +0000 Original-Received: (at 69083) by debbugs.gnu.org; 20 Feb 2024 19:30:19 +0000 Original-Received: from localhost ([127.0.0.1]:46582 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rcVoo-0002Cb-LU for submit@debbugs.gnu.org; Tue, 20 Feb 2024 14:30:19 -0500 Original-Received: from mail-out.m-online.net ([212.18.0.10]:41605) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rcVoi-0002CL-Sd for 69083@debbugs.gnu.org; Tue, 20 Feb 2024 14:30:16 -0500 Original-Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 4TfTxn1vlYz1s94n; Tue, 20 Feb 2024 20:29:48 +0100 (CET) Original-Received: from localhost (dynscan1.mnet-online.de [192.168.6.68]) by mail.m-online.net (Postfix) with ESMTP id 4TfTxm64Zqz1qqlW; Tue, 20 Feb 2024 20:29:48 +0100 (CET) X-Virus-Scanned: amavis at mnet-online.de Original-Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan1.mail.m-online.net [192.168.6.68]) (amavis, port 10024) with ESMTP id DZoXOjh4GzOS; Tue, 20 Feb 2024 20:29:47 +0100 (CET) X-Auth-Info: 0ZVOo3kQrM0X3uQXzMBI+ARJZgeO5W2G1FUduyMSkGxNZxdsiN4jxnw5y/sGef1K Original-Received: from Whiteflame (ppp-212-114-182-230.dynamic.mnet-online.de [212.114.182.230]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPSA; Tue, 20 Feb 2024 20:29:47 +0100 (CET) In-reply-to: <861q9exmhe.fsf@gnu.org> 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:280352 Archived-At: Eli Zaretskii writes: >> From: Raffael Stocker >> Cc: 69083@debbugs.gnu.org >> Date: Wed, 14 Feb 2024 21:20:35 +0100 >>=20 >> > So we need to load that DLL at run time via LoadLibrary, ... >>=20 >> 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. Yes, I can do that. I will leave the initialisation in the setup_w32_kbdhook function as the branch with the call to LoadLibrary should be executed only once. I experimented a bit today and found that the remove_w32_kbdhook function is not actually getting called when Emacs is being killed. It is only called when the Emacs window receives a WM_DESTROY message. But we get relevant WM_* messages, when - creating second frame with =E2=80=98C-x 5 2=E2=80=99 and closing it us= ing =E2=80=98C-x 5 0=E2=80=99: WM_EMACS_DESTROYWINDOW, then WM_DESTROY - creating second frame with =E2=80=98C-x 5 2=E2=80=99 and closing it by= clicking =E2=80=98X=E2=80=99 in frame decoration: WM_CLOSE, then WM_EMACS_DESTROYWINDOW, then WM_DESTROY - closing the last frame by clicking =E2=80=98X=E2=80=99 in frame decora= tion: WM_CLOSE - killing Emacs with =E2=80=98C-x C-c=E2=80=99: nothing At least, if Emacs is not run as daemon. Then, the WM_DESTROY is only handled when there is still another frame, and in this case =E2=80=98remove_w32_kbdhook=E2=80=99 will not remove the hook as kbdhook.ho= ok_count > 1. If Emacs is run as daemon, it cleans up the keyboard hook when closing the last window. Is there a better place where the remove_w32_kbdhook call could go such that cleanup can always happen? Or should we just not care? > This URL: > > https://stackoverflow.com/questions/8606300/how-to-detect-windows-is-lo= cked > > 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. I have read this post, but it seems to be wrong. I don't have any elevated privileges and it works without showing any prompt, at least on Windows 10. I'll try to have my colleague test it as well, she runs Windows 11. >> Unfortunately, this didn't work for me. I tried calling >> =E2=80=98EnumWindows(find_child_console, ...)=E2=80=99 with a =E2=80=98c= hild_process=E2=80=99 instance >> containing the current process id as returned by =E2=80=98GetCurrentProc= essId=E2=80=99, >> 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'm not quite sure myself what I mean. I will experiment with this a bit more and try to find out what's happening. Regards, Raffael