mlx repaired

This commit is contained in:
Angel Ortigosa Perez
2026-01-25 14:53:53 +01:00
parent edc4144d38
commit ccde6227cb
20 changed files with 1037 additions and 61 deletions

216
lib/mlx/ffi/python/.gitignore vendored Normal file
View File

@@ -0,0 +1,216 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[codz]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py.cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
# Pipfile.lock
# UV
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# uv.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
# poetry.lock
# poetry.toml
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
# pdm.lock
# pdm.toml
.pdm-python
.pdm-build/
# pixi
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
# pixi.lock
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
# in the .venv directory. It is recommended not to include this directory in version control.
.pixi
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# Redis
*.rdb
*.aof
*.pid
# RabbitMQ
mnesia/
rabbitmq/
rabbitmq-data/
# ActiveMQ
activemq-data/
# SageMath parsed files
*.sage.py
# Environments
.env
.envrc
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
# .idea/
# Abstra
# Abstra is an AI-powered process automation framework.
# Ignore directories containing user credentials, local state, and settings.
# Learn more at https://abstra.io/docs
.abstra/
# Visual Studio Code
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
# and can be added to the global gitignore or merged into this file. However, if you prefer,
# you could uncomment the following to ignore the entire vscode folder
# .vscode/
# Ruff stuff:
.ruff_cache/
# PyPI configuration file
.pypirc
# Marimo
marimo/_static/
marimo/_lsp/
__marimo__/
# Streamlit
.streamlit/secrets.toml

View File

@@ -0,0 +1,30 @@
# Python Bindings for MLX42
This directory contains Python bindings for the MLX42 graphics library, allowing you to use MLX42 functionalities in Python applications (using ^=3.13).
## Building
```bash
git clone https://github.com/codam-coding-college/MLX42.git
cd MLX42
cmake -B build -DBUILD_SHARED_LIBS=ON && cmake --build build --parallel
```
Within MLX42's root directory, there is an example script located at `ffi/python/example.py`.
You can run this script to see how to use the Python bindings:
In case you have a static library (`libmlx42.a`), you can create a shared library from it using `clang` or `gcc`:
```bash
clang -shared -o libmlx42.so libmlx42.a
```
Afterwards, place the shared library in the `ffi/python/` directory:
```bash
# You can also move the shared library onto a system path
mv build/libmlx42.so ffi/python/libmlx42.so
cd ffi/python
python3 example.py
```
# Example Output
![Example Output](../../docs/assets/python.png)

View File

@@ -0,0 +1,89 @@
# ============================================================================
# Codam Coding College, Amsterdam 2018-2025, All Rights Reserved.
# See README in the root project for more information.
# ============================================================================
import signal
import logging
import random
from libmlx import *
# ============================================================================
WIDTH = 512
HEIGHT = 512
# ============================================================================
def ft_pixel(r, g, b, a):
""" Packs RGBA values into a single 32-bit integer. """
return (r << 24) | (g << 16) | (b << 8) | a
@mlx_loop_hook_func
def ft_randomize(param):
for i in range(image.contents.width):
for y in range(image.contents.height):
color = ft_pixel(
random.randint(0, 255), # R
random.randint(0, 255), # G
random.randint(0, 255), # B
random.randint(0, 255) # A
)
mlx.mlx_put_pixel(image, i, y, color)
@mlx_loop_hook_func
def ft_hook(param):
mlx_ptr = ctypes.cast(param, ctypes.POINTER(mlx_t))
if mlx.mlx_is_key_down(mlx_ptr, MLX_KEY_ESCAPE):
mlx.mlx_close_window(mlx_ptr)
if mlx.mlx_is_key_down(mlx_ptr, MLX_KEY_UP):
image.contents.instances[0].y -= 5
if mlx.mlx_is_key_down(mlx_ptr, MLX_KEY_DOWN):
image.contents.instances[0].y += 5
if mlx.mlx_is_key_down(mlx_ptr, MLX_KEY_LEFT):
image.contents.instances[0].x -= 5
if mlx.mlx_is_key_down(mlx_ptr, MLX_KEY_RIGHT):
image.contents.instances[0].x += 5
def main():
# Better to encapsulate in a class, but keeping it simple for the example
global image
global mlx_ptr
# Initialize MLX
mlx_ptr = mlx.mlx_init(500, 800, b"MLX42 Python Example", True)
if not mlx_ptr:
logging.critical(f"Error: {mlx.mlx_strerror(mlx.mlx_get_errno()).decode()}")
exit(1)
# Create a new image
image = mlx.mlx_new_image(mlx_ptr, 128, 128)
if not image:
mlx.mlx_close_window(mlx_ptr)
logging.critical(f"Error: {mlx.mlx_strerror(mlx.mlx_get_errno()).decode()}")
exit(1)
# Display the image in the window
if mlx.mlx_image_to_window(mlx_ptr, image, 0, 0) == -1:
mlx.mlx_delete_image(mlx_ptr, image)
mlx.mlx_close_window(mlx_ptr)
logging.critical(f"Error: {mlx.mlx_strerror(mlx.mlx_get_errno()).decode()}")
exit(1)
mlx.mlx_loop_hook(mlx_ptr, ft_randomize, ctypes.cast(mlx_ptr, c_void_p))
mlx.mlx_loop_hook(mlx_ptr, ft_hook, ctypes.cast(mlx_ptr, c_void_p))
mlx.mlx_loop(mlx_ptr)
mlx.mlx_terminate(mlx_ptr)
# When receiving SIGINT (Ctrl+C), terminate MLX properly
# Otherwise we get issues related to callbacks as MLX is trying
# invoke it during termination.
def signal_handler(sig, frame):
mlx.mlx_terminate(mlx_ptr)
# ============================================================================
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal_handler)
main()

View File

@@ -0,0 +1,334 @@
# ============================================================================
# Codam Coding College, Amsterdam 2018-2025, All Rights Reserved.
# See README in the root project for more information.
# ============================================================================
# Python FFI bindings for MLX42
# ============================================================================
# This module provides Python bindings to the MLX42 graphics library using
# the ctypes library. It handles loading the appropriate shared library based
# on the operating system and defines necessary types, constants, and function
# prototypes for interacting with MLX42.
# ============================================================================
import ctypes
from os import path
from logging import critical
from platform import system
# ============================================================================
try: # Load FFI Library
lib = 'libmlx42'
system = system()
if system == "Linux":
lib_name = f"{lib}.so"
elif system == "Darwin":
lib_name = f"{lib}.dylib"
elif system == "Windows":
lib_name = f"{lib}.dll"
else:
raise RuntimeError("Unsupported operating system")
# Check if in the current working directory first
if path.exists(f"./{lib_name}"):
mlx = ctypes.CDLL(f"./{lib_name}")
else:
mlx = ctypes.CDLL(lib_name)
except OSError as e:
critical(f"Could not load the MLX42 library '{lib_name}'.")
critical(e)
exit(1)
# Type Definitions, Constants, and Enums
# ============================================================================
c_int32 = ctypes.c_int32
c_uint32 = ctypes.c_uint32
c_uint8 = ctypes.c_uint8
c_size_t = ctypes.c_size_t
c_bool = ctypes.c_bool
c_double = ctypes.c_double
c_char_p = ctypes.c_char_p
c_void_p = ctypes.c_void_p
# keys_t
MLX_KEY_SPACE = 32
MLX_KEY_APOSTROPHE = 39
MLX_KEY_COMMA = 44
MLX_KEY_MINUS = 45
MLX_KEY_PERIOD = 46
MLX_KEY_SLASH = 47
MLX_KEY_0 = 48
MLX_KEY_1 = 49
MLX_KEY_2 = 50
MLX_KEY_3 = 51
MLX_KEY_4 = 52
MLX_KEY_5 = 53
MLX_KEY_6 = 54
MLX_KEY_7 = 55
MLX_KEY_8 = 56
MLX_KEY_9 = 57
MLX_KEY_SEMICOLON = 59
MLX_KEY_EQUAL = 61
MLX_KEY_A = 65
MLX_KEY_B = 66
MLX_KEY_C = 67
MLX_KEY_D = 68
MLX_KEY_E = 69
MLX_KEY_F = 70
MLX_KEY_G = 71
MLX_KEY_H = 72
MLX_KEY_I = 73
MLX_KEY_J = 74
MLX_KEY_K = 75
MLX_KEY_L = 76
MLX_KEY_M = 77
MLX_KEY_N = 78
MLX_KEY_O = 79
MLX_KEY_P = 80
MLX_KEY_Q = 81
MLX_KEY_R = 82
MLX_KEY_S = 83
MLX_KEY_T = 84
MLX_KEY_U = 85
MLX_KEY_V = 86
MLX_KEY_W = 87
MLX_KEY_X = 88
MLX_KEY_Y = 89
MLX_KEY_Z = 90
MLX_KEY_LEFT_BRACKET = 91
MLX_KEY_BACKSLASH = 92
MLX_KEY_RIGHT_BRACKET = 93
MLX_KEY_GRAVE_ACCENT = 96
MLX_KEY_ESCAPE = 256
MLX_KEY_ENTER = 257
MLX_KEY_TAB = 258
MLX_KEY_BACKSPACE = 259
MLX_KEY_INSERT = 260
MLX_KEY_DELETE = 261
MLX_KEY_RIGHT = 262
MLX_KEY_LEFT = 263
MLX_KEY_DOWN = 264
MLX_KEY_UP = 265
MLX_KEY_PAGE_UP = 266
MLX_KEY_PAGE_DOWN = 267
MLX_KEY_HOME = 268
MLX_KEY_END = 269
MLX_KEY_CAPS_LOCK = 280
MLX_KEY_SCROLL_LOCK = 281
MLX_KEY_NUM_LOCK = 282
MLX_KEY_PRINT_SCREEN = 283
MLX_KEY_PAUSE = 284
MLX_KEY_F1 = 290
MLX_KEY_F2 = 291
MLX_KEY_F3 = 292
MLX_KEY_F4 = 293
MLX_KEY_F5 = 294
MLX_KEY_F6 = 295
MLX_KEY_F7 = 296
MLX_KEY_F8 = 297
MLX_KEY_F9 = 298
MLX_KEY_F10 = 299
MLX_KEY_F11 = 300
MLX_KEY_F12 = 301
MLX_KEY_F13 = 302
MLX_KEY_F14 = 303
MLX_KEY_F15 = 304
MLX_KEY_F16 = 305
MLX_KEY_F17 = 306
MLX_KEY_F18 = 307
MLX_KEY_F19 = 308
MLX_KEY_F20 = 309
MLX_KEY_F21 = 310
MLX_KEY_F22 = 311
MLX_KEY_F23 = 312
MLX_KEY_F24 = 313
MLX_KEY_F25 = 314
MLX_KEY_KP_0 = 320
MLX_KEY_KP_1 = 321
MLX_KEY_KP_2 = 322
MLX_KEY_KP_3 = 323
MLX_KEY_KP_4 = 324
MLX_KEY_KP_5 = 325
MLX_KEY_KP_6 = 326
MLX_KEY_KP_7 = 327
MLX_KEY_KP_8 = 328
MLX_KEY_KP_9 = 329
MLX_KEY_KP_DECIMAL = 330
MLX_KEY_KP_DIVIDE = 331
MLX_KEY_KP_MULTIPLY = 332
MLX_KEY_KP_SUBTRACT = 333
MLX_KEY_KP_ADD = 334
MLX_KEY_KP_ENTER = 335
MLX_KEY_KP_EQUAL = 336
MLX_KEY_LEFT_SHIFT = 340
MLX_KEY_LEFT_CONTROL = 341
MLX_KEY_LEFT_ALT = 342
MLX_KEY_LEFT_SUPER = 343
MLX_KEY_RIGHT_SHIFT = 344
MLX_KEY_RIGHT_CONTROL = 345
MLX_KEY_RIGHT_ALT = 346
MLX_KEY_RIGHT_SUPER = 347
MLX_KEY_MENU = 348
# cursor_t
MLX_CURSOR_ARROW = 0x00036001
MLX_CURSOR_IBEAM = 0x00036002
MLX_CURSOR_CROSSHAIR = 0x00036003
MLX_CURSOR_HAND = 0x00036004
MLX_CURSOR_HRESIZE = 0x00036005
MLX_CURSOR_VRESIZE = 0x00036006
# mouse_mode_t
MLX_MOUSE_NORMAL = 0x00034001
MLX_MOUSE_HIDDEN = 0x00034002
MLX_MOUSE_DISABLED = 0x00034003
# mouse_key_t
MLX_MOUSE_BUTTON_LEFT = 0
MLX_MOUSE_BUTTON_RIGHT = 1
MLX_MOUSE_BUTTON_MIDDLE = 2
# modifier_key_t
MLX_SHIFT = 0x0001
MLX_CONTROL = 0x0002
MLX_ALT = 0x0004
MLX_SUPERKEY = 0x0008
MLX_CAPSLOCK = 0x0010
MLX_NUMLOCK = 0x0020
MLX_RELEASE = 0
MLX_PRESS = 1
MLX_REPEAT = 2
MLX_STRETCH_IMAGE = 0, # Should images resize with the window as it's being resized or not. Default: false
MLX_FULLSCREEN = 1, # Should the window be in Fullscreen, note it will fullscreen at the given resolution. Default: false
MLX_MAXIMIZED = 2, # Start the window in a maximized state, overwrites the fullscreen state if this is true. Default: false
MLX_DECORATED = 3, # Have the window be decorated with a window bar. Default: true
MLX_HEADLESS = 4, # Run in headless mode, no window is created. (NOTE: Still requires some form of window manager such as xvfb)
MLX_SETTINGS_MAX = 5, # Setting count.
# Structures
# ============================================================================
class mlx_texture_t(ctypes.Structure):
_fields_ = [
("width", c_uint32),
("height", c_uint32),
("bytes_per_pixel", c_uint8),
("pixels", ctypes.POINTER(c_uint8))
]
class mlx_instance_t(ctypes.Structure):
_fields_ = [
("x", c_int32),
("y", c_int32),
("z", c_int32),
("enabled", c_bool)
]
class xpm_t(ctypes.Structure):
_fields_ = [
("texture", mlx_texture_t),
("color_count", c_int32),
("cpp", c_int32),
("mode", ctypes.c_char)
]
class mlx_key_data_t(ctypes.Structure):
_fields_ = [
("key", c_int32), # ENUM
("action", c_int32), # ENUM
("os_key", c_int32),
("modifier", c_int32) # ENUM
]
class mlx_image_t(ctypes.Structure):
_fields_ = [
("width", c_uint32),
("height", c_uint32),
("pixels", ctypes.POINTER(c_uint8)),
("instances", ctypes.POINTER(mlx_instance_t)),
("count", c_size_t),
("enabled", c_bool),
("context", c_void_p)
]
class mlx_instance_t(ctypes.Structure):
_fields_ = [
("x", c_int32),
("y", c_int32),
("z", c_int32),
("enabled", c_bool)
]
class mlx_t(ctypes.Structure):
_fields_ = [
("window", c_void_p),
("context", c_void_p),
("width", c_int32),
("height", c_int32),
("delta_time", c_double)
]
# Function Callbacks
# ============================================================================
mlx_scrollfunc = ctypes.CFUNCTYPE(None, c_double, c_double, c_void_p)
mlx_mousefunc = ctypes.CFUNCTYPE(None, c_int32, c_int32, c_int32, c_void_p)
mlx_cursorfunc = ctypes.CFUNCTYPE(None, c_double, c_double, c_void_p)
mlx_keyfunc = ctypes.CFUNCTYPE(None, mlx_key_data_t, c_void_p)
mlx_resizefunc = ctypes.CFUNCTYPE(None, c_int32, c_int32, c_void_p)
mlx_closefunc = ctypes.CFUNCTYPE(None, c_void_p)
mlx_loop_hook_func = ctypes.CFUNCTYPE(None, c_void_p)
# Function Signatures
# ============================================================================
# Error
mlx.mlx_strerror.argtypes = [c_int32] # errno_t enum
mlx.mlx_strerror.restype = c_char_p
mlx.mlx_get_errno.argtypes = []
mlx.mlx_get_errno.restype = c_int32 # errno_t enum
# Generic MLX Functions
mlx.mlx_init.argtypes = [c_int32, c_int32, c_char_p, c_bool]
mlx.mlx_init.restype = ctypes.POINTER(mlx_t)
mlx.mlx_set_setting.argtypes = [ctypes.c_int, c_int32] # mlx_settings_t
mlx.mlx_set_setting.restype = None
mlx.mlx_close_window.argtypes = [ctypes.POINTER(mlx_t)]
mlx.mlx_close_window.restype = None
mlx.mlx_loop.argtypes = [ctypes.POINTER(mlx_t)]
mlx.mlx_loop.restype = None
mlx.mlx_terminate.argtypes = [ctypes.POINTER(mlx_t)]
mlx.mlx_terminate.restype = None
mlx.mlx_get_time.argtypes = []
mlx.mlx_get_time.restype = c_double
# Image Functions
mlx.mlx_put_pixel.argtypes = [ctypes.POINTER(mlx_image_t), c_uint32, c_uint32, c_uint32]
mlx.mlx_put_pixel.restype = None
mlx.mlx_new_image.argtypes = [ctypes.POINTER(mlx_t), c_uint32, c_uint32]
mlx.mlx_new_image.restype = ctypes.POINTER(mlx_image_t)
mlx.mlx_image_to_window.argtypes = [ctypes.POINTER(mlx_t), ctypes.POINTER(mlx_image_t), c_int32, c_int32]
mlx.mlx_image_to_window.restype = c_int32
mlx.mlx_delete_image.argtypes = [ctypes.POINTER(mlx_t), ctypes.POINTER(mlx_image_t)]
mlx.mlx_delete_image.restype = None
# Input
mlx.mlx_is_key_down.argtypes = [ctypes.POINTER(mlx_t), ctypes.c_int] # keys_t
mlx.mlx_is_key_down.restype = c_bool
# Hooks
mlx.mlx_loop_hook.argtypes = [ctypes.POINTER(mlx_t), mlx_loop_hook_func, c_void_p]
mlx.mlx_loop_hook.restype = c_bool