- struct json_object *args = json_object_new_string(id.c_str());
- api_call(request->api, _afm_main, __FUNCTION__, args);
+ struct closure_data *cdata;
+
+ /* tentatively store the client and client context, as the afb_req_t
+ * request will no longer be available in the async callback handler. This
+ * is similar to that is done showWindow(), handleRequest() in
+ * homescreen.cpp, but allows to fake the subscription here as well to
+ * avoid clients create/install dummy event handlers as to 'register' (or
+ * to keep track of applications started).
+ *
+ * In case api_callback() does return an error we'll remove then the client
+ * and client context there. We pass the closure_data with the client context
+ * and the application id to remove it.
+ */
+ if (!instance || id.empty())
+ return;
+
+ cdata = static_cast<struct closure_data *>(calloc(1, sizeof(*cdata)));
+ cdata->hs_instance = instance;
+ cdata->appid = id;
+
+ struct HS_ClientManager *clientManager = instance->client_manager;
+ if (!clientManager) {
+ return;
+ }
+
+ clientManager->addClient(request, id);
+ api_call(request->api, _afm_main, __FUNCTION__, json_object_new_string(id.c_str()), cdata);