+ delete g_afb_instance;
+ g_afb_instance = nullptr;
+ return -1;
+}
+
+int binding_init() noexcept
+{
+ try
+ {
+ return _binding_init();
+ }
+ catch (std::exception &e)
+ {
+ HMI_ERROR("wm", "Uncaught exception in binding_init(): %s", e.what());
+ }
+ return -1;
+}
+
+static void cbRemoveClientCtxt(void *data)
+{
+ wm::WMClientCtxt *ctxt = (wm::WMClientCtxt *)data;
+ if (ctxt == nullptr)
+ {
+ return;
+ }
+ HMI_DEBUG("wm", "remove app %s", ctxt->name.c_str());
+ g_afb_instance->wmgr.onApplicationTerminated(*ctxt);
+ delete ctxt;
+}
+
+static void createSecurityContext(afb_req req, const char* appid, const char* role)
+{
+ wm::WMClientCtxt *ctxt = (wm::WMClientCtxt *)afb_req_context_get(req);
+ if (!ctxt)
+ {
+ // Create Security Context at first time
+ const char *new_role = g_afb_instance->wmgr.convertRoleOldToNew(role);
+ wm::WMClientCtxt *ctxt = new wm::WMClientCtxt(appid, new_role);
+ HMI_DEBUG("wm", "create session for %s", ctxt->name.c_str());
+ afb_req_session_set_LOA(req, 1);
+ afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
+ }
+}
+
+void windowmanager_requestsurface(afb_req req) noexcept
+{
+ std::lock_guard<std::mutex> guard(binding_m);
+#ifdef ST
+ ST();
+#endif
+ if (g_afb_instance == nullptr)
+ {
+ afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
+ return;
+ }
+
+ try
+ {
+ const char *a_drawing_name = afb_req_value(req, "drawing_name");
+ if (!a_drawing_name)
+ {
+ afb_req_fail(req, "failed", "Need char const* argument drawing_name");
+ return;
+ }
+
+ const char *appid = afb_req_get_application_id(req);
+ auto ret = g_afb_instance->wmgr.api_request_surface(
+ appid, a_drawing_name);
+ if (ret.is_err())
+ {
+ afb_req_fail(req, "failed", ret.unwrap_err());
+ return;
+ }
+
+ createSecurityContext(req, appid, a_drawing_name);
+
+ afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
+ }
+ catch (std::exception &e)
+ {
+ afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
+ return;
+ }
+}
+
+void windowmanager_requestsurfacexdg(afb_req req) noexcept
+{
+ std::lock_guard<std::mutex> guard(binding_m);
+#ifdef ST
+ ST();
+#endif
+ if (g_afb_instance == nullptr)
+ {
+ afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
+ return;
+ }
+
+ try
+ {
+ json_object *jreq = afb_req_json(req);
+
+ json_object *j_drawing_name = nullptr;
+ if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
+ {
+ afb_req_fail(req, "failed", "Need char const* argument drawing_name");
+ return;
+ }
+ char const *a_drawing_name = json_object_get_string(j_drawing_name);
+
+ json_object *j_ivi_id = nullptr;
+ if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
+ {
+ afb_req_fail(req, "failed", "Need char const* argument ivi_id");
+ return;
+ }
+ char const *a_ivi_id = json_object_get_string(j_ivi_id);
+ char const *appid = afb_req_get_application_id(req);
+ auto ret = g_afb_instance->wmgr.api_request_surface(
+ appid, a_drawing_name, a_ivi_id);
+
+ if (ret != nullptr)
+ {
+ afb_req_fail(req, "failed", ret);
+ return;
+ }
+
+ createSecurityContext(req, appid, a_drawing_name);
+
+ afb_req_success(req, NULL, "success");
+ }
+ catch (std::exception &e)
+ {
+ afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
+ return;
+ }
+}
+
+void windowmanager_activatewindow(afb_req req) noexcept
+{
+ std::lock_guard<std::mutex> guard(binding_m);
+#ifdef ST
+ ST();
+#endif
+ if (g_afb_instance == nullptr)
+ {
+ afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
+ return;
+ }
+
+ try
+ {
+ const char *a_drawing_name = afb_req_value(req, "drawing_name");
+ if (!a_drawing_name)
+ {
+ afb_req_fail(req, "failed", "Need char const* argument drawing_name");
+ return;
+ }
+
+ const char *a_drawing_area = afb_req_value(req, "drawing_area");
+ if (!a_drawing_area)
+ {
+ afb_req_fail(req, "failed", "Need char const* argument drawing_area");
+ return;
+ }
+
+ g_afb_instance->wmgr.api_activate_surface(
+ afb_req_get_application_id(req),
+ a_drawing_name, a_drawing_area,
+ [&req](const char *errmsg) {
+ if (errmsg != nullptr)
+ {
+ HMI_ERROR("wm", errmsg);
+ afb_req_fail(req, "failed", errmsg);
+ return;
+ }
+ afb_req_success(req, NULL, "success");
+ });
+ }
+ catch (std::exception &e)
+ {
+ HMI_WARNING("wm", "failed: Uncaught exception while calling activatesurface: %s", e.what());
+ g_afb_instance->wmgr.exceptionProcessForTransition();
+ return;
+ }
+}
+
+void windowmanager_deactivatewindow(afb_req req) noexcept
+{
+ std::lock_guard<std::mutex> guard(binding_m);
+#ifdef ST
+ ST();
+#endif
+ if (g_afb_instance == nullptr)
+ {
+ afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
+ return;
+ }
+
+ try
+ {
+ const char *a_drawing_name = afb_req_value(req, "drawing_name");
+ if (!a_drawing_name)
+ {
+ afb_req_fail(req, "failed", "Need char const* argument drawing_name");
+ return;
+ }
+
+ g_afb_instance->wmgr.api_deactivate_surface(
+ afb_req_get_application_id(req), a_drawing_name,
+ [&req](const char *errmsg) {
+ if (errmsg != nullptr)
+ {
+ HMI_ERROR("wm", errmsg);
+ afb_req_fail(req, "failed", errmsg);
+ return;
+ }
+ afb_req_success(req, NULL, "success");
+ });
+ }
+ catch (std::exception &e)
+ {
+ HMI_WARNING("wm", "failed: Uncaught exception while calling deactivatesurface: %s", e.what());
+ g_afb_instance->wmgr.exceptionProcessForTransition();
+ return;
+ }
+}
+
+void windowmanager_enddraw(afb_req req) noexcept
+{
+ std::lock_guard<std::mutex> guard(binding_m);
+#ifdef ST
+ ST();
+#endif
+ if (g_afb_instance == nullptr)
+ {
+ afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
+ return;
+ }
+
+ try
+ {
+ const char *a_drawing_name = afb_req_value(req, "drawing_name");
+ if (!a_drawing_name)
+ {
+ afb_req_fail(req, "failed", "Need char const* argument drawing_name");
+ return;
+ }
+ afb_req_success(req, NULL, "success");
+
+ g_afb_instance->wmgr.api_enddraw(
+ afb_req_get_application_id(req), a_drawing_name);
+ }
+ catch (std::exception &e)
+ {
+ HMI_WARNING("wm", "failed: Uncaught exception while calling enddraw: %s", e.what());
+ g_afb_instance->wmgr.exceptionProcessForTransition();
+ return;
+ }