glue: renamed output files to better represent their actual purpose
[staging/windowmanager.git] / generate-binding-glue.py
1 #!/usr/bin/python3
2
3 import sys
4
5 OUT = sys.stdout
6
7 def set_output(f):
8     global OUT
9     OUT = f
10
11 def p(*args):
12     OUT.write('\n'.join(args))
13     OUT.write('\n')
14
15 def emit_func_impl(api, f):
16     args = f.get('args', [])
17     if len(args) > 0:
18         p('   json_object *jreq = afb_req_json(req);', '')
19         for arg in args:
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,
24               '      return;',
25               '   }',
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());',
30       '      return;',
31       '   }', '')
32     p('   afb_req_success(req, ret.unwrap(), "success");')
33
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?");',
38       '      return;',
39       '   }', '',
40       '   try {', '   // BEGIN impl')
41     emit_func_impl(api, f)
42     p('   // END impl',
43       '   } catch (std::exception &e) {',
44       '      afb_req_fail_f(req, "failed", "Uncaught exception while calling %(name)s: %%s", e.what());' % f,
45       '      return;',
46       '   }', '')
47     p('}', '')
48
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)
53     p('   {}', '};')
54
55 def emit_binding(api):
56     p('namespace {', '')
57     for func in api['functions']:
58         emit_func(api, func)
59     p('} // namespace', '')
60     emit_afb_verbs(api)
61
62 def generate_names(api):
63     for f in api['functions']:
64         f['impl_name'] = '%s_%s_thunk' % (api['name'], f['name'])
65
66 def emit_afb_api(api):
67     p('#include "result.hpp"', '')
68     p('#include <json-c/json.h>', '')
69     p('namespace wm {', '')
70     p('struct App;', '')
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', []))) + ');')
76     p('};', '')
77     p('} // namespace wm')
78
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
82 API = {
83         'name': 'winman',
84         'api': 'g_afb_instance->app.api.', # where are our API functions
85         'functions': [
86             {
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' },
92                 ],
93             },
94             { 'name': 'debug_status', },
95             { 'name': 'debug_layers', },
96             { 'name': 'debug_surfaces', },
97         ]
98 }
99
100 def main():
101     with open('afb_binding_glue.inl', 'w') as out:
102         set_output(out)
103         p('// This file was generated, do not edit', '')
104         generate_names(API)
105         emit_binding(API)
106     with open('afb_binding_api.hpp', 'w') as out:
107         set_output(out)
108         p('// This file was generated, do not edit', '')
109         emit_afb_api(API)
110
111 __name__ == '__main__' and main()