From 1958da2130c04fbcc0bf1da2c40b57c785657e48 Mon Sep 17 00:00:00 2001 From: Marcus Fritzsch Date: Thu, 13 Jul 2017 09:30:04 +0200 Subject: glue: renamed output files to better represent their actual purpose Signed-off-by: Marcus Fritzsch --- generate-binding-glue.py | 111 +++++++++++++++++++++++++++++++++++++++++++++++ generate-binding.py | 111 ----------------------------------------------- src/CMakeLists.txt | 8 ++-- src/app.hpp | 2 +- src/main.cpp | 2 +- 5 files changed, 117 insertions(+), 117 deletions(-) create mode 100644 generate-binding-glue.py delete mode 100644 generate-binding.py diff --git a/generate-binding-glue.py b/generate-binding-glue.py new file mode 100644 index 0000000..cec4fea --- /dev/null +++ b/generate-binding-glue.py @@ -0,0 +1,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 ', '') + p('namespace wm {', '') + p('struct App;', '') + p('struct binding_api {') + p(' typedef wm::result 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_glue.inl', 'w') as out: + set_output(out) + p('// This file was generated, do not edit', '') + generate_names(API) + emit_binding(API) + with open('afb_binding_api.hpp', 'w') as out: + set_output(out) + p('// This file was generated, do not edit', '') + emit_afb_api(API) + +__name__ == '__main__' and main() diff --git a/generate-binding.py b/generate-binding.py deleted file mode 100644 index 6c700de..0000000 --- a/generate-binding.py +++ /dev/null @@ -1,111 +0,0 @@ -#!/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 ', '') - p('namespace wm {', '') - p('struct App;', '') - p('struct binding_api {') - p(' typedef wm::result 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() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a0d3db6..ebb1946 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,9 +8,9 @@ pkg_check_modules(SD REQUIRED libsystemd>=222) set(CMAKE_SHARED_MODULE_PREFIX "") add_custom_command( - OUTPUT afb_api.hpp afb_binding.inl - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../generate-binding.py - COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/../generate-binding.py) + OUTPUT afb_binding_api.hpp afb_binding_glue.inl + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../generate-binding-glue.py + COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/../generate-binding-glue.py) add_library(winman MODULE main.cpp @@ -18,7 +18,7 @@ add_library(winman MODULE wayland.hpp util.cpp util.hpp - ${IVI_CON_PROTO} json_helper.cpp json_helper.hpp app.hpp app.cpp result.hpp afb_api.hpp afb_binding.inl) + ${IVI_CON_PROTO} json_helper.cpp json_helper.hpp app.hpp app.cpp result.hpp afb_binding_api.hpp afb_binding_glue.inl) target_include_directories(winman PRIVATE diff --git a/src/app.hpp b/src/app.hpp index f6238af..0fb929d 100644 --- a/src/app.hpp +++ b/src/app.hpp @@ -10,7 +10,7 @@ #include "wayland.hpp" #include "result.hpp" -#include "afb_api.hpp" +#include "afb_binding_api.hpp" namespace wl { struct display; diff --git a/src/main.cpp b/src/main.cpp index 6de8597..8bb0b5c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -109,7 +109,7 @@ int binding_init() noexcept { } // namespace -#include "afb_binding.inl" +#include "afb_binding_glue.inl" extern "C" const struct afb_binding_v2 afbBindingV2 = { "winman", NULL, NULL, winman_verbs, NULL, binding_init, NULL, 1}; -- cgit 1.2.3-korg