From c720d0ed2d95b6f73a6e26bfda1ef853cd41be25 Mon Sep 17 00:00:00 2001 From: Marcus Fritzsch Date: Tue, 5 Sep 2017 09:54:58 +0200 Subject: [PATCH] AFBClient: move dispatch_internal() to Impl ... * ... move json argument helper to anon namespace * call api_call() with this-> Signed-off-by: Marcus Fritzsch --- AFBClient.cpp | 69 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/AFBClient.cpp b/AFBClient.cpp index 4b37740..fbffb2c 100644 --- a/AFBClient.cpp +++ b/AFBClient.cpp @@ -57,6 +57,7 @@ class AFBClient::Impl { int api_call(const char *verb, json_object *object, const std::function &onReply); + void dispatch_internal(); public: void event(char const *et, char const *label); @@ -114,7 +115,7 @@ void onHangup(void *closure, afb_wsj1 *wsj1) { UNUSED(closure); UNUSED(wsj1); fputs("Hangup, the WindowManager vanished\n", stderr); - exit(1); + exit(1); // XXX: there should be something ... *better* here. } constexpr struct afb_wsj1_itf itf = { @@ -124,10 +125,10 @@ constexpr struct afb_wsj1_itf itf = { // XXX: I am not sure this is the right thing to do though... std::recursive_mutex dispatch_mutex; -void dispatch_internal(struct sd_event *loop) { - std::lock_guard guard(dispatch_mutex); - TRACE(); - sd_event_run(loop, -1); +json_object *drawing_name_json_argument(char const *label) { + json_object *j = json_object_new_object(); + json_object_object_add(j, "drawing_name", json_object_new_string(label)); + return j; } } // namespace @@ -209,12 +210,6 @@ int AFBClient::Impl::dispatch() { return sd_event_run(this->loop, 1); } -json_object *drawing_name_json_argument(char const *label) { - json_object *j = json_object_new_object(); - json_object_object_add(j, "drawing_name", json_object_new_string(label)); - return j; -} - int AFBClient::Impl::requestSurface(const char *label) { TRACE(); @@ -227,26 +222,28 @@ int AFBClient::Impl::requestSurface(const char *label) { int rc = -1; /* send the request */ - int rc2 = api_call("request_surface", j, [&rc](bool ok, json_object *j) { - if (ok) { - int id = json_object_get_int(json_object_object_get(j, "response")); - char *buf; - asprintf(&buf, "%d", id); - printf("setenv(\"QT_IVI_SURFACE_ID\", %s, 1)\n", buf); - if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) { - fprintf(stderr, "putenv failed: %m\n"); - rc = -errno; + int rc2 = + this->api_call("request_surface", j, [&rc](bool ok, json_object *j) { + if (ok) { + int id = + json_object_get_int(json_object_object_get(j, "response")); + char *buf; + asprintf(&buf, "%d", id); + printf("setenv(\"QT_IVI_SURFACE_ID\", %s, 1)\n", buf); + if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) { + fprintf(stderr, "putenv failed: %m\n"); + rc = -errno; + } else { + rc = 0; // Single point of success + } } else { - rc = 0; // Single point of success + fprintf(stderr, "Could not get surface ID from WM: %s\n", + j != nullptr ? json_object_to_json_string_ext( + j, JSON_C_TO_STRING_PRETTY) + : "no-info"); + rc = -EINVAL; } - } else { - fprintf(stderr, "Could not get surface ID from WM: %s\n", - j != nullptr ? json_object_to_json_string_ext( - j, JSON_C_TO_STRING_PRETTY) - : "no-info"); - rc = -EINVAL; - } - }); + }); if (rc2 < 0) { rc = rc2; @@ -262,7 +259,7 @@ int AFBClient::Impl::requestSurface(const char *label) { int AFBClient::Impl::activateSurface(const char *label) { TRACE(); json_object *j = drawing_name_json_argument(label); - return api_call("activate_surface", j, [](bool ok, json_object *j) { + return this->api_call("activate_surface", j, [](bool ok, json_object *j) { if (!ok) { fprintf(stderr, "API Call activate_surface() failed: %s\n", j != nullptr ? json_object_to_json_string_ext( @@ -275,7 +272,7 @@ int AFBClient::Impl::activateSurface(const char *label) { int AFBClient::Impl::deactivateSurface(const char *label) { TRACE(); json_object *j = drawing_name_json_argument(label); - return api_call("deactivate_surface", j, [](bool ok, json_object *j) { + return this->api_call("deactivate_surface", j, [](bool ok, json_object *j) { if (!ok) { fprintf(stderr, "API Call deactivate_surface() failed: %s\n", j != nullptr ? json_object_to_json_string_ext( @@ -288,7 +285,7 @@ int AFBClient::Impl::deactivateSurface(const char *label) { int AFBClient::Impl::endDraw(const char *label) { TRACE(); json_object *j = drawing_name_json_argument(label); - return api_call("enddraw", j, [](bool ok, json_object *j) { + return this->api_call("enddraw", j, [](bool ok, json_object *j) { if (!ok) { fprintf(stderr, "API Call endDraw() failed: %s\n", j != nullptr ? json_object_to_json_string_ext( @@ -389,7 +386,7 @@ int AFBClient::Impl::api_call( while (!returned.load(std::memory_order_consume)) { std::lock_guard guard(dispatch_mutex); if (!returned.load(std::memory_order_consume)) { - dispatch_internal(this->loop); + this->dispatch_internal(); } } @@ -400,6 +397,12 @@ int AFBClient::Impl::api_call( return rc; } +void AFBClient::Impl::dispatch_internal() { + std::lock_guard guard(dispatch_mutex); + TRACE(); + sd_event_run(this->loop, -1); +} + void AFBClient::Impl::event(char const *et, char const *label) { TRACE(); auto oet = make_event_type(et); -- 2.16.6