11 #define UNUSED(x) (void)(x)
15 constexpr const int token_maxlen = 20;
16 constexpr const char *const wmAPI = "winman";
22 #define CONCAT_(X, Y) X##Y
23 #define CONCAT(X, Y) CONCAT_(X, Y)
26 ScopeTrace __attribute__((unused)) CONCAT(trace_scope_, __LINE__)(__func__)
28 ScopeTrace __attribute__((unused)) CONCAT(named_trace_scope_, __LINE__)(#N)
33 ScopeTrace(char const *func) : f(func) {
34 fprintf(stderr, "%*s%s -->\n", 2 * indent++, "", this->f);
36 ~ScopeTrace() { fprintf(stderr, "%*s%s <--\n", 2 * --indent, "", this->f); }
38 int ScopeTrace::indent = 0;
41 /* called when wsj1 receives a method invocation */
42 void onCall(void *closure, const char *api, const char *verb,
43 struct afb_wsj1_msg *msg) {
47 printf("ON-CALL %s/%s:\n%s\n", api, verb,
48 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
49 JSON_C_TO_STRING_PRETTY));
51 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", nullptr);
53 fprintf(stderr, "replying failed: %m\n");
56 /* called when wsj1 receives an event */
57 void onEvent(void *closure, const char *event, afb_wsj1_msg *msg) {
60 printf("ON-EVENT %s:\n%s\n", event,
61 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
62 JSON_C_TO_STRING_PRETTY));
66 /* called when wsj1 hangsup */
67 void onHangup(void *closure, afb_wsj1 *wsj1) {
71 printf("ON-HANGUP\n");
78 AFBClient &AFBClient::instance() {
84 AFBClient::AFBClient() : wsj1{}, itf{}, loop{} {
86 itf.on_hangup = onHangup;
88 itf.on_event = onEvent;
91 AFBClient::~AFBClient() {
98 int AFBClient::init(int port, char const *token) {
100 char *uribuf = nullptr;
103 if (!token || strlen(token) > token_maxlen) {
104 fprintf(stderr, "Token is invalid\n");
109 for (char const *p = token; *p; p++) {
111 fprintf(stderr, "Token is invalid\n");
117 if (port < 1 && port > 0xffff) {
118 fprintf(stderr, "Port is invalid\n");
123 /* get the default event loop */
124 rc = sd_event_default(&loop);
126 fprintf(stderr, "Connection to default event loop failed: %s\n",
131 asprintf(&uribuf, "ws://localhost:%d/api?token=%s", port, token);
133 /* connect the websocket wsj1 to the uri given by the first argument */
134 wsj1 = afb_ws_client_connect_wsj1(loop, uribuf, &itf, nullptr);
135 if (wsj1 == nullptr) {
136 sd_event_unref(loop);
137 fprintf(stderr, "Connection to %s failed: %m\n", uribuf);
148 int AFBClient::dispatch(uint64_t timeout) {
149 return sd_event_run(loop, timeout);
152 int AFBClient::requestSurface(const char *label) {
154 json_object *jp = json_object_new_object();
155 json_object_object_add(jp, "drawing_name", json_object_new_string(label));
157 /* send the request */
158 int rc2 = call("request_surface", jp, [&rc](bool ok, json_object *j) {
160 int id = json_object_get_int(json_object_object_get(j, "response"));
162 asprintf(&buf, "%d", id);
163 printf("setenv(\"QT_IVI_SURFACE_ID\", %s, 1)\n", buf);
164 if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) {
165 fprintf(stderr, "putenv failed: %m\n");
168 rc = 0; // Single point of success
172 stderr, "Could not get surface ID from WM: %s\n",
173 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
179 return rc2 < 0 ? rc2 : rc;
182 int AFBClient::activateSurface(const char *label) {
184 json_object *j = json_object_new_object();
185 json_object_object_add(j, "drawing_name", json_object_new_string(label));
186 return call("activate_surface", j, [](bool ok, json_object *j) {
189 stderr, "API Call activate_surface() failed: %s\n",
190 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
196 int AFBClient::deactivateSurface(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("deactivate_surface", j, [](bool ok, json_object *j) {
203 stderr, "API Call deactivate_surface() failed: %s\n",
204 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
210 int AFBClient::endDraw(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("enddraw", j, [](bool ok, json_object *j) {
217 stderr, "API Call endDraw() failed: %s\n",
218 j ? json_object_to_json_string_ext(j, JSON_C_TO_STRING_PRETTY)
224 /// object will be json_object_put
225 int AFBClient::call(const char *verb, json_object *object,
226 std::function<void(bool, json_object *)> onReply) {
229 // We need to wrap the actual onReply call once in order to
230 // *look* like a normal functions pointer (std::functions<>
231 // with captures cannot convert to function pointers).
232 // Alternatively we could setup a local struct and use it as
233 // closure, but I think it is cleaner this way.
235 bool returned = false;
236 std::function<void(bool, json_object *)> wrappedOnReply =
237 [&returned, &call_rc, &onReply](bool ok, json_object *j) {
238 TRACEN(wrappedOnReply);
239 call_rc = ok ? 0 : -EINVAL;
240 // We know it failed, but there may be an explanation in the
246 // make the actual call, use wrappedOnReply as closure
247 int rc = afb_wsj1_call_j(
248 wsj1, wmAPI, verb, object,
249 [](void *closure, afb_wsj1_msg *msg) {
252 reinterpret_cast<std::function<void(bool, json_object *)> *>(
254 (*onReply)(!!afb_wsj1_msg_is_reply_ok(msg),
255 afb_wsj1_msg_object_j(msg));
261 stderr, "calling %s/%s(%s) failed: %m\n", wmAPI, verb,
262 json_object_to_json_string_ext(object, JSON_C_TO_STRING_PRETTY));
263 // Call the reply handler regardless with a NULL json_object*
264 onReply(false, nullptr);
266 // We need to dispatch until "returned" got set, this is necessary
267 // if events get triggered by the call (and would be dispatched before
268 // the actual call-reply).
273 // return the actual API call result
280 void AFBClient::set_event_handler(enum EventType et,
281 std::function<void(char const *)> func) {