From 25a97b3169370abf6a8466639d169ea84fdea642 Mon Sep 17 00:00:00 2001 From: Matthias Blaicher Date: Wed, 1 Oct 2025 09:02:14 +0200 Subject: [PATCH] Allow full backend selection by name This MR enables manual selection of all backends, as well as querying the default backend name. Changes: - Extracted backend name detection into new get_default_backend_name() function that returns the backend name string instead of the backend object - Refactored default_backend() to delegate to get_default_backend_name() and get_backend_by_name() - Enhanced get_backend_by_name() to support all backends ('wgl', 'x11', 'darwin', 'egl') - Improved error message in get_backend_by_name() to list all supported backends --- glcontext/__init__.py | 59 ++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/glcontext/__init__.py b/glcontext/__init__.py index 8f7bd29..1b3e762 100644 --- a/glcontext/__init__.py +++ b/glcontext/__init__.py @@ -3,21 +3,11 @@ __version__ = '2.3.7' -def default_backend(): - """Get default backend based on the detected platform. - Supports detecting an existing context and standalone contexts. - If no context if found for the platform we return the linux backend. - - Example:: - - # Get the available backend - backend = get_default_backend(standalone=False/True) - # Create an opengl 3.3 context or detect the currently active - # context requiring at least opengl 3.3 support. - ctx = backend(330) +def get_default_backend_name(): + """Get the default backend name based on the detected platform. Returns: - A backend object for creating and/or detecting context + str: The name of the default backend ('wgl', 'x11', or 'darwin') """ PLATFORMS = {'windows', 'linux', 'darwin'} @@ -32,23 +22,52 @@ def default_backend(): target = 'linux' if target == 'windows': - return _wgl() + return 'wgl' if target == 'linux': - return _x11() + return 'x11' if target == 'darwin': - return _darwin() + return 'darwin' raise ValueError("Cannot find suitable default backend") +def default_backend(): + """Get default backend based on the detected platform. + Supports detecting an existing context and standalone contexts. + If no context if found for the platform we return the linux backend. + + Example:: + + # Get the available backend + backend = get_default_backend(standalone=False/True) + # Create an opengl 3.3 context or detect the currently active + # context requiring at least opengl 3.3 support. + ctx = backend(330) + + Returns: + A backend object for creating and/or detecting context + """ + return get_backend_by_name(get_default_backend_name()) + + def get_backend_by_name(name: str): """Request a specific backend by name""" - if name == 'egl': - return _egl() - - raise ValueError("Cannot find supported backend: '{}'".format(name)) + BACKENDS = { + 'wgl': _wgl, + 'x11': _x11, + 'darwin': _darwin, + 'egl': _egl, + } + + backend = BACKENDS.get(name) + if backend: + return backend() + + raise ValueError("Cannot find supported backend: '{}'. Supported backends: {}".format( + name, ', '.join(BACKENDS.keys()) + )) def _wgl():