aboutsummaryrefslogtreecommitdiff
path: root/bindgen
diff options
context:
space:
mode:
authorMatheus Catarino <matheus-catarino@hotmail.com>2025-06-25 14:30:44 -0300
committerMatheus Catarino <matheus-catarino@hotmail.com>2025-06-30 17:13:49 -0300
commita6cb14c1c804346287060a38d1f7c361095da1a8 (patch)
tree9e9456bcece7b8836cee2f67fbe08fb89d3e8332 /bindgen
parentda4efba3e39ea65e51954109faac3daa757f0839 (diff)
gen_d: sokol_nuklear and sokol_args support
Diffstat (limited to 'bindgen')
-rw-r--r--bindgen/gen_all.py2
-rw-r--r--bindgen/gen_d.py48
2 files changed, 49 insertions, 1 deletions
diff --git a/bindgen/gen_all.py b/bindgen/gen_all.py
index 7f34a89c..42ce9626 100644
--- a/bindgen/gen_all.py
+++ b/bindgen/gen_all.py
@@ -48,9 +48,11 @@ for task in zig_tasks:
# D
d_tasks = [
*tasks,
+ [ '../sokol_args.h', 'sargs_', [] ],
[ '../sokol_fetch.h', 'sfetch_', [] ],
[ '../util/sokol_memtrack.h', 'smemtrack_', [] ],
[ '../util/sokol_imgui.h', 'simgui_', ['sg_', 'sapp_'] ],
+ [ '../util/sokol_nuklear.h', 'snk_', ['sg_', 'sapp_'] ],
]
gen_d.prepare()
for task in d_tasks:
diff --git a/bindgen/gen_d.py b/bindgen/gen_d.py
index d98d0385..6bcedb73 100644
--- a/bindgen/gen_d.py
+++ b/bindgen/gen_d.py
@@ -24,6 +24,7 @@ module_names = {
'slog_': 'log',
'sg_': 'gfx',
'sapp_': 'app',
+ 'sargs_': 'args',
'stm_': 'time',
'saudio_': 'audio',
'sgl_': 'gl',
@@ -32,6 +33,7 @@ module_names = {
'sglue_': 'glue',
'sfetch_': 'fetch',
'simgui_': 'imgui',
+ 'snk_': 'nuklear',
'smemtrack_': 'memtrack',
}
@@ -39,6 +41,7 @@ c_source_paths = {
'slog_': 'sokol-d/src/sokol/c/sokol_log.c',
'sg_': 'sokol-d/src/sokol/c/sokol_gfx.c',
'sapp_': 'sokol-d/src/sokol/c/sokol_app.c',
+ 'sargs_': 'sokol-d/src/sokol/c/sokol_args.c',
'stm_': 'sokol-d/src/sokol/c/sokol_time.c',
'saudio_': 'sokol-d/src/sokol/c/sokol_audio.c',
'sgl_': 'sokol-d/src/sokol/c/sokol_gl.c',
@@ -47,6 +50,7 @@ c_source_paths = {
'sglue_': 'sokol-d/src/sokol/c/sokol_glue.c',
'sfetch_': 'sokol-d/src/sokol/c/sokol_fetch.c',
'simgui_': 'sokol-d/src/sokol/c/sokol_imgui.c',
+ 'snk_': 'sokol-d/src/sokol/c/sokol_nuklear.c',
'smemtrack_': 'sokol-d/src/sokol/c/sokol_memtrack.c',
}
@@ -56,7 +60,8 @@ ignores = [
]
c_callbacks = [
- 'slog_func'
+ 'slog_func',
+ 'nk_plugin_filter',
]
overrides = {
@@ -77,6 +82,9 @@ overrides = {
'sdtx_font.font_index': 'uint32_t',
'SGL_NO_ERROR': 'SGL_ERROR_NO_ERROR',
'sfetch_continue': 'continue_fetching',
+ 'struct nk_context': 'NkContext',
+ 'nk_handle': 'NkHandle',
+ 'nk_flags': 'NkFlags',
}
prim_types = {
@@ -123,10 +131,26 @@ class TypeConverter:
self.enum_types = enum_types
def as_d_type(self, c_type, is_param=False, decl_name=None):
+ c_type = c_type.strip()
+ # Check for override first
+ if c_type in overrides:
+ return overrides[c_type]
+
d_to_c_types = {v: k for k, v in prim_types.items()}
if c_type in d_to_c_types:
c_type = d_to_c_types[c_type]
+ # Handle struct keyword in type (e.g., "struct nk_context *")
+ if c_type.startswith('struct '):
+ c_type = c_type[7:].strip() # Remove 'struct' prefix
+ if c_type in overrides:
+ return overrides[c_type]
+ if c_type.endswith('*'):
+ # External struct pointer, treat as opaque
+ return 'void*' if is_param else 'void*'
+ # External struct by value, treat as named type if overridden
+ return overrides.get(c_type, c_type)
+
if util.is_func_ptr(c_type):
return self.as_func_ptr_type(c_type, decl_name)
@@ -167,6 +191,14 @@ class TypeConverter:
if util.is_array_type(c_type):
return self.as_array_type(c_type)
+ # Handle external types (e.g., nk_handle, nk_flags) not in struct_types or prim_types
+ if c_type not in self.struct_types and c_type not in prim_types:
+ if c_type.endswith('*'):
+ # Treat pointer to unknown type as void*
+ return 'void*' if is_param else 'void*'
+ # Treat unknown type by value as itself (assuming it's defined elsewhere)
+ return overrides.get(c_type, c_type)
+
raise ValueError(f"Unsupported C type: {c_type} in declaration: {decl_name or 'unknown'}")
def as_d_struct_type(self, s):
@@ -274,6 +306,17 @@ def pre_parse(inp):
enum_types.append(enum_name)
enum_items[enum_name] = [as_enum_item_name(item['name']) for item in decl['items']]
+def gen_nuklear_types():
+ """Generate type declarations for Nuklear external types."""
+ l("/++ Nuklear external type declarations +/")
+ l("extern(C) struct NkContext;")
+ l("extern(C) union NkHandle {")
+ l(" void* ptr;")
+ l(" int id;")
+ l("}")
+ l("alias NkFlags = uint;")
+ l("alias nk_plugin_filter = extern(C) int function(const(NkContext)*, NkHandle, int*, int) @system @nogc nothrow;")
+
def gen_struct(decl, type_converter):
struct_name = overrides.get(decl['name'], decl['name'])
d_type = type_converter.as_d_struct_type(struct_name)
@@ -417,6 +460,9 @@ def gen_module(inp, dep_prefixes, c_header_path):
l(f'module sokol.{inp["module"]};')
logging.info(f"Generating imports for module {inp['module']}")
gen_imports(inp, dep_prefixes)
+ # Add Nuklear types for the nuklear module
+ if inp['module'] == 'nuklear':
+ gen_nuklear_types()
pre_parse(inp)
type_converter = TypeConverter(inp['prefix'], struct_types, enum_types)
for decl in inp['decls']: