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', [])
33 func_name = f.get('name', [])
34 if "wm_subscribe" == func_name:
35 p(' json_object *jreq = afb_req_json(req);')
36 p(' json_object *j = nullptr;')
37 p(' if (! json_object_object_get_ex(jreq, "event", &j)) {')
38 p(' afb_req_fail(req, "failed", "Need char const* argument event");')
41 p(' int event_type = json_object_get_int(j);')
42 p(' const char *event_name = g_afb_instance->app.kListEventName[event_type];')
43 p(' struct afb_event event = g_afb_instance->app.map_afb_event[event_name];')
44 p(' int ret = afb_req_subscribe(req, event);')
46 ' afb_req_fail(req, "failed", "Error: afb_req_subscribe()");',
49 p(' afb_req_success(req, NULL, "success");')
53 p(' json_object *jreq = afb_req_json(req);', '')
55 arg['jtype'] = arg.get('jtype', arg['type']) # add jtype default
56 p(' json_object *j_%(name)s = nullptr;' % arg,
57 ' if (! json_object_object_get_ex(jreq, "%(name)s", &j_%(name)s)) {' % arg,
58 ' afb_req_fail(req, "failed", "Need %(type)s argument %(name)s");' % arg,
61 ' %(type)s a_%(name)s = json_object_get_%(jtype)s(j_%(name)s);' % arg, '')
62 p(' auto ret = %(api)s' % api + '%(name)s(' % f + ', '.join(map(lambda x: 'a_' + x['name'], args)) + ');')
63 p(' if (ret.is_err()) {',
64 ' afb_req_fail(req, "failed", ret.unwrap_err());',
67 p(' afb_req_success(req, ret.unwrap(), "success");')
69 def emit_func(api, f):
70 p('void %(impl_name)s(afb_req req) noexcept {' % f)
71 p(' std::lock_guard<std::mutex> guard(binding_m);')
75 p(' if (g_afb_instance == nullptr) {',
76 ' afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");',
79 ' try {', ' // BEGIN impl')
80 emit_func_impl(api, f)
82 ' } catch (std::exception &e) {',
83 ' afb_req_fail_f(req, "failed", "Uncaught exception while calling %(name)s: %%s", e.what());' % f,
88 def emit_afb_verbs(api):
89 p('const struct afb_verb_v2 %(name)s_verbs[] = {' % api)
90 for f in api['functions']:
91 p(' { "%(name)s", %(impl_name)s, nullptr, nullptr, AFB_SESSION_NONE },' % f)
94 def emit_binding(api):
96 p('std::mutex binding_m;', '')
97 for func in api['functions']:
99 p('} // namespace', '')
102 def generate_names(api):
103 for f in api['functions']:
104 f['impl_name'] = '%s_%s_thunk' % (api['name'], f['name'])
106 def emit_afb_api(api):
107 p('#include "result.hpp"', '')
108 p('#include <json-c/json.h>', '')
109 p('namespace wm {', '')
111 p('struct binding_api {')
112 p(' typedef wm::result<json_object *> result_type;')
113 p(' struct wm::App *app;')
114 p(' void send_event(char const *evname, char const *label);')
115 p(' void send_event(char const *evname, char const *label, char const *area);')
116 for f in api['functions']:
117 p(' result_type %(name)s(' % f + ', '.join(map(lambda x: '%(type)s %(name)s' % x, f.get('args', []))) + ');')
119 p('} // namespace wm', '')
121 # names must always be valid in c and unique for each function (that is its arguments)
122 # arguments will be looked up from json request, range checking needs to be implemented
123 # by the actual API call
125 'name': 'windowmanager',
126 'api': 'g_afb_instance->app.api.', # where are our API functions
129 'name': 'requestsurface',
130 #'return_type': 'int', # Or do they return all just some json?
131 'args': [ # describes the functions arguments, and their names as found in the json request
132 { 'name': 'drawing_name', 'type': 'char const*', 'jtype': 'string' },
136 'name': 'activatesurface',
138 { 'name': 'drawing_name', 'type': 'char const*', 'jtype': 'string' },
139 { 'name': 'drawing_area', 'type': 'char const*', 'jtype': 'string' },
143 'name': 'deactivatesurface',
145 { 'name': 'drawing_name', 'type': 'char const*', 'jtype': 'string' },
151 { 'name': 'drawing_name', 'type': 'char const*', 'jtype': 'string' },
154 { 'name': 'wm_subscribe', },
156 { 'name': 'list_drawing_names', },
159 { 'name': 'debug_status', },
160 { 'name': 'debug_layers', },
161 { 'name': 'debug_surfaces', },
162 { 'name': 'debug_terminate' },
167 with open('afb_binding_glue.inl', 'w') as out:
169 p('// This file was generated, do not edit', '')
172 with open('afb_binding_api.hpp', 'w') as out:
174 p('// This file was generated, do not edit', '')
177 __name__ == '__main__' and main()