1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
#!/usr/bin/python3
import sys
OUT = sys.stdout
def set_output(f):
global OUT
OUT = f
def p(*args):
OUT.write('\n'.join(args))
OUT.write('\n')
def emit_func_impl(api, f):
args = f.get('args', [])
if len(args) > 0:
p(' json_object *jreq = afb_req_json(req);', '')
for arg in args:
arg['jtype'] = arg.get('jtype', arg['type']) # add jtype default
p(' json_object *j_%(name)s = nullptr;' % arg,
' if (! json_object_object_get_ex(jreq, "%(name)s", &j_%(name)s)) {' % arg,
' afb_req_fail(req, "failed", "Need %(type)s argument %(name)s");' % arg,
' return;',
' }',
' %(type)s a_%(name)s = json_object_get_%(jtype)s(j_%(name)s);' % arg, '')
p(' auto ret = %(api)s' % api + '%(name)s(' % f + ', '.join(map(lambda x: 'a_' + x['name'], args)) + ');')
p(' if (ret.is_err()) {',
' afb_req_fail(req, "failed", ret.unwrap_err());',
' return;',
' }', '')
p(' afb_req_success(req, ret.unwrap(), "success");')
def emit_func(api, f):
p('void %(impl_name)s(afb_req req) noexcept {' % f)
p(' if (g_afb_instance == nullptr) {',
' afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");',
' return;',
' }', '',
' try {', ' // BEGIN impl')
emit_func_impl(api, f)
p(' // END impl',
' } catch (std::exception &e) {',
' afb_req_fail_f(req, "failed", "Uncaught exception while calling %(name)s: %%s", e.what());' % f,
' return;',
' }', '')
p('}', '')
def emit_afb_verbs(api):
p('const struct afb_verb_v2 %(name)s_verbs[] = {' % api)
for f in api['functions']:
p(' { "%(name)s", %(impl_name)s, nullptr, nullptr, AFB_SESSION_NONE },' % f)
p(' {}', '};')
def emit_binding(api):
p('namespace {', '')
for func in api['functions']:
emit_func(api, func)
p('} // namespace', '')
emit_afb_verbs(api)
def generate_names(api):
for f in api['functions']:
f['impl_name'] = '%s_%s_thunk' % (api['name'], f['name'])
def emit_afb_api(api):
p('#include "result.hpp"', '')
p('#include <json-c/json.h>', '')
p('namespace wm {', '')
p('struct App;', '')
p('struct binding_api {')
p(' typedef wm::result<json_object *> result_type;')
p(' struct wm::App *app;')
for f in api['functions']:
p(' result_type %(name)s(' % f + ', '.join(map(lambda x: '%(type)s %(name)s' % x, f.get('args', []))) + ');')
p('};', '')
p('} // namespace wm')
# names must always be valid in c and unique for each function (that is its arguments)
# arguments will be looked up from json request, range checking needs to be implemented
# by the actual API call
API = {
'name': 'winman',
'api': 'g_afb_instance->app.api.', # where are our API functions
'functions': [
{
'name': 'register_surface',
#'return_type': 'int', # Or do they return all just some json?
'args': [ # describes the functions arguments, and their names as found in the json request
{ 'name': 'appid', 'type': 'uint32_t', 'jtype': 'int' }, # XXX: lookup jtypes automatically? i.e. char*|const char* would be string?
{ 'name': 'surfaceid', 'type': 'uint32_t', 'jtype': 'int' },
],
},
{ 'name': 'debug_status', },
{ 'name': 'debug_layers', },
{ 'name': 'debug_surfaces', },
]
}
def main():
with open('afb_binding.inl', 'w') as out:
set_output(out)
p('// This file was generated, do not edit', '')
generate_names(API)
emit_binding(API)
with open('afb_api.hpp', 'w') as out:
set_output(out)
p('// This file was generated, do not edit', '')
emit_afb_api(API)
__name__ == '__main__' and main()
|