4 # Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
28 OUT.write('\n'.join(args))
31 def emit_func_impl(api, f):
32 args = f.get('args', [])
34 p(' json_object *jreq = afb_req_json(req);', '')
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,
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());',
48 p(' afb_req_success(req, ret.unwrap(), "success");')
50 def emit_func(api, f):
51 p('void %(impl_name)s(afb_req req) noexcept {' % f)
52 p(' std::lock_guard<std::mutex> guard(binding_m);')
56 p(' if (g_afb_instance == nullptr) {',
57 ' afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");',
60 ' try {', ' // BEGIN impl')
61 emit_func_impl(api, f)
63 ' } catch (std::exception &e) {',
64 ' afb_req_fail_f(req, "failed", "Uncaught exception while calling %(name)s: %%s", e.what());' % f,
69 def emit_afb_verbs(api):
70 p('const struct afb_verb_v2 %(name)s_verbs[] = {' % api)
71 for f in api['functions']:
72 p(' { "%(name)s", %(impl_name)s, nullptr, nullptr, AFB_SESSION_NONE },' % f)
75 def emit_binding(api):
77 p('std::mutex binding_m;', '')
78 for func in api['functions']:
80 p('} // namespace', '')
83 def generate_names(api):
84 for f in api['functions']:
85 f['impl_name'] = '%s_%s_thunk' % (api['name'], f['name'])
87 def emit_afb_api(api):
88 p('#include "result.hpp"', '')
89 p('#include <json-c/json.h>', '')
90 p('namespace wm {', '')
92 p('struct binding_api {')
93 p(' typedef wm::result<json_object *> result_type;')
94 p(' struct wm::App *app;')
95 p(' void send_event(char const *evname, char const *label);')
96 for f in api['functions']:
97 p(' result_type %(name)s(' % f + ', '.join(map(lambda x: '%(type)s %(name)s' % x, f.get('args', []))) + ');')
99 p('} // namespace wm', '')
101 # names must always be valid in c and unique for each function (that is its arguments)
102 # arguments will be looked up from json request, range checking needs to be implemented
103 # by the actual API call
105 'name': 'windowmanager',
106 'api': 'g_afb_instance->app.api.', # where are our API functions
109 'name': 'requestsurface',
110 #'return_type': 'int', # Or do they return all just some json?
111 'args': [ # describes the functions arguments, and their names as found in the json request
112 { 'name': 'drawing_name', 'type': 'char const*', 'jtype': 'string' },
116 'name': 'activatesurface',
118 { 'name': 'drawing_name', 'type': 'char const*', 'jtype': 'string' },
122 'name': 'deactivatesurface',
124 { 'name': 'drawing_name', 'type': 'char const*', 'jtype': 'string' },
130 { 'name': 'drawing_name', 'type': 'char const*', 'jtype': 'string' },
133 { 'name': 'list_drawing_names', },
136 { 'name': 'debug_status', },
137 { 'name': 'debug_layers', },
138 { 'name': 'debug_surfaces', },
139 { 'name': 'debug_terminate' },
144 with open('afb_binding_glue.inl', 'w') as out:
146 p('// This file was generated, do not edit', '')
149 with open('afb_binding_api.hpp', 'w') as out:
151 p('// This file was generated, do not edit', '')
154 __name__ == '__main__' and main()