SDL icon indicating copy to clipboard operation
SDL copied to clipboard

testcolorspace keeps emitting SDL_EVENT_KEY_DOWN

Open madebr opened this issue 1 year ago • 5 comments

With testcolorspace of current master, when holding a keyboard button down for a second and then releasing, the SDL_EVENT_KEY_DOWN event keeps getting emitted. In the mean time, the window keeps getting destroyed and recreated. With this patch, logging the events,

--- a/test/testcolorspace.c
+++ b/test/testcolorspace.c
@@ -317,10 +317,14 @@ static void RenderBlendTexture(void)
 static void loop(void)
 {
     SDL_Event event;
+    static int counter;
 
     /* Check for events */
     while (SDL_PollEvent(&event)) {
         if (event.type == SDL_EVENT_KEY_DOWN) {
+
+            SDL_Log("[%" SDL_PRIu64 "] SDL_EVENT_KEY_DOWN event.keysym.scancode=%d event.repeat=%u event.windowIDid=%" SDL_PRIu32 " window.id=%" SDL_PRIu32 " [counter=%d]",
+                    event.key.timestamp, event.key.keysym.scancode, event.key.repeat,event.key.windowID, SDL_GetWindowID(window), counter++);
             switch (event.key.keysym.sym) {
             case SDLK_ESCAPE:
                 done = 1;

the output is:

INFO: Created renderer software
INFO: [7189332899] SDL_EVENT_KEY_DOWN event.keysym.scancode=81 event.repeat=1 event.windowIDid=3 window.id=3 [counter=182]
INFO: Created renderer opengl
INFO: [7219635929] SDL_EVENT_KEY_DOWN event.keysym.scancode=81 event.repeat=1 event.windowIDid=3 window.id=3 [counter=183]
INFO: Created renderer opengles2
INFO: [7249938959] SDL_EVENT_KEY_DOWN event.keysym.scancode=81 event.repeat=1 event.windowIDid=3 window.id=3 [counter=184]
INFO: Created renderer software
INFO: [7280241989] SDL_EVENT_KEY_DOWN event.keysym.scancode=81 event.repeat=1 event.windowIDid=3 window.id=3 [counter=185]
INFO: Created renderer opengl
INFO: [7310545019] SDL_EVENT_KEY_DOWN event.keysym.scancode=81 event.repeat=1 event.windowIDid=3 window.id=3 [counter=186]
INFO: Created renderer opengles2
INFO: [7340848049] SDL_EVENT_KEY_DOWN event.keysym.scancode=81 event.repeat=1 event.windowIDid=3 window.id=3 [counter=187]
INFO: Created renderer software
INFO: [7371151079] SDL_EVENT_KEY_DOWN event.keysym.scancode=81 event.repeat=1 event.windowIDid=3 window.id=3 [counter=188]
INFO: Created renderer opengl
INFO: [7401454109] SDL_EVENT_KEY_DOWN event.keysym.scancode=81 event.repeat=1 event.windowIDid=3 window.id=3 [counter=189]
(etc etc)

System: Fedora 38, default GNOME

/cc @Kontrabant since this might be something Wayland related

madebr avatar Feb 01 '24 19:02 madebr

We should be resetting the keyboard state when the window with the keyboard focus is destroyed. Is that not happening?

slouken avatar Feb 01 '24 19:02 slouken

The window is not destroyed in testcolorspace, only the renderer is.

madebr avatar Feb 01 '24 20:02 madebr

No, the renderer is recreating the window because it's not created with the OpenGL flag.

slouken avatar Feb 01 '24 21:02 slouken

In SDL_DestroyWindow, the only keyboard related thing I see is: https://github.com/libsdl-org/SDL/blob/dcfb069c756654551e0d48db608bdc03c10686a5/src/video/SDL_video.c#L3622-L3628 When I copy that into SDL_RecreateWIndow, the behavior remains the same.

--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -2241,6 +2241,16 @@ int SDL_RecreateWindow(SDL_Window *window, Uint32 flags)
         return SDL_ContextNotSupported("Metal");
     }
 
+    /* Make sure this window no longer has focus */
+    SDL_bool has_keyboard_focus = SDL_GetKeyboardFocus() == window;
+    SDL_bool has_mouse_focus = SDL_GetMouseFocus() == window;
+    if (has_keyboard_focus) {
+        SDL_SetKeyboardFocus(NULL);
+    }
+    if (has_mouse_focus) {
+        SDL_SetMouseFocus(NULL);
+    }
+
     if (window->flags & SDL_WINDOW_EXTERNAL) {
         /* Can't destroy and re-create external windows, hrm */
         flags |= SDL_WINDOW_EXTERNAL;
@@ -2361,6 +2371,15 @@ int SDL_RecreateWindow(SDL_Window *window, Uint32 flags)
         _this->SetWindowHitTest(window, SDL_TRUE);
     }
 
+#if 0
+    if (has_keyboard_focus) {
+        SDL_SetKeyboardFocus(window);
+    }
+    if (has_mouse_focus) {
+        SDL_SetMouseFocus(window);
+    }
+#endif
+
     SDL_FinishWindowCreation(window, flags);
 
     return 0;

madebr avatar Feb 01 '24 22:02 madebr

This is wayland specific. Using the x11 video driver works just fine.

madebr avatar Feb 01 '24 22:02 madebr

I cannot reproduce this anymore. Let's assume this is fixed.

madebr avatar Feb 18 '24 18:02 madebr