Commit a1d288bea1c108b0eb714d5ef995021ade85ed74

Cameron Gutman 2021-01-26T00:05:01

DirectFB: Split input grab handling into keyboard and mouse parts The grabbed_window field is superfluous now since SDL added the SDL_GetGrabbedWindow() function, so it can be removed. DirectFB_SetWindowMouseGrab() is also simplified because SDL handles ungrabbing any previously grabbed window prior to calling SetWindowMouseGrab() now. Compile-tested only.

diff --git a/src/video/directfb/SDL_DirectFB_WM.c b/src/video/directfb/SDL_DirectFB_WM.c
index 46ddffd..8dd48a3 100644
--- a/src/video/directfb/SDL_DirectFB_WM.c
+++ b/src/video/directfb/SDL_DirectFB_WM.c
@@ -287,9 +287,8 @@ WMPos(DFB_WindowData * p, int x, int y)
 int
 DirectFB_WM_ProcessEvent(_THIS, SDL_Window * window, DFBWindowEvent * evt)
 {
-    SDL_DFB_DEVICEDATA(_this);
     SDL_DFB_WINDOWDATA(window);
-    DFB_WindowData *gwindata = ((devdata->grabbed_window) ? (DFB_WindowData *) ((devdata->grabbed_window)->driverdata) : NULL);
+    SDL_Window *grabbed_window = SDL_GetGrabbedWindow();
     IDirectFBWindow *dfbwin = windata->dfbwin;
     DFBWindowOptions wopts;
 
@@ -327,9 +326,9 @@ DirectFB_WM_ProcessEvent(_THIS, SDL_Window * window, DFBWindowEvent * evt)
                 /* fall through */
             default:
                 windata->wm_grab = pos;
-                if (gwindata != NULL)
-                    SDL_DFB_CHECK(gwindata->dfbwin->UngrabPointer(gwindata->dfbwin));
-                SDL_DFB_CHECK(dfbwin->GrabPointer(dfbwin));
+                if (grabbed_window != NULL)
+                    DirectFB_SetWindowMouseGrab(_this, grabbed_window, SDL_FALSE);
+                DirectFB_SetWindowMouseGrab(_this, window, SDL_TRUE);
                 windata->wm_lastx = evt->cx;
                 windata->wm_lasty = evt->cy;
             }
@@ -359,9 +358,9 @@ DirectFB_WM_ProcessEvent(_THIS, SDL_Window * window, DFBWindowEvent * evt)
                     SDL_DFB_CHECK(dfbwin->Resize(dfbwin, cw + dx, ch + dy));
                 }
             }
-            SDL_DFB_CHECK(dfbwin->UngrabPointer(dfbwin));
-            if (gwindata != NULL)
-                SDL_DFB_CHECK(gwindata->dfbwin->GrabPointer(gwindata->dfbwin));
+            DirectFB_SetWindowMouseGrab(_this, window, SDL_FALSE);
+            if (grabbed_window != NULL)
+                DirectFB_SetWindowMouseGrab(_this, grabbed_window, SDL_TRUE);
             windata->wm_grab = WM_POS_NONE;
             return 1;
         }
diff --git a/src/video/directfb/SDL_DirectFB_events.c b/src/video/directfb/SDL_DirectFB_events.c
index b65ce56..1d4c199 100644
--- a/src/video/directfb/SDL_DirectFB_events.c
+++ b/src/video/directfb/SDL_DirectFB_events.c
@@ -312,15 +312,16 @@ ProcessInputEvent(_THIS, DFBInputEvent * ievt)
     int kbd_idx;
     Uint32 unicode;
     char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
+    SDL_Window* grabbed_window = SDL_GetGrabbedWindow();
 
     if (!devdata->use_linux_input) {
         if (ievt->type == DIET_AXISMOTION) {
-            if ((devdata->grabbed_window != NULL) && (ievt->flags & DIEF_AXISREL)) {
+            if ((grabbed_window != NULL) && (ievt->flags & DIEF_AXISREL)) {
                 if (ievt->axis == DIAI_X)
-                    SDL_SendMouseMotion_ex(devdata->grabbed_window, ievt->device_id, 1,
+                    SDL_SendMouseMotion_ex(grabbed_window, ievt->device_id, 1,
                                         ievt->axisrel, 0, 0);
                 else if (ievt->axis == DIAI_Y)
-                    SDL_SendMouseMotion_ex(devdata->grabbed_window, ievt->device_id, 1, 0,
+                    SDL_SendMouseMotion_ex(grabbed_window, ievt->device_id, 1, 0,
                                         ievt->axisrel, 0);
             }
         }
@@ -339,7 +340,7 @@ ProcessInputEvent(_THIS, DFBInputEvent * ievt)
                     SDL_Mouse *mouse = SDL_GetMouse(ievt->device_id);
                     SDL_Window *window = SDL_GetWindowFromID(mouse->focus);
 #else
-                    SDL_Window *window = devdata->grabbed_window;
+                    SDL_Window *window = grabbed_window;
 #endif
                     if (window) {
                         DFB_WindowData *windata =
@@ -359,10 +360,10 @@ ProcessInputEvent(_THIS, DFBInputEvent * ievt)
                 }
             } else if (ievt->flags & DIEF_AXISREL) {
                 if (ievt->axis == DIAI_X)
-                    SDL_SendMouseMotion_ex(devdata->grabbed_window, ievt->device_id, 1,
+                    SDL_SendMouseMotion_ex(grabbed_window, ievt->device_id, 1,
                                         ievt->axisrel, 0, 0);
                 else if (ievt->axis == DIAI_Y)
-                    SDL_SendMouseMotion_ex(devdata->grabbed_window, ievt->device_id, 1, 0,
+                    SDL_SendMouseMotion_ex(grabbed_window, ievt->device_id, 1, 0,
                                         ievt->axisrel, 0);
             }
             break;
@@ -386,19 +387,19 @@ ProcessInputEvent(_THIS, DFBInputEvent * ievt)
             break;
         case DIET_BUTTONPRESS:
             if (ievt->buttons & DIBM_LEFT)
-                SDL_SendMouseButton_ex(devdata->grabbed_window, ievt->device_id, SDL_PRESSED, 1);
+                SDL_SendMouseButton_ex(grabbed_window, ievt->device_id, SDL_PRESSED, 1);
             if (ievt->buttons & DIBM_MIDDLE)
-                SDL_SendMouseButton_ex(devdata->grabbed_window, ievt->device_id, SDL_PRESSED, 2);
+                SDL_SendMouseButton_ex(grabbed_window, ievt->device_id, SDL_PRESSED, 2);
             if (ievt->buttons & DIBM_RIGHT)
-                SDL_SendMouseButton_ex(devdata->grabbed_window, ievt->device_id, SDL_PRESSED, 3);
+                SDL_SendMouseButton_ex(grabbed_window, ievt->device_id, SDL_PRESSED, 3);
             break;
         case DIET_BUTTONRELEASE:
             if (!(ievt->buttons & DIBM_LEFT))
-                SDL_SendMouseButton_ex(devdata->grabbed_window, ievt->device_id, SDL_RELEASED, 1);
+                SDL_SendMouseButton_ex(grabbed_window, ievt->device_id, SDL_RELEASED, 1);
             if (!(ievt->buttons & DIBM_MIDDLE))
-                SDL_SendMouseButton_ex(devdata->grabbed_window, ievt->device_id, SDL_RELEASED, 2);
+                SDL_SendMouseButton_ex(grabbed_window, ievt->device_id, SDL_RELEASED, 2);
             if (!(ievt->buttons & DIBM_RIGHT))
-                SDL_SendMouseButton_ex(devdata->grabbed_window, ievt->device_id, SDL_RELEASED, 3);
+                SDL_SendMouseButton_ex(grabbed_window, ievt->device_id, SDL_RELEASED, 3);
             break;
         default:
             break;              /* please gcc */
diff --git a/src/video/directfb/SDL_DirectFB_video.c b/src/video/directfb/SDL_DirectFB_video.c
index 452ea60..cca2fd7 100644
--- a/src/video/directfb/SDL_DirectFB_video.c
+++ b/src/video/directfb/SDL_DirectFB_video.c
@@ -114,6 +114,7 @@ DirectFB_CreateDevice(int devindex)
     device->MinimizeWindow = DirectFB_MinimizeWindow;
     device->RestoreWindow = DirectFB_RestoreWindow;
     device->SetWindowMouseGrab = DirectFB_SetWindowMouseGrab;
+    device->SetWindowKeyboardGrab = DirectFB_SetWindowKeyboardGrab;
     device->DestroyWindow = DirectFB_DestroyWindow;
     device->GetWindowWMInfo = DirectFB_GetWindowWMInfo;
 
@@ -260,7 +261,6 @@ DirectFB_VideoInit(_THIS)
 
     devdata->dfb = dfb;
     devdata->firstwin = NULL;
-    devdata->grabbed_window = NULL;
 
     _this->driverdata = devdata;
 
diff --git a/src/video/directfb/SDL_DirectFB_video.h b/src/video/directfb/SDL_DirectFB_video.h
index eff1c86..e6a840e 100644
--- a/src/video/directfb/SDL_DirectFB_video.h
+++ b/src/video/directfb/SDL_DirectFB_video.h
@@ -153,10 +153,6 @@ struct _DFB_DeviceData
     int                 use_linux_input;
     int                 has_own_wm;
 
-
-    /* window grab */
-    SDL_Window          *grabbed_window;
-
     /* global events */
     IDirectFBEventBuffer *events;
 };
diff --git a/src/video/directfb/SDL_DirectFB_window.c b/src/video/directfb/SDL_DirectFB_window.c
index 5cebac2..b8c78fe 100644
--- a/src/video/directfb/SDL_DirectFB_window.c
+++ b/src/video/directfb/SDL_DirectFB_window.c
@@ -385,23 +385,24 @@ DirectFB_RestoreWindow(_THIS, SDL_Window * window)
 void
 DirectFB_SetWindowMouseGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
 {
-    SDL_DFB_DEVICEDATA(_this);
     SDL_DFB_WINDOWDATA(window);
-    DFB_WindowData *gwindata = ((devdata->grabbed_window) ? (DFB_WindowData *) ((devdata->grabbed_window)->driverdata) : NULL);
 
-    if ((window->flags & SDL_WINDOW_INPUT_GRABBED)) {
-        if (gwindata != NULL)
-        {
-            SDL_DFB_CHECK(gwindata->dfbwin->UngrabPointer(gwindata->dfbwin));
-            SDL_DFB_CHECK(gwindata->dfbwin->UngrabKeyboard(gwindata->dfbwin));
-        }
+    if (grabbed) {
         SDL_DFB_CHECK(windata->dfbwin->GrabPointer(windata->dfbwin));
-        SDL_DFB_CHECK(windata->dfbwin->GrabKeyboard(windata->dfbwin));
-        devdata->grabbed_window = window;
     } else {
         SDL_DFB_CHECK(windata->dfbwin->UngrabPointer(windata->dfbwin));
+    }
+}
+
+void
+DirectFB_SetWindowKeyboardGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
+{
+    SDL_DFB_WINDOWDATA(window);
+
+    if (grabbed) {
+        SDL_DFB_CHECK(windata->dfbwin->GrabKeyboard(windata->dfbwin));
+    } else {
         SDL_DFB_CHECK(windata->dfbwin->UngrabKeyboard(windata->dfbwin));
-        devdata->grabbed_window = NULL;
     }
 }
 
diff --git a/src/video/directfb/SDL_DirectFB_window.h b/src/video/directfb/SDL_DirectFB_window.h
index 6e0c09d..e3135c5 100644
--- a/src/video/directfb/SDL_DirectFB_window.h
+++ b/src/video/directfb/SDL_DirectFB_window.h
@@ -70,6 +70,7 @@ extern void DirectFB_MaximizeWindow(_THIS, SDL_Window * window);
 extern void DirectFB_MinimizeWindow(_THIS, SDL_Window * window);
 extern void DirectFB_RestoreWindow(_THIS, SDL_Window * window);
 extern void DirectFB_SetWindowMouseGrab(_THIS, SDL_Window * window, SDL_bool grabbed);
+extern void DirectFB_SetWindowKeyboardGrab(_THIS, SDL_Window * window, SDL_bool grabbed);
 extern void DirectFB_DestroyWindow(_THIS, SDL_Window * window);
 extern SDL_bool DirectFB_GetWindowWMInfo(_THIS, SDL_Window * window,
                                          struct SDL_SysWMinfo *info);