14 #include <systemd/sd-event.h>
16 #include <json-c/json.h>
19 #include <afb/afb-ws-client.h>
20 #include <afb/afb-wsj1.h>
23 #define UNUSED(x) (void)(x)
27 constexpr const int token_maxlen = 20;
28 constexpr const char *const wmAPI = "winman";
34 #define CONCAT_(X, Y) X##Y
35 #define CONCAT(X, Y) CONCAT_(X, Y)
38 ScopeTrace __attribute__((unused)) CONCAT(trace_scope_, __LINE__)(__func__)
40 ScopeTrace __attribute__((unused)) CONCAT(named_trace_scope_, __LINE__)(#N)
43 thread_local static int indent;
45 ScopeTrace(char const *func) : f(func) {
46 fprintf(stderr, "%*s%s -->\n", 2 * indent++, "", this->f);
48 ~ScopeTrace() { fprintf(stderr, "%*s%s <--\n", 2 * --indent, "", this->f); }
50 thread_local int ScopeTrace::indent = 0;
53 /* called when wsj1 receives a method invocation */
54 void onCall(void *closure, const char *api, const char *verb,
55 struct afb_wsj1_msg *msg) {
59 printf("ON-CALL %s/%s:\n%s\n", api, verb,
60 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
61 JSON_C_TO_STRING_PRETTY));
63 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", nullptr);
65 fprintf(stderr, "replying failed: %m\n");
68 /* called when wsj1 receives an event */
69 void onEvent(void *closure, const char *event, afb_wsj1_msg *msg) {
72 printf("ON-EVENT %s:\n%s\n", event,
73 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
74 JSON_C_TO_STRING_PRETTY));
78 /* called when wsj1 hangsup */
79 void onHangup(void *closure, afb_wsj1 *wsj1) {
83 printf("ON-HANGUP\n");
88 static struct afb_wsj1_itf itf = {
89 onHangup, onCall, onEvent,
92 // XXX: I am not sure this is the right thing to do though...
93 std::recursive_mutex dispatch_mutex;
95 void dispatch_internal(struct sd_event *loop) {
96 std::lock_guard<std::recursive_mutex> guard(dispatch_mutex);
98 sd_event_run(loop, -1);
101 /// object will be json_object_put
102 int api_call(struct sd_event *loop, struct afb_wsj1 *wsj1, const char *verb,
104 std::function<void(bool, json_object *)> onReply) {
107 // We need to wrap the actual onReply call once in order to
108 // *look* like a normal functions pointer (std::functions<>
109 // with captures cannot convert to function pointers).
110 // Alternatively we could setup a local struct and use it as
111 // closure, but I think it is cleaner this way.
113 bool returned = false;
114 std::function<void(bool, json_object *)> wrappedOnReply =
115 [&returned, &call_rc, &onReply](bool ok, json_object *j) {
116 TRACEN(wrappedOnReply);
117 call_rc = ok ? 0 : -EINVAL;
118 // We know it failed, but there may be an explanation in the
127 // make the actual call, use wrappedOnReply as closure
128 int rc = afb_wsj1_call_j(
129 wsj1, wmAPI, verb, object,
130 [](void *closure, afb_wsj1_msg *msg) {
133 reinterpret_cast<std::function<void(bool, json_object *)> *>(
135 (*onReply)(!!afb_wsj1_msg_is_reply_ok(msg),
136 afb_wsj1_msg_object_j(msg));
142 stderr, "calling %s/%s(%s) failed: %m\n", wmAPI, verb,
143 json_object_to_json_string_ext(object, JSON_C_TO_STRING_PRETTY));
144 // Call the reply handler regardless with a NULL json_object*
145 onReply(false, nullptr);
147 // We need to dispatch until "returned" got set, this is necessary
148 // if events get triggered by the call (and would be dispatched before
149 // the actual call-reply).
151 dispatch_internal(loop);
154 // return the actual API call result
164 // ___| | __ _ ___ ___ |_ _|_ __ ___ _ __ | |
165 // / __| |/ _` / __/ __| | || '_ ` _ \| '_ \| |
166 // | (__| | (_| \__ \__ \ | || | | | | | |_) | |
167 // \___|_|\__,_|___/___/ |___|_| |_| |_| .__/|_|
169 class AFBClient::Impl {
170 friend class AFBClient;
172 // This is the AFBClient interface impl
173 int init(int port, char const *token);
177 int requestSurface(const char *label);
178 int activateSurface(const char *label);
179 int deactivateSurface(const char *label);
180 int endDraw(const char *label);
182 void set_event_handler(enum EventType et,
183 std::function<void(char const *label)> f);
188 struct afb_wsj1 *wsj1;
189 struct sd_event *loop;
192 AFBClient::Impl::Impl() : wsj1{}, loop{} { TRACE(); }
194 AFBClient::Impl::~Impl() {
196 afb_wsj1_unref(wsj1);
197 sd_event_unref(loop);
201 int AFBClient::Impl::init(int port, char const *token) {
203 char *uribuf = nullptr;
206 if (!token || strlen(token) > token_maxlen) {
207 fprintf(stderr, "Token is invalid\n");
212 for (char const *p = token; *p; p++) {
214 fprintf(stderr, "Token is invalid\n");
220 if (port < 1 && port > 0xffff) {
221 fprintf(stderr, "Port is invalid\n");
226 /* get the default event loop */
227 rc = sd_event_default(&loop);
229 fprintf(stderr, "Connection to default event loop failed: %s\n",
234 asprintf(&uribuf, "ws://localhost:%d/api?token=%s", port, token);
236 /* connect the websocket wsj1 to the uri given by the first argument */
237 wsj1 = afb_ws_client_connect_wsj1(loop, uribuf, &itf, nullptr);
238 if (wsj1 == nullptr) {
239 sd_event_unref(loop);
240 fprintf(stderr, "Connection to %s failed: %m\n", uribuf);
251 int AFBClient::Impl::dispatch() {
252 std::lock_guard<std::recursive_mutex> guard(dispatch_mutex);
253 return sd_event_run(loop, 1);
256 int AFBClient::Impl::requestSurface(const char *label) {
258 json_object *jp = json_object_new_object();
259 json_object_object_add(jp, "drawing_name", json_object_new_string(label));
261 /* send the request */
263 loop, wsj1, "request_surface", jp, [&rc](bool ok, json_object *j) {
266 json_object_get_int(json_object_object_get(j, "response"));
268 asprintf(&buf, "%d", id);
269 printf("setenv(\"QT_IVI_SURFACE_ID\", %s, 1)\n", buf);
270 if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) {
271 fprintf(stderr, "putenv failed: %m\n");
274 rc = 0; // Single point of success
277 fprintf(stderr, "Could not get surface ID from WM: %s\n",
278 j ? json_object_to_json_string_ext(
279 j, JSON_C_TO_STRING_PRETTY)
285 return rc2 < 0 ? rc2 : rc;
288 int AFBClient::Impl::activateSurface(const char *label) {
290 json_object *j = json_object_new_object();
291 json_object_object_add(j, "drawing_name", json_object_new_string(label));
292 return api_call(loop, wsj1, "activate_surface", j, [](bool ok,
296 stderr, "API Call activate_surface() failed: %s\n",
297 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
303 int AFBClient::Impl::deactivateSurface(const char *label) {
305 json_object *j = json_object_new_object();
306 json_object_object_add(j, "drawing_name", json_object_new_string(label));
307 return api_call(loop, wsj1, "deactivate_surface", j, [](bool ok,
311 stderr, "API Call deactivate_surface() failed: %s\n",
312 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
318 int AFBClient::Impl::endDraw(const char *label) {
320 json_object *j = json_object_new_object();
321 json_object_object_add(j, "drawing_name", json_object_new_string(label));
322 return api_call(loop, wsj1, "enddraw", j, [](bool ok, json_object *j) {
325 stderr, "API Call endDraw() failed: %s\n",
326 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
332 void AFBClient::Impl::set_event_handler(
333 enum EventType et, std::function<void(char const *)> func) {
340 // _ _ _____ ____ ____ _ _ _
341 // ___| | __ _ ___ ___ / \ | ___| __ ) / ___| (_) ___ _ __ | |_
342 // / __| |/ _` / __/ __| / _ \ | |_ | _ \| | | | |/ _ \ '_ \| __|
343 // | (__| | (_| \__ \__ \ / ___ \| _| | |_) | |___| | | __/ | | | |_
344 // \___|_|\__,_|___/___/ /_/ \_\_| |____/ \____|_|_|\___|_| |_|\__|
346 int AFBClient::init(int port, char const *token) {
347 return this->d->init(port, token);
350 int AFBClient::dispatch() { return this->d->dispatch(); }
352 int AFBClient::requestSurface(const char *label) {
353 return this->d->requestSurface(label);
356 int AFBClient::activateSurface(const char *label) {
357 return this->d->activateSurface(label);
360 int AFBClient::deactivateSurface(const char *label) {
361 return this->d->deactivateSurface(label);
364 int AFBClient::endDraw(const char *label) { return this->d->endDraw(label); }
366 void AFBClient::set_event_handler(enum EventType et,
367 std::function<void(char const *label)> f) {
368 return this->d->set_event_handler(et, std::move(f));
371 AFBClient &AFBClient::instance() {
373 static AFBClient obj;
377 AFBClient::AFBClient() : d(new Impl) {}
379 AFBClient::~AFBClient() { delete d; }