git: fc00c0377974 - stable/14 - vt(4): Always call vt_window_switch() in vtterm_cnungrab()

From: Vladimir Kondratyev <wulf_at_FreeBSD.org>
Date: Sat, 17 Feb 2024 21:32:33 UTC
The branch stable/14 has been updated by wulf:

URL: https://cgit.FreeBSD.org/src/commit/?id=fc00c037797457c4bf7e06c207297bf06cc92f33

commit fc00c037797457c4bf7e06c207297bf06cc92f33
Author:     Jean-Sébastien Pédron <dumbbell@FreeBSD.org>
AuthorDate: 2023-11-24 17:30:35 +0000
Commit:     Vladimir Kondratyev <wulf@FreeBSD.org>
CommitDate: 2024-02-17 20:58:34 +0000

    vt(4): Always call vt_window_switch() in vtterm_cnungrab()
    
    [Why]
    This ensures that vtterm_cnungrab() is the mirror of vtterm_cngrab().
    And the latter always call vt_window_switch() and thus the backend's
    vd_postswitch().
    
    This makes sure that whatever the backend did during vtterm_cngrab(), it
    can undo it during vtterm_cnungrab().
    
    Reviewed by:    manu
    Approved by:    manu
    Differential Revision:  https://reviews.freebsd.org/D42752
    
    (cherry picked from commit f18b3ce0b7be0b24a743ec59077ca8e7929a353c)
---
 sys/dev/vt/vt_core.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/sys/dev/vt/vt_core.c b/sys/dev/vt/vt_core.c
index bfd99665be4a..4a1031f1aa69 100644
--- a/sys/dev/vt/vt_core.c
+++ b/sys/dev/vt/vt_core.c
@@ -2046,7 +2046,7 @@ static void
 vtterm_cnungrab(struct terminal *tm)
 {
 	struct vt_device *vd;
-	struct vt_window *vw;
+	struct vt_window *vw, *grabwindow;
 
 	vw = tm->tm_softc;
 	vd = vw->vw_device;
@@ -2055,10 +2055,19 @@ vtterm_cnungrab(struct terminal *tm)
 	if (vtterm_cnungrab_noswitch(vd, vw) != 0)
 		return;
 
-	if (!cold && vd->vd_grabwindow != vw)
-		vt_window_switch(vd->vd_grabwindow);
-
+	/*
+	 * We set `vd_grabwindow` to NULL before calling vt_window_switch()
+	 * because it allows the underlying vt(4) backend to distinguish a
+	 * "grab" from an "ungrab" of the console.
+	 *
+	 * This is used by `vt_drmfb` in drm-kmod to call either
+	 * fb_debug_enter() or fb_debug_leave() appropriately.
+	 */
+	grabwindow = vd->vd_grabwindow;
 	vd->vd_grabwindow = NULL;
+
+	if (!cold)
+		vt_window_switch(grabwindow);
 }
 
 static void