11{% - macro render_method_call (method ) -%}
2- bindings.{{ method.call_symbol }}(
2+ {{ 'cnativeApiBindings' if method.static else ' bindings' }} .{{ method.call_symbol }}(
33{% - if method .needs_instance_handle %}
44 _nativeHandle{% if method .call_args |length > 0 %} , {% endif %}
55{% - endif %}
@@ -9,8 +9,86 @@ bindings.{{ method.call_symbol }}(
99)
1010{% - endmacro -%}
1111
12+ {% - macro render_method_return_type (method ) -%}
13+ {% - if method .call_symbol .endswith ('_get_arguments' ) and method .return_bridge == 'string' -%}
14+ List<String >
15+ {% - else -%}
16+ {{ method.api_return_type }}
17+ {% - endif -%}
18+ {% - endmacro -%}
19+
20+ {% - macro render_method_param (method , param ) -%}
21+ {% - if method .call_symbol .endswith ('_set_program' ) and param .name == 'arguments' and param .api_type == 'String' -%}
22+ List<String > {{ param.name }}
23+ {% - else -%}
24+ {{ param.api_type }} {{ param.name }}
25+ {% - endif -%}
26+ {% - endmacro -%}
27+
1228{% - macro render_method_body (method ) -%}
13- {% - if method .return_bridge == 'string' %}
29+ {% - if method .call_symbol .endswith ('_get_arguments' ) and method .return_bridge == 'string' %}
30+ final outArguments = pkgffi.malloc<ffi .Pointer <ffi.Pointer <ffi.Char >>>();
31+ final outCount = pkgffi.malloc<ffi .Size >();
32+ try {
33+ final success = bindings.{{ method.call_symbol }}(
34+ _nativeHandle,
35+ outArguments,
36+ outCount,
37+ );
38+ if (!success || outArguments.value == ffi.nullptr) {
39+ return <String >[];
40+ }
41+
42+ final count = outCount.value;
43+ final nativeArguments = outArguments.value;
44+ final result = <String >[];
45+ for (var i = 0; i < count; i++) {
46+ final nativeArgument = nativeArguments[i];
47+ result.add(
48+ nativeArgument == ffi.nullptr
49+ ? ''
50+ : nativeArgument.cast<pkgffi .Utf8 >().toDartString(),
51+ );
52+ }
53+
54+ for (var i = 0; i < count; i++) {
55+ final nativeArgument = nativeArguments[i];
56+ if (nativeArgument != ffi.nullptr) {
57+ bindings.free_c_str(nativeArgument);
58+ }
59+ }
60+ pkgffi.malloc.free(nativeArguments);
61+ return result;
62+ } finally {
63+ pkgffi.malloc.free(outCount);
64+ pkgffi.malloc.free(outArguments);
65+ }
66+ {% - elif method .call_symbol .endswith ('_set_program' ) and method .params |length == 2 %}
67+ final executablePathNative =
68+ executablePath.toNativeUtf8().cast<ffi .Char >();
69+ final argumentsNative = arguments.isEmpty
70+ ? ffi.nullptr.cast<ffi .Pointer <ffi.Char >>()
71+ : pkgffi.malloc<ffi .Pointer <ffi.Char >>(arguments.length);
72+ try {
73+ for (var i = 0; i < arguments.length; i++) {
74+ argumentsNative[i] = arguments[i].toNativeUtf8().cast<ffi .Char >();
75+ }
76+ return bindings.{{ method.call_symbol }}(
77+ _nativeHandle,
78+ executablePathNative,
79+ argumentsNative,
80+ arguments.length,
81+ );
82+ } finally {
83+ for (var i = 0; i < arguments.length; i++) {
84+ pkgffi.malloc.free(argumentsNative[i]);
85+ }
86+ if (arguments.isNotEmpty) {
87+ pkgffi.malloc.free(argumentsNative);
88+ }
89+ pkgffi.malloc.free(executablePathNative);
90+ }
91+ {% - elif method .return_bridge == 'string' %}
1492 final cString = {{ render_method_call(method) }};
1593 if (cString == ffi.nullptr) return '';
1694 final value = cString.cast<pkgffi .Utf8 >().toDartString();
@@ -50,6 +128,11 @@ bindings.{{ method.call_symbol }}(
50128{% - endmacro -%}
51129
52130{% - macro render_callable (method ) -%}
131+ {% - if method .call_symbol .endswith ('_get_arguments' ) and method .return_bridge == 'string' %}
132+ {{ render_method_body(method) }}
133+ {% - elif method .call_symbol .endswith ('_set_program' ) and method .params |length == 2 %}
134+ {{ render_method_body(method) }}
135+ {% - else %}
53136{% - for line in method .pre_call_lines %}
54137 {{ line }}
55138{% - endfor %}
@@ -64,6 +147,7 @@ bindings.{{ method.call_symbol }}(
64147{% - else %}
65148{{ render_method_body(method) }}
66149{% - endif %}
150+ {% - endif %}
67151{% - endmacro -%}
68152
69153{% - macro render_class (item , mapping ) -%}
@@ -76,13 +160,13 @@ class {{ item.name }} with CNativeApiBindingsMixin {
76160{% - for method in item .methods %}
77161{% - if not method .skip %}
78162 {% - if method .is_property %}
79- {{ 'static ' if method.static else '' }}{{ method.api_return_type }} get {{ method.property_name }} {
163+ {{ 'static ' if method.static else '' }}{{ render_method_return_type( method) }} get {{ method.property_name }} {
80164{{ render_callable(method) }}
81165 }
82166 {% - else %}
83- {{ 'static ' if method.static else '' }}{{ method.api_return_type }} {{ method.api_name }}(
167+ {{ 'static ' if method.static else '' }}{{ render_method_return_type( method) }} {{ method.api_name }}(
84168 {% - for param in method .params %}
85- {{ param.api_type }} {{ param.name }}{% if not loop .last %} , {% endif %}
169+ {{ render_method_param(method, param) }}{% if not loop .last %} , {% endif %}
86170 {% - endfor %}
87171 ) {
88172{{ render_callable(method) }}
@@ -106,25 +190,65 @@ class {{ item.name }} with CNativeApiBindingsMixin implements NativeHandleWrappe
106190 late final {{ item.handle_alias }} _nativeHandle;
107191 final bool _ownsHandle;
108192
193+ {% - if item .name == 'LaunchAtLogin' %}
194+ {{ item.name }}({
195+ String? id,
196+ String? displayName,
197+ {{ item.handle_alias }}? nativeHandle,
198+ }) : _ownsHandle = nativeHandle == null {
199+ if (nativeHandle != null) {
200+ _nativeHandle = nativeHandle;
201+ return;
202+ }
203+
204+ if (id != null && displayName != null) {
205+ final idNative = id.toNativeUtf8().cast<ffi .Char >();
206+ final displayNameNative = displayName.toNativeUtf8().cast<ffi .Char >();
207+ try {
208+ _nativeHandle = bindings.{{ item.create_symbol }}_with_id_and_name(
209+ idNative,
210+ displayNameNative,
211+ );
212+ } finally {
213+ pkgffi.malloc.free(displayNameNative);
214+ pkgffi.malloc.free(idNative);
215+ }
216+ return;
217+ }
218+
219+ if (id != null) {
220+ final idNative = id.toNativeUtf8().cast<ffi .Char >();
221+ try {
222+ _nativeHandle = bindings.{{ item.create_symbol }}_with_id(idNative);
223+ } finally {
224+ pkgffi.malloc.free(idNative);
225+ }
226+ return;
227+ }
228+
229+ _nativeHandle = bindings.{{ item.create_symbol }}();
230+ }
231+ {% - else %}
109232 {{ item.name }}([{{ item.handle_alias }}? nativeHandle])
110233 : _ownsHandle = nativeHandle == null {
111234 _nativeHandle = nativeHandle ?? bindings.{{ item.create_symbol }}();
112235 }
113236 {% - endif %}
237+ {% - endif %}
114238
115239 @override
116240 {{ item.handle_alias }} get nativeHandle => _nativeHandle;
117241
118242{% - for method in item .methods %}
119243{% - if not method .skip %}
120244 {% - if method .is_property %}
121- {{ 'static ' if method.static else '' }}{{ method.api_return_type }} get {{ method.property_name }} {
245+ {{ 'static ' if method.static else '' }}{{ render_method_return_type( method) }} get {{ method.property_name }} {
122246{{ render_callable(method) }}
123247 }
124248 {% - else %}
125- {{ 'static ' if method.static else '' }}{{ method.api_return_type }} {{ method.api_name }}(
249+ {{ 'static ' if method.static else '' }}{{ render_method_return_type( method) }} {{ method.api_name }}(
126250 {% - for param in method .params %}
127- {{ param.api_type }} {{ param.name }}{% if not loop .last %} , {% endif %}
251+ {{ render_method_param(method, param) }}{% if not loop .last %} , {% endif %}
128252 {% - endfor %}
129253 ) {
130254{{ render_callable(method) }}
0 commit comments