diff --git a/CHANGELOG.md b/CHANGELOG.md index 57fce80..4423251 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,16 @@ # Change Log +## 2.0.1 + +* Support setting backend arguments using environment variables. + * `GLCONTEXT_GLVERSION` for setting opengl version + * `GLCONTEXT_LINUX_LIBGL` for specifying libgl name + * `GLCONTEXT_LINUX_LIBX11` for specifying libx11 name + * `GLCONTEXT_LINUX_LIBEGL` for specifying libegl name + * `GLCONTEXT_WIN_LIBGL` for specifying dll name +* x11: More details in error messages + ## 2.0.0 Support passing in values to backends for more detailed diff --git a/README.md b/README.md index 001bd43..82e97a2 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,24 @@ Parameters * `libgl` (`str`): Name of gl library to load (default: `libGL.so`) * `libegl` (`str`): Name of gl library to load (default: `libEGL.so`) +## Environment Variables + +Environment variables can be set to configure backends. +These will get first priority if defined. + +```bash +# Override OpenGL version code. For example: 410 (for opengl 4.1) +GLCONTEXT_GLVERSION +# Override libgl on linux. For example: libGL.1.so +GLCONTEXT_LINUX_LIBGL +# Override libx11 on linux. For exampleØ libX11.x.so +GLCONTEXT_LINUX_LIBX11 +# Override libegl on linux. For exampleØ libEGL.x.so +GLCONTEXT_LINUX_LIBEGL +# Override gl dll on windows. For example: opengl32_custom.dll +GLCONTEXT_WIN_LIBGL +``` + ## Running tests ``` diff --git a/glcontext/__init__.py b/glcontext/__init__.py index dd4a339..2ed0acb 100644 --- a/glcontext/__init__.py +++ b/glcontext/__init__.py @@ -1,4 +1,6 @@ -__version__ = '2.0.0' +import os + +__version__ = '2.1.0' def default_backend(): @@ -54,8 +56,10 @@ def _wgl(): from glcontext import wgl def create(*args, **kwargs): - supported_args = ['glversion', 'mode', 'libgl'] - return wgl.create_context(**_strip_kwargs(kwargs, supported_args)) + _apply_env_var(kwargs, 'glversion', 'GLCONTEXT_GLVERSION', arg_type=int) + _apply_env_var(kwargs, 'libgl', 'GLCONTEXT_WIN_LIBGL') + kwargs = _strip_kwargs(kwargs, ['glversion', 'mode', 'libgl']) + return wgl.create_context(**kwargs) return create @@ -63,24 +67,37 @@ def create(*args, **kwargs): def _x11(): """Create x11 backend""" from glcontext import x11 + def create(*args, **kwargs): - supported_args = ['glversion', 'mode', 'libgl', 'libx11'] - return x11.create_context(**_strip_kwargs(kwargs, supported_args)) + _apply_env_var(kwargs, 'glversion', 'GLCONTEXT_GLVERSION', arg_type=int) + _apply_env_var(kwargs, 'libgl', 'GLCONTEXT_LINUX_LIBGL') + _apply_env_var(kwargs, 'libx11', 'GLCONTEXT_LINUX_LIBX11') + kwargs = _strip_kwargs(kwargs, ['glversion', 'mode', 'libgl', 'libx11']) + return x11.create_context(**kwargs) + return create + def _darwin(): """Create darwin/cgl context""" from glcontext import darwin + def create(*args, **kwargs): - supported_args = ['mode'] - return darwin.create_context(**_strip_kwargs(kwargs, supported_args)) + return darwin.create_context(**_strip_kwargs(kwargs, ['mode'])) + return create + def _egl(): from glcontext import egl + def create(*args, **kwargs): - supported_args = ['glversion', 'mode', 'libgl', 'libegl'] - return egl.create_context(**_strip_kwargs(kwargs, supported_args)) + _apply_env_var(kwargs, 'glversion', 'GLCONTEXT_GLVERSION', arg_type=int) + _apply_env_var(kwargs, 'libgl', 'GLCONTEXT_LINUX_LIBGL') + _apply_env_var(kwargs, 'libegl', 'GLCONTEXT_LINUX_LIBEGL') + kwargs = _strip_kwargs(kwargs, ['glversion', 'mode', 'libgl', 'libegl']) + return egl.create_context(**kwargs) + return create @@ -95,3 +112,10 @@ def _strip_kwargs(kwargs: dict, supported_args: list): - Removes unsupported arguments """ return {k: v for k, v in kwargs.items() if v is not None and k in supported_args} + + +def _apply_env_var(kwargs, arg_name, env_name, arg_type=str): + """Injects an environment variable into the arg dict if present""" + value = os.environ.get(env_name, kwargs.get(arg_name)) + if value: + kwargs[arg_name] = arg_type(value) diff --git a/setup.py b/setup.py index 276f246..28eac84 100644 --- a/setup.py +++ b/setup.py @@ -58,7 +58,7 @@ setup( name='glcontext', - version='2.0.0', + version='2.1.0', description='Portable OpenGL Context', long_description=open('README.md').read(), long_description_content_type='text/markdown',