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 constexpr 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(
240 loop, uribuf, const_cast<struct afb_wsj1_itf *>(&itf), nullptr);
241 if (wsj1 == nullptr) {
242 sd_event_unref(loop);
243 fprintf(stderr, "Connection to %s failed: %m\n", uribuf);
254 int AFBClient::Impl::dispatch() {
255 std::lock_guard<std::recursive_mutex> guard(dispatch_mutex);
256 return sd_event_run(loop, 1);
259 int AFBClient::Impl::requestSurface(const char *label) {
261 json_object *jp = json_object_new_object();
262 json_object_object_add(jp, "drawing_name", json_object_new_string(label));
264 /* send the request */
266 loop, wsj1, "request_surface", jp, [&rc](bool ok, json_object *j) {
269 json_object_get_int(json_object_object_get(j, "response"));
271 asprintf(&buf, "%d", id);
272 printf("setenv(\"QT_IVI_SURFACE_ID\", %s, 1)\n", buf);
273 if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) {
274 fprintf(stderr, "putenv failed: %m\n");
277 rc = 0; // Single point of success
280 fprintf(stderr, "Could not get surface ID from WM: %s\n",
281 j ? json_object_to_json_string_ext(
282 j, JSON_C_TO_STRING_PRETTY)
288 return rc2 < 0 ? rc2 : rc;
291 int AFBClient::Impl::activateSurface(const char *label) {
293 json_object *j = json_object_new_object();
294 json_object_object_add(j, "drawing_name", json_object_new_string(label));
295 return api_call(loop, wsj1, "activate_surface", j, [](bool ok,
299 stderr, "API Call activate_surface() failed: %s\n",
300 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
306 int AFBClient::Impl::deactivateSurface(const char *label) {
308 json_object *j = json_object_new_object();
309 json_object_object_add(j, "drawing_name", json_object_new_string(label));
310 return api_call(loop, wsj1, "deactivate_surface", j, [](bool ok,
314 stderr, "API Call deactivate_surface() failed: %s\n",
315 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
321 int AFBClient::Impl::endDraw(const char *label) {
323 json_object *j = json_object_new_object();
324 json_object_object_add(j, "drawing_name", json_object_new_string(label));
325 return api_call(loop, wsj1, "enddraw", j, [](bool ok, json_object *j) {
328 stderr, "API Call endDraw() failed: %s\n",
329 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
335 void AFBClient::Impl::set_event_handler(
336 enum EventType et, std::function<void(char const *)> func) {
343 // _ _ _____ ____ ____ _ _ _
344 // ___| | __ _ ___ ___ / \ | ___| __ ) / ___| (_) ___ _ __ | |_
345 // / __| |/ _` / __/ __| / _ \ | |_ | _ \| | | | |/ _ \ '_ \| __|
346 // | (__| | (_| \__ \__ \ / ___ \| _| | |_) | |___| | | __/ | | | |_
347 // \___|_|\__,_|___/___/ /_/ \_\_| |____/ \____|_|_|\___|_| |_|\__|
349 int AFBClient::init(int port, char const *token) {
350 return this->d->init(port, token);
353 int AFBClient::dispatch() { return this->d->dispatch(); }
355 int AFBClient::requestSurface(const char *label) {
356 return this->d->requestSurface(label);
359 int AFBClient::activateSurface(const char *label) {
360 return this->d->activateSurface(label);
363 int AFBClient::deactivateSurface(const char *label) {
364 return this->d->deactivateSurface(label);
367 int AFBClient::endDraw(const char *label) { return this->d->endDraw(label); }
369 void AFBClient::set_event_handler(enum EventType et,
370 std::function<void(char const *label)> f) {
371 return this->d->set_event_handler(et, std::move(f));
374 AFBClient &AFBClient::instance() {
376 static AFBClient obj;
380 AFBClient::AFBClient() : d(new Impl) {}
382 AFBClient::~AFBClient() { delete d; }