Implementation:Google deepmind Dm control GLFW GUI
| Knowledge Sources | |
|---|---|
| Domains | Visualization, GUI, Input Handling |
| Last Updated | 2026-02-15 04:00 GMT |
Overview
This module implements the GLFW-based windowing system for the dm_control viewer, providing the main application window, keyboard input handling, and mouse input handling including movement, clicks, double-clicks, and scroll.
Description
The GLFW GUI module serves as the concrete windowing backend for the dm_control viewer. DoubleBufferedGlfwContext extends GLFWContext to create a visible, double-buffered, multisampled GLFW window with 4x MSAA. A decorator _check_valid_backend ensures that GLFW is the active rendering backend before initialization.
GlfwKeyboard and GlfwMouse extend InputEventsProcessor (from the base module) to register GLFW callbacks for key events, cursor movement, mouse buttons, and scroll wheel input. These classes forward events thread-safely to registered listeners via QuietSet observable subjects. GlfwMouse integrates a DoubleClickDetector for detecting double-click gestures and handles framebuffer-to-window coordinate scaling to account for high-DPI displays.
GlfwWindow ties everything together: it creates the rendering context, keyboard, and mouse handlers, sets up a FullscreenQuadRenderer for displaying rendered frames, and provides the main event_loop() that repeatedly calls a tick function, renders pixels to the fullscreen quad, swaps buffers, and polls GLFW events. The window supports fullscreen toggling, title updates, file drop callbacks, and clean resource shutdown.
Usage
Use this module as the windowing backend when launching the dm_control interactive viewer. It is instantiated by the viewer application infrastructure and provides the OS-level window management, input capture, and display output through the GLFW library.
Code Reference
Source Location
- Repository: Google_deepmind_Dm_control
- File: dm_control/viewer/gui/glfw_gui.py
- Lines: 1-325
Signature
class DoubleBufferedGlfwContext(glfw_renderer.GLFWContext):
def __init__(self, width, height, title): ...
def _platform_init(self, width, height): ...
@property
def window(self): ...
class GlfwKeyboard(base.InputEventsProcessor):
def __init__(self, context): ...
# on_key: util.QuietSet - observable for key events
class GlfwMouse(base.InputEventsProcessor):
def __init__(self, context): ...
# on_move: util.QuietSet - observable for mouse movement
# on_click: util.QuietSet - observable for mouse clicks
# on_double_click: util.QuietSet - observable for double clicks
# on_scroll: util.QuietSet - observable for scroll wheel
@property
def position(self): ...
class GlfwWindow:
def __init__(self, width, height, title, context=None): ...
@property
def shape(self): ...
@property
def position(self): ...
@property
def keyboard(self): ...
@property
def mouse(self): ...
def set_title(self, title): ...
def set_full_screen(self, enable): ...
def toggle_full_screen(self): ...
@property
def is_full_screen(self): ...
def event_loop(self, tick_func): ...
def update(self, render_func): ...
def close(self): ...
Import
from dm_control.viewer.gui import glfw_gui
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| width | int | Yes | Initial window width in pixels |
| height | int | Yes | Initial window height in pixels |
| title | str | Yes | Window title string |
| context | GLFWContext | No | Optional pre-existing GLFW rendering context |
| tick_func | callable | Yes (event_loop) | Function called every frame during the event loop |
| render_func | callable | Yes (update) | Function returning a 3D numpy array of pixels (height, width, 3) |
Outputs
| Name | Type | Description |
|---|---|---|
| shape | tuple | Window framebuffer dimensions (width, height) in pixels |
| position | tuple | Top-left corner coordinates (x, y) |
| keyboard | GlfwKeyboard | Keyboard input handler with on_key observable |
| mouse | GlfwMouse | Mouse input handler with on_move, on_click, on_double_click, on_scroll observables |
Usage Examples
Basic Usage
from dm_control.viewer.gui import glfw_gui
import numpy as np
# Create a GLFW window
window = glfw_gui.GlfwWindow(width=640, height=480, title='MuJoCo Viewer')
# Register keyboard listener
def on_key(key, action, mods):
print(f'Key: {key}, Action: {action}')
window.keyboard.on_key += on_key
# Run the event loop with a render function
def render():
return np.zeros((480, 640, 3), dtype=np.uint8)
window.event_loop(render)