Add an another example for layout configuration
[apps/agl-service-windowmanager-2017.git] / generate-binding-glue.py
1 #!/usr/bin/python3
2
3 #
4 # Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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     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");')
39         p('      return;')
40         p('   }')
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);')
45         p('   if (ret) {',
46           '      afb_req_fail(req, "failed", "Error: afb_req_subscribe()");',
47           '      return;',
48           '   }')
49         p('   afb_req_success(req, NULL, "success");')
50
51     else:
52         if len(args) > 0:
53             p('   json_object *jreq = afb_req_json(req);', '')
54             for arg in args:
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,
59                   '      return;',
60                   '   }',
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());',
65           '      return;',
66           '   }', '')
67         p('   afb_req_success(req, ret.unwrap(), "success");')
68
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);')
72     p('   #ifdef ST')
73     p('   ST();')
74     p('   #endif')
75     p('   if (g_afb_instance == nullptr) {',
76       '      afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");',
77       '      return;',
78       '   }', '',
79       '   try {', '   // BEGIN impl')
80     emit_func_impl(api, f)
81     p('   // END impl',
82       '   } catch (std::exception &e) {',
83       '      afb_req_fail_f(req, "failed", "Uncaught exception while calling %(name)s: %%s", e.what());' % f,
84       '      return;',
85       '   }', '')
86     p('}', '')
87
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)
92     p('   {}', '};')
93
94 def emit_binding(api):
95     p('namespace {')
96     p('std::mutex binding_m;', '')
97     for func in api['functions']:
98         emit_func(api, func)
99     p('} // namespace', '')
100     emit_afb_verbs(api)
101
102 def generate_names(api):
103     for f in api['functions']:
104         f['impl_name'] = '%s_%s_thunk' % (api['name'], f['name'])
105
106 def emit_afb_api(api):
107     p('#include "result.hpp"', '')
108     p('#include <json-c/json.h>', '')
109     p('namespace wm {', '')
110     p('struct App;', '')
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', []))) + ');')
118     p('};', '')
119     p('} // namespace wm', '')
120
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
124 API = {
125         'name': 'windowmanager',
126         'api': 'g_afb_instance->app.api.', # where are our API functions
127         'functions': [
128             {
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' },
133                 ],
134             },
135             {
136                 'name': 'activatesurface',
137                 'args': [
138                     { 'name': 'drawing_name', 'type': 'char const*', 'jtype': 'string' },
139                     { 'name': 'drawing_area', 'type': 'char const*', 'jtype': 'string' },
140                 ],
141             },
142             {
143                 'name': 'deactivatesurface',
144                 'args': [
145                     { 'name': 'drawing_name', 'type': 'char const*', 'jtype': 'string' },
146                 ],
147             },
148             {
149                 'name': 'enddraw',
150                 'args': [
151                     { 'name': 'drawing_name', 'type': 'char const*', 'jtype': 'string' },
152                 ],
153             },
154             { 'name': 'wm_subscribe', },
155
156             { 'name': 'list_drawing_names', },
157             { 'name': 'ping' },
158
159             { 'name': 'debug_status', },
160             { 'name': 'debug_layers', },
161             { 'name': 'debug_surfaces', },
162             { 'name': 'debug_terminate' },
163         ]
164 }
165
166 def main():
167     with open('afb_binding_glue.inl', 'w') as out:
168         set_output(out)
169         p('// This file was generated, do not edit', '')
170         generate_names(API)
171         emit_binding(API)
172     with open('afb_binding_api.hpp', 'w') as out:
173         set_output(out)
174         p('// This file was generated, do not edit', '')
175         emit_afb_api(API)
176
177 __name__ == '__main__' and main()