AFBClient: trace dispatch_internal(), header hygiene
[staging/windowmanager.git] / AFBClient.cpp
index 407aeac..9bcb880 100644 (file)
@@ -6,8 +6,18 @@
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
+
 #include <unistd.h>
 
+#include <systemd/sd-event.h>
+
+#include <json-c/json.h>
+
+extern "C" {
+#include <afb/afb-ws-client.h>
+#include <afb/afb-wsj1.h>
+}
+
 #define UNUSED(x) (void)(x)
 
 namespace {
@@ -28,14 +38,14 @@ constexpr const char *const wmAPI = "winman";
     ScopeTrace __attribute__((unused)) CONCAT(named_trace_scope_, __LINE__)(#N)
 
 struct ScopeTrace {
-    static int indent;
+    thread_local static int indent;
     char const *f{};
     ScopeTrace(char const *func) : f(func) {
         fprintf(stderr, "%*s%s -->\n", 2 * indent++, "", this->f);
     }
     ~ScopeTrace() { fprintf(stderr, "%*s%s <--\n", 2 * --indent, "", this->f); }
 };
-int ScopeTrace::indent = 0;
+thread_local int ScopeTrace::indent = 0;
 #endif
 
 /* called when wsj1 receives a method invocation */
@@ -73,6 +83,15 @@ void onHangup(void *closure, afb_wsj1 *wsj1) {
     exit(0);
 }
 
+static struct afb_wsj1_itf itf = {
+    onHangup, onCall, onEvent,
+};
+
+void dispatch_internal(AFBClient *c, uint64_t timeout) {
+   TRACE();
+   c->dispatch(timeout);
+}
+
 }  // namespace
 
 AFBClient &AFBClient::instance() {
@@ -81,12 +100,7 @@ AFBClient &AFBClient::instance() {
     return obj;
 }
 
-AFBClient::AFBClient() : wsj1{}, itf{}, loop{} {
-    TRACE();
-    itf.on_hangup = onHangup;
-    itf.on_call = onCall;
-    itf.on_event = onEvent;
-}
+AFBClient::AFBClient() : wsj1{}, loop{} { TRACE(); }
 
 AFBClient::~AFBClient() {
     TRACE();
@@ -267,7 +281,7 @@ int AFBClient::call(const char *verb, json_object *object,
         // if events get triggered by the call (and would be dispatched before
         // the actual call-reply).
         while (!returned) {
-            dispatch(-1);
+            dispatch_internal(this, -1);
         }
 
         // return the actual API call result