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: Mon, 12 Feb 2024 20:04:50 +0100 Message-ID: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16003"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.10.8; emacs 29.2 To: 69083@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Feb 12 20:37:01 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 1rZc6u-0003xa-Uj for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 12 Feb 2024 20:37:01 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rZc6i-0006Bj-JK; Mon, 12 Feb 2024 14:36: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 1rZc6g-0006BN-En for bug-gnu-emacs@gnu.org; Mon, 12 Feb 2024 14:36:46 -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 1rZc6g-0007lW-6n for bug-gnu-emacs@gnu.org; Mon, 12 Feb 2024 14:36:46 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rZc6w-00058M-Id for bug-gnu-emacs@gnu.org; Mon, 12 Feb 2024 14:37:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Raffael Stocker Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 12 Feb 2024 19:37:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 69083 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.170776661619701 (code B ref -1); Mon, 12 Feb 2024 19:37:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 12 Feb 2024 19:36:56 +0000 Original-Received: from localhost ([127.0.0.1]:52515 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rZc6p-00057d-3n for submit@debbugs.gnu.org; Mon, 12 Feb 2024 14:36:55 -0500 Original-Received: from lists.gnu.org ([2001:470:142::17]:56002) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rZc6m-00056s-Ed for submit@debbugs.gnu.org; Mon, 12 Feb 2024 14:36:53 -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 1rZc6N-00069Y-Jz for bug-gnu-emacs@gnu.org; Mon, 12 Feb 2024 14:36:28 -0500 Original-Received: from mail-out.m-online.net ([212.18.0.9]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rZc6K-0007k9-Ga for bug-gnu-emacs@gnu.org; Mon, 12 Feb 2024 14:36:26 -0500 Original-Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 4TYZSz3YxMz1r1sg for ; Mon, 12 Feb 2024 20:36:19 +0100 (CET) Original-Received: from localhost (dynscan1.mnet-online.de [192.168.6.68]) by mail.m-online.net (Postfix) with ESMTP id 4TYZSz3TMtz1qqlS for ; Mon, 12 Feb 2024 20:36:19 +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 SqAYZKzWCJVT for ; Mon, 12 Feb 2024 20:36:18 +0100 (CET) X-Auth-Info: tSfx832Ou24euc5EdZ/yFyWL4qJpZebs2tDmg6bkzPkIQmpWuyWOt+aGP8rrlTfk Original-Received: from Whiteflame (ppp-212-114-182-193.dynamic.mnet-online.de [212.114.182.193]) (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 for ; Mon, 12 Feb 2024 20:36:18 +0100 (CET) Received-SPF: pass client-ip=212.18.0.9; envelope-from=r.stocker@mnet-mail.de; helo=mail-out.m-online.net X-Spam_score_int: 7 X-Spam_score: 0.7 X-Spam_bar: / X-Spam_report: (0.7 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action 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:279921 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi, when locking the computer using WIN+L on Windows, Emacs is supposed to reset its keyboard hook state. This is done in the function =E2=80=98reset_w32_kbdhook_state=E2=80=99 in =E2=80=98w32fns.c=E2=80=99, wh= ich is called upon receiving the =E2=80=98WM_WTSSESSION_CHANGE=E2=80=99 message with the =E2=80=98WTS_SE= SSION_LOCK=E2=80=99 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 =E2=80=98wtsapi32.dll=E2=80=99. I am not sure whether there is= a different way of resetting the state that doesn't require the introduction of a new dependency. 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. I also noticed that while the keyboard hook is set up in console mode using =E2=80=98setup_w32_kbdhook=E2=80=99, there does not seem to be a corr= esponding call to =E2=80=98remove_w32_kbdhook=E2=80=99. Also, in console mode the ke= yboard hook is always installed, while in GUI Emacs it is only installed when =E2=80=98WINDOWSNT=E2=80=99 is defined. I have zero experience in Windows programming, so it would be highly desirable if someone with more knowledge could comment on the issue and my proposed solution. Regards, Raffael --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=reset_kbdhook.patch Content-Description: session notification patch diff --git a/configure.ac b/configure.ac index 847fdbd54d2..415430de417 100644 --- a/configure.ac +++ b/configure.ac @@ -3090,6 +3090,7 @@ AC_DEFUN fi W32_LIBS="$W32_LIBS -lwinmm -lusp10 -lgdi32 -lcomdlg32" W32_LIBS="$W32_LIBS -lmpr -lwinspool -lole32 -lcomctl32" + W32_LIBS="$W32_LIBS -lwtsapi32" W32_RES_LINK="\$(EMACSRES)" CLIENTRES="emacsclient.res" CLIENTW="emacsclientw\$(EXEEXT)" diff --git a/src/w32console.c b/src/w32console.c index 0936b5f37e6..0bd23f06832 100644 --- a/src/w32console.c +++ b/src/w32console.c @@ -821,8 +821,12 @@ initialize_w32_display (struct terminal *term, int *width, int *height) /* Setup w32_display_info structure for this frame. */ w32_initialize_display_info (build_string ("Console")); +#ifdef WINDOWSNT /* Set up the keyboard hook. */ - setup_w32_kbdhook (); + /* FIXME where can this be cleaned up, that is, where can we call + remove_w32_kbdhook? */ + setup_w32_kbdhook (NULL); +#endif } diff --git a/src/w32fns.c b/src/w32fns.c index 8d4bd00b91c..1d5a591466c 100644 --- a/src/w32fns.c +++ b/src/w32fns.c @@ -49,6 +49,7 @@ #define _WIN32_WINNT 0x0600 #ifdef WINDOWSNT #include #include /* for _getmbcp */ +#include /* for WTS(Un)RegisterSessionNotificationEx */ #endif /* WINDOWSNT */ #if CYGWIN @@ -2744,7 +2745,7 @@ funhook (int code, WPARAM w, LPARAM l) /* Set up the hook; can be called several times, with matching remove_w32_kbdhook calls. */ void -setup_w32_kbdhook (void) +setup_w32_kbdhook (HWND hwnd) { kbdhook.hook_count++; @@ -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); } } /* Remove the hook. */ void -remove_w32_kbdhook (void) +remove_w32_kbdhook (HWND hwnd) { kbdhook.hook_count--; if (kbdhook.hook_count == 0 && w32_kbdhook_active) { UnhookWindowsHookEx (kbdhook.hook); + if (hwnd != NULL) + WTSUnRegisterSessionNotificationEx (WTS_CURRENT_SERVER, hwnd); kbdhook.hook = NULL; } } @@ -5301,13 +5307,13 @@ w32_wnd_proc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) #ifdef WINDOWSNT case WM_CREATE: - setup_w32_kbdhook (); + setup_w32_kbdhook (hwnd); goto dflt; #endif case WM_DESTROY: #ifdef WINDOWSNT - remove_w32_kbdhook (); + remove_w32_kbdhook (hwnd); #endif CoUninitialize (); return 0; diff --git a/src/w32term.h b/src/w32term.h index 29ace0b2797..374c8055ed8 100644 --- a/src/w32term.h +++ b/src/w32term.h @@ -779,8 +779,8 @@ #define FILE_NOTIFICATIONS_SIZE 16384 #ifdef WINDOWSNT /* Keyboard hooks. */ -extern void setup_w32_kbdhook (void); -extern void remove_w32_kbdhook (void); +extern void setup_w32_kbdhook (HWND); +extern void remove_w32_kbdhook (HWND); extern int check_w32_winkey_state (int); #define w32_kbdhook_active (os_subtype != OS_SUBTYPE_9X) #else --=-=-=--