12 OUT.write('\n'.join(args))
15 def emit_func_impl(api, f):
16 args = f.get('args', [])
18 p(' json_object *jreq = afb_req_json(req);', '')
20 arg['jtype'] = arg.get('jtype', arg['type']) # add jtype default
21 p(' json_object *j_%(name)s = nullptr;' % arg,
22 ' if (! json_object_object_get_ex(jreq, "%(name)s", &j_%(name)s)) {' % arg,
23 ' afb_req_fail(req, "failed", "Need %(type)s argument %(name)s");' % arg,
26 ' %(type)s a_%(name)s = json_object_get_%(jtype)s(j_%(name)s);' % arg, '')
27 p(' auto ret = %(api)s' % api + '%(name)s(' % f + ', '.join(map(lambda x: 'a_' + x['name'], args)) + ');')
28 p(' if (ret.is_err()) {',
29 ' afb_req_fail(req, "failed", ret.unwrap_err());',
32 p(' afb_req_success(req, ret.unwrap(), "success");')
34 def emit_func(api, f):
35 p('void %(impl_name)s(afb_req req) noexcept {' % f)
36 p(' if (g_afb_instance == nullptr) {',
37 ' afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");',
40 ' try {', ' // BEGIN impl')
41 emit_func_impl(api, f)
43 ' } catch (std::exception &e) {',
44 ' afb_req_fail_f(req, "failed", "Uncaught exception while calling %(name)s: %%s", e.what());' % f,
49 def emit_afb_verbs(api):
50 p('const struct afb_verb_v2 %(name)s_verbs[] = {' % api)
51 for f in api['functions']:
52 p(' { "%(name)s", %(impl_name)s, nullptr, nullptr, AFB_SESSION_NONE },' % f)
55 def emit_binding(api):
57 for func in api['functions']:
59 p('} // namespace', '')
62 def generate_names(api):
63 for f in api['functions']:
64 f['impl_name'] = '%s_%s_thunk' % (api['name'], f['name'])
66 def emit_afb_api(api):
67 p('#include "result.hpp"', '')
68 p('#include <json-c/json.h>', '')
69 p('namespace wm {', '')
71 p('struct binding_api {')
72 p(' typedef wm::result<json_object *> result_type;')
73 p(' struct wm::App *app;')
74 for f in api['functions']:
75 p(' result_type %(name)s(' % f + ', '.join(map(lambda x: '%(type)s %(name)s' % x, f.get('args', []))) + ');')
77 p('} // namespace wm')
79 # names must always be valid in c and unique for each function (that is its arguments)
80 # arguments will be looked up from json request, range checking needs to be implemented
81 # by the actual API call
84 'api': 'g_afb_instance->app.api.', # where are our API functions
87 'name': 'register_surface',
88 #'return_type': 'int', # Or do they return all just some json?
89 'args': [ # describes the functions arguments, and their names as found in the json request
90 { 'name': 'appid', 'type': 'uint32_t', 'jtype': 'int' }, # XXX: lookup jtypes automatically? i.e. char*|const char* would be string?
91 { 'name': 'surfaceid', 'type': 'uint32_t', 'jtype': 'int' },
94 { 'name': 'debug_status', },
95 { 'name': 'debug_layers', },
96 { 'name': 'debug_surfaces', },
97 { 'name': 'debug_terminate' },
102 with open('afb_binding_glue.inl', 'w') as out:
104 p('// This file was generated, do not edit', '')
107 with open('afb_binding_api.hpp', 'w') as out:
109 p('// This file was generated, do not edit', '')
112 __name__ == '__main__' and main()