From 25f5be82ff6a2691a315d4de6fa43df699af73e1 Mon Sep 17 00:00:00 2001 From: Kyle Bayes Date: Tue, 21 Oct 2025 09:35:20 -0700 Subject: [PATCH] Provide option to disable keyboard events for SDL. PiperOrigin-RevId: 822154396 Change-Id: Ie4c8537de2c4b01437aae00ed6d6af1a8f419a73 --- src/experimental/studio/app.cc | 10 ++++++---- src/experimental/toolbox/window.cc | 20 ++++++++++++++------ src/experimental/toolbox/window.h | 7 ++++++- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/experimental/studio/app.cc b/src/experimental/studio/app.cc index e97d16b5..cd702338 100644 --- a/src/experimental/studio/app.cc +++ b/src/experimental/studio/app.cc @@ -56,14 +56,16 @@ namespace mujoco::studio { // - "passive" mode // - async physics -static constexpr toolbox::Window::Config kWindowConfig = +static constexpr toolbox::Window::Config kWindowConfig = { #if defined(USE_FILAMENT_VULKAN) - toolbox::Window::Config::kFilamentVulkan; + .render_config = toolbox::Window::RenderConfig::kFilamentVulkan, #elif defined(USE_FILAMENT_OPENGL) - toolbox::Window::Config::kFilamentOpenGL; + .render_config = toolbox::Window::RenderConfig::kFilamentOpenGL, #elif defined(USE_CLASSIC_OPENGL) - toolbox::Window::Config::kClassicOpenGL; + .render_config = toolbox::Window::RenderConfig::kClassicOpenGL, #endif + .enable_keyboard = true, +}; static void ToggleWindow(bool& window) { diff --git a/src/experimental/toolbox/window.cc b/src/experimental/toolbox/window.cc index bd6d2fcf..9c239b04 100644 --- a/src/experimental/toolbox/window.cc +++ b/src/experimental/toolbox/window.cc @@ -83,19 +83,26 @@ Window::Window(std::string_view title, int width, int height, Config config, int window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI; - if (config == kFilamentVulkan) { + if (!config_.enable_keyboard) { + SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE); + SDL_EventState(SDL_KEYDOWN, SDL_DISABLE); + SDL_EventState(SDL_KEYUP, SDL_DISABLE); + } + + RenderConfig render_config = config_.render_config; + if (render_config == kFilamentVulkan) { window_flags |= SDL_WINDOW_VULKAN; - } else if (config == kFilamentWebGL) { + } else if (render_config == kFilamentWebGL) { window_flags |= SDL_WINDOW_OPENGL; SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); - } else if (config == kClassicOpenGL || config == kFilamentOpenGL) { + } else if (render_config == kClassicOpenGL || render_config == kFilamentOpenGL) { window_flags |= SDL_WINDOW_OPENGL; SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); } else { - mju_error("Unsupported window config: %d", config); + mju_error("Unsupported window config: %d", render_config); } sdl_window_ = @@ -107,7 +114,7 @@ Window::Window(std::string_view title, int width, int height, Config config, InitImGui(sdl_window_, load_asset_fn); - if (config == kFilamentWebGL || config == kClassicOpenGL) { + if (render_config == kFilamentWebGL || render_config == kClassicOpenGL) { SDL_GLContext gl_context = SDL_GL_CreateContext(sdl_window_); SDL_GL_MakeCurrent(sdl_window_, gl_context); } @@ -174,7 +181,8 @@ void Window::EndFrame() { void Window::Present() { // Filament (with the exception of WebGL) handles the swapchain internally. - if (config_ != kFilamentVulkan && config_ != kFilamentOpenGL) { + if (config_.render_config != kFilamentVulkan + && config_.render_config != kFilamentOpenGL) { SDL_GL_SwapWindow(sdl_window_); } } diff --git a/src/experimental/toolbox/window.h b/src/experimental/toolbox/window.h index ec221209..adf6d2b3 100644 --- a/src/experimental/toolbox/window.h +++ b/src/experimental/toolbox/window.h @@ -30,13 +30,18 @@ namespace mujoco::toolbox { class Window { public: // Configures the window for the specified rendering backend. - enum Config { + enum RenderConfig { kClassicOpenGL, kFilamentVulkan, kFilamentOpenGL, kFilamentWebGL, }; + struct Config { + RenderConfig render_config = kClassicOpenGL; + bool enable_keyboard = true; + }; + Window(std::string_view title, int width, int height, Config config, const LoadAssetFn& load_asset_fn); ~Window();