12 #include <systemd/sd-event.h>
14 #include <json-c/json.h>
17 #include <afb/afb-ws-client.h>
18 #include <afb/afb-wsj1.h>
21 #define UNUSED(x) (void)(x)
25 constexpr const int token_maxlen = 20;
26 constexpr const char *const wmAPI = "winman";
32 #define CONCAT_(X, Y) X##Y
33 #define CONCAT(X, Y) CONCAT_(X, Y)
36 ScopeTrace __attribute__((unused)) CONCAT(trace_scope_, __LINE__)(__func__)
38 ScopeTrace __attribute__((unused)) CONCAT(named_trace_scope_, __LINE__)(#N)
41 thread_local static int indent;
43 ScopeTrace(char const *func) : f(func) {
44 fprintf(stderr, "%*s%s -->\n", 2 * indent++, "", this->f);
46 ~ScopeTrace() { fprintf(stderr, "%*s%s <--\n", 2 * --indent, "", this->f); }
48 thread_local int ScopeTrace::indent = 0;
51 /* called when wsj1 receives a method invocation */
52 void onCall(void *closure, const char *api, const char *verb,
53 struct afb_wsj1_msg *msg) {
57 printf("ON-CALL %s/%s:\n%s\n", api, verb,
58 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
59 JSON_C_TO_STRING_PRETTY));
61 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", nullptr);
63 fprintf(stderr, "replying failed: %m\n");
66 /* called when wsj1 receives an event */
67 void onEvent(void *closure, const char *event, afb_wsj1_msg *msg) {
70 printf("ON-EVENT %s:\n%s\n", event,
71 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
72 JSON_C_TO_STRING_PRETTY));
76 /* called when wsj1 hangsup */
77 void onHangup(void *closure, afb_wsj1 *wsj1) {
81 printf("ON-HANGUP\n");
86 static struct afb_wsj1_itf itf = {
87 onHangup, onCall, onEvent,
90 void dispatch_internal(AFBClient *c, uint64_t timeout) {
97 AFBClient &AFBClient::instance() {
103 AFBClient::AFBClient() : wsj1{}, loop{} { TRACE(); }
105 AFBClient::~AFBClient() {
107 afb_wsj1_unref(wsj1);
108 sd_event_unref(loop);
112 int AFBClient::init(int port, char const *token) {
114 char *uribuf = nullptr;
117 if (!token || strlen(token) > token_maxlen) {
118 fprintf(stderr, "Token is invalid\n");
123 for (char const *p = token; *p; p++) {
125 fprintf(stderr, "Token is invalid\n");
131 if (port < 1 && port > 0xffff) {
132 fprintf(stderr, "Port is invalid\n");
137 /* get the default event loop */
138 rc = sd_event_default(&loop);
140 fprintf(stderr, "Connection to default event loop failed: %s\n",
145 asprintf(&uribuf, "ws://localhost:%d/api?token=%s", port, token);
147 /* connect the websocket wsj1 to the uri given by the first argument */
148 wsj1 = afb_ws_client_connect_wsj1(loop, uribuf, &itf, nullptr);
149 if (wsj1 == nullptr) {
150 sd_event_unref(loop);
151 fprintf(stderr, "Connection to %s failed: %m\n", uribuf);
162 int AFBClient::dispatch(uint64_t timeout) {
163 return sd_event_run(loop, timeout);
166 int AFBClient::requestSurface(const char *label) {
168 json_object *jp = json_object_new_object();
169 json_object_object_add(jp, "drawing_name", json_object_new_string(label));
171 /* send the request */
172 int rc2 = call("request_surface", jp, [&rc](bool ok, json_object *j) {
174 int id = json_object_get_int(json_object_object_get(j, "response"));
176 asprintf(&buf, "%d", id);
177 printf("setenv(\"QT_IVI_SURFACE_ID\", %s, 1)\n", buf);
178 if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) {
179 fprintf(stderr, "putenv failed: %m\n");
182 rc = 0; // Single point of success
186 stderr, "Could not get surface ID from WM: %s\n",
187 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
193 return rc2 < 0 ? rc2 : rc;
196 int AFBClient::activateSurface(const char *label) {
198 json_object *j = json_object_new_object();
199 json_object_object_add(j, "drawing_name", json_object_new_string(label));
200 return call("activate_surface", j, [](bool ok, json_object *j) {
203 stderr, "API Call activate_surface() failed: %s\n",
204 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
210 int AFBClient::deactivateSurface(const char *label) {
212 json_object *j = json_object_new_object();
213 json_object_object_add(j, "drawing_name", json_object_new_string(label));
214 return call("deactivate_surface", j, [](bool ok, json_object *j) {
217 stderr, "API Call deactivate_surface() failed: %s\n",
218 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
224 int AFBClient::endDraw(const char *label) {
226 json_object *j = json_object_new_object();
227 json_object_object_add(j, "drawing_name", json_object_new_string(label));
228 return call("enddraw", j, [](bool ok, json_object *j) {
231 stderr, "API Call endDraw() failed: %s\n",
232 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
238 /// object will be json_object_put
239 int AFBClient::call(const char *verb, json_object *object,
240 std::function<void(bool, json_object *)> onReply) {
243 // We need to wrap the actual onReply call once in order to
244 // *look* like a normal functions pointer (std::functions<>
245 // with captures cannot convert to function pointers).
246 // Alternatively we could setup a local struct and use it as
247 // closure, but I think it is cleaner this way.
249 bool returned = false;
250 std::function<void(bool, json_object *)> wrappedOnReply =
251 [&returned, &call_rc, &onReply](bool ok, json_object *j) {
252 TRACEN(wrappedOnReply);
253 call_rc = ok ? 0 : -EINVAL;
254 // We know it failed, but there may be an explanation in the
263 // make the actual call, use wrappedOnReply as closure
264 int rc = afb_wsj1_call_j(
265 wsj1, wmAPI, verb, object,
266 [](void *closure, afb_wsj1_msg *msg) {
269 reinterpret_cast<std::function<void(bool, json_object *)> *>(
271 (*onReply)(!!afb_wsj1_msg_is_reply_ok(msg),
272 afb_wsj1_msg_object_j(msg));
278 stderr, "calling %s/%s(%s) failed: %m\n", wmAPI, verb,
279 json_object_to_json_string_ext(object, JSON_C_TO_STRING_PRETTY));
280 // Call the reply handler regardless with a NULL json_object*
281 onReply(false, nullptr);
283 // We need to dispatch until "returned" got set, this is necessary
284 // if events get triggered by the call (and would be dispatched before
285 // the actual call-reply).
287 dispatch_internal(this, -1);
290 // return the actual API call result
297 void AFBClient::set_event_handler(enum EventType et,
298 std::function<void(char const *)> func) {