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 std::recursive_mutex dispatch_mutex;
94 void dispatch_internal(struct sd_event *loop) {
95 std::lock_guard<std::recursive_mutex> guard(dispatch_mutex);
97 sd_event_run(loop, -1);
100 /// object will be json_object_put
101 int api_call(struct sd_event *loop, struct afb_wsj1 *wsj1, const char *verb,
103 std::function<void(bool, json_object *)> onReply) {
106 // We need to wrap the actual onReply call once in order to
107 // *look* like a normal functions pointer (std::functions<>
108 // with captures cannot convert to function pointers).
109 // Alternatively we could setup a local struct and use it as
110 // closure, but I think it is cleaner this way.
112 bool returned = false;
113 std::function<void(bool, json_object *)> wrappedOnReply =
114 [&returned, &call_rc, &onReply](bool ok, json_object *j) {
115 TRACEN(wrappedOnReply);
116 call_rc = ok ? 0 : -EINVAL;
117 // We know it failed, but there may be an explanation in the
126 // make the actual call, use wrappedOnReply as closure
127 int rc = afb_wsj1_call_j(
128 wsj1, wmAPI, verb, object,
129 [](void *closure, afb_wsj1_msg *msg) {
132 reinterpret_cast<std::function<void(bool, json_object *)> *>(
134 (*onReply)(!!afb_wsj1_msg_is_reply_ok(msg),
135 afb_wsj1_msg_object_j(msg));
141 stderr, "calling %s/%s(%s) failed: %m\n", wmAPI, verb,
142 json_object_to_json_string_ext(object, JSON_C_TO_STRING_PRETTY));
143 // Call the reply handler regardless with a NULL json_object*
144 onReply(false, nullptr);
146 // We need to dispatch until "returned" got set, this is necessary
147 // if events get triggered by the call (and would be dispatched before
148 // the actual call-reply).
150 dispatch_internal(loop);
153 // return the actual API call result
162 AFBClient &AFBClient::instance() {
164 static AFBClient obj;
168 AFBClient::AFBClient() : wsj1{}, loop{} { TRACE(); }
170 AFBClient::~AFBClient() {
172 afb_wsj1_unref(wsj1);
173 sd_event_unref(loop);
177 int AFBClient::init(int port, char const *token) {
179 char *uribuf = nullptr;
182 if (!token || strlen(token) > token_maxlen) {
183 fprintf(stderr, "Token is invalid\n");
188 for (char const *p = token; *p; p++) {
190 fprintf(stderr, "Token is invalid\n");
196 if (port < 1 && port > 0xffff) {
197 fprintf(stderr, "Port is invalid\n");
202 /* get the default event loop */
203 rc = sd_event_default(&loop);
205 fprintf(stderr, "Connection to default event loop failed: %s\n",
210 asprintf(&uribuf, "ws://localhost:%d/api?token=%s", port, token);
212 /* connect the websocket wsj1 to the uri given by the first argument */
213 wsj1 = afb_ws_client_connect_wsj1(loop, uribuf, &itf, nullptr);
214 if (wsj1 == nullptr) {
215 sd_event_unref(loop);
216 fprintf(stderr, "Connection to %s failed: %m\n", uribuf);
227 int AFBClient::dispatch() {
228 std::lock_guard<std::recursive_mutex> guard(dispatch_mutex);
229 return sd_event_run(loop, 1);
232 int AFBClient::requestSurface(const char *label) {
234 json_object *jp = json_object_new_object();
235 json_object_object_add(jp, "drawing_name", json_object_new_string(label));
237 /* send the request */
239 loop, wsj1, "request_surface", jp, [&rc](bool ok, json_object *j) {
242 json_object_get_int(json_object_object_get(j, "response"));
244 asprintf(&buf, "%d", id);
245 printf("setenv(\"QT_IVI_SURFACE_ID\", %s, 1)\n", buf);
246 if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) {
247 fprintf(stderr, "putenv failed: %m\n");
250 rc = 0; // Single point of success
253 fprintf(stderr, "Could not get surface ID from WM: %s\n",
254 j ? json_object_to_json_string_ext(
255 j, JSON_C_TO_STRING_PRETTY)
261 return rc2 < 0 ? rc2 : rc;
264 int AFBClient::activateSurface(const char *label) {
266 json_object *j = json_object_new_object();
267 json_object_object_add(j, "drawing_name", json_object_new_string(label));
268 return api_call(loop, wsj1, "activate_surface", j, [](bool ok,
272 stderr, "API Call activate_surface() failed: %s\n",
273 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
279 int AFBClient::deactivateSurface(const char *label) {
281 json_object *j = json_object_new_object();
282 json_object_object_add(j, "drawing_name", json_object_new_string(label));
283 return api_call(loop, wsj1, "deactivate_surface", j, [](bool ok,
287 stderr, "API Call deactivate_surface() failed: %s\n",
288 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
294 int AFBClient::endDraw(const char *label) {
296 json_object *j = json_object_new_object();
297 json_object_object_add(j, "drawing_name", json_object_new_string(label));
298 return api_call(loop, wsj1, "enddraw", j, [](bool ok, json_object *j) {
301 stderr, "API Call endDraw() failed: %s\n",
302 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
308 void AFBClient::set_event_handler(enum EventType et,
309 std::function<void(char const *)> func) {