diff options
Diffstat (limited to 'generate-binding-glue.py')
-rw-r--r-- | generate-binding-glue.py | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/generate-binding-glue.py b/generate-binding-glue.py new file mode 100644 index 0000000..cec4fea --- /dev/null +++ b/generate-binding-glue.py @@ -0,0 +1,111 @@ +#!/usr/bin/python3 + +import sys + +OUT = sys.stdout + +def set_output(f): + global OUT + OUT = f + +def p(*args): + OUT.write('\n'.join(args)) + OUT.write('\n') + +def emit_func_impl(api, f): + args = f.get('args', []) + if len(args) > 0: + p(' json_object *jreq = afb_req_json(req);', '') + for arg in args: + arg['jtype'] = arg.get('jtype', arg['type']) # add jtype default + p(' json_object *j_%(name)s = nullptr;' % arg, + ' if (! json_object_object_get_ex(jreq, "%(name)s", &j_%(name)s)) {' % arg, + ' afb_req_fail(req, "failed", "Need %(type)s argument %(name)s");' % arg, + ' return;', + ' }', + ' %(type)s a_%(name)s = json_object_get_%(jtype)s(j_%(name)s);' % arg, '') + p(' auto ret = %(api)s' % api + '%(name)s(' % f + ', '.join(map(lambda x: 'a_' + x['name'], args)) + ');') + p(' if (ret.is_err()) {', + ' afb_req_fail(req, "failed", ret.unwrap_err());', + ' return;', + ' }', '') + p(' afb_req_success(req, ret.unwrap(), "success");') + +def emit_func(api, f): + p('void %(impl_name)s(afb_req req) noexcept {' % f) + p(' if (g_afb_instance == nullptr) {', + ' afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");', + ' return;', + ' }', '', + ' try {', ' // BEGIN impl') + emit_func_impl(api, f) + p(' // END impl', + ' } catch (std::exception &e) {', + ' afb_req_fail_f(req, "failed", "Uncaught exception while calling %(name)s: %%s", e.what());' % f, + ' return;', + ' }', '') + p('}', '') + +def emit_afb_verbs(api): + p('const struct afb_verb_v2 %(name)s_verbs[] = {' % api) + for f in api['functions']: + p(' { "%(name)s", %(impl_name)s, nullptr, nullptr, AFB_SESSION_NONE },' % f) + p(' {}', '};') + +def emit_binding(api): + p('namespace {', '') + for func in api['functions']: + emit_func(api, func) + p('} // namespace', '') + emit_afb_verbs(api) + +def generate_names(api): + for f in api['functions']: + f['impl_name'] = '%s_%s_thunk' % (api['name'], f['name']) + +def emit_afb_api(api): + p('#include "result.hpp"', '') + p('#include <json-c/json.h>', '') + p('namespace wm {', '') + p('struct App;', '') + p('struct binding_api {') + p(' typedef wm::result<json_object *> result_type;') + p(' struct wm::App *app;') + for f in api['functions']: + p(' result_type %(name)s(' % f + ', '.join(map(lambda x: '%(type)s %(name)s' % x, f.get('args', []))) + ');') + p('};', '') + p('} // namespace wm') + +# names must always be valid in c and unique for each function (that is its arguments) +# arguments will be looked up from json request, range checking needs to be implemented +# by the actual API call +API = { + 'name': 'winman', + 'api': 'g_afb_instance->app.api.', # where are our API functions + 'functions': [ + { + 'name': 'register_surface', + #'return_type': 'int', # Or do they return all just some json? + 'args': [ # describes the functions arguments, and their names as found in the json request + { 'name': 'appid', 'type': 'uint32_t', 'jtype': 'int' }, # XXX: lookup jtypes automatically? i.e. char*|const char* would be string? + { 'name': 'surfaceid', 'type': 'uint32_t', 'jtype': 'int' }, + ], + }, + { 'name': 'debug_status', }, + { 'name': 'debug_layers', }, + { 'name': 'debug_surfaces', }, + ] +} + +def main(): + with open('afb_binding_glue.inl', 'w') as out: + set_output(out) + p('// This file was generated, do not edit', '') + generate_names(API) + emit_binding(API) + with open('afb_binding_api.hpp', 'w') as out: + set_output(out) + p('// This file was generated, do not edit', '') + emit_afb_api(API) + +__name__ == '__main__' and main() |