15 #include <systemd/sd-event.h>
17 #include <json-c/json.h>
20 #include <afb/afb-ws-client.h>
21 #include <afb/afb-wsj1.h>
24 #define UNUSED(x) (void)(x)
28 constexpr const int token_maxlen = 20;
29 constexpr const char *const wmAPI = "winman";
35 #define CONCAT_(X, Y) X##Y
36 #define CONCAT(X, Y) CONCAT_(X, Y)
39 ScopeTrace __attribute__((unused)) CONCAT(trace_scope_, __LINE__)(__func__)
41 ScopeTrace __attribute__((unused)) CONCAT(named_trace_scope_, __LINE__)(#N)
44 thread_local static int indent;
46 ScopeTrace(char const *func) : f(func) {
47 fprintf(stderr, "%*s%s -->\n", 2 * indent++, "", this->f);
49 ~ScopeTrace() { fprintf(stderr, "%*s%s <--\n", 2 * --indent, "", this->f); }
51 thread_local int ScopeTrace::indent = 0;
54 /* called when wsj1 receives a method invocation */
55 void onCall(void *closure, const char *api, const char *verb,
56 struct afb_wsj1_msg *msg) {
60 printf("ON-CALL %s/%s:\n%s\n", api, verb,
61 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
62 JSON_C_TO_STRING_PRETTY));
64 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", nullptr);
66 fprintf(stderr, "replying failed: %m\n");
69 /* called when wsj1 receives an event */
70 void onEvent(void *closure, const char *event, afb_wsj1_msg *msg) {
73 printf("ON-EVENT %s:\n%s\n", event,
74 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
75 JSON_C_TO_STRING_PRETTY));
79 /* called when wsj1 hangsup */
80 void onHangup(void *closure, afb_wsj1 *wsj1) {
84 printf("ON-HANGUP\n");
89 static struct afb_wsj1_itf itf = {
90 onHangup, onCall, onEvent,
93 // XXX: I am not sure this is the right thing to do though...
94 std::recursive_mutex dispatch_mutex;
96 void dispatch_internal(struct sd_event *loop) {
97 std::lock_guard<std::recursive_mutex> guard(dispatch_mutex);
99 sd_event_run(loop, -1);
102 /// object will be json_object_put
103 int api_call(struct sd_event *loop, struct afb_wsj1 *wsj1, const char *verb,
105 std::function<void(bool, json_object *)> onReply) {
108 // We need to wrap the actual onReply call once in order to
109 // *look* like a normal functions pointer (std::functions<>
110 // with captures cannot convert to function pointers).
111 // Alternatively we could setup a local struct and use it as
112 // closure, but I think it is cleaner this way.
114 std::atomic<bool> returned;
115 returned.store(false, std::memory_order_relaxed);
116 std::function<void(bool, json_object *)> wrappedOnReply =
117 [&returned, &call_rc, &onReply](bool ok, json_object *j) {
118 TRACEN(wrappedOnReply);
119 call_rc = ok ? 0 : -EINVAL;
120 // We know it failed, but there may be an explanation in the
126 returned.store(true, std::memory_order_release);
129 // make the actual call, use wrappedOnReply as closure
130 int rc = afb_wsj1_call_j(
131 wsj1, wmAPI, verb, object,
132 [](void *closure, afb_wsj1_msg *msg) {
135 reinterpret_cast<std::function<void(bool, json_object *)> *>(
137 (*onReply)(!!afb_wsj1_msg_is_reply_ok(msg),
138 afb_wsj1_msg_object_j(msg));
144 stderr, "calling %s/%s(%s) failed: %m\n", wmAPI, verb,
145 json_object_to_json_string_ext(object, JSON_C_TO_STRING_PRETTY));
146 // Call the reply handler regardless with a NULL json_object*
147 onReply(false, nullptr);
149 // We need to dispatch until "returned" got set, this is necessary
150 // if events get triggered by the call (and would be dispatched before
151 // the actual call-reply).
152 while (!returned.load(std::memory_order_consume)) {
153 dispatch_internal(loop);
156 // return the actual API call result
166 // ___| | __ _ ___ ___ |_ _|_ __ ___ _ __ | |
167 // / __| |/ _` / __/ __| | || '_ ` _ \| '_ \| |
168 // | (__| | (_| \__ \__ \ | || | | | | | |_) | |
169 // \___|_|\__,_|___/___/ |___|_| |_| |_| .__/|_|
171 class AFBClient::Impl {
172 friend class AFBClient;
174 // This is the AFBClient interface impl
175 int init(int port, char const *token);
179 int requestSurface(const char *label);
180 int activateSurface(const char *label);
181 int deactivateSurface(const char *label);
182 int endDraw(const char *label);
184 void set_event_handler(enum EventType et,
185 std::function<void(char const *label)> f);
190 struct afb_wsj1 *wsj1;
191 struct sd_event *loop;
194 AFBClient::Impl::Impl() : wsj1{}, loop{} { TRACE(); }
196 AFBClient::Impl::~Impl() {
198 afb_wsj1_unref(wsj1);
199 sd_event_unref(loop);
203 int AFBClient::Impl::init(int port, char const *token) {
205 char *uribuf = nullptr;
208 if (!token || strlen(token) > token_maxlen) {
209 fprintf(stderr, "Token is invalid\n");
214 for (char const *p = token; *p; p++) {
216 fprintf(stderr, "Token is invalid\n");
222 if (port < 1 && port > 0xffff) {
223 fprintf(stderr, "Port is invalid\n");
228 /* get the default event loop */
229 rc = sd_event_default(&loop);
231 fprintf(stderr, "Connection to default event loop failed: %s\n",
236 asprintf(&uribuf, "ws://localhost:%d/api?token=%s", port, token);
238 /* connect the websocket wsj1 to the uri given by the first argument */
239 wsj1 = afb_ws_client_connect_wsj1(loop, uribuf, &itf, nullptr);
240 if (wsj1 == nullptr) {
241 sd_event_unref(loop);
242 fprintf(stderr, "Connection to %s failed: %m\n", uribuf);
253 int AFBClient::Impl::dispatch() {
254 std::lock_guard<std::recursive_mutex> guard(dispatch_mutex);
255 return sd_event_run(loop, 1);
258 int AFBClient::Impl::requestSurface(const char *label) {
260 json_object *jp = json_object_new_object();
261 json_object_object_add(jp, "drawing_name", json_object_new_string(label));
263 /* send the request */
265 loop, wsj1, "request_surface", jp, [&rc](bool ok, json_object *j) {
268 json_object_get_int(json_object_object_get(j, "response"));
270 asprintf(&buf, "%d", id);
271 printf("setenv(\"QT_IVI_SURFACE_ID\", %s, 1)\n", buf);
272 if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) {
273 fprintf(stderr, "putenv failed: %m\n");
276 rc = 0; // Single point of success
279 fprintf(stderr, "Could not get surface ID from WM: %s\n",
280 j ? json_object_to_json_string_ext(
281 j, JSON_C_TO_STRING_PRETTY)
287 return rc2 < 0 ? rc2 : rc;
290 int AFBClient::Impl::activateSurface(const char *label) {
292 json_object *j = json_object_new_object();
293 json_object_object_add(j, "drawing_name", json_object_new_string(label));
294 return api_call(loop, wsj1, "activate_surface", j, [](bool ok,
298 stderr, "API Call activate_surface() failed: %s\n",
299 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
305 int AFBClient::Impl::deactivateSurface(const char *label) {
307 json_object *j = json_object_new_object();
308 json_object_object_add(j, "drawing_name", json_object_new_string(label));
309 return api_call(loop, wsj1, "deactivate_surface", j, [](bool ok,
313 stderr, "API Call deactivate_surface() failed: %s\n",
314 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
320 int AFBClient::Impl::endDraw(const char *label) {
322 json_object *j = json_object_new_object();
323 json_object_object_add(j, "drawing_name", json_object_new_string(label));
324 return api_call(loop, wsj1, "enddraw", j, [](bool ok, json_object *j) {
327 stderr, "API Call endDraw() failed: %s\n",
328 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
334 void AFBClient::Impl::set_event_handler(
335 enum EventType et, std::function<void(char const *)> func) {
342 // _ _ _____ ____ ____ _ _ _
343 // ___| | __ _ ___ ___ / \ | ___| __ ) / ___| (_) ___ _ __ | |_
344 // / __| |/ _` / __/ __| / _ \ | |_ | _ \| | | | |/ _ \ '_ \| __|
345 // | (__| | (_| \__ \__ \ / ___ \| _| | |_) | |___| | | __/ | | | |_
346 // \___|_|\__,_|___/___/ /_/ \_\_| |____/ \____|_|_|\___|_| |_|\__|
348 int AFBClient::init(int port, char const *token) {
349 return this->d->init(port, token);
352 int AFBClient::dispatch() { return this->d->dispatch(); }
354 int AFBClient::requestSurface(const char *label) {
355 return this->d->requestSurface(label);
358 int AFBClient::activateSurface(const char *label) {
359 return this->d->activateSurface(label);
362 int AFBClient::deactivateSurface(const char *label) {
363 return this->d->deactivateSurface(label);
366 int AFBClient::endDraw(const char *label) { return this->d->endDraw(label); }
368 void AFBClient::set_event_handler(enum EventType et,
369 std::function<void(char const *label)> f) {
370 return this->d->set_event_handler(et, std::move(f));
373 AFBClient &AFBClient::instance() {
375 static AFBClient obj;
379 AFBClient::AFBClient() : d(new Impl) {}
381 AFBClient::~AFBClient() { delete d; }