Refactor keyboard grab to be managed by the video core

This gives us flexibility to add others hints to control keyboard grab behavior
without having to touch all of the backends. It also allows us to possibly
expose keyboard grab separately from mouse grab for applications that want to
manage those independently.
main
Cameron Gutman 2021-01-23 16:22:44 -06:00
parent f40551c5f6
commit e1f73e642b
11 changed files with 55 additions and 24 deletions

View File

@ -233,6 +233,7 @@ struct SDL_VideoDevice
int (*SetWindowGammaRamp) (_THIS, SDL_Window * window, const Uint16 * ramp);
int (*GetWindowGammaRamp) (_THIS, SDL_Window * window, Uint16 * ramp);
void (*SetWindowGrab) (_THIS, SDL_Window * window, SDL_bool grabbed);
void (*SetWindowKeyboardGrab) (_THIS, SDL_Window * window, SDL_bool grabbed);
void (*DestroyWindow) (_THIS, SDL_Window * window);
int (*CreateWindowFramebuffer) (_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch);
int (*UpdateWindowFramebuffer) (_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects);

View File

@ -2655,6 +2655,9 @@ SDL_UpdateWindowGrab(SDL_Window * window)
if (_this->SetWindowGrab) {
_this->SetWindowGrab(_this, grabbed_window, SDL_FALSE);
}
if (_this->SetWindowKeyboardGrab) {
_this->SetWindowKeyboardGrab(_this, grabbed_window, SDL_FALSE);
}
}
_this->grabbed_window = window;
} else if (grabbed_window == window) {
@ -2664,6 +2667,13 @@ SDL_UpdateWindowGrab(SDL_Window * window)
if (_this->SetWindowGrab) {
_this->SetWindowGrab(_this, window, grabbed);
}
if (_this->SetWindowKeyboardGrab) {
if (grabbed && SDL_GetHintBoolean(SDL_HINT_GRAB_KEYBOARD, SDL_FALSE)) {
_this->SetWindowKeyboardGrab(_this, window, SDL_TRUE);
} else {
_this->SetWindowKeyboardGrab(_this, window, SDL_FALSE);
}
}
}
void

View File

@ -201,6 +201,7 @@ Wayland_CreateDevice(int devindex)
device->MaximizeWindow = Wayland_MaximizeWindow;
device->MinimizeWindow = Wayland_MinimizeWindow;
device->SetWindowGrab = Wayland_SetWindowGrab;
device->SetWindowKeyboardGrab = Wayland_SetWindowKeyboardGrab;
device->RestoreWindow = Wayland_RestoreWindow;
device->SetWindowBordered = Wayland_SetWindowBordered;
device->SetWindowResizable = Wayland_SetWindowResizable;

View File

@ -676,12 +676,20 @@ Wayland_SetWindowGrab(_THIS, SDL_Window *window, SDL_bool grabbed)
if (grabbed) {
Wayland_input_confine_pointer(window, data->input);
} else {
Wayland_input_unconfine_pointer(data->input);
}
}
if (SDL_GetHintBoolean(SDL_HINT_GRAB_KEYBOARD, SDL_FALSE))
void
Wayland_SetWindowKeyboardGrab(_THIS, SDL_Window *window, SDL_bool grabbed)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
if (grabbed) {
Wayland_input_grab_keyboard(window, data->input);
} else {
Wayland_input_ungrab_keyboard(window);
Wayland_input_unconfine_pointer(data->input);
}
}

View File

@ -92,6 +92,7 @@ extern void Wayland_SetWindowFullscreen(_THIS, SDL_Window * window,
extern void Wayland_MaximizeWindow(_THIS, SDL_Window * window);
extern void Wayland_MinimizeWindow(_THIS, SDL_Window * window);
extern void Wayland_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed);
extern void Wayland_SetWindowKeyboardGrab(_THIS, SDL_Window *window, SDL_bool grabbed);
extern void Wayland_RestoreWindow(_THIS, SDL_Window * window);
extern void Wayland_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered);
extern void Wayland_SetWindowResizable(_THIS, SDL_Window * window, SDL_bool resizable);

View File

@ -161,6 +161,7 @@ WIN_CreateDevice(int devindex)
device->SetWindowGammaRamp = WIN_SetWindowGammaRamp;
device->GetWindowGammaRamp = WIN_GetWindowGammaRamp;
device->SetWindowGrab = WIN_SetWindowGrab;
device->SetWindowKeyboardGrab = WIN_SetWindowKeyboardGrab;
device->DestroyWindow = WIN_DestroyWindow;
device->GetWindowWMInfo = WIN_GetWindowWMInfo;
device->CreateWindowFramebuffer = WIN_CreateWindowFramebuffer;

View File

@ -782,14 +782,6 @@ WIN_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
{
WIN_UpdateClipCursor(window);
if (grabbed) {
if (SDL_GetHintBoolean(SDL_HINT_GRAB_KEYBOARD, SDL_FALSE)) {
WIN_GrabKeyboard(window);
}
} else {
WIN_UngrabKeyboard(window);
}
if (window->flags & SDL_WINDOW_FULLSCREEN) {
UINT flags = SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE;
@ -800,6 +792,16 @@ WIN_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
}
}
void
WIN_SetWindowKeyboardGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
{
if (grabbed) {
WIN_GrabKeyboard(window);
} else {
WIN_UngrabKeyboard(window);
}
}
void
WIN_DestroyWindow(_THIS, SDL_Window * window)
{

View File

@ -77,6 +77,7 @@ extern void WIN_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay
extern int WIN_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp);
extern int WIN_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp);
extern void WIN_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed);
extern void WIN_SetWindowKeyboardGrab(_THIS, SDL_Window * window, SDL_bool grabbed);
extern void WIN_DestroyWindow(_THIS, SDL_Window * window);
extern SDL_bool WIN_GetWindowWMInfo(_THIS, SDL_Window * window,
struct SDL_SysWMinfo *info);

View File

@ -225,6 +225,7 @@ X11_CreateDevice(int devindex)
device->SetWindowFullscreen = X11_SetWindowFullscreen;
device->SetWindowGammaRamp = X11_SetWindowGammaRamp;
device->SetWindowGrab = X11_SetWindowGrab;
device->SetWindowKeyboardGrab = X11_SetWindowKeyboardGrab;
device->DestroyWindow = X11_DestroyWindow;
device->CreateWindowFramebuffer = X11_CreateWindowFramebuffer;
device->UpdateWindowFramebuffer = X11_UpdateWindowFramebuffer;

View File

@ -1566,7 +1566,6 @@ X11_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
Display *display = data->videodata->display;
SDL_bool oldstyle_fullscreen;
SDL_bool grab_keyboard;
/* ICCCM2.0-compliant window managers can handle fullscreen windows
If we're using XVidMode to change resolution we need to confine
@ -1607,21 +1606,26 @@ X11_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
/* Raise the window if we grab the mouse */
X11_XRaiseWindow(display, data->xwindow);
/* Now grab the keyboard */
if (SDL_GetHintBoolean(SDL_HINT_GRAB_KEYBOARD, SDL_FALSE)) {
grab_keyboard = SDL_TRUE;
} else {
/* We need to do this with the old style override_redirect
fullscreen window otherwise we won't get keyboard focus.
*/
grab_keyboard = oldstyle_fullscreen;
}
if (grab_keyboard) {
X11_XGrabKeyboard(display, data->xwindow, True, GrabModeAsync,
GrabModeAsync, CurrentTime);
/* Now grab the keyboard on old-style fullscreen */
if (oldstyle_fullscreen) {
X11_SetWindowKeyboardGrab(_this, window, SDL_TRUE);
}
} else {
X11_XUngrabPointer(display, CurrentTime);
}
X11_XSync(display, False);
}
void
X11_SetWindowKeyboardGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
Display *display = data->videodata->display;
if (grabbed) {
X11_XGrabKeyboard(display, data->xwindow, True, GrabModeAsync,
GrabModeAsync, CurrentTime);
} else {
X11_XUngrabKeyboard(display, CurrentTime);
}
X11_XSync(display, False);

View File

@ -100,6 +100,7 @@ extern void X11_SetWindowResizable(_THIS, SDL_Window * window, SDL_bool resizabl
extern void X11_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen);
extern int X11_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp);
extern void X11_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed);
extern void X11_SetWindowKeyboardGrab(_THIS, SDL_Window * window, SDL_bool grabbed);
extern void X11_DestroyWindow(_THIS, SDL_Window * window);
extern SDL_bool X11_GetWindowWMInfo(_THIS, SDL_Window * window,
struct SDL_SysWMinfo *info);