| 1 |
""" |
| 2 |
Python bindings for GLFW. |
| 3 |
""" |
| 4 |
|
| 5 |
from __future__ import print_function |
| 6 |
from __future__ import division |
| 7 |
from __future__ import unicode_literals |
| 8 |
|
| 9 |
import ctypes |
| 10 |
import os |
| 11 |
import glob |
| 12 |
import sys |
| 13 |
import subprocess |
| 14 |
import textwrap |
| 15 |
|
| 16 |
|
| 17 |
def _find_library_candidates(library_names, |
| 18 |
library_file_extensions, |
| 19 |
library_search_paths): |
| 20 |
""" |
| 21 |
Finds and returns filenames which might be the library you are looking for. |
| 22 |
""" |
| 23 |
candidates = [] |
| 24 |
for search_path in library_search_paths: |
| 25 |
if not search_path: |
| 26 |
continue |
| 27 |
for library_name in library_names: |
| 28 |
glob_query = os.path.join(search_path, '*'+library_name+'.*') |
| 29 |
for filename in glob.iglob(glob_query): |
| 30 |
filename = os.path.realpath(filename) |
| 31 |
if filename in candidates: |
| 32 |
continue |
| 33 |
basename = os.path.basename(filename) |
| 34 |
if basename.startswith('lib'+library_name): |
| 35 |
basename_end = basename[len('lib'+library_name):] |
| 36 |
elif basename.startswith(library_name): |
| 37 |
basename_end = basename[len(library_name):] |
| 38 |
else: |
| 39 |
continue |
| 40 |
for file_extension in library_file_extensions: |
| 41 |
if basename_end.startswith(file_extension): |
| 42 |
if basename_end[len(file_extension):][:1] in ('', '.'): |
| 43 |
candidates.append(filename) |
| 44 |
elif basename_end.endswith(file_extension): |
| 45 |
basename_middle = basename_end[:-len(file_extension)] |
| 46 |
if all(c in '0123456789.' for c in basename_middle): |
| 47 |
candidates.append(filename) |
| 48 |
return candidates |
| 49 |
|
| 50 |
|
| 51 |
def _load_library(library_names, library_file_extensions, |
| 52 |
library_search_paths, version_check_callback): |
| 53 |
""" |
| 54 |
Finds, loads and returns the most recent version of the library. |
| 55 |
""" |
| 56 |
candidates = _find_library_candidates(library_names, |
| 57 |
library_file_extensions, |
| 58 |
library_search_paths) |
| 59 |
library_versions = [] |
| 60 |
for filename in set(candidates): |
| 61 |
version = version_check_callback(filename) |
| 62 |
if version is not None and version >= (3, 0, 0): |
| 63 |
library_versions.append((version, filename)) |
| 64 |
|
| 65 |
if not library_versions: |
| 66 |
return None |
| 67 |
library_versions.sort() |
| 68 |
return ctypes.CDLL(library_versions[-1][1]) |
| 69 |
|
| 70 |
|
| 71 |
def _load_first_library(library_names, library_file_extensions, |
| 72 |
library_search_paths): |
| 73 |
""" |
| 74 |
Finds, loads and returns the first found version of the library. |
| 75 |
""" |
| 76 |
candidates = _find_library_candidates( |
| 77 |
library_names, |
| 78 |
library_file_extensions, |
| 79 |
library_search_paths |
| 80 |
) |
| 81 |
library = None |
| 82 |
for filename in candidates: |
| 83 |
if os.path.isfile(filename): |
| 84 |
try: |
| 85 |
library = ctypes.CDLL(filename) |
| 86 |
break |
| 87 |
except OSError: |
| 88 |
pass |
| 89 |
if library is not None: |
| 90 |
major_value = ctypes.c_int(0) |
| 91 |
major = ctypes.pointer(major_value) |
| 92 |
minor_value = ctypes.c_int(0) |
| 93 |
minor = ctypes.pointer(minor_value) |
| 94 |
rev_value = ctypes.c_int(0) |
| 95 |
rev = ctypes.pointer(rev_value) |
| 96 |
if hasattr(library, 'glfwGetVersion'): |
| 97 |
library.glfwGetVersion(major, minor, rev) |
| 98 |
version = (major_value.value, minor_value.value, rev_value.value) |
| 99 |
if version >= (3, 0, 0): |
| 100 |
return library |
| 101 |
return None |
| 102 |
|
| 103 |
|
| 104 |
def _glfw_get_version(filename): |
| 105 |
""" |
| 106 |
Queries and returns the library version tuple or None by using a |
| 107 |
subprocess. |
| 108 |
""" |
| 109 |
version_checker_source = ''' |
| 110 |
import sys |
| 111 |
import ctypes |
| 112 |
|
| 113 |
def get_version(library_handle): |
| 114 |
""" |
| 115 |
Queries and returns the library version tuple or None. |
| 116 |
""" |
| 117 |
major_value = ctypes.c_int(0) |
| 118 |
major = ctypes.pointer(major_value) |
| 119 |
minor_value = ctypes.c_int(0) |
| 120 |
minor = ctypes.pointer(minor_value) |
| 121 |
rev_value = ctypes.c_int(0) |
| 122 |
rev = ctypes.pointer(rev_value) |
| 123 |
if hasattr(library_handle, 'glfwGetVersion'): |
| 124 |
library_handle.glfwGetVersion(major, minor, rev) |
| 125 |
version = (major_value.value, |
| 126 |
minor_value.value, |
| 127 |
rev_value.value) |
| 128 |
return version |
| 129 |
else: |
| 130 |
return None |
| 131 |
|
| 132 |
if sys.version_info[0] == 2: |
| 133 |
input_func = raw_input |
| 134 |
else: |
| 135 |
input_func = input |
| 136 |
filename = input_func().strip() |
| 137 |
|
| 138 |
try: |
| 139 |
library_handle = ctypes.CDLL(filename) |
| 140 |
except OSError: |
| 141 |
pass |
| 142 |
else: |
| 143 |
version = get_version(library_handle) |
| 144 |
print(version) |
| 145 |
''' |
| 146 |
|
| 147 |
args = [sys.executable, '-c', textwrap.dedent(version_checker_source)] |
| 148 |
process = subprocess.Popen(args, universal_newlines=True, |
| 149 |
stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 150 |
out = process.communicate(filename)[0] |
| 151 |
out = out.strip() |
| 152 |
if out: |
| 153 |
return eval(out) |
| 154 |
else: |
| 155 |
return None |
| 156 |
|
| 157 |
|
| 158 |
def _get_library_search_paths(): |
| 159 |
""" |
| 160 |
Returns a list of library search paths, considering of the current working |
| 161 |
directory, default paths and paths from environment variables. |
| 162 |
""" |
| 163 |
package_path = os.path.abspath(os.path.dirname(__file__)) |
| 164 |
search_paths = [ |
| 165 |
'', |
| 166 |
package_path, |
| 167 |
sys.prefix + '/lib', |
| 168 |
'/usr/lib64', |
| 169 |
'/usr/local/lib64', |
| 170 |
'/usr/lib', '/usr/local/lib', |
| 171 |
'/opt/homebrew/lib', |
| 172 |
'/run/current-system/sw/lib', |
| 173 |
'/usr/lib/x86_64-linux-gnu/', |
| 174 |
'/usr/lib/aarch64-linux-gnu/', |
| 175 |
'/usr/lib/arm-linux-gnueabihf', |
| 176 |
'/usr/lib/riscv64-linux-gnu', |
| 177 |
'/usr/lib/powerpc64le-linux-gnu', |
| 178 |
'/usr/lib/loongarch64-linux-gnu', |
| 179 |
'/usr/lib/s390x-linux-gnu', |
| 180 |
'/usr/lib/i386-linux-gnu', |
| 181 |
'/usr/lib/arm-linux-gnueabi', |
| 182 |
'/usr/lib/sparc64-linux-gnu', |
| 183 |
'/usr/lib/mips64el-linux-gnuabi64', |
| 184 |
] |
| 185 |
|
| 186 |
package_path_variant = _get_package_path_variant(package_path) |
| 187 |
if package_path_variant: |
| 188 |
search_paths.insert(1, package_path_variant) |
| 189 |
|
| 190 |
if sys.platform == 'darwin': |
| 191 |
path_environment_variable = 'DYLD_LIBRARY_PATH' |
| 192 |
else: |
| 193 |
path_environment_variable = 'LD_LIBRARY_PATH' |
| 194 |
if path_environment_variable in os.environ: |
| 195 |
search_paths.extend(os.environ[path_environment_variable].split(':')) |
| 196 |
return search_paths |
| 197 |
|
| 198 |
|
| 199 |
def _get_frozen_library_search_paths(): |
| 200 |
""" |
| 201 |
Returns a list of library search paths for frozen executables. |
| 202 |
""" |
| 203 |
current_path = os.path.abspath(os.getcwd()) |
| 204 |
executable_path = os.path.abspath(os.path.dirname(sys.executable)) |
| 205 |
package_path = os.path.abspath(os.path.dirname(__file__)) |
| 206 |
package_path_variant = _get_package_path_variant(package_path) |
| 207 |
return [ |
| 208 |
executable_path, |
| 209 |
package_path_variant, |
| 210 |
package_path, |
| 211 |
current_path |
| 212 |
] |
| 213 |
|
| 214 |
|
| 215 |
def _get_package_path_variant(package_path): |
| 216 |
if sys.platform in ('darwin', 'win32'): |
| 217 |
return None |
| 218 |
|
| 219 |
if os.environ.get('PYGLFW_LIBRARY_VARIANT', '').lower() in ['wayland', 'x11']: |
| 220 |
return os.path.join(package_path, os.environ['PYGLFW_LIBRARY_VARIANT'].lower()) |
| 221 |
elif os.environ.get('XDG_SESSION_TYPE') == 'wayland': |
| 222 |
return os.path.join(package_path, 'wayland') |
| 223 |
else: |
| 224 |
|
| 225 |
return os.path.join(package_path, 'x11') |
| 226 |
|
| 227 |
|
| 228 |
if os.environ.get('PYGLFW_LIBRARY', ''): |
| 229 |
try: |
| 230 |
glfw = ctypes.CDLL(os.environ['PYGLFW_LIBRARY']) |
| 231 |
except OSError: |
| 232 |
glfw = None |
| 233 |
elif sys.platform == 'win32': |
| 234 |
glfw = None |
| 235 |
|
| 236 |
|
| 237 |
try: |
| 238 |
glfw = ctypes.CDLL('glfw3.dll') |
| 239 |
except OSError: |
| 240 |
pass |
| 241 |
|
| 242 |
|
| 243 |
if glfw is None: |
| 244 |
try: |
| 245 |
|
| 246 |
msvcr = ctypes.CDLL(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'msvcr120.dll')) |
| 247 |
glfw = ctypes.CDLL(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'glfw3.dll')) |
| 248 |
except OSError: |
| 249 |
pass |
| 250 |
|
| 251 |
|
| 252 |
if glfw is None: |
| 253 |
try: |
| 254 |
glfw = ctypes.CDLL(os.path.join(sys.prefix, 'Library', 'bin', 'glfw3.dll')) |
| 255 |
except OSError: |
| 256 |
pass |
| 257 |
elif not getattr(sys, "frozen", False): |
| 258 |
glfw = _load_library(['glfw', 'glfw3'], ['.so', '.dylib'], |
| 259 |
_get_library_search_paths(), _glfw_get_version) |
| 260 |
else: |
| 261 |
|
| 262 |
glfw = _load_first_library(['glfw', 'glfw3'], ['.so', '.dylib'], |
| 263 |
_get_frozen_library_search_paths()) |