CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-glfw

A ctypes-based wrapper for GLFW3 that provides Python bindings for OpenGL, OpenGL ES, and Vulkan development on desktop platforms.

Pending
Overview
Eval results
Files

monitor-display.mddocs/

Monitor and Display

Monitor enumeration, video mode queries, display configuration, and gamma correction for multi-monitor setups and fullscreen applications.

Capabilities

Monitor Enumeration

Discover and access connected monitors.

def get_monitors() -> list:
    """
    Get a list of currently connected monitors.
    
    Returns:
        list: List of GLFWmonitor handles
    """

def get_primary_monitor():
    """
    Get the primary monitor.
    
    Returns:
        GLFWmonitor: Primary monitor handle
    """

Monitor Properties

Query monitor position, size, and other properties.

def get_monitor_pos(monitor) -> tuple[int, int]:
    """
    Get the position of the monitor's viewport on the virtual screen.
    
    Parameters:
        monitor: Monitor handle
        
    Returns:
        tuple: (x, y) position in screen coordinates
    """

def get_monitor_workarea(monitor) -> tuple[int, int, int, int]:
    """
    Get the work area of the monitor.
    
    The work area is the area of the monitor not occluded by 
    the operating system task bar or dock.
    
    Parameters:
        monitor: Monitor handle
        
    Returns:
        tuple: (x, y, width, height) of work area
    """

def get_monitor_physical_size(monitor) -> tuple[int, int]:
    """
    Get the physical size of the monitor in millimeters.
    
    Parameters:
        monitor: Monitor handle
        
    Returns:
        tuple: (width, height) in millimeters
    """

def get_monitor_content_scale(monitor) -> tuple[float, float]:
    """
    Get the content scale for the specified monitor.
    
    The content scale is the ratio between the current DPI 
    and the platform's default DPI.
    
    Parameters:
        monitor: Monitor handle
        
    Returns:
        tuple: (x_scale, y_scale) content scale factors
    """

def get_monitor_name(monitor) -> bytes:
    """
    Get the name of the specified monitor.
    
    Parameters:
        monitor: Monitor handle
        
    Returns:
        bytes: Human-readable monitor name
    """

Monitor User Data

Associate custom data with monitors.

def set_monitor_user_pointer(monitor, pointer) -> None:
    """
    Set the user pointer of the monitor.
    
    Parameters:
        monitor: Monitor handle
        pointer: User data (any Python object)
    """

def get_monitor_user_pointer(monitor):
    """
    Get the user pointer of the monitor.
    
    Parameters:
        monitor: Monitor handle
        
    Returns:
        User data object previously set
    """

Monitor Callbacks

Handle monitor configuration changes.

def set_monitor_callback(cbfun) -> callable:
    """
    Set the monitor configuration callback.
    
    Parameters:
        cbfun: Callback function or None
            Signature: callback(monitor, event: int)
            
    Returns:
        callable: Previously set callback function
    """

Monitor Event Constants

# Monitor events
CONNECTED: int = 0x00040001
DISCONNECTED: int = 0x00040002

Video Modes

Query and work with display video modes.

def get_video_modes(monitor) -> list:
    """
    Get the available video modes for the specified monitor.
    
    Parameters:
        monitor: Monitor handle
        
    Returns:
        list: List of GLFWvidmode objects
    """

def get_video_mode(monitor):
    """
    Get the current mode of the specified monitor.
    
    Parameters:
        monitor: Monitor handle
        
    Returns:
        GLFWvidmode: Current video mode
    """

Video Mode Type

# Video mode data structure
GLFWvidmode = namedtuple('GLFWvidmode', ['size', 'bits', 'refresh_rate'])
Size = namedtuple('Size', ['width', 'height'])
Bits = namedtuple('Bits', ['red', 'green', 'blue'])

# Access video mode properties:
# mode.size.width, mode.size.height
# mode.bits.red, mode.bits.green, mode.bits.blue  
# mode.refresh_rate

Gamma Correction

Control monitor gamma correction and color adjustment.

def set_gamma(monitor, gamma: float) -> None:
    """
    Generate a gamma ramp and set it for the specified monitor.
    
    This function generates a 256-element gamma ramp from the specified 
    exponent and then sets it as the current gamma ramp.
    
    Parameters:
        monitor: Monitor handle
        gamma: Desired exponent (typically 1.0-3.0)
    """

def get_gamma_ramp(monitor):
    """
    Get the current gamma ramp for the specified monitor.
    
    Parameters:
        monitor: Monitor handle
        
    Returns:
        GLFWgammaramp: Current gamma ramp
    """

def set_gamma_ramp(monitor, ramp) -> None:
    """
    Set the current gamma ramp for the specified monitor.
    
    Parameters:
        monitor: Monitor handle
        ramp: Gamma ramp data (3-tuple of RGB arrays)
    """

Gamma Ramp Type

# Gamma ramp data structure
GLFWgammaramp = namedtuple('GLFWgammaramp', ['red', 'green', 'blue'])

# Each channel (red, green, blue) is a list of values
# By default, values are normalized to 0.0-1.0 range
# Set glfw.NORMALIZE_GAMMA_RAMPS = False for 0-65535 integer values

Platform-Specific Functions

Platform-specific functions for accessing native monitor handles and properties.

Windows

def get_win32_adapter(monitor) -> str:
    """
    Get the adapter device name of the specified monitor.
    
    Parameters:
        monitor: Monitor handle
        
    Returns:
        str: Adapter device name, or None
    """

def get_win32_monitor(monitor) -> str:
    """
    Get the display device name of the specified monitor.
    
    Parameters:
        monitor: Monitor handle
        
    Returns:
        str: Display device name, or None
    """

macOS

def get_cocoa_monitor(monitor) -> int:
    """
    Get the CGDirectDisplayID of the specified monitor.
    
    Parameters:
        monitor: Monitor handle
        
    Returns:
        int: CGDirectDisplayID
    """

X11

def get_x11_adapter(monitor) -> int:
    """
    Get the RRCrtc of the specified monitor.
    
    Parameters:
        monitor: Monitor handle
        
    Returns:
        int: RRCrtc identifier
    """

def get_x11_monitor(monitor) -> int:
    """
    Get the RROutput of the specified monitor.
    
    Parameters:
        monitor: Monitor handle
        
    Returns:
        int: RROutput identifier
    """

Wayland

def get_wayland_monitor(monitor) -> ctypes.c_void_p:
    """
    Get the struct wl_output* of the specified monitor.
    
    Parameters:
        monitor: Monitor handle
        
    Returns:
        ctypes.c_void_p: Wayland output handle
    """

Usage Examples

Monitor Information

import glfw

glfw.init()

# Get all monitors
monitors = glfw.get_monitors()
primary = glfw.get_primary_monitor()

print(f"Found {len(monitors)} monitor(s)")
print(f"Primary monitor: {glfw.get_monitor_name(primary).decode('utf-8')}")

for i, monitor in enumerate(monitors):
    name = glfw.get_monitor_name(monitor).decode('utf-8')
    x, y = glfw.get_monitor_pos(monitor)
    width, height = glfw.get_monitor_physical_size(monitor)
    xscale, yscale = glfw.get_monitor_content_scale(monitor)
    
    print(f"Monitor {i}: {name}")
    print(f"  Position: {x}, {y}")
    print(f"  Physical size: {width}x{height}mm")
    print(f"  Content scale: {xscale}x{yscale}")
    
    # Get current video mode
    mode = glfw.get_video_mode(monitor)
    print(f"  Current mode: {mode.size.width}x{mode.size.height} @ {mode.refresh_rate}Hz")
    print(f"  Color bits: R{mode.bits.red} G{mode.bits.green} B{mode.bits.blue}")

glfw.terminate()

Video Mode Selection

import glfw

glfw.init()
monitor = glfw.get_primary_monitor()

# Get all available video modes
modes = glfw.get_video_modes(monitor)
print(f"Available video modes for {glfw.get_monitor_name(monitor).decode('utf-8')}:")

for mode in modes:
    print(f"  {mode.size.width}x{mode.size.height} @ {mode.refresh_rate}Hz "
          f"(R{mode.bits.red}G{mode.bits.green}B{mode.bits.blue})")

# Find highest resolution mode
highest_res = max(modes, key=lambda m: m.size.width * m.size.height)
print(f"Highest resolution: {highest_res.size.width}x{highest_res.size.height}")

# Create fullscreen window with specific mode
window = glfw.create_window(highest_res.size.width, highest_res.size.height, 
                           "Fullscreen", monitor, None)

glfw.terminate()

Monitor Events

import glfw

def monitor_callback(monitor, event):
    name = glfw.get_monitor_name(monitor).decode('utf-8') if monitor else "Unknown"
    if event == glfw.CONNECTED:
        print(f"Monitor connected: {name}")
    elif event == glfw.DISCONNECTED:
        print(f"Monitor disconnected: {name}")

glfw.init()

# Set monitor callback
glfw.set_monitor_callback(monitor_callback)

# Main loop to handle events (monitors can be hot-plugged)
# In a real application, this would be your main event loop
import time
for i in range(100):  # Run for a while to catch monitor events
    glfw.poll_events()
    time.sleep(0.1)

glfw.terminate()

Gamma Correction

import glfw

glfw.init()
monitor = glfw.get_primary_monitor()

# Save original gamma ramp
original_ramp = glfw.get_gamma_ramp(monitor)

try:
    # Set gamma correction
    glfw.set_gamma(monitor, 1.5)  # Brighten display
    
    # Or set custom gamma ramp
    size = len(original_ramp.red)
    red = [i / (size - 1) for i in range(size)]    # Linear ramp
    green = [i / (size - 1) for i in range(size)]  # Linear ramp  
    blue = [i / (size - 1) for i in range(size)]   # Linear ramp
    
    custom_ramp = (red, green, blue)
    glfw.set_gamma_ramp(monitor, custom_ramp)
    
    # Do work with modified gamma...
    
finally:
    # Restore original gamma ramp
    glfw.set_gamma_ramp(monitor, original_ramp)

glfw.terminate()

Multi-Monitor Setup

import glfw

glfw.init()

monitors = glfw.get_monitors()
if len(monitors) >= 2:
    # Create windows on different monitors
    for i, monitor in enumerate(monitors[:2]):
        mode = glfw.get_video_mode(monitor)
        x, y = glfw.get_monitor_pos(monitor)
        
        # Create window positioned on this monitor
        window = glfw.create_window(800, 600, f"Window on Monitor {i+1}", None, None)
        glfw.set_window_pos(window, x + 100, y + 100)
        
        print(f"Created window on {glfw.get_monitor_name(monitor).decode('utf-8')}")

glfw.terminate()

Install with Tessl CLI

npx tessl i tessl/pypi-glfw

docs

index.md

input-handling.md

library-management.md

monitor-display.md

opengl-context.md

vulkan-support.md

window-management.md

tile.json