Bug 431133 - XKeyboardUngrab generated FocusOut event improperly handled
Summary: XKeyboardUngrab generated FocusOut event improperly handled
Status: CONFIRMED
Alias: None
Product: kwin
Classification: Plasma
Component: platform-x11-standalone (other bugs)
Version First Reported In: 5.20.4
Platform: Debian testing Linux
: NOR normal
Target Milestone: ---
Assignee: KWin default assignee
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-01-04 05:11 UTC by Geoffrey McRae
Modified: 2023-09-06 13:33 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed/Implemented In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Geoffrey McRae 2021-01-04 05:11:27 UTC
When an application calls XKeyboardUngrab as per the X11 specifications, a `FocusOut` event is generated with the mode set to `NotifyUngrab`. kwin incorrectly processes this event and unhighlights the application caption bar as well as the icon on the task bar.

STEPS TO REPRODUCE
1. Compile the following application using:

gcc reproduce.c -L/usr/X11R6/lib -lX11 -o reproduce


#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

int main(void) {
   Display *d;
   Window w;
   XEvent e;
   const char *msg = "Hello, World!";
   int s;
   bool toggle = false;

   d = XOpenDisplay(NULL);
   if (d == NULL) {
      fprintf(stderr, "Cannot open display\n");
      exit(1);
   }

   s = DefaultScreen(d);
   w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1,
                           BlackPixel(d, s), WhitePixel(d, s));
   XSelectInput(d, w, ExposureMask | KeyPressMask);
   XMapWindow(d, w);

   while (1) {
      XNextEvent(d, &e);
      if (e.type == Expose) {
         XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
         XDrawString(d, w, DefaultGC(d, s), 10, 50, msg, strlen(msg));
      }
      if (e.type == KeyPress)
      {
        toggle = !toggle;
        if (toggle)
          XGrabKeyboard(
            d,
            w,
            true,
            GrabModeAsync,
            GrabModeAsync,
            CurrentTime);
        else
          XUngrabKeyboard(d, CurrentTime);
      }
   }

   XCloseDisplay(d);
   return 0;
}

2. Run the resulting application
3. Press any key on the keyboard to toggle the keyboard grab/ungrab

OBSERVED RESULT

The application shows that it has lost focus and the taskbar panel, and in the case of a full-screen application the taskbar will overlap the application.

EXPECTED RESULT

The application to show it still has focus and the taskbar panel to remain behind the application.
Comment 1 Vlad Zahorodnii 2021-01-04 10:48:31 UTC
> `FocusOut` event is generated with the mode set to `NotifyUngrab`
Shouldn't it be NotifyGrab?
Comment 2 Geoffrey McRae 2021-01-04 11:01:34 UTC
As per: https://tronche.com/gui/x/xlib/input/XUngrabKeyboard.html

> The XUngrabKeyboard() function releases the keyboard and any queued events if this client has it actively grabbed from either XGrabKeyboard() or XGrabKey(). XUngrabKeyboard() does not release the keyboard and any queued events if the specified time is earlier than the last-keyboard-grab time or is later than the current X server time. It also generates FocusIn and FocusOut events. The X server automatically performs an UngrabKeyboard request if the event window for an active keyboard grab becomes not viewable.
Comment 3 Geoffrey McRae 2021-01-04 11:05:25 UTC
Sorry I misread what you stated.

The problem is when the application calls XKeyboardUngrab, which sends the FocusOut event with NotifyUngrab as far as I am aware. Either way, the sample application provided reliably replicates the problem.
Comment 4 David Edmundson 2023-09-06 10:39:02 UTC
This bug was reported against an outdated version of KWin. We have made many changes since the. 
If the issue persists in newer versions can you reopen the bug report updating the version number.
Comment 5 Geoffrey McRae 2023-09-06 10:42:42 UTC
Or, you could use the minimal code I produced and check if it's still an issue instead of just flat out closing an issue that is years old because of your inaction. When this was opened it was the most current version.
Comment 6 Geoffrey McRae 2023-09-06 13:32:13 UTC
Confirmed still bugged on 5.27.7. Minimum viable sample still reproduces the issue. Application incorrectly looses focus when `XKeyboardUngrab` is called.
Comment 7 David Edmundson 2023-09-06 13:33:45 UTC
Thanks