app/api: add demo_activate_all()
[staging/windowmanager.git] / generate-binding-glue.py
1 #!/usr/bin/python3
2
3 #
4 # Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 import sys
20
21 OUT = sys.stdout
22
23 def set_output(f):
24     global OUT
25     OUT = f
26
27 def p(*args):
28     OUT.write('\n'.join(args))
29     OUT.write('\n')
30
31 def emit_func_impl(api, f):
32     args = f.get('args', [])
33     if len(args) > 0:
34         p('   json_object *jreq = afb_req_json(req);', '')
35         for arg in args:
36             arg['jtype'] = arg.get('jtype', arg['type']) # add jtype default
37             p('   json_object *j_%(name)s = nullptr;' % arg,
38               '   if (! json_object_object_get_ex(jreq, "%(name)s", &j_%(name)s)) {' % arg,
39               '      afb_req_fail(req, "failed", "Need %(type)s argument %(name)s");' % arg,
40               '      return;',
41               '   }',
42               '   %(type)s a_%(name)s = json_object_get_%(jtype)s(j_%(name)s);' % arg, '')
43     p('   auto ret = %(api)s' % api + '%(name)s(' % f + ', '.join(map(lambda x: 'a_' + x['name'], args)) + ');')
44     p('   if (ret.is_err()) {',
45       '      afb_req_fail(req, "failed", ret.unwrap_err());',
46       '      return;',
47       '   }', '')
48     p('   afb_req_success(req, ret.unwrap(), "success");')
49
50 def emit_func(api, f):
51     p('void %(impl_name)s(afb_req req) noexcept {' % f)
52     p('   if (g_afb_instance == nullptr) {',
53       '      afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");',
54       '      return;',
55       '   }', '',
56       '   try {', '   // BEGIN impl')
57     emit_func_impl(api, f)
58     p('   // END impl',
59       '   } catch (std::exception &e) {',
60       '      afb_req_fail_f(req, "failed", "Uncaught exception while calling %(name)s: %%s", e.what());' % f,
61       '      return;',
62       '   }', '')
63     p('}', '')
64
65 def emit_afb_verbs(api):
66     p('const struct afb_verb_v2 %(name)s_verbs[] = {' % api)
67     for f in api['functions']:
68         p('   { "%(name)s", %(impl_name)s, nullptr, nullptr, AFB_SESSION_NONE },' % f)
69     p('   {}', '};')
70
71 def emit_binding(api):
72     p('namespace {', '')
73     for func in api['functions']:
74         emit_func(api, func)
75     p('} // namespace', '')
76     emit_afb_verbs(api)
77
78 def generate_names(api):
79     for f in api['functions']:
80         f['impl_name'] = '%s_%s_thunk' % (api['name'], f['name'])
81
82 def emit_afb_api(api):
83     p('#include "result.hpp"', '')
84     p('#include <json-c/json.h>', '')
85     p('namespace wm {', '')
86     p('struct App;', '')
87     p('struct binding_api {')
88     p('   typedef wm::result<json_object *> result_type;')
89     p('   struct wm::App *app;')
90     for f in api['functions']:
91         p('   result_type %(name)s(' % f + ', '.join(map(lambda x: '%(type)s %(name)s' % x, f.get('args', []))) + ');')
92     p('};', '')
93     p('} // namespace wm')
94
95 # names must always be valid in c and unique for each function (that is its arguments)
96 # arguments will be looked up from json request, range checking needs to be implemented
97 # by the actual API call
98 API = {
99         'name': 'winman',
100         'api': 'g_afb_instance->app.api.', # where are our API functions
101         'functions': [
102             {
103                 'name': 'register_surface',
104                 #'return_type': 'int', # Or do they return all just some json?
105                 'args': [ # describes the functions arguments, and their names as found in the json request
106                     { 'name': 'appid', 'type': 'uint32_t', 'jtype': 'int' }, # XXX: lookup jtypes automatically? i.e. char*|const char* would be string?
107                     { 'name': 'surfaceid', 'type': 'uint32_t', 'jtype': 'int' },
108                 ],
109             },
110             { 'name': 'demo_activate_surface', 'args': [ { 'name': 'surfaceid', 'type': 'uint32_t', 'jtype': 'int' } ] },
111             { 'name': 'demo_activate_all' },
112             { 'name': 'debug_status', },
113             { 'name': 'debug_layers', },
114             { 'name': 'debug_surfaces', },
115             { 'name': 'debug_terminate' },
116         ]
117 }
118
119 def main():
120     with open('afb_binding_glue.inl', 'w') as out:
121         set_output(out)
122         p('// This file was generated, do not edit', '')
123         generate_names(API)
124         emit_binding(API)
125     with open('afb_binding_api.hpp', 'w') as out:
126         set_output(out)
127         p('// This file was generated, do not edit', '')
128         emit_afb_api(API)
129
130 __name__ == '__main__' and main()